Skip to content

Commit 7d78f70

Browse files
author
anduo
committed
init
0 parents  commit 7d78f70

File tree

177 files changed

+9579
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

177 files changed

+9579
-0
lines changed

.gitignore

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
.idea
2+
*.iml
3+
# Created by .ignore support plugin (hsz.mobi)
4+
### Eclipse template
5+
6+
.metadata
7+
bin/
8+
tmp/
9+
*.tmp
10+
*.bak
11+
*.swp
12+
*~.nib
13+
local.properties
14+
.settings/
15+
.loadpath
16+
.recommenders
17+
18+
# Eclipse Core
19+
.project
20+
21+
# External tool builders
22+
.externalToolBuilders/
23+
24+
# Locally stored "Eclipse launch configurations"
25+
*.launch
26+
27+
# PyDev specific (Python IDE for Eclipse)
28+
*.pydevproject
29+
30+
# CDT-specific (C/C++ Development Tooling)
31+
.cproject
32+
33+
# JDT-specific (Eclipse Java Development Tools)
34+
.classpath
35+
36+
# Java annotation processor (APT)
37+
.factorypath
38+
39+
# PDT-specific (PHP Development Tools)
40+
.buildpath
41+
42+
# sbteclipse plugin
43+
.target
44+
45+
# Tern plugin
46+
.tern-project
47+
48+
# TeXlipse plugin
49+
.texlipse
50+
51+
# STS (Spring Tool Suite)
52+
.springBeans
53+
54+
# Code Recommenders
55+
.recommenders/
56+
57+
# Scala IDE specific (Scala & Java development for Eclipse)
58+
.cache-main
59+
.scala_dependencies
60+
.worksheet
61+
### Java template
62+
# Compiled class file
63+
*.class
64+
65+
# Log file
66+
*.log
67+
68+
# BlueJ files
69+
*.ctxt
70+
71+
# Mobile Tools for Java (J2ME)
72+
.mtj.tmp/
73+
74+
# Package Files #
75+
*.jar
76+
*.war
77+
*.ear
78+
*.zip
79+
*.tar.gz
80+
*.rar
81+
82+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
83+
hs_err_pid*
84+
### Maven template
85+
target/
86+
pom.xml.tag
87+
pom.xml.releaseBackup
88+
pom.xml.versionsBackup
89+
pom.xml.next
90+
release.properties
91+
dependency-reduced-pom.xml
92+
buildNumber.properties
93+
.mvn/timing.properties
94+
95+
# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
96+
!/.mvn/wrapper/maven-wrapper.jar
97+
.~lock.*
98+

doc/CleanCodeHandbook_v1.0.1.pdf

1.61 MB
Binary file not shown.

doc/algorithm-zh-hans.pdf

8.34 MB
Binary file not shown.

doc/leetcode-cpp.pdf

837 KB
Binary file not shown.

doc/leetcode-frequencey.xlsx

404 KB
Binary file not shown.
2.49 MB
Binary file not shown.

pom.xml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>me.anduo</groupId>
8+
<artifactId>jiuzhang</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<build>
12+
<sourceDirectory>src</sourceDirectory>
13+
<plugins>
14+
<plugin>
15+
<artifactId>maven-compiler-plugin</artifactId>
16+
<configuration>
17+
<source>1.8</source>
18+
<target>1.8</target>
19+
</configuration>
20+
</plugin>
21+
</plugins>
22+
</build>
23+
24+
</project>

readme.md

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
Leetcode 题解
2+
---
3+
不是每道题都会一一解答,重在思想,使用模板来解题,达到触类旁通!
4+
5+
doc中有好几份文档,比较经典,可以参考。
6+
7+
- [CleanCodeHandbook](./doc/CleanCodeHandbook_v1.0.1.pdf)
8+
- [leetcode-cpp](./doc/leetcode-cpp.pdf)
9+
- [leetcode高频面试题总结](./doc/leetcode-frequencey.xlsx)
10+
- [algorithm](./doc/algorithm-zh-hans.pdf)
11+
- [手写代码必备手册](./doc/手写代码必备手册(C++版).pdf)
12+
13+
---
14+
15+
# 基本数据结构定义
16+
- [ListNode](./src/entity/ListNode.java) 链表节点
17+
- [TreeNode](./src/entity/TreeNode.java) 树节点
18+
- [UndirectedGraphNode](./src/entity/UndirectedGraphNode.java) 无向图
19+
- [TreeLinkNode](./src/entity/TreeLinkNode.java) 带链接的树节点
20+
- [RandomListNode](./src/entity/RandomListNode.java) 随机链接节点
21+
- [Interval](./src/entity/Interval.java) 间隔
22+
23+
# 常用解题模板
24+
- [二分查找](./src/template/bs_template.md)
25+
- [宽度优先搜索](./src/template/bfs_template.md)
26+
- [深度优先搜索](./src/template/dfs_template.md)
27+
- [二叉树递归遍历](./src/template/binary_tree_preorder_traversal_template.md)
28+
- [动态规划](./src/template/dynamic_programing_template.md)
29+
- [排列组合](./src/template/permute_template.md)
30+
- [堆排序](./src/template/heapify_template.md)
31+
32+
# leetcode高频题目录
33+
## frequencey 5
34+
1. [leetcode1:tow-sum](https://leetcode.com/problems/two-sum)
35+
- 难度 ※※
36+
- 数据结构 array、set
37+
- 算法 sort、tow pointers
38+
- 思路 hashmap
39+
- 解题 [TwoSum.java](./src/learn/freq05/TwoSum.java)
40+
2. [leetcode8:string-to-integer](https://leetcode.com/problems/string-to-integer-atoi)
41+
- 难度 ※※
42+
- 数据结构 string
43+
- 算法 math
44+
- 思路
45+
1. trim后看是不是空
46+
2. 第一位是不是有正负 符号
47+
3. 接下去位数有没有 str.charAt(i)<'0'||str.charAt(i)>'9'
48+
4. 如果是正常数值value=10*value+str.charAt(i)-'0';
49+
5. 最后在和integer max/min value
50+
- 解题 [StringToInteger.java](./src/learn/freq05/StringtoInteger.java)
51+
3. [leetcode15:3sum](https://leetcode.com/problems/3sum)
52+
- 难度 ※※※
53+
- 数据结构 array
54+
- 算法 two pointer
55+
- 思路 for循环 外层控制I 内层控制left,right 然后i+left+right==0 就是答案。 注意先sort 数组 当中去重
56+
- 题解 [ThreeSum.java](./src/learn/freq05/ThreeSum.java)
57+
4. [leetcode20:valid-parentheses](https://leetcode.com/problems/valid-parentheses)
58+
- 难度 ※※
59+
- 数据结构 string
60+
- 算法 stack
61+
- 思路 用stack实现,左括号add 右括号pop 然后比较是否是pair
62+
- 题解 [ValidParentheses.java](./src/learn/freq05/ValidParentheses.java)
63+
5. [leetcode21:merge-two-sorted-lists](https://leetcode.com/problems/merge-two-sorted-lists)
64+
- 难度 ※※
65+
- 数据结构 linkedlist
66+
- 算法 sort, two pointers, merge
67+
- 思路 遍历过程中比较两个链表的头元素
68+
- 题解 [Merge2SortedList.java](./src/learn/freq05/Merge2SortedList.java)
69+
6. [leetcode28:implement-strstr](https://leetcode.com/problems/implement-strstr)
70+
- 难度 ※※※※
71+
- 数据结构 string
72+
- 算法 two pointers, KMP, rolling hash, BM
73+
- 思路
74+
```java
75+
for (int i = 0; i < h_len - n_len + 1; i++) { //剩下的不够匹配完整needle就不用再循环了
76+
for (int j = 0; j < n_len; j++) { //因为是从haystack的i位和needle的0位开始比
77+
if (haystack.charAt(i + j)!=needle.charAt(j)){//所以直接是haystack i+j..和neddle的j比 j从0开始
78+
```
79+
- 题解 [ImptStrStr.java](./src/learn/freq05/ImptStrStr.java)
80+
7. [leetcode50:powx-n](https://leetcode.com/problems/powx-n)
81+
- 难度 ※※※
82+
- 算法 binary search, math
83+
- 思路
84+
1. 先处理负指数,1.0/pow2(x,-n)
85+
2. 然后再用二分思想处理问题 因为指数的运算规律2^8=2^4*2^4 所以 double result=pow2(x, n/2);然后 return的时候如果指数为偶数就 return result*result 基数就在补乘个x (因为在/2的时候基数会损失一个 9/2=4
86+
- 题解 [Pow.java](./src/learn/freq05/Pow.java)
87+
8. [leetcode56:merge-intervals](https://leetcode.com/problems/merge-intervals)
88+
- 难度 ※※※※
89+
- 数据结构 array, linked list, red-black tree
90+
- 算法 sort, merge
91+
- 思路
92+
1. arraylist 转数组,先写comparator给数组排序,按start比。
93+
2. 排序好后弄一个 prev=0位 然后 和从1遍历的curr比
94+
- 如果curr.start>=prev.end 说明有 interval那时候只要在比较prev.end和curr.end 那个end 大就更新prev.edm
95+
- 如果 curr.start<prev.end 说明没有interval直接 result.add(prev); prev=curr;循环结束后再加一次即可。
96+
- 题解 [MergeInterval.java](./src/learn/freq05/MergeInterval.java)
97+
9. [leetcode57:insert-intervals](https://leetcode.com/problems/insert-interval)
98+
- 难度 ※※※※
99+
- 数据结构 array, linked list, red-black tree
100+
- 算法 sort, merge
101+
- 思路 把要插入的add到linked list后 再sort。。然后接下去和上一题完全一样
102+
- 题解 [InsertInterval.java](./src/learn/freq05/InsertInterval.java)
103+
10. [leetcode65:valid-number](https://leetcode.com/problems/valid-number)
104+
- 难度 ※※※※※
105+
- 数据结构 string
106+
- 算法 math
107+
- 思路
108+
1. 如果遍历中有些东西如果有一次就够了可以立个boolean的flag
109+
2. 注意. Arrays.toString(char) 会返回带括号和逗号的数组形式的string 所以要,而不是一个正常string 所以要s = new String(temp);
110+
3. 如果实在做不出 try{ Double.valueOf(s); return true;}catch (Exception e){ return false; }
111+
4. 正则做法 Pattern p = Pattern.compile("^[\\+\\-]?((\\d+(\\.\\d*)?)|(\\.\\d+))(e[\\+\\-]?\\d+)?$");
112+
- 题解[IsNumber.java](./src/learn/freq05/IsNumber.java)
113+
114+
11. [leetcode70:climbing-stairs](https://leetcode.com/problems/climbing-stairs)
115+
- 难度 ※※※※※
116+
- 算法 dp
117+
- 思路
118+
Dynamic Programming, 先确定初始问题, 在递推 recursion解决后面的子问题
119+
```bash
120+
if (n >= 3) { ways= climbStairs(n - 1) + climbStairs(n - 2);}
121+
```
122+
123+
但是这么写会超时.所以用iterator 的方法 复杂度变为0(n)
124+
125+
```base
126+
for(int i=3;i<=n;i++){
127+
n3=n1+n2; // 本质还是f(n)=f(n-1)+f(n-2)
128+
n1=n2; //每当i++时候
129+
n2=n3; //n1=上一次的n2 n2等于上一次的n3
130+
}
131+
```
132+
- 题解 [ClimbStairs.java](./src/learn/freq05/ClimbStairs.java)
133+
134+
12. [leetcode73:set-matrix-zeros](https://leetcode.com/problems/set-matrix-zeroes)
135+
- 难度 ※※※
136+
- 数据结构 array
137+
- 思路 这题和CC1501_7类似 但是cc 150的解法是用了 rows 和columns2个数组来储存0的坐标,比如matrix[i][j]=0 columns[i]++ rows[j]++,然后再次遍历这个matrix,当if(columns[i]!=0||rows[j]!=0)这个matrix[i][j]=0, leetcode上要求不需求辅助空间 那么我们就把matrix的第0行和第0列当做columns[] rows[] 来存0.先检查第0行和第0列 然后在检查从下标1开始的行和列 然后处理下标1开始的行和列,最后再处理第0行和第0
138+
- 题解 [SetMatrixZeroes.java](./src/learn/freq05/SetMatrixZeroes.java)
139+
140+
13. [leetcode88:merge-sorted-array](https://leetcode.com/problems/merge-sorted-array)
141+
- 难度 ※※
142+
- 数据结构 array
143+
- 算法 two pointers, merge
144+
- 思路
145+
1. AB不是都sorted了吗,我们不从头开始比谁小,我们从AB的尾巴开始比谁大
146+
2. 谁大,谁就放到A的[(a最后一个有数据的下标)+(b.length)],然后该--下标的--,接下去就和普通mergesort一样(不要忘记检查2个数组是否为空 比方说 AB有的时候就把B一个一个赋值到A里)
147+
- 题解 [MergeSortedArray.java](./src/learn/freq05/MergeSortedArray.java)
148+
149+
14. [leetcode98:validate-binary-search-tree](https://leetcode.com/problems/validate-binary-search-tree)
150+
- 难度 ※※※
151+
- 数据结构 tree
152+
- 算法 dfs
153+
- 思路 即如果一棵二叉树是BST,那么它的中序遍历是一个递增的数组。所以可以对中序遍历算法稍加修改,
154+
```java
155+
static int lastVisit=Integer.MIN_VALUE;
156+
public boolean isValidBST(TreeNode root) {
157+
if(root==null){ return true;}
158+
if(!isValidBST(root.left)){return false;}//从左子树最左节点开始
159+
if(root.val<=lastVisit){return false;}
160+
lastVisit=root.val;//中间结点
161+
if(!isValidBST(root.right)){return false;}//右子树最左开始
162+
return true;
163+
}
164+
```
165+
- 题解 [ValidateBST.java](./src/learn/freq05/ValidateBST.java)
166+
167+
15. [leetcode125:valid-palindrome](https://leetcode.com/problems/valid-palindrome)
168+
- 难度 ※※
169+
- 数据结构 string
170+
- 算法 two pointers
171+
- 思路 2个指针从头++尾巴--开始对比,然后要处理非字母和数字的字符 可以先tolowercase 然后 ((temp.charAt(start) >= '0' && temp.charAt(start) <= '9') || (temp
172+
.charAt(start) >= 'a' && temp.charAt(start) <= 'z'))
173+
- 题解 [ValidPalindrome.java](./src/learn/freq05/ValidPalindrome.java)
174+
175+
16. [leetcode127:word-ladder](https://leetcode.com/problems/word-ladder)
176+
- 难度 ※※※
177+
- 数据结构 graph
178+
- 算法 bfs, shortest path
179+
- 思路 因为每次只能在词里改一个字母,我们先把start放到一个queue里(可以用linkedlist实现 然后另外一个linkedlist存Integer的distance)然后只要queue里还有词。我们把词取出来,从首字母for循环遍历到尾巴字母,里面再一个循环 每个字母(char temp='a';temp<='z';temp++) 从A遍历到Z 然后再转回string看看字典里有没有,字典里面有再放入word的那个的queue 以此循环 直到==end 或者wordqueue空了返回0
180+
- 题解 [WordLadder.java](./src/learn/freq05/WordLadder.java)
181+
182+
## frequency4

src/entity/Interval.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package entity;
2+
3+
/**
4+
* Created by anduo on 17-3-12.
5+
*/ // Definition for an interval.
6+
public class Interval {
7+
public int start;
8+
public int end;
9+
10+
public Interval() {
11+
start = 0;
12+
end = 0;
13+
}
14+
15+
public Interval(int s, int e) {
16+
start = s;
17+
end = e;
18+
}
19+
20+
}

src/entity/ListNode.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package entity;
2+
3+
/**
4+
* Created by anduo on 17-3-12.
5+
*/
6+
public class ListNode {
7+
public int val;
8+
public ListNode next;
9+
10+
public ListNode(int x) {
11+
val = x;
12+
next = null;
13+
}
14+
}

src/entity/RandomListNode.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package entity;
2+
3+
/**
4+
* Created by anduo on 17-3-12.
5+
*/
6+
public class RandomListNode {
7+
public int label; // 相当于value
8+
public RandomListNode next, random;
9+
10+
public RandomListNode(int x) {
11+
this.label = x;
12+
}
13+
}

src/entity/TreeLinkNode.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package entity;
2+
3+
/**
4+
* Created by anduo on 17-3-12.
5+
*/
6+
public class TreeLinkNode {
7+
public int val;
8+
public TreeLinkNode left, right, next;
9+
10+
public TreeLinkNode(int x) {
11+
val = x;
12+
}
13+
}

src/entity/TreeNode.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package entity;
2+
3+
/**
4+
* Created by anduo on 17-3-12.
5+
*/
6+
public class TreeNode {
7+
public int val;
8+
public TreeNode left;
9+
public TreeNode right;
10+
11+
public TreeNode(int x) {
12+
val = x;
13+
}
14+
}

0 commit comments

Comments
 (0)