auto commit

This commit is contained in:
CyC2018 2018-04-04 15:36:04 +08:00
parent 9b1e265edc
commit cf796faa06
2 changed files with 81 additions and 93 deletions

View File

@ -3301,8 +3301,9 @@ class MyStack {
public void push(int x) { public void push(int x) {
queue.add(x); queue.add(x);
for(int i = 1; i < queue.size(); i++){ // 翻转 int cnt = queue.size();
queue.add(queue.remove()); while (cnt-- > 1) {
queue.add(queue.poll());
} }
} }
@ -3341,20 +3342,14 @@ class MinStack {
public void push(int x) { public void push(int x) {
dataStack.add(x); dataStack.add(x);
if(x < min) { min = Math.min(min, x);
min = x;
}
minStack.add(min); minStack.add(min);
} }
public void pop() { public void pop() {
dataStack.pop(); dataStack.pop();
minStack.pop(); minStack.pop();
if(!minStack.isEmpty()) { min = minStack.isEmpty() ? min = Integer.MAX_VALUE : minStack.peek();
min = minStack.peek();
} else{
min = Integer.MAX_VALUE;
}
} }
public int top() { public int top() {
@ -3382,17 +3377,15 @@ Output : true
```java ```java
public boolean isValid(String s) { public boolean isValid(String s) {
Stack<Character> stack = new Stack<>(); Stack<Character> stack = new Stack<>();
for(int i = 0; i < s.length(); i++){ for (char c : s.toCharArray()) {
char c = s.charAt(i);
if (c == '(' || c == '{' || c == '[') stack.push(c); if (c == '(' || c == '{' || c == '[') stack.push(c);
else { else {
if (stack.isEmpty()) return false; if (stack.isEmpty()) return false;
char cStack = stack.pop(); char cStack = stack.pop();
if(c == ')' && cStack != '(' || boolean b1 = c == ')' && cStack != '(';
c == ']' && cStack != '[' || boolean b2 = c == ']' && cStack != '[';
c == '}' && cStack != '{' ) { boolean b3 = c == '}' && cStack != '{';
return false; if (b1 || b2 || b3) return false;
}
} }
} }
return stack.isEmpty(); return stack.isEmpty();
@ -3729,14 +3722,9 @@ For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums sh
```java ```java
public void moveZeroes(int[] nums) { public void moveZeroes(int[] nums) {
int n = nums.length;
int idx = 0; int idx = 0;
for(int i = 0; i < n; i++){ for (int num : nums) if (num != 0) nums[idx++] = num;
if(nums[i] != 0) nums[idx++] = nums[i]; while (idx < nums.length) nums[idx++] = 0;
}
while(idx < n){
nums[idx++] = 0;
}
} }
``` ```
@ -3751,6 +3739,11 @@ Input: nums = [1,2,2,4]
Output: [2,3] Output: [2,3]
``` ```
```html
Input: nums = [1,2,2,4]
Output: [2,3]
```
最直接的方法是先对数组进行排序,这种方法时间复杂度为 O(nlogn)。本题可以以 O(n) 的时间复杂度、O(1) 空间复杂度来求解。 最直接的方法是先对数组进行排序,这种方法时间复杂度为 O(nlogn)。本题可以以 O(n) 的时间复杂度、O(1) 空间复杂度来求解。
主要思想是通过交换数组元素,使得数组上的元素在正确的位置上。 主要思想是通过交换数组元素,使得数组上的元素在正确的位置上。
@ -3765,31 +3758,28 @@ Output: [2,3]
```java ```java
public int[] findErrorNums(int[] nums) { public int[] findErrorNums(int[] nums) {
for (int i = 0; i < nums.length; i++) { for (int i = 0; i < nums.length; i++) {
while(nums[i] != i + 1 && nums[i] != nums[nums[i] - 1]) { while (nums[i] != i + 1) {
if (nums[i] == nums[nums[i] - 1]) {
return new int[]{nums[nums[i] - 1], i + 1};
}
swap(nums, i, nums[i] - 1); swap(nums, i, nums[i] - 1);
} }
} }
for(int i = 0; i < nums.length; i++){
if(i + 1 != nums[i]) {
return new int[]{nums[i], i + 1};
}
}
return null; return null;
} }
private void swap(int[] nums, int i, int j) { private void swap(int[] nums, int i, int j) {
int tmp = nums[i]; int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
nums[i] = nums[j];
nums[j] = tmp;
} }
``` ```
**找出数组中重复的数,数组值在 [0, n-1] 之间** **找出数组中重复的数,数组值在 [1, n] 之间**
[Leetcode : 287. Find the Duplicate Number (Medium)](https://leetcode.com/problems/find-the-duplicate-number/description/) [Leetcode : 287. Find the Duplicate Number (Medium)](https://leetcode.com/problems/find-the-duplicate-number/description/)
要求不能修改数组,也不能使用额外的空间。
二分查找解法: 二分查找解法:
```java ```java
@ -3817,7 +3807,6 @@ public int findDuplicate(int[] nums) {
slow = nums[slow]; slow = nums[slow];
fast = nums[nums[fast]]; fast = nums[nums[fast]];
} }
fast = 0; fast = 0;
while (slow != fast) { while (slow != fast) {
slow = nums[slow]; slow = nums[slow];
@ -4014,6 +4003,8 @@ public ListNode deleteDuplicates(ListNode head) {
[Leetcode : 234. Palindrome Linked List (Easy)](https://leetcode.com/problems/palindrome-linked-list/description/) [Leetcode : 234. Palindrome Linked List (Easy)](https://leetcode.com/problems/palindrome-linked-list/description/)
要求以 O(1) 的空间复杂度来求解。
切成两半,把后半段反转,然后比较两半是否相等。 切成两半,把后半段反转,然后比较两半是否相等。
```java ```java
@ -4307,19 +4298,13 @@ There are two left leaves in the binary tree, with values 9 and 15 respectively.
```java ```java
public int sumOfLeftLeaves(TreeNode root) { public int sumOfLeftLeaves(TreeNode root) {
if(root == null) { if(root == null) return 0;
return 0; if(isLeaf(root.left)) return root.left.val + sumOfLeftLeaves(root.right);
}
if(isLeaf(root.left)) {
return root.left.val + sumOfLeftLeaves(root.right);
}
return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right); return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
} }
private boolean isLeaf(TreeNode node){ private boolean isLeaf(TreeNode node){
if(node == null) { if(node == null) return false;
return false;
}
return node.left == null && node.right == null; return node.left == null && node.right == null;
} }
``` ```
@ -4704,14 +4689,14 @@ void dfs(TreeNode root){
```java ```java
public List<Integer> preorderTraversal(TreeNode root) { public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<>(); List<Integer> ret = new ArrayList<>();
if (root == null) return ret;
Stack<TreeNode> stack = new Stack<>(); Stack<TreeNode> stack = new Stack<>();
stack.push(root); stack.push(root);
while (!stack.isEmpty()) { while (!stack.isEmpty()) {
TreeNode node = stack.pop(); TreeNode node = stack.pop();
if (node == null) continue;
ret.add(node.val); ret.add(node.val);
if (node.right != null) stack.push(node.right); stack.push(node.right); // 先右后左,保证左子树先遍历
if (node.left != null) stack.push(node.left); // 先添加右子树再添加左子树,这样是为了让左子树在栈顶 stack.push(node.left);
} }
return ret; return ret;
} }
@ -4726,14 +4711,14 @@ public List<Integer> preorderTraversal(TreeNode root) {
```java ```java
public List<Integer> postorderTraversal(TreeNode root) { public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<>(); List<Integer> ret = new ArrayList<>();
if (root == null) return ret;
Stack<TreeNode> stack = new Stack<>(); Stack<TreeNode> stack = new Stack<>();
stack.push(root); stack.push(root);
while (!stack.isEmpty()) { while (!stack.isEmpty()) {
TreeNode node = stack.pop(); TreeNode node = stack.pop();
if (node == null) continue;
ret.add(node.val); ret.add(node.val);
if (node.left != null) stack.push(node.left); stack.push(node.left);
if (node.right != null) stack.push(node.right); stack.push(node.right);
} }
Collections.reverse(ret); Collections.reverse(ret);
return ret; return ret;
@ -4747,11 +4732,12 @@ public List<Integer> postorderTraversal(TreeNode root) {
```java ```java
public List<Integer> inorderTraversal(TreeNode root) { public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<>(); List<Integer> ret = new ArrayList<>();
if (root == null) return ret;
Stack<TreeNode> stack = new Stack<>(); Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root; TreeNode cur = root;
while (cur != null || !stack.isEmpty()) { while (cur != null || !stack.isEmpty()) {
while(cur != null) { // 模拟递归栈的不断深入 while (cur != null) {
stack.add(cur); stack.push(cur);
cur = cur.left; cur = cur.left;
} }
TreeNode node = stack.pop(); TreeNode node = stack.pop();

View File

@ -90,6 +90,8 @@
在一个长度为 n 的数组里的所有数字都在 0 到 n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。例如,如果输入长度为 7 的数组 {2, 3, 1, 0, 2, 5, 3},那么对应的输出是第一个重复的数字 2。 在一个长度为 n 的数组里的所有数字都在 0 到 n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。例如,如果输入长度为 7 的数组 {2, 3, 1, 0, 2, 5, 3},那么对应的输出是第一个重复的数字 2。
要求复杂度为 O(N) + O(1),时间复杂度 O(N),空间复杂度 O(1)。因此不能使用排序的方法,也不能使用额外的标记数组。
## 解题思路 ## 解题思路
这种数组元素在 [0, n-1] 范围内的问题,可以将值为 i 的元素放到第 i 个位置上。 这种数组元素在 [0, n-1] 范围内的问题,可以将值为 i 的元素放到第 i 个位置上。