Skip to content

Commit 763737a

Browse files
committed
commit
1 parent 3095511 commit 763737a

File tree

14 files changed

+571
-163
lines changed

14 files changed

+571
-163
lines changed

.idea/modules.xml

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

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
1+
import org.junit.Test;
2+
13
/**
24
* Author: 王俊超
35
* Date: 2015-06-18
46
* Time: 09:17
57
* Declaration: All Rights Reserved !!!
68
*/
79
public class Main {
8-
public static void main(String[] args) {
10+
@Test
11+
public void test0() {
912
int[] result = new Solution().twoSum(new int[]{0, 4, 3, 0}, 0);
1013

1114
System.out.println("[" + result[0] + " ," + result[1] + "]");
1215
}
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+
}
1335
}

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

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import java.util.Arrays;
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
25

36
/**
47
* Author: 王俊超
@@ -7,27 +10,6 @@
710
* Declaration: All Rights Reserved !!!
811
*/
912
public class Solution {
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-
3113

3214
/**
3315
* <pre>
@@ -59,34 +41,34 @@ public int compareTo(Node o) {
5941
public int[] twoSum(int[] nums, int target) {
6042
int[] result = {0, 0};
6143

62-
Node[] tmp = new Node[nums.length];
44+
// 因为无素可能会重复
45+
Map<Integer, List<Integer>> map = new HashMap<>(nums.length);
46+
6347
for (int i = 0; i < nums.length; i++) {
64-
tmp[i] = new Node(nums[i], 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+
}
6555
}
6656

67-
Arrays.sort(tmp);
68-
69-
int lo = 0;
70-
int hi = nums.length - 1;
71-
72-
73-
while (lo < hi) {
74-
if (tmp[lo].val + tmp[hi].val == target) {
75-
76-
if (tmp[lo].idx > tmp[hi].idx) {
77-
result[0] = tmp[hi].idx + 1;
78-
result[1] = tmp[lo].idx + 1;
79-
} else {
80-
result[0] = tmp[lo].idx + 1;
81-
result[1] = tmp[hi].idx + 1;
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));
8268
}
83-
break;
84-
} else if (tmp[lo].val + tmp[hi].val > target) {
85-
hi--;
86-
} else {
87-
lo++;
8869
}
8970
}
71+
9072
return result;
9173
}
9274
}

[001][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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-06-14 13:21
7+
**/
8+
public class Main {
9+
10+
@Test
11+
public void test() {
12+
int[] nums1 = {1, 3};
13+
int[] nums2 = {2};
14+
Solution solution = new Solution();
15+
Assert.assertEquals(2, solution.findMedianSortedArrays(nums1, nums2), 0.000001);
16+
}
17+
18+
@Test
19+
public void test2() {
20+
int[] nums1 = {1, 2};
21+
int[] nums2 = {3, 4};
22+
Solution solution = new Solution();
23+
Assert.assertEquals(2.5, solution.findMedianSortedArrays(nums1, nums2), 0.000001);
24+
}
25+
26+
@Test
27+
public void test3() {
28+
int[] nums1 = {6};
29+
int[] nums2 = {1, 2, 3, 4, 5};
30+
Solution solution = new Solution();
31+
Assert.assertEquals(3.5, solution.findMedianSortedArrays(nums1, nums2), 0.000001);
32+
}
33+
34+
35+
}

[004][Median of Two Sorted Arrays]/src/Solution.java

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,57 +23,68 @@ public class Solution {
2323
* @return
2424
*/
2525
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
26-
27-
if (nums1 == null) {
28-
nums1 = new int[0];
29-
}
30-
31-
if (nums2 == null) {
32-
nums2 = new int[0];
26+
int total = nums1.length + nums2.length;
27+
if (total % 2 != 0) {
28+
return findKth(nums1, 0, nums2, 0, total / 2 + 1);
29+
} else {
30+
return (findKth(nums1, 0, nums2, 0, total / 2)
31+
+ findKth(nums1, 0, nums2, 0, total / 2 + 1)
32+
) / 2.0;
3333
}
34+
}
3435

35-
int len1 = nums1.length;
36-
int len2 = nums2.length;
36+
/**
37+
* 在数组两个数组中找第k大的元素,k=1, 2, 3, ...
38+
*
39+
* @param nums1
40+
* @param nums2
41+
* @param k
42+
* @return
43+
**/
3744

38-
if (len1 < len2) {
39-
// 确保第一个数组比第二个数组长度大
40-
return findMedianSortedArrays(nums2, nums1);
45+
public int findKth(int[] nums1, final int start1,
46+
int[] nums2, final int start2,
47+
final int k) {
48+
// 将要查找长度小的放在前面
49+
if (nums1.length - start1 > nums2.length - start2) {
50+
return findKth(nums2, start2, nums1, start1, k);
4151
}
4252

43-
// 如果长度小的数组长度为0,就返回前一个数组的中位数
44-
if (len2 == 0) {
45-
return (nums1[(len1 - 1) / 2] + nums1[len1 / 2]) / 2.0;
53+
if (nums1.length == start1) {
54+
return nums2[start2 + k - 1];
4655
}
4756

57+
// 第一个,就找两个中比较小的
58+
if (k == 1) {
59+
return Math.min(nums1[start1], nums2[start2]);
60+
}
4861

49-
int lo = 0;
50-
int hi = len2 * 2;
51-
int mid1;
52-
int mid2;
53-
double l1;
54-
double l2;
55-
double r1;
56-
double r2;
57-
58-
while (lo <= hi) {
59-
mid2 = (lo + hi) / 2;
60-
mid1 = len1 + len2 - mid2;
61-
62-
l1 = (mid1 == 0) ? Integer.MIN_VALUE : nums1[(mid1 - 1) / 2];
63-
l2 = (mid2 == 0) ? Integer.MIN_VALUE : nums2[(mid2 - 1) / 2];
6462

65-
r1 = (mid1 == len1 * 2) ? Integer.MAX_VALUE : nums1[mid1 / 2];
66-
r2 = (mid2 == len2 * 2) ? Integer.MAX_VALUE : nums2[mid2 / 2];
63+
// 在num1, nums2数组中各找一半的数,相当于每次丢掉要查总数的1/4
64+
int half = Math.min(k / 2, nums1.length - start1);
65+
// nums2数组中可找的位置
66+
int ia = half + start1;
67+
// nums2数组中可找的位置
68+
int ib = k - half + start2;
6769

68-
if (l1 > r2) {
69-
lo = mid2 + 1;
70-
} else if (l2 > r1) {
71-
hi = mid2 - 1;
72-
} else {
73-
return (Math.max(l1, l2) + Math.min(r1, r2)) / 2;
74-
}
70+
// nums1[start, ..., ia-1, ia, ..., nums1.length]
71+
// nums2[start, ..., ib-1, ib, ..., nums2.length]
72+
// 说明子数组nums1[start, ..., ia-1]可以弃了,要找的值在nums1[ia, ..., nums1.length]
73+
// 和nums2[start, ..., ib-1, ib, ..., nums2.length]中
74+
if (nums1[ia - 1] < nums2[ib - 1]) {
75+
// k - (ia - start1) = k - (half + start1 - start1)=k - half
76+
return findKth(nums1, ia, nums2, start2, k - (ia - start1));
7577
}
7678

77-
return -1;
79+
// 说明子数组nums2[start, ..., ib-1]可以弃了,要找的值在nums2[ib, ..., nums2.length]
80+
// nums1[start, ..., ia-1, ia, ..., nums1.length]
81+
else if (nums1[ia - 1] > nums2[ib - 1]) {
82+
// k - (ib - start2) = k - (k - half + start2 - start2)
83+
return findKth(nums1, start1, nums2, ib, half);
84+
}
85+
// 两个值相等说明找到了,
86+
else {
87+
return nums1[ia - 1];
88+
}
7889
}
7990
}

0 commit comments

Comments
 (0)