auto commit

This commit is contained in:
CyC2018
2018-06-08 16:03:22 +08:00
parent 498268e40c
commit 34072ecf26
3 changed files with 39 additions and 84 deletions

View File

@ -3820,44 +3820,11 @@ public int maximumProduct(int[] nums) {
[232. Implement Queue using Stacks (Easy)](https://leetcode.com/problems/implement-queue-using-stacks/description/) [232. Implement Queue using Stacks (Easy)](https://leetcode.com/problems/implement-queue-using-stacks/description/)
一个栈实现:
```java
class MyQueue {
private Stack<Integer> st = new Stack();
public void push(int x) {
Stack<Integer> temp = new Stack();
while (!st.isEmpty()) {
temp.push(st.pop());
}
st.push(x);
while (!temp.isEmpty()) {
st.push(temp.pop());
}
}
public int pop() {
return st.pop();
}
public int peek() {
return st.peek();
}
public boolean empty() {
return st.isEmpty();
}
}
```
两个栈实现:
```java ```java
class MyQueue { class MyQueue {
private Stack<Integer> in = new Stack(); private Stack<Integer> in = new Stack<>();
private Stack<Integer> out = new Stack(); private Stack<Integer> out = new Stack<>();
public void push(int x) { public void push(int x) {
in.push(x); in.push(x);
@ -3891,6 +3858,8 @@ class MyQueue {
[225. Implement Stack using Queues (Easy)](https://leetcode.com/problems/implement-stack-using-queues/description/) [225. Implement Stack using Queues (Easy)](https://leetcode.com/problems/implement-stack-using-queues/description/)
在将一个元素 x 插入队列时,需要让除了 x 之外的所有元素出队列,再入队列。此时 x 在队首,第一个出队列,实现了后进先出顺序。
```java ```java
class MyStack { class MyStack {
@ -3926,8 +3895,6 @@ class MyStack {
[155. Min Stack (Easy)](https://leetcode.com/problems/min-stack/description/) [155. Min Stack (Easy)](https://leetcode.com/problems/min-stack/description/)
用两个栈实现,一个存储数据,一个存储最小值。
```java ```java
class MinStack { class MinStack {
@ -3958,7 +3925,7 @@ class MinStack {
} }
public int getMin() { public int getMin() {
return min; return minStack.peek();
} }
} }
``` ```
@ -3979,14 +3946,19 @@ Output : true
public boolean isValid(String s) { public boolean isValid(String s) {
Stack<Character> stack = new Stack<>(); Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) { for (char c : s.toCharArray()) {
if (c == '(' || c == '{' || c == '[') stack.push(c); if (c == '(' || c == '{' || c == '[') {
else { stack.push(c);
if (stack.isEmpty()) return false; } else {
if (stack.isEmpty()) {
return false;
}
char cStack = stack.pop(); char cStack = stack.pop();
boolean b1 = c == ')' && cStack != '('; boolean b1 = c == ')' && cStack != '(';
boolean b2 = c == ']' && cStack != '['; boolean b2 = c == ']' && cStack != '[';
boolean b3 = c == '}' && cStack != '{'; boolean b3 = c == '}' && cStack != '{';
if (b1 || b2 || b3) return false; if (b1 || b2 || b3) {
return false;
}
} }
} }
return stack.isEmpty(); return stack.isEmpty();
@ -3995,28 +3967,28 @@ public boolean isValid(String s) {
**数组中元素与下一个比它大的元素之间的距离** **数组中元素与下一个比它大的元素之间的距离**
[739. Daily Temperatures (Medium)](https://leetcode.com/problems/daily-temperatures/description/)
```html ```html
Input: [73, 74, 75, 71, 69, 72, 76, 73] Input: [73, 74, 75, 71, 69, 72, 76, 73]
Output: [1, 1, 4, 2, 1, 1, 0, 0] Output: [1, 1, 4, 2, 1, 1, 0, 0]
``` ```
[739. Daily Temperatures (Medium)](https://leetcode.com/problems/daily-temperatures/description/)
在遍历数组时用 Stack 把数组中的数存起来,如果当前遍历的数比栈顶元素来的大,说明栈顶元素的下一个比它大的数就是当前元素。 在遍历数组时用 Stack 把数组中的数存起来,如果当前遍历的数比栈顶元素来的大,说明栈顶元素的下一个比它大的数就是当前元素。
```java ```java
public int[] dailyTemperatures(int[] temperatures) { public int[] dailyTemperatures(int[] temperatures) {
int n = temperatures.length; int n = temperatures.length;
int[] ret = new int[n]; int[] dist = new int[n];
Stack<Integer> stack = new Stack<>(); Stack<Integer> indexs = new Stack<>();
for (int i = 0; i < n; i++) { for (int curIndex = 0; curIndex < n; curIndex++) {
while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) { while (!indexs.isEmpty() && temperatures[curIndex] > temperatures[indexs.peek()]) {
int idx = stack.pop(); int preIndex = indexs.pop();
ret[idx] = i - idx; dist[preIndex] = curIndex - preIndex;
} }
stack.add(i); indexs.add(curIndex);
} }
return ret; return dist;
} }
``` ```

View File

@ -1,21 +1,6 @@
<!-- GFM-TOC --> <!-- GFM-TOC -->
* [Google Java Style Guide](#google-java-style-guide)
* [Google C++ Style Guide](#google-c-style-guide)
* [Google Python Style Guide](#google-python-style-guide)
<!-- GFM-TOC --> <!-- GFM-TOC -->
# Google Java Style Guide - [Twitter Java Style Guide](https://github.com/twitter/commons/blob/master/src/java/com/twitter/common/styleguide.mde)
- [Google Java Style Guide](http://google.github.io/styleguide/javaguide.html)
- http://www.hawstein.com/posts/google-java-style.html
- http://google.github.io/styleguide/javaguide.html
# Google C++ Style Guide
- http://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/contents/
- http://google.github.io/styleguide/cppguide.html
# Google Python Style Guide
- http://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/contents/
- http://google.github.io/styleguide/pyguide.html

View File

@ -115,21 +115,19 @@ public class ThreeSumFast {
``` ```
```java ```java
public class ThreeSumFast implements ThreeSum { public class BinarySearch {
@Override public static int search(int[] nums, int target) {
public int count(int[] nums) { int l = 0, h = nums.length - 1;
Arrays.sort(nums); while (l <= h) {
int N = nums.length; int m = l + (h - l) / 2;
int cnt = 0; if (target == nums[m])
for (int i = 0; i < N; i++) return m;
for (int j = i + 1; j < N; j++) { else if (target > nums[m])
int target = -nums[i] - nums[j]; l = m + 1;
int index = BinarySearch.search(nums, target); else
// 应该注意这里的下标必须大于 j否则会重复统计。 h = m - 1;
if (index > j)
cnt++;
} }
return cnt; return -1;
} }
} }
``` ```