第一题:155. 最小栈
代码实现
class MinStack { //栈stack用来保存数值 //栈minStack用来保存最小值 Stack<Integer> stack; Stack<Integer> minStack; public MinStack() { stack = new Stack<>(); minStack = new Stack<>(); } public void push(int x) { stack.push(x); if (minStack.isEmpty() || x <= minStack.peek()) { minStack.push(x); } } public void pop() { if (stack.pop().equals(minStack.peek())) { minStack.pop(); } } public int top() { return stack.peek(); } public int getMin() { return minStack.peek(); } }
第二题:20. 有效的括号
代码实现
class Solution { public boolean isValid(String s) { Stack<Character> stack=new Stack<>(); for(int i=0;i<s.length();i++){ char c= s.charAt(i); //左括号进栈 if(c=='('||c=='{'||c=='['){ stack.push(c); } else{ if(stack.isEmpty()){return false;} Character topChar=stack.pop(); //右括号进行判断,若不同类型返回false; if(c==')'&&topChar!='('){ return false; } if(c==']'&&topChar!='['){ return false; } if(c=='}'&&topChar!='{'){ return false; } } } return stack.isEmpty(); } }
第三题:225. 用队列实现栈
代码实现
class MyStack { //用两个队列来实现栈 Queue<Integer> queue1; Queue<Integer> queue2; public MyStack() { queue1 = new LinkedList<Integer>(); queue2 = new LinkedList<Integer>(); } public void push(int x) { //当为空的时候,插入x queue2.offer(x); while (!queue1.isEmpty()) { queue2.offer(queue1.poll()); } Queue<Integer> temp = queue1; queue1 = queue2; queue2 = temp; } public int pop() { return queue1.poll(); } public int top() { return queue1.peek(); } public boolean empty() { return queue1.isEmpty(); } }
第四题:232. 用栈实现队列
代码实现
1.class CQueue { //创建两个栈 Stack<Integer> stack1; Stack<Integer> stack2; public CQueue() { stack1=new Stack<Integer>(); stack2=new Stack<Integer>(); } public void appendTail(int value) { //入队直接压入栈中 stack1.push(value); } public int deleteHead() { if(stack2.empty()){ while(!stack1.empty()){ stack2.push(stack1.pop()); } } if(stack2.empty()){ return -1; } return stack2.pop(); } }
第五题:622. 设计循环队列
代码实现
class MyCircularQueue { final int[] array; final int k; int left;//指向队首元素 int right; public MyCircularQueue(int k) { this.array=new int[k]; this.k=k; Arrays.fill(array,-1); left=k; right=k; } public boolean enQueue(int value) { if(isFull())return false; else{ array[right%k]=value; right++; return true; } } public boolean deQueue() { if(isEmpty())return false; else{ array[left%k]=-1; left++; return true; } } public int Front() { if(isEmpty())return -1; return array[left%k]; } public int Rear() { if(isEmpty())return -1; //(right-1)%k 防止数组越界 right%k-1会越界 else return array[(right-1)%k]; } public boolean isEmpty() { if(left==right)return true; return false; } public boolean isFull() { if(right-left==k)return true; return false; } }
代码实现
class Solution { public int[] shuffle(int[] nums, int n) { int[] arr=new int[2*n]; for(int i=0;i<n;i++){ arr[2*i]=nums[i]; arr[2*i+1]=nums[i+n]; } return arr; } }
第七题:1929. 数组串联
代码实现
class Solution { public int[] getConcatenation(int[] nums) { int n = nums.length; int[] ans = new int[n * 2]; for(int i = 0; i < n; i++){ ans[i] = nums[i]; ans[i + n] = nums[i]; } return ans; } }
第八题:1920. 基于排列构建数组
代码实现
class Solution { public int[] buildArray(int[] nums) { int[] arr=new int[nums.length]; for(int i=0;i<nums.length;i++){ arr[i]=nums[nums[i]]; } return arr; } }
第九题:1480. 一维数组的动态和
代码实现
class Solution { public int[] runningSum(int[] nums) { int n = nums.length; int[] ans = new int[n]; int sum=0; for (int i = 0; i < n; i++) { sum=sum+ nums[i]; ans[i]=s; } return ans; } }
代码实现:
class Solution { public String reverseLeftWords(String s, int n) { StringBuilder ans=new StringBuilder(); for(int i=n;i<s.length();i++){ ans.append(s.charAt(i)); } for(int i=0;i<n;i++){ ans.append(s.charAt(i)); } return ans.toString(); } }