Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 456c2a1

Browse filesBrowse files
committed
commit
1 parent eec73ef commit 456c2a1
Copy full SHA for 456c2a1

File tree

Expand file treeCollapse file tree

11 files changed

+554
-191
lines changed
Filter options
Expand file treeCollapse file tree

11 files changed

+554
-191
lines changed

‎.idea/modules.xml

Copy file name to clipboardExpand all lines: .idea/modules.xml
+3Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/workspace.xml

Copy file name to clipboardExpand all lines: .idea/workspace.xml
+222-191Lines changed: 222 additions & 191 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+21Lines 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>
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-07-06 20:12
7+
**/
8+
public class Main {
9+
@Test
10+
public void test1() {
11+
int[][] matrix = {
12+
{1, 4, 7, 11, 15},
13+
{2, 5, 8, 12, 19},
14+
{3, 6, 9, 16, 22},
15+
{10, 13, 14, 17, 24},
16+
{18, 21, 23, 26, 30}
17+
};
18+
Solution solution = new Solution();
19+
20+
Assert.assertEquals(true, solution.searchMatrix(matrix, 5));
21+
Assert.assertEquals(false, solution.searchMatrix(matrix, 20));
22+
}
23+
}
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2019-07-06 20:06
4+
**/
5+
public class Solution {
6+
/**
7+
* <pre>
8+
* Write an efficient algorithm that searches for a value in an m x n matrix.
9+
* This matrix has the following properties:
10+
*
11+
* Integers in each row are sorted in ascending from left to right.
12+
* Integers in each column are sorted in ascending from top to bottom.
13+
* Example:
14+
*
15+
* Consider the following matrix:
16+
*
17+
* [
18+
* [1, 4, 7, 11, 15],
19+
* [2, 5, 8, 12, 19],
20+
* [3, 6, 9, 16, 22],
21+
* [10, 13, 14, 17, 24],
22+
* [18, 21, 23, 26, 30]
23+
* ]
24+
* Given target = 5, return true.
25+
*
26+
* Given target = 20, return false.
27+
* </pre>
28+
*
29+
* @param matrix
30+
* @param target
31+
* @return
32+
*/
33+
public boolean searchMatrix(int[][] matrix, int target) {
34+
35+
if (matrix == null ||matrix.length == 0) {
36+
return false;
37+
}
38+
39+
int i = 0;
40+
int j = matrix[0].length - 1;
41+
while (i < matrix.length && j > -1) {
42+
if (matrix[i][j] == target) {
43+
return true;
44+
} else if (target < matrix[i][j]) {
45+
j--;
46+
} else {
47+
i++;
48+
}
49+
}
50+
51+
return false;
52+
}
53+
}
+21Lines 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>
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import org.junit.Test;
2+
3+
/**
4+
* @author: wangjunchao(王俊超)
5+
* @time: 2019-07-06 20:59
6+
**/
7+
public class Main {
8+
@Test
9+
public void test1() {
10+
Solution solution = new Solution();
11+
System.out.println(solution.diffWaysToCompute("2-1-1"));
12+
}
13+
14+
@Test
15+
public void test2() {
16+
Solution solution = new Solution();
17+
System.out.println(solution.diffWaysToCompute("2*3-4*5"));
18+
}
19+
20+
@Test
21+
public void test3() {
22+
Solution solution = new Solution();
23+
System.out.println(solution.diffWaysToCompute("2"));
24+
}
25+
}
+112Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import java.util.ArrayList;
2+
import java.util.LinkedList;
3+
import java.util.List;
4+
5+
/**
6+
* @author: wangjunchao(王俊超)
7+
* @time: 2019-07-06 20:25
8+
**/
9+
public class Solution {
10+
/**
11+
* <pre>
12+
* Given a string of numbers and operators, return all possible results from computing all the
13+
* different possible ways to group numbers and operators. The valid operators are +, - and *.
14+
*
15+
* Example 1:
16+
*
17+
* Input: "2-1-1"
18+
* Output: [0, 2]
19+
* Explanation:
20+
* ((2-1)-1) = 0
21+
* (2-(1-1)) = 2
22+
* Example 2:
23+
*
24+
* Input: "2*3-4*5"
25+
* Output: [-34, -14, -10, -10, 10]
26+
* Explanation:
27+
* (2*(3-(4*5))) = -34
28+
* ((2*3)-(4*5)) = -14
29+
* ((2*(3-4))*5) = -10
30+
* (2*((3-4)*5)) = -10
31+
* (((2*3)-4)*5) = 10
32+
* </pre>
33+
* @param input
34+
* @return
35+
*/
36+
public List<Integer> diffWaysToCompute(String input) {
37+
List<Character> operator = new ArrayList<>();
38+
List<Integer> operand = new ArrayList<>();
39+
40+
int start = 0;
41+
int end;
42+
while (start < input.length()) {
43+
end = start + 1;
44+
while (end < input.length()
45+
&& input.charAt(end) >= '0'
46+
&& input.charAt(end) <= '9') {
47+
end++;
48+
}
49+
operand.add(Integer.parseInt(input.substring(start, end)));
50+
51+
if (end < input.length()) {
52+
operator.add(input.charAt(end));
53+
}
54+
start = end + 1;
55+
}
56+
57+
return diffWaysToCompute(operand, operator);
58+
}
59+
60+
/**
61+
* @param operand
62+
* @param operator
63+
* @return
64+
*/
65+
private List<Integer> diffWaysToCompute(List<Integer> operand, List<Character> operator) {
66+
67+
List<Integer> result = new LinkedList<>();
68+
69+
if (operator.isEmpty()) {
70+
result.addAll(operand);
71+
return result;
72+
}
73+
74+
75+
if (operator.size() == 1) {
76+
result.add(calculate(operand.get(0), operand.get(1), operator.get(0)));
77+
return result;
78+
}
79+
80+
for (int i = 0; i < operator.size(); i++) {
81+
List<Integer> left = diffWaysToCompute(
82+
new ArrayList<>(operand.subList(0, i + 1)),
83+
new ArrayList<>(operator.subList(0, i)));
84+
List<Integer> right = diffWaysToCompute(
85+
new ArrayList<>(operand.subList(i + 1, operand.size())),
86+
new ArrayList<>(operator.subList(i + 1, operator.size())));
87+
88+
for (int x : left) {
89+
for (int y : right) {
90+
result.add(calculate(x, y, operator.get(i)));
91+
}
92+
}
93+
}
94+
95+
return result;
96+
97+
}
98+
99+
private Integer calculate(Integer a, Integer b, Character op) {
100+
101+
switch (op) {
102+
case '+':
103+
return a + b;
104+
case '-':
105+
return a - b;
106+
case '*':
107+
return a * b;
108+
default:
109+
throw new RuntimeException("no such " + a + op + b + " option exception");
110+
}
111+
}
112+
}

‎[0504][Base 7]/[0504][Base 7].iml

Copy file name to clipboard
+21Lines 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>

‎[0504][Base 7]/src/Main.java

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-07-06 20:00
7+
**/
8+
public class Main {
9+
@Test
10+
public void test1() {
11+
Solution solution = new Solution();
12+
Assert.assertEquals("202", solution.convertToBase7(100));
13+
}
14+
15+
@Test
16+
public void test2() {
17+
Solution solution = new Solution();
18+
Assert.assertEquals("-10", solution.convertToBase7(-7));
19+
}
20+
}

‎[0504][Base 7]/src/Solution.java

Copy file name to clipboard
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2019-07-06 19:59
4+
**/
5+
public class Solution {
6+
/**
7+
* <pre>
8+
* Given an integer, return its base 7 string representation.
9+
*
10+
* Example 1:
11+
* Input: 100
12+
* Output: "202"
13+
* Example 2:
14+
* Input: -7
15+
* Output: "-10"
16+
* Note: The input will be in range of [-1e7, 1e7].
17+
* </pre>
18+
* @param num
19+
* @return
20+
*/
21+
public String convertToBase7(int num) {
22+
String result = "";
23+
boolean positive = num >= 0;
24+
num = Math.abs(num);
25+
26+
do {
27+
result = (num % 7) + result;
28+
num /= 7;
29+
} while (num > 0);
30+
31+
return positive ? result : "-" + result;
32+
}
33+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.