Skip to content

Commit fb8ea7e

Browse files
committed
update
1 parent 55d872e commit fb8ea7e

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package problem41;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* Created by bjyangrubing on 2016/8/25.
7+
*/
8+
public class TwoNumbersWithSum {
9+
10+
ArrayList<ArrayList<Integer>> rtList = new ArrayList<ArrayList<Integer>>();
11+
12+
/**
13+
* 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
14+
*
15+
* @param array
16+
* @param sum
17+
* @return
18+
*/
19+
public ArrayList<Integer> FindNumbersWithSum(int[] array, int sum) {
20+
ArrayList<Integer> rtList = new ArrayList<Integer>();
21+
if (array == null || array.length <= 0)
22+
return rtList;
23+
24+
for (int i = 0, j = array.length - 1; i < array.length - 1 && i < j; ) {
25+
if (array[i] + array[j] == sum) {
26+
rtList.add(array[i]);
27+
rtList.add(array[j]);
28+
break;
29+
} else if (array[i] + array[j] < sum) {
30+
i++;
31+
} else
32+
j--;
33+
}
34+
35+
return rtList;
36+
}
37+
38+
/**
39+
* 输入一个整数s,打印出所有和为s的连续正数序列.
40+
*/
41+
public ArrayList<ArrayList<Integer>> FindContinuousSequence(int sum) {
42+
if (sum < 3)
43+
return rtList;
44+
int small = 1;
45+
int big = 2;
46+
47+
int middle = (1 + sum) / 2; //small的终止条件
48+
int curSum = small + big;
49+
50+
while (small < middle) {
51+
if (curSum == sum) {
52+
addToRtList(small, big);
53+
}
54+
55+
while (small < middle && curSum > sum) {
56+
curSum -= small;
57+
small++;
58+
if (curSum == sum) {
59+
addToRtList(small, big);
60+
}
61+
}
62+
big++;
63+
curSum += big;
64+
65+
}
66+
67+
return rtList;
68+
}
69+
70+
private void addToRtList(int small, int big) {
71+
ArrayList<Integer> list = new ArrayList<Integer>();
72+
73+
for (int i = small; i <= big; i++) {
74+
list.add(i);
75+
}
76+
77+
rtList.add(list);
78+
}
79+
}

swordForOffer/src/test/java/problem40/NumbersAppearOnceTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ public void test()
2121
n.FindNumsAppearOnce(new int[]
2222
{ 1, 2, 3, 3, 4, 4, 5, 5, 6, 6 }, num1, num2);
2323
Assert.assertTrue(num1[0] == 1 || num2[0] == 1);
24-
2524
}
2625
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package problem41;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
7+
/**
8+
* Created by bjyangrubing on 2016/8/25.
9+
*/
10+
public class TwoNumbersWithSumTest
11+
{
12+
@Test
13+
public void test()
14+
{
15+
TwoNumbersWithSum t = new TwoNumbersWithSum();
16+
17+
ArrayList<ArrayList<Integer>> list = t.FindContinuousSequence(9);
18+
19+
for (int i = 0; i < list.size(); i++)
20+
{
21+
System.out.println(list.get(i));
22+
}
23+
24+
ArrayList<Integer> list1 = t.FindNumbersWithSum(new int[]
25+
{ 1, 2, 3, 4, 5, 6, 7 }, 8);
26+
27+
for (int i = 0; i < list1.size(); i++)
28+
{
29+
System.out.println(list1.get(i));
30+
}
31+
32+
}
33+
34+
}

0 commit comments

Comments
 (0)