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 2bcd6ab

Browse filesBrowse files
committed
commit
1 parent e1e62aa commit 2bcd6ab
Copy full SHA for 2bcd6ab

File tree

Expand file treeCollapse file tree

25 files changed

+1238
-329
lines changed
Filter options
Expand file treeCollapse file tree

25 files changed

+1238
-329
lines changed

‎.idea/modules.xml

Copy file name to clipboardExpand all lines: .idea/modules.xml
+6Lines changed: 6 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
+337-329Lines changed: 337 additions & 329 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎[0230][Kth Smallest Element in a BST]/[0230][Kth Smallest Element in a BST].iml

Copy file name to clipboardExpand all lines: [0230][Kth Smallest Element in a BST]/[0230][Kth Smallest Element in a BST].iml
+2Lines 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>
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
<orderEntry type="library" name="R User Library" level="project" />
21+
<orderEntry type="library" name="R Skeletons" level="application" />
22+
</component>
23+
</module>
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
import java.util.Arrays;
5+
6+
/**
7+
* @author: wangjunchao(王俊超)
8+
* @time: 2019-07-02 15:56
9+
**/
10+
public class Main {
11+
@Test
12+
public void test1() {
13+
int[] nums = {1, 2, 3};
14+
Solution solution = new Solution(nums);
15+
16+
int[] shuffle = solution.shuffle();
17+
System.out.println(Arrays.toString(shuffle));
18+
19+
int[] reset = solution.reset();
20+
Assert.assertArrayEquals(nums, reset);
21+
22+
shuffle = solution.shuffle();
23+
System.out.println(Arrays.toString(shuffle));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
29+
Solution solution = new Solution(nums);
30+
31+
int[] shuffle = solution.shuffle();
32+
System.out.println(Arrays.toString(shuffle));
33+
34+
int[] reset = solution.reset();
35+
Assert.assertArrayEquals(nums, reset);
36+
37+
shuffle = solution.shuffle();
38+
System.out.println(Arrays.toString(shuffle));
39+
}
40+
}
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.Arrays;
2+
import java.util.Random;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-07-02 15:51
7+
**/
8+
public class Solution {
9+
10+
private int[] original;
11+
12+
/**
13+
* <pre>
14+
* // Init an array with set 1, 2, and 3.
15+
* int[] nums = {1,2,3};
16+
* Solution solution = new Solution(nums);
17+
*
18+
* // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3]
19+
* must equally likely to be returned.
20+
* solution.shuffle();
21+
*
22+
* // Resets the array back to its original configuration [1,2,3].
23+
* solution.reset();
24+
*
25+
* // Returns the random shuffling of array [1,2,3].
26+
* solution.shuffle();
27+
* </pre>
28+
*
29+
* @param nums
30+
*/
31+
public Solution(int[] nums) {
32+
original = nums;
33+
}
34+
35+
/**
36+
* Resets the array to its original configuration and return it.
37+
*/
38+
public int[] reset() {
39+
return Arrays.copyOf(original, original.length);
40+
}
41+
42+
/**
43+
* Returns a random shuffling of the array.
44+
*/
45+
public int[] shuffle() {
46+
int[] copy = Arrays.copyOf(original, original.length);
47+
48+
Random random = new Random();
49+
for (int i = 0; i < copy.length - 1; i++) {
50+
swap(copy, i, i + random.nextInt(copy.length - i));
51+
}
52+
53+
return copy;
54+
}
55+
56+
private void swap(int[] copy, int i, int j) {
57+
int t = copy[i];
58+
copy[i] = copy[j];
59+
copy[j] = t;
60+
}
61+
}

‎[0401][Binary Watch]/[0401][Binary Watch].iml

Copy file name to clipboardExpand all lines: [0401][Binary Watch]/[0401][Binary Watch].iml
+2Lines 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>
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
<orderEntry type="library" name="R User Library" level="project" />
21+
<orderEntry type="library" name="R Skeletons" level="application" />
22+
</component>
23+
</module>
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-07-02 08:18
7+
**/
8+
public class Main {
9+
@Test
10+
public void test1() {
11+
int[] arr = {3, 2, 1};
12+
Solution solution = new Solution();
13+
Assert.assertEquals(1, solution.thirdMax(arr));
14+
}
15+
16+
@Test
17+
public void test2() {
18+
int[] arr = {2, 1};
19+
Solution solution = new Solution();
20+
Assert.assertEquals(2, solution.thirdMax(arr));
21+
}
22+
23+
@Test
24+
public void test3() {
25+
int[] arr = {2, 3, 2, 1};
26+
Solution solution = new Solution();
27+
Assert.assertEquals(1, solution.thirdMax(arr));
28+
}
29+
30+
@Test
31+
public void test4() {
32+
int[] arr = {1, 2, 2, 5, 3, 5};
33+
Solution solution = new Solution();
34+
Assert.assertEquals(2, solution.thirdMax(arr));
35+
}
36+
}
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.SortedSet;
2+
import java.util.TreeSet;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-07-02 08:09
7+
**/
8+
public class Solution {
9+
/**
10+
* <pre>
11+
* Given a non-empty array of integers, return the third maximum number in this array.
12+
* If it does not exist, return the maximum number. The time complexity must be in O(n).
13+
*
14+
* Example 1:
15+
* Input: [3, 2, 1]
16+
*
17+
* Output: 1
18+
*
19+
* Explanation: The third maximum is 1.
20+
* Example 2:
21+
* Input: [1, 2]
22+
*
23+
* Output: 2
24+
*
25+
* Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
26+
* Example 3:
27+
* Input: [2, 2, 3, 1]
28+
*
29+
* Output: 1
30+
*
31+
* Explanation: Note that the third maximum here means the third maximum distinct number.
32+
* Both numbers with value 2 are both considered as second maximum.
33+
* </pre>
34+
*
35+
* @param nums
36+
* @return
37+
*/
38+
public int thirdMax(int[] nums) {
39+
SortedSet<Integer> result = new TreeSet<>();
40+
41+
for (int i : nums) {
42+
addResult(i, result);
43+
}
44+
45+
return result.size() == 3 ? result.first() : result.last();
46+
}
47+
48+
private void addResult(int i, SortedSet<Integer> result) {
49+
if (result.size() < 3) {
50+
result.add(i);
51+
} else if (result.size() == 3 && result.first() < i && !result.contains(i)) {
52+
result.remove(result.first());
53+
result.add(i);
54+
}
55+
}
56+
}

‎[0429][N-ary Tree Level Order Traversal]/[0429][N-ary Tree Level Order Traversal].iml

Copy file name to clipboardExpand all lines: [0429][N-ary Tree Level Order Traversal]/[0429][N-ary Tree Level Order Traversal].iml
+2Lines 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>
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
<orderEntry type="library" name="R User Library" level="project" />
21+
<orderEntry type="library" name="R Skeletons" level="application" />
22+
</component>
23+
</module>
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import org.junit.Test;
2+
3+
/**
4+
* @author: wangjunchao(王俊超)
5+
* @time: 2019-07-02 08:38
6+
**/
7+
public class Main {
8+
@Test
9+
public void test1() {
10+
int[] arr = {4, 3, 2, 7, 8, 2, 3, 1};
11+
Solution solution = new Solution();
12+
System.out.println(solution.findDisappearedNumbers(arr));
13+
}
14+
}
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-07-02 08:29
7+
**/
8+
public class Solution {
9+
/**
10+
* <pre>
11+
* Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear
12+
* twice and others appear once.
13+
*
14+
* Find all the elements of [1, n] inclusive that do not appear in this array.
15+
*
16+
* Could you do it without extra space and in O(n) runtime? You may assume the returned list
17+
* does not count as extra space.
18+
*
19+
* Example:
20+
*
21+
* Input:
22+
* [4,3,2,7,8,2,3,1]
23+
*
24+
* Output:
25+
* [5,6]
26+
* </pre>
27+
*
28+
* @param nums
29+
* @return
30+
*/
31+
public List<Integer> findDisappearedNumbers(int[] nums) {
32+
33+
// i表示第几个元素,从1开始计数
34+
for (int i = 1; i <= nums.length; i++) {
35+
// nums[idx - 1] != idx 对于nums[8] = 9的情况
36+
// nums[idx - 1] == nums[nums[idx - 1] - 1] 位置被占用了,表示有相同的数
37+
while (nums[i - 1] != i && nums[i - 1] != nums[nums[i - 1] - 1]) {
38+
swap(nums, i - 1, nums[i - 1] - 1);
39+
}
40+
}
41+
42+
List<Integer> result = new ArrayList<>();
43+
for (int i = 1; i <= nums.length; i++) {
44+
if (nums[i - 1] != i) {
45+
result.add(i);
46+
}
47+
}
48+
49+
return result;
50+
}
51+
52+
private void swap(int[] nums, int x, int y) {
53+
int tmp = nums[x];
54+
nums[x] = nums[y];
55+
nums[y] = tmp;
56+
}
57+
}

0 commit comments

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