Skip to content

Commit 0a701f1

Browse files
committed
commit
1 parent 3f5a844 commit 0a701f1

File tree

22 files changed

+924
-272
lines changed

22 files changed

+924
-272
lines changed

.idea/modules.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 295 additions & 272 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
14+
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-07-10 13:23
7+
**/
8+
public class Main {
9+
@Test
10+
public void test1() {
11+
Solution solution = new Solution();
12+
Assert.assertEquals(7, solution.calculate("3+2*2"));
13+
}
14+
15+
@Test
16+
public void test2() {
17+
Solution solution = new Solution();
18+
Assert.assertEquals(5, solution.calculate(" 3 + 5 / 2 "));
19+
}
20+
21+
@Test
22+
public void test3() {
23+
Solution solution = new Solution();
24+
Assert.assertEquals(1, solution.calculate("1-1+1"));
25+
}
26+
27+
@Test
28+
public void test4() {
29+
Solution solution = new Solution();
30+
Assert.assertEquals(1, solution.calculate("1+1-1"));
31+
}
32+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.util.Arrays;
2+
import java.util.Deque;
3+
import java.util.HashSet;
4+
import java.util.LinkedList;
5+
import java.util.Set;
6+
7+
/**
8+
* @author: wangjunchao(王俊超)
9+
* @time: 2019-07-10 10:54
10+
**/
11+
public class Solution {
12+
public int calculate(String s) {
13+
14+
if (s == null || s.length() < 1) {
15+
throw new IllegalArgumentException("s: " + s);
16+
}
17+
18+
Deque<Integer> operands = new LinkedList<>();
19+
Deque<Character> operators = new LinkedList<>();
20+
21+
int idx = 0;
22+
while (idx < s.length()) {
23+
while (idx < s.length() && s.charAt(idx) == ' ') {
24+
idx++;
25+
}
26+
27+
// 找操作数
28+
int start = idx;
29+
idx++;
30+
while (idx < s.length() && Character.isDigit(s.charAt(idx))) {
31+
idx++;
32+
}
33+
// 操作数入栈
34+
operands.addLast(Integer.parseInt(s.substring(start, idx)));
35+
36+
// 找idx后的第一个非空白字符
37+
while (idx < s.length() && s.charAt(idx) == ' ') {
38+
idx++;
39+
}
40+
41+
// 如果是乘法或者除法,就进行一次计算
42+
if (idx < s.length() && (s.charAt(idx) == '*' || s.charAt(idx) == '/')) {
43+
char op = s.charAt(idx);
44+
// 下一个数字的第一个位置
45+
while (idx < s.length() && !Character.isDigit(s.charAt(idx))) {
46+
idx++;
47+
}
48+
49+
start = idx;
50+
while (idx < s.length() && Character.isDigit(s.charAt(idx))) {
51+
idx++;
52+
}
53+
54+
int operand = Integer.parseInt(s.substring(start, idx));
55+
int prev = operands.removeLast();
56+
int result = calculate(prev, operand, op);
57+
// 计算结果放在新的字符串首位,idx变成第一位
58+
// 比如 1*2+3 => 2+3
59+
s = result + s.substring(idx);
60+
idx = 0;
61+
} else if (idx < s.length() && (s.charAt(idx) == '+' || s.charAt(idx) == '-')){
62+
// 如果是加减
63+
operators.addLast(s.charAt(idx));
64+
idx++;
65+
} else {
66+
idx++;
67+
}
68+
}
69+
70+
while (!operators.isEmpty()) {
71+
int a = operands.removeFirst();
72+
int b = operands.removeFirst();
73+
int r = calculate(a, b, operators.removeFirst());
74+
operands.addFirst(r);
75+
}
76+
77+
return operands.getLast();
78+
}
79+
80+
private int calculate(int a, int b, char op) {
81+
switch (op) {
82+
case '+':
83+
return a + b;
84+
case '-':
85+
return a - b;
86+
case '*':
87+
return a * b;
88+
case '/':
89+
return a / b;
90+
default:
91+
return 0;
92+
}
93+
}
94+
}

[0400][Nth Digit]/[0400][Nth Digit].iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="R User Library" level="project" />
11+
<orderEntry type="library" name="R Skeletons" level="application" />
1012
</component>
1113
</module>

[0412][Fizz Buzz]/[0412][Fizz Buzz].iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@
1717
<SOURCES />
1818
</library>
1919
</orderEntry>
20+
<orderEntry type="library" name="R User Library" level="project" />
21+
<orderEntry type="library" name="R Skeletons" level="application" />
2022
</component>
2123
</module>

[0434][Number of Segments in a String]/[0434][Number of Segments in a String].iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@
1717
<SOURCES />
1818
</library>
1919
</orderEntry>
20+
<orderEntry type="library" name="R User Library" level="project" />
21+
<orderEntry type="library" name="R Skeletons" level="application" />
2022
</component>
2123
</module>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
14+
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>

[0455][Assign Cookies]/src/Main.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-07-10 08:39
7+
**/
8+
public class Main {
9+
@Test
10+
public void test1() {
11+
Solution solution = new Solution();
12+
int[] g = {1, 2, 3};
13+
int[] s = {1, 1};
14+
Assert.assertEquals(1, solution.findContentChildren(g, s));
15+
}
16+
17+
@Test
18+
public void test2() {
19+
Solution solution = new Solution();
20+
int[] g = {1, 2};
21+
int[] s = {1, 2, 3};
22+
Assert.assertEquals(2, solution.findContentChildren(g, s));
23+
}
24+
}

0 commit comments

Comments
 (0)