Skip to content

Commit 535fb1c

Browse files
author
zhangruihao.zhang
committed
week1补充
1 parent b43e991 commit 535fb1c

5 files changed

Lines changed: 160 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717

1818
## 注意事项
1919

20-
1. **代码文件命名规则:**`LeetCode_题目序号_学号`,比如学号为 `0` 的学员完成 [LeetCode 的第 2 题](https://leetcode.com/problems/add-two-numbers/description/) 后,请将代码文件名保存为 `LeetCode_2_0.py` (假设你使用的是 Python 语言)。
20+
1. **代码文件命名规则:**`LeetCode_题目序号_学号`,比如学号为 `0` 的学员完成 [LeetCode_33_108 的第 2 题](https://leetcode.com/problems/add-two-numbers/description/) 后,请将代码文件名保存为 `LeetCode_2_0.py` (假设你使用的是 Python 语言)。
2121
2. **作业公布地址:** 我们会在置顶的 issue 中公布当周的算法练习题以及其他注意事项。
2222
3. 如果对 Git 和 GitHub 不太了解,请参考 [Git 官方文档](https://git-scm.com/book/zh/v2) 或者极客时间的[《玩转 Git 三剑客》](https://time.geekbang.org/course/intro/145)视频课程。
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import org.junit.Test;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* @author zhangruihao.zhang
7+
* @version v1.0.0
8+
* @since 2019/04/29
9+
*/
10+
public class LeetCode_20_108 {
11+
public static void main(String[] args) {
12+
String str = "()[]{}";
13+
System.out.println(isValid(str));
14+
}
15+
public static boolean isValid(String s) {
16+
Stack stack = new Stack();
17+
for(int i=0;i<s.length();i++){
18+
char c = s.charAt(i);
19+
if(stack.peek().equals(anotherChar(c))){
20+
stack.pop();
21+
}else{
22+
stack.push(c);
23+
}
24+
}
25+
return stack.isEmpty();
26+
}
27+
28+
private static char anotherChar(char c){
29+
if(c == '('){
30+
return ')';
31+
}
32+
if(c == ')'){
33+
return '(';
34+
}
35+
if(c == '{'){
36+
return '}';
37+
}
38+
if(c == '}'){
39+
return '{';
40+
}
41+
if(c == '['){
42+
return ']';
43+
}
44+
if(c == ']'){
45+
return '[';
46+
}
47+
return '0';
48+
}
49+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @author zhangruihao.zhang
3+
* @version v1.0.0
4+
* @since 2019/04/29
5+
*/
6+
public class LeetCode_33_108 {
7+
class Solution {
8+
public int search(int[] nums, int target) {
9+
if(nums == null || nums.length == 0){
10+
return -1;
11+
}
12+
13+
if(nums[0] == target){
14+
return 0;
15+
}
16+
if(nums[nums.length - 1] == target){
17+
return nums.length - 1;
18+
}
19+
20+
int end = nums.length - 1;
21+
int min = min(nums);
22+
//如果没有旋转,并且最大的数小于target
23+
if(min == 0 && nums[end]<target){
24+
return -1;
25+
}
26+
27+
if(nums[end] > target){
28+
//target处于旋转的后半段
29+
return binarySerach(nums,min,end,target);
30+
}else{
31+
//target有可能处于旋转的前半段
32+
return binarySerach(nums,0,min-1,target);
33+
}
34+
}
35+
36+
//查找旋转点
37+
private int min(int[] nums){
38+
int low = 0;
39+
int high = nums.length - 1;
40+
int mid;
41+
while(low < high){
42+
mid = low + ((high-low)>>1);
43+
if(nums[mid]>nums[high]){
44+
low = mid + 1;
45+
}else{
46+
high = mid;
47+
}
48+
}
49+
return low;
50+
}
51+
52+
//二分查找
53+
private int binarySerach(int[] nums,int left,int right,int target){
54+
int mid = 0;
55+
while(left<right){
56+
mid = left + ((right - left)>>1);
57+
if(nums[mid]==target){
58+
return mid;
59+
}
60+
if(nums[mid]<target){
61+
left = mid + 1;
62+
continue;
63+
}
64+
if(nums[mid]>target){
65+
right = mid - 1;
66+
continue;
67+
}
68+
}
69+
if(nums[left] == target){
70+
return left;
71+
}
72+
return -1;
73+
}
74+
}
75+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* @author zhangruihao.zhang
5+
* @version v1.0.0
6+
* @since 2019/04/29
7+
*/
8+
public class LeetCode_503_108 {
9+
class Solution {
10+
public int[] nextGreaterElements(int[] nums) {
11+
12+
Stack<Integer> stack = new Stack<>();
13+
int index[] = new int[nums.length];
14+
for (int i = 0; i < index.length; i++) {
15+
index[i] = -1;
16+
}
17+
if (nums.length == 1) {
18+
return index;
19+
}
20+
int count = 0;
21+
while (count < 2) {
22+
for (int i = 0; i < nums.length; i++) {
23+
while (!stack.isEmpty() && nums[i] > nums[stack.peek()]) {
24+
index[stack.pop()] = nums[i];
25+
}
26+
stack.push(i);
27+
}
28+
count++;
29+
}
30+
return index;
31+
}
32+
}
33+
}

Week_01/id_127/NOTE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# 学习笔记
22

3-
### LeetCode-83思路:
3+
### LeetCode_33_108-83思路:
44

55
见图片: `83.jpeg`
66

7-
### LeetCode-21思路:
7+
### LeetCode_33_108-21思路:
88

0 commit comments

Comments
 (0)