Skip to content

Commit c3f123f

Browse files
committed
commit
1 parent f755077 commit c3f123f

File tree

606 files changed

+20480
-1
lines changed

Some content is hidden

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

606 files changed

+20480
-1
lines changed

.idea/encodings.xml

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

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

[0000][common]/[0000][common].iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
</component>
11+
</module>

[0000][common]/src/FileRename.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.File;
2+
3+
/**
4+
* @author: wangjunchao(王俊超)
5+
* @time: 2019-06-14 10:00
6+
**/
7+
public class FileRename {
8+
public static void main(String[] args) {
9+
String pathName = "/Users/wangjunchao/Project/leetcode";
10+
File path = new File("/Users/wangjunchao/Project/leetcode");
11+
12+
File[] files = path.listFiles();
13+
renameFile(files);
14+
}
15+
16+
public static void renameFile(File[] files) {
17+
for (File f : files) {
18+
if (f.isDirectory()) {
19+
renameFile(f.listFiles());
20+
}
21+
}
22+
23+
for (File f : files) {
24+
String name = f.getName();
25+
name = name.replaceFirst("\\[", "[0");
26+
System.out.println(name);
27+
f.renameTo(new File(f.getParent() + "/" + name));
28+
}
29+
}
30+
}

[0000][common]/src/MaxSubArray.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author: wangjunchao(王俊超)
7+
* @time: 2019-06-16 11:16
8+
**/
9+
public class MaxSubArray {
10+
public int[] solve(int[] array) {
11+
if (array == null || array.length < 1) {
12+
return null;
13+
}
14+
15+
16+
// result[0] 最大的子数组和
17+
// result[1] 最大的子数组的起始位置
18+
// result[2] 最大的子数组的结束位置
19+
int[] result = {Integer.MIN_VALUE, -1, -1};
20+
21+
int sum = 0;
22+
int begin = 0;
23+
for (int i = 0; i < array.length; i++) {
24+
if (sum >= 0) {
25+
sum += array[i];
26+
} else {
27+
sum = array[i];
28+
begin = i;
29+
}
30+
31+
if (result[0] <= sum) {
32+
result[0] = sum;
33+
result[1] = begin;
34+
result[2] = i;
35+
}
36+
}
37+
38+
return result;
39+
}
40+
41+
@Test
42+
public void test1() {
43+
MaxSubArray maxSubArray = new MaxSubArray();
44+
45+
System.out.println(Arrays.toString(maxSubArray.solve(new int[]{1, 2, 3, 4, 5, 6})));
46+
}
47+
}

[0001][Two Sum]/[0001][Two Sum].iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
</component>
11+
</module>

[0001][Two Sum]/[0001][TwoSum].iml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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="library" name="R User Library" level="project" />
11+
<orderEntry type="library" name="R Skeletons" level="application" />
12+
</component>
13+
</module>

[0001][Two Sum]/src/Main.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import org.junit.Test;
2+
3+
/**
4+
* Author: 王俊超
5+
* Date: 2015-06-18
6+
* Time: 09:17
7+
* Declaration: All Rights Reserved !!!
8+
*/
9+
public class Main {
10+
@Test
11+
public void test0() {
12+
int[] result = new Solution().twoSum(new int[]{0, 4, 3, 0}, 0);
13+
14+
System.out.println("[" + result[0] + " ," + result[1] + "]");
15+
}
16+
17+
@Test
18+
public void test1() {
19+
int[] result = new Solution().twoSum(new int[]{3, 2, 4}, 6);
20+
System.out.println("[" + result[0] + " ," + result[1] + "]");
21+
}
22+
23+
@Test
24+
public void test2() {
25+
int[] result = new Solution().twoSum(new int[]{3, 2, 4}, 6);
26+
System.out.println("[" + result[0] + " ," + result[1] + "]");
27+
}
28+
@Test
29+
public void test3() {
30+
int[] nums = {876,879,155,291,431,296,592,965,502,173,869,504,258,342,192,478,270,341,811,794,472,625,229,829,122,858,738,481,102,946,305,399,216,752,413,352,271,193,534,508,152,989,154,456,168,510,391,28,317,409,609,532,784,160,696,105,245,231,20,17,81,781,79,816,918,838,123,602,338,997,192,947,388,515,510,441,175,539,708,980,207,336,524,610,3,427,282,84,953,855,117,737,288,371,623,484,738,874,426,202,481,132,499,500,89,786,276,221,857,398,242,639,771,149,758,775,39,836,70,903,193,959,169,851,798,815,755,498,308,70,217,765,504,498,56,547,578,977,882,909,9,874,223,39,429,982,129,712,77,996,43,613,800,810,73,993,763,978,912,255,468,937,987,701,155,347,980,147,698,41,353,178,396,241,71,482,40,593,993,959,193,544,376,752,804,194,800,837,673,261,348,963,918,217,945,271,493,538,203,54,850,753,954,312,584,399,504,62,124,790,542,239,662,410,12,362,798,726,798,780,785,737,280,931,452,643,362,190,975,520,219,330,290,451,22,756,837,787,758,661,75,697,419,485,290,84,401,447,400,311,121,216,574,724,733,496,680,831,736,43,578,201,109,197,125,66,739,339,925,148,381,513,152,305,603,516,979,133,993,430,167,826,526,290,562,559,947,448,903,289,259,221,915,71,879,639,390,588,496,430,778,722,421,821,436,621,959,728,81,117,202,17,408,829,438,970,93,738,838,902,248,128,903,800,567,829,3,407,306,773,71,323,492,305,301,28,220,455,320,478,873,483,521,260,460,342,846,577,874,530,588,965,985,606,410,443,662,81,667,27,912,602,957,822,164,489,942,414,549,991,747,680,498,831,805,89,846,467,909,7,651,250,534,984,587,348,150,329,194,20,519,250,232,224,378,539,83,177,872,130,419,387,654,917,259,447,979,184,965,51,349,422,983,682,172,177,177,484,652,930,495,65,511,318,621,297,803,476,370,826,328,150,354,393,900,340,73,781,70,260,293,862,335,395,51,326,363,78,968,446,565,683,654,767,719,324,2,617,451,56,789,464,119,53,269,369,137,612,54,217,719,823,601,663,310,594,301,636,22,333,351,126,810,812,827,634,441,534,434,967,637,795,335,965,876,778,987,217,451,264,341,566,656,612,413,682,429,161,801,167,309,846,754,541,9,711,707,848,989,580,20,431,163,252,200,54,56,666,425,592,513,230,894,20,260,282,297,129,414,326,577,184,698,620,138,131,236,848,995,879,354,107,67,92,260,531,757,640,305,848,959,416,109,513,769,131,501,197,225,358,67,663,761,742,83,648,230,59,873,231,228,470,503,615,245,258,84,832,132,156,324,27,583,766,676,130,978,306,387,733,592,763,592,487,504,493,139,897,290,432,976,946,24,586,104,648,333,2,359,166,968,990,39,353,376,839,9,75,874,203,762,489,21,14,888,570,449,539,772,919,697,883,278,18,151,113,148,330,158,772,852,93,288,213,299,338,297,862,371,708,815,108,326,115,923,541,144,521,441,99,773,950,519,948,258,328,624,936,681,935,328,70,826,110,153,236,191,222,340,653,918,976,857,184,193,397,39,190,147,763,760,95,917,559,529,680,376,389,215,705,586,205,653,324,960,33,404,888,680,95,263,860,150,683,930,588,9,690,919,745,815,331,425,879,648,398,2,997,865,429,399,264,704,699,333,126,753,565,529,35,520,94,401,552,592,543,864,23,764,763,51,631,348,198,255,73,281,996,371,23,581,84,367,469,604,716,393,942,764,239,502,501,973,438,760,398,158,853,178,348,659,1000,739,296,444,587,528,355,867,615,847,885,160,357,618,959,330,82,182,59,224,355,250,270,447,534,97,590,284,909,406,954,419,909,158,626,818,350,994,609,540,957,152,827,830,386,380,318,580,853,440,789,432,710,955,381,241,930,880,632,750,876,189,662,127,434,38,144,20,424,27,466,538,158,416,508,990,650,698,990,970,663,121,9,713,489,977,530,694,141,930,169,695,305,567,368,777,442,668,746,618,86,592,185,328,772,213,644,440,178,243,774,467,991,455,404,919,197,830,568,661,826,841,695,52,982,515,47,47,198,9,272,425,975,472,9,302,338,470,542,247,492,367,180,708,521,592,58,572,887,670,314,191,280,256,845,971,157,725,862,452,76,200,538,44,324,992,459,196,18,64,147,423,187,191,246,305,973,802,832,436,444,242,979,351,733,459,825,833,691,372,861,617,618,190,57,848,527,56,378,533,308,430,473,701,401,871,790,459,216,983,305,61,391,251,447,661,951,150,28,572,206,299,477,703,301,227,960,866,450,335,337,852,906,956,873,893,867,196,131,456,608,688,840,569,91,922,606,961,906,836,168,838,91,607,186,754,708,477,248,138,211,458,17,509,645,629,816,47,185,661,856,508,984,320,763,297,9,446,970,472,12,386,476,686,940,387,721,546,206,110,349,88,781,150,308,136,809,670,291,767,889,926,999,832,462,706,13,9,753,458,309,984,404,801,366,56,611,38,691,174,670,306,229,12,151,697,415,180,655,418,975,781,40,448,625,775,722,350,163,397,634,102,961,322,354,836,652,877,997,397,957,640,70,467,976,901,792,173,869,248,829,919,89,324,9,639,560,744,890,846,452,197,558,756,988,771,573,494,64,423,348,296,587,327,909,371,24,369,174,132,197,412,142,257,790,770,171,875,724,608,329,256,626,868,22,311,499,933,173,78,631,931,191,132,970,194,778,33,832,75,76,63,271,905,164,970,716,216,828,56,131,898,565,791,47,634,205,118,280,605,896,433,38,39,303,242,746,673,541,759,588,990,586,244,152,586,371,666,361,691,815,658,537,371,482,656,117,316,327,368,657,848,557,761,221,147,673,945,914,976,579,804,405,182,89,429,133,485,939,586,452,20,292,108,747,188,899,293,125,976,573,162,592,880,241,685,191,539,361,430,84,791,903,475,96,388,485,416,583,944,939,987,939,545,474,272,494,664,543,480,812,212,400,728,28,379,410,127,607,59,614,883,509,695,765,533,665,754,848,268,159,678,807,325,125,92,208,216,337,697,778,466,861,22,950,74,804,925,617,159,73,676,712,558,487,711,774,383,817,737,555,811,304,743,27,67,535,426,766,615,102,437,765,291,718,641,951,255,375,442,204,108,455,592,364,457,758,486,593,780,277,789,323,404,473,258,953,318,898,555,390,727,510,783,427,806,92,33,474,858,851,783,12,752,356,942,307,235,397,915,502,939};
31+
int target = 28;
32+
int[] result = new Solution().twoSum(nums, target);
33+
System.out.println("[" + result[0] + " ," + result[1] + "]");
34+
}
35+
}

[0001][Two Sum]/src/Solution.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
/**
7+
* Author: 王俊超
8+
* Date: 2015-06-17
9+
* Time: 20:27
10+
* Declaration: All Rights Reserved !!!
11+
*/
12+
public class Solution {
13+
14+
/**
15+
* <pre>
16+
* Given an array of integers, find two numbers such that they add up to a specific target number.
17+
* The function twoSum should return indices of the two numbers such that they add up to the target,
18+
* where index1 must be less than index2. Please note that your returned answers (both index1 and index2)
19+
* are not zero-based.
20+
* You may assume that each input would have exactly one solution.
21+
*
22+
* Input: numbers={2, 7, 11, 15}, target=9
23+
* Output: index1=1, index2=2
24+
*
25+
* 题目大意
26+
* 给定一个整数数组,找出其中两个数满足相加等于你指定的目标数字。
27+
* 要求:这个函数twoSum必须要返回能够相加等于目标数字的两个数的索引,且index1必须要小于index2。
28+
* 请注意一点,你返回的结果(包括index1和index2)都不是基于0开始的。你可以假设每一个输入肯定只有一个结果。
29+
*
30+
* 解题思路
31+
* 创建一个辅助类数组,对辅助类进行排序,使用两个指针,开始时分别指向数组的两端,看这两个下标对应的值是否
32+
* 等于目标值,如果等于就从辅助类中找出记录的下标,构造好返回结果,返回。如果大于就让右边的下标向左移,
33+
* 进入下一次匹配,如果小于就让左边的下标向右移动,进入下一次匹配,直到所有的数据都处理完
34+
* </pre>
35+
*
36+
* @param nums
37+
* @param target
38+
* @return
39+
*/
40+
41+
public int[] twoSum(int[] nums, int target) {
42+
int[] result = {0, 0};
43+
44+
// 因为无素可能会重复
45+
Map<Integer, List<Integer>> map = new HashMap<>(nums.length);
46+
47+
for (int i = 0; i < nums.length; i++) {
48+
if (map.containsKey(nums[i])) {
49+
map.get(nums[i]).add(i);
50+
} else {
51+
List<Integer> list = new ArrayList<>();
52+
list.add(i);
53+
map.put(nums[i], list);
54+
}
55+
}
56+
57+
for (int num : nums) {
58+
int gap = target - num;
59+
if (map.containsKey(gap)) {
60+
// 同样的元素最多只可以有两个
61+
if (gap == num && map.get(num).size() >= 2) {
62+
List<Integer> list = map.get(num);
63+
result[0] = Math.min(list.get(0), list.get(1));
64+
result[1] = Math.max(list.get(0), list.get(1));
65+
} else if (gap != num){
66+
result[0] = Math.min(map.get(num).get(0), map.get(gap).get(0));
67+
result[1] = Math.max(map.get(num).get(0), map.get(gap).get(0));
68+
}
69+
}
70+
}
71+
72+
return result;
73+
}
74+
}

[0001][Two Sum]/src/Solution2.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.util.Arrays;
2+
3+
/**
4+
* Author: 王俊超
5+
* Date: 2015-06-17
6+
* Time: 20:27
7+
* Declaration: All Rights Reserved !!!
8+
*/
9+
public class Solution2 {
10+
private static class Node implements Comparable<Node> {
11+
int val;
12+
int idx;
13+
14+
public Node() {
15+
}
16+
17+
public Node(int val, int idx) {
18+
this.val = val;
19+
this.idx = idx;
20+
}
21+
22+
@Override
23+
public int compareTo(Node o) {
24+
if (o == null) {
25+
return -1;
26+
}
27+
return this.val - o.val;
28+
}
29+
}
30+
31+
32+
/**
33+
* <pre>
34+
* Given an array of integers, find two numbers such that they add up to a specific target number.
35+
* The function twoSum should return indices of the two numbers such that they add up to the target,
36+
* where index1 must be less than index2. Please note that your returned answers (both index1 and index2)
37+
* are not zero-based.
38+
* You may assume that each input would have exactly one solution.
39+
*
40+
* Input: numbers={2, 7, 11, 15}, target=9
41+
* Output: index1=1, index2=2
42+
*
43+
* 题目大意
44+
* 给定一个整数数组,找出其中两个数满足相加等于你指定的目标数字。
45+
* 要求:这个函数twoSum必须要返回能够相加等于目标数字的两个数的索引,且index1必须要小于index2。
46+
* 请注意一点,你返回的结果(包括index1和index2)都不是基于0开始的。你可以假设每一个输入肯定只有一个结果。
47+
*
48+
* 解题思路
49+
* 创建一个辅助类数组,对辅助类进行排序,使用两个指针,开始时分别指向数组的两端,看这两个下标对应的值是否
50+
* 等于目标值,如果等于就从辅助类中找出记录的下标,构造好返回结果,返回。如果大于就让右边的下标向左移,
51+
* 进入下一次匹配,如果小于就让左边的下标向右移动,进入下一次匹配,直到所有的数据都处理完
52+
* </pre>
53+
*
54+
* @param nums
55+
* @param target
56+
* @return
57+
*/
58+
59+
public int[] twoSum(int[] nums, int target) {
60+
int[] result = {0, 0};
61+
62+
Node[] tmp = new Node[nums.length];
63+
for (int i = 0; i < nums.length; i++) {
64+
tmp[i] = new Node(nums[i], i);
65+
}
66+
67+
// 先排序,然后左右夹逼,排序O(n log n),左右夹逼O(n),最终O(n log n)。但是注
68+
// 意,这题需要返回的是下标,而不是数字本身,因此这个方法不好。
69+
Arrays.sort(tmp);
70+
71+
int lo = 0;
72+
int hi = nums.length - 1;
73+
74+
75+
while (lo < hi) {
76+
if (tmp[lo].val + tmp[hi].val == target) {
77+
78+
if (tmp[lo].idx > tmp[hi].idx) {
79+
result[0] = tmp[hi].idx + 1;
80+
result[1] = tmp[lo].idx + 1;
81+
} else {
82+
result[0] = tmp[lo].idx + 1;
83+
result[1] = tmp[hi].idx + 1;
84+
}
85+
break;
86+
} else if (tmp[lo].val + tmp[hi].val > target) {
87+
hi--;
88+
} else {
89+
lo++;
90+
}
91+
}
92+
return result;
93+
}
94+
}

0 commit comments

Comments
 (0)