From 34072ecf26087837d68480b7ac33e5b6c295ff82 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Fri, 8 Jun 2018 16:03:22 +0800 Subject: [PATCH] auto commit --- notes/Leetcode 题解.md | 76 +++++++++++++----------------------------- notes/代码风格规范.md | 19 ++--------- notes/算法.md | 28 ++++++++-------- 3 files changed, 39 insertions(+), 84 deletions(-) diff --git a/notes/Leetcode 题解.md b/notes/Leetcode 题解.md index a4bb7a80..73395093 100644 --- a/notes/Leetcode 题解.md +++ b/notes/Leetcode 题解.md @@ -3820,44 +3820,11 @@ public int maximumProduct(int[] nums) { [232. Implement Queue using Stacks (Easy)](https://leetcode.com/problems/implement-queue-using-stacks/description/) -一个栈实现: - -```java -class MyQueue { - private Stack st = new Stack(); - - public void push(int x) { - Stack 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 class MyQueue { - private Stack in = new Stack(); - private Stack out = new Stack(); + private Stack in = new Stack<>(); + private Stack out = new Stack<>(); public void push(int 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/) +在将一个元素 x 插入队列时,需要让除了 x 之外的所有元素出队列,再入队列。此时 x 在队首,第一个出队列,实现了后进先出顺序。 + ```java class MyStack { @@ -3926,8 +3895,6 @@ class MyStack { [155. Min Stack (Easy)](https://leetcode.com/problems/min-stack/description/) -用两个栈实现,一个存储数据,一个存储最小值。 - ```java class MinStack { @@ -3958,7 +3925,7 @@ class MinStack { } public int getMin() { - return min; + return minStack.peek(); } } ``` @@ -3979,14 +3946,19 @@ Output : true public boolean isValid(String s) { Stack stack = new Stack<>(); for (char c : s.toCharArray()) { - if (c == '(' || c == '{' || c == '[') stack.push(c); - else { - if (stack.isEmpty()) return false; + if (c == '(' || c == '{' || c == '[') { + stack.push(c); + } else { + if (stack.isEmpty()) { + return false; + } char cStack = stack.pop(); boolean b1 = c == ')' && cStack != '('; boolean b2 = c == ']' && cStack != '['; boolean b3 = c == '}' && cStack != '{'; - if (b1 || b2 || b3) return false; + if (b1 || b2 || b3) { + return false; + } } } return stack.isEmpty(); @@ -3995,28 +3967,28 @@ public boolean isValid(String s) { **数组中元素与下一个比它大的元素之间的距离** +[739. Daily Temperatures (Medium)](https://leetcode.com/problems/daily-temperatures/description/) + ```html Input: [73, 74, 75, 71, 69, 72, 76, 73] Output: [1, 1, 4, 2, 1, 1, 0, 0] ``` -[739. Daily Temperatures (Medium)](https://leetcode.com/problems/daily-temperatures/description/) - 在遍历数组时用 Stack 把数组中的数存起来,如果当前遍历的数比栈顶元素来的大,说明栈顶元素的下一个比它大的数就是当前元素。 ```java public int[] dailyTemperatures(int[] temperatures) { int n = temperatures.length; - int[] ret = new int[n]; - Stack stack = new Stack<>(); - for (int i = 0; i < n; i++) { - while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) { - int idx = stack.pop(); - ret[idx] = i - idx; + int[] dist = new int[n]; + Stack indexs = new Stack<>(); + for (int curIndex = 0; curIndex < n; curIndex++) { + while (!indexs.isEmpty() && temperatures[curIndex] > temperatures[indexs.peek()]) { + int preIndex = indexs.pop(); + dist[preIndex] = curIndex - preIndex; } - stack.add(i); + indexs.add(curIndex); } - return ret; + return dist; } ``` diff --git a/notes/代码风格规范.md b/notes/代码风格规范.md index 62049b68..b1ce8137 100644 --- a/notes/代码风格规范.md +++ b/notes/代码风格规范.md @@ -1,21 +1,6 @@ -* [Google Java Style Guide](#google-java-style-guide) -* [Google C++ Style Guide](#google-c-style-guide) -* [Google Python Style Guide](#google-python-style-guide) -# Google Java Style Guide - -- 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 +- [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) diff --git a/notes/算法.md b/notes/算法.md index 430ff34a..18366255 100644 --- a/notes/算法.md +++ b/notes/算法.md @@ -115,21 +115,19 @@ public class ThreeSumFast { ``` ```java -public class ThreeSumFast implements ThreeSum { - @Override - public int count(int[] nums) { - Arrays.sort(nums); - int N = nums.length; - int cnt = 0; - for (int i = 0; i < N; i++) - for (int j = i + 1; j < N; j++) { - int target = -nums[i] - nums[j]; - int index = BinarySearch.search(nums, target); - // 应该注意这里的下标必须大于 j,否则会重复统计。 - if (index > j) - cnt++; - } - return cnt; +public class BinarySearch { + public static int search(int[] nums, int target) { + int l = 0, h = nums.length - 1; + while (l <= h) { + int m = l + (h - l) / 2; + if (target == nums[m]) + return m; + else if (target > nums[m]) + l = m + 1; + else + h = m - 1; + } + return -1; } } ```