Skip to content

Commit 5cd5824

Browse files
authored
Merge pull request #1 from momo1314/master
update
2 parents 781b0fc + a419263 commit 5cd5824

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-0
lines changed

.gitignore

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Example user template template
3+
### Example user template
4+
5+
# IntelliJ project files
6+
.idea
7+
*.iml
8+
out
9+
gen
10+
### Java template
11+
# Compiled class file
12+
*.class
13+
14+
# Log file
15+
*.log
16+
17+
# BlueJ files
18+
*.ctxt
19+
20+
# Mobile Tools for Java (J2ME)
21+
.mtj.tmp/
22+
23+
# Package Files #
24+
*.jar
25+
*.war
26+
*.nar
27+
*.ear
28+
*.zip
29+
*.tar.gz
30+
*.rar
31+
32+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
33+
hs_err_pid*
34+
35+
.gitignore
36+
.idea/

DP/edit-distance-72.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//建立数组dp[][]来存储 以word1[i]为结尾的字符串 转换成 以word2[j]为结尾的字符串 所需的最小操作数
2+
//当新遍历一对来自word1,word2的字符
3+
//若 word1[i] == word2[j] 表示不需要操作,则dp[i][j]=dp[i-1][j-1]
4+
//若 word1[i] != word2[j] 则可以有三种情况
5+
// 1、替换 word1[i] 把 word1[i] 替换成 word2[j] 需要 dp[i-1][j-1]+1步
6+
// 2、删除 word1[i] 把 word1[i] 删除成 word1[i-1] 需要 dp[i][j-1]+1步
7+
// 3、删除 word2[j] 把 word2[j] 删除成 word2[j-1] 需要 dp[i-1][j]+1步(增加word1和删除word2一个效果)
8+
// 取这三个中最小值
9+
class Solution {
10+
public int minDistance(String word1, String word2) {
11+
int len1 = word1.length();
12+
int len2 = word2.length();
13+
int[][] dp = new int[len1+1][len2+1];
14+
15+
for(int i = 1 ; i <= len1 ; i++) {
16+
dp[i][0] = i;
17+
}
18+
for(int i = 1 ; i <= len2 ;i++) {
19+
dp[0][i] = i;
20+
}
21+
for(int i = 1 ; i <= len1 ; i++) {
22+
for(int j = 1; j <= len2 ; j++) {
23+
if(word1.charAt(i-1) == word2.charAt(j-1)) {
24+
dp[i][j] = dp[i-1][j-1];
25+
} else {
26+
dp[i][j] = Math.min(dp[i-1][j-1] , Math.min(dp[i-1][j],dp[i][j-1])) + 1;
27+
}
28+
}
29+
}
30+
return dp[len1][len2];
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 滑动窗口解法,双指针
3+
*/
4+
class Solution {
5+
public List<Integer> findAnagrams(String s, String p) {
6+
if(s == null || s.length() == 0) return new ArrayList<>();
7+
List<Integer> res = new ArrayList<>();
8+
int[] needs = new int[26];
9+
int[] windows = new int[26];
10+
int left = 0 , right = 0 , total = p.length();
11+
12+
for(char a : p.toCharArray()) {
13+
needs[a - 'a']++;
14+
}
15+
16+
while(right < s.length()) {
17+
char now = s.charAt(right);
18+
if(needs[now - 'a'] > 0) {
19+
windows[now - 'a']++;
20+
if(windows[now - 'a'] <= needs[now - 'a']) {
21+
total -= 1;
22+
}
23+
}
24+
while(total == 0) {
25+
if(right-left+1 == p.length()){
26+
res.add(left);
27+
}
28+
char c = s.charAt(left);
29+
if(needs[c - 'a'] > 0) {
30+
windows[c - 'a']--;
31+
if(windows[c - 'a'] < needs[c - 'a']) {
32+
total++;
33+
}
34+
}
35+
left++;
36+
}
37+
right++;
38+
}
39+
return res;
40+
}
41+
}

stack/basic-calculator-ii-227.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 简单计算器2 简单用栈实现,未使用后缀表达式。
3+
*/
4+
class Solution {
5+
public int calculate(String s) {
6+
Stack<Integer> stack = new Stack<>();
7+
int num = 0 , res = 0;
8+
char op = '+';
9+
for(int i = 0 ; i < s.length() ; i++) {
10+
if(s.charAt(i) >='0'){
11+
num = num*10+(s.charAt(i)-'0');
12+
}
13+
if( (s.charAt(i)<'0'&& s.charAt(i)!=' ') || i==s.length()-1) {
14+
if(op=='+') stack.push(num);
15+
if(op=='-') stack.push(-num);
16+
if(op=='*'||op=='/'){
17+
int temp = (op=='*')? stack.peek()*num:stack.peek()/num;
18+
stack.pop();
19+
stack.push(temp);
20+
}
21+
op=s.charAt(i);
22+
num=0;
23+
}
24+
25+
}
26+
while(!stack.isEmpty()) {
27+
res += stack.peek();
28+
stack.pop();
29+
}
30+
return res;
31+
}
32+
}

stack/mini-parser-385.java

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* 简单入栈出栈操作
3+
* // This is the interface that allows for creating nested lists.
4+
* // You should not implement it, or speculate about its implementation
5+
* public interface NestedInteger {
6+
* // Constructor initializes an empty nested list.
7+
* public NestedInteger();
8+
*
9+
* // Constructor initializes a single integer.
10+
* public NestedInteger(int value);
11+
*
12+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
13+
* public boolean isInteger();
14+
*
15+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
16+
* // Return null if this NestedInteger holds a nested list
17+
* public Integer getInteger();
18+
*
19+
* // Set this NestedInteger to hold a single integer.
20+
* public void setInteger(int value);
21+
*
22+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
23+
* public void add(NestedInteger ni);
24+
*
25+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
26+
* // Return null if this NestedInteger holds a single integer
27+
* public List<NestedInteger> getList();
28+
* }
29+
*/
30+
class Solution {
31+
public NestedInteger deserialize(String s) {
32+
if(s.charAt(0) != '[') return new NestedInteger(Integer.valueOf(s));
33+
Stack<NestedInteger> stack = new Stack<>();
34+
NestedInteger result = new NestedInteger();
35+
for(int i = 0 ; i < s.length() ; i ++){
36+
char c = s.charAt(i);
37+
if(c == '[') {
38+
NestedInteger node = new NestedInteger();
39+
if(!stack.isEmpty()) {
40+
stack.peek().add(node);
41+
}
42+
stack.push(node);
43+
}
44+
if(c >= '0' && c <= '9' || c == '-') {
45+
String num = c+"";
46+
while(i+1 < s.length() && s.charAt(i+1) >= '0' && s.charAt(i+1)<='9') {
47+
num = num+s.charAt(++i);
48+
}
49+
NestedInteger node = new NestedInteger(Integer.valueOf(num));
50+
if(stack.isEmpty()) {
51+
stack.push(node);
52+
} else {
53+
stack.peek().add(node);
54+
}
55+
}
56+
if(c == ']') {
57+
result = stack.pop();
58+
}
59+
}
60+
return result;
61+
}
62+
}

0 commit comments

Comments
 (0)