Skip to content

Commit 55c257d

Browse files
committed
Update a few easy problems
1 parent 157cea9 commit 55c257d

239 files changed

Lines changed: 7218 additions & 5238 deletions

File tree

Some content is hidden

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

Java/2 Sum.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,26 @@ Always check (target - current) from the HashMap.
4141
(key, value) = (numbers[i], i)
4242
Note: return index+1 because this is not 0-based.
4343
*/
44-
45-
public class Solution {
46-
//Using HashMap
47-
public int[] twoSum(int[] numbers, int target) {
48-
if (numbers == null || numbers.length == 0) {
44+
class Solution {
45+
final static int RESULT_SIZE = 2;
46+
final static int RESULT_INDEX1 = 0;
47+
final static int RESULT_INDEX2 = 1;
48+
public int[] twoSum(int[] nums, int target) {
49+
if (nums == null || nums.length == 0) {
4950
return null;
5051
}
51-
int[] rst = new int[2];
52-
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
53-
for (int i = 0; i < numbers.length; i++) {
54-
if (map.containsKey(target - numbers[i])) {
55-
rst[0] = map.get(target - numbers[i]) + 1;
56-
rst[1] = i + 1;
57-
break;
52+
final int[] result = new int[RESULT_SIZE];
53+
final Map<Integer, Integer> recordMap = new HashMap<>();
54+
for (int i = 0; i < nums.length; i++) {
55+
if (recordMap.containsKey(target - nums[i])) {
56+
result[RESULT_INDEX1] = recordMap.get(target - nums[i]);
57+
result[RESULT_INDEX2] = i;
58+
return result;
5859
} else {
59-
map.put(numbers[i], i);
60+
recordMap.put(nums[i], i);
6061
}
6162
}
62-
return rst;
63+
return result;
6364
}
6465
}
6566

Java/3 Sum Closest.java

100644100755
File mode changed.

Java/3 Sum Smaller.java

100644100755
File mode changed.

Java/3 Sum.java

100644100755
File mode changed.

Java/A+B.java

100644100755
File mode changed.

Java/Add Binary.java

100644100755
File mode changed.

Java/Add Two Numbers II.java

100644100755
File mode changed.

Java/Add Two Numbers.java

100644100755
File mode changed.

Java/Array Partition I.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
E
2+
3+
从结果出发, 只需要找到加法的结果而不强调具体配对找到排列取单数位的规律再考虑负数和正数的相同规律即可找到排列求解的方法
4+
5+
6+
```
7+
/*
8+
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
9+
10+
Example 1:
11+
Input: [1,4,3,2]
12+
13+
Output: 4
14+
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
15+
Note:
16+
n is a positive integer, which is in the range of [1, 10000].
17+
All the integers in the array will be in the range of [-10000, 10000].
18+
*/
19+
20+
class Solution {
21+
public int arrayPairSum(int[] nums) {
22+
Arrays.sort(nums);
23+
int result = 0;
24+
for (int i = 0; i < nums.length; i++) {
25+
if (i % 2 == 0) {
26+
result += nums[i];
27+
}
28+
}
29+
return result;
30+
}
31+
}
32+
```

Java/Backpack II.java

100644100755
File mode changed.

0 commit comments

Comments
 (0)