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

Sign up
Appearance settings

Commit 2bd811a

Browse files
1 & 2
1 parent cfa7d2c commit 2bd811a

File tree

2 files changed

+73
-19
lines changed
  • codes/java/leetcodes/src/main/java/com/hit/basmath/learn

2 files changed

+73
-19
lines changed

‎codes/java/leetcodes/src/main/java/com/hit/basmath/learn/hash_table/_1.java‎

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,61 @@
1919
* return [0, 1].
2020
*/
2121
public class _1 {
22+
/**
23+
* Solution 1: Violence method
24+
*
25+
* @param nums
26+
* @param target
27+
* @return
28+
*/
2229
public int[] twoSum(int[] nums, int target) {
23-
Map<Integer, Integer> map = new HashMap();
2430
for (int i = 0; i < nums.length; i++) {
25-
int a = nums[i];
26-
// note that the numbers in the array can be negative as well
27-
if (map.get(target - a) != null) {
28-
returnnewint[]{map.get(target - a), i};
31+
for (int j = i + 1; j < nums.length; j++) {
32+
if (nums[i] + nums[j] == target) {
33+
returnnewint[]{i, j};
34+
}
2935
}
30-
// The following should be after the check above,
31-
// otherwise it will fail for the case where target = 6 and there's a 3 in the original array.
32-
map.put(a, i);
36+
}
37+
return null;
38+
}
39+
40+
/**
41+
* Solution 2: Two pass hash table
42+
*
43+
* @param nums
44+
* @param target
45+
* @return
46+
*/
47+
public int[] twoSum2(int[] nums, int target) {
48+
Map<Integer, Integer> aMap = new HashMap<>();
49+
for (int i = 0; i < nums.length; i++) {
50+
aMap.put(nums[i], i);
51+
}
52+
for (int i = 0; i < nums.length; i++) {
53+
int complement = target - nums[i];
54+
if (aMap.containsKey(complement) &&
55+
aMap.get(complement) != i) {
56+
return new int[]{i, aMap.get(complement)};
57+
}
58+
}
59+
return null;
60+
}
61+
62+
/**
63+
* Solution 3: One pass hash table
64+
*
65+
* @param nums
66+
* @param target
67+
* @return
68+
*/
69+
public int[] twoSum3(int[] nums, int target) {
70+
Map<Integer, Integer> aMap = new HashMap<>();
71+
for (int i = 0; i < nums.length; i++) {
72+
int complement = target - nums[i];
73+
if (aMap.containsKey(complement)) {
74+
return new int[]{aMap.get(complement), i};
75+
}
76+
aMap.put(nums[i], i);
3377
}
3478
return null;
3579
}

‎codes/java/leetcodes/src/main/java/com/hit/basmath/learn/linked_list/_2.java‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,30 @@
1818
*/
1919
public class _2 {
2020
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
21-
ListNode prev = new ListNode(0);
22-
ListNode head = prev;
21+
ListNode dummyHead = new ListNode(0);
22+
ListNode p = l1, q = l2, curr = dummyHead;
2323
int carry = 0;
24-
while (l1 != null || l2 != null || carry != 0) {
25-
ListNode cur = new ListNode(0);
26-
int sum = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + carry;
27-
cur.val = sum % 10;
24+
while (p != null || q != null) {
25+
int x = (p != null) ? p.val : 0;
26+
int y = (q != null) ? q.val : 0;
27+
int sum = carry + x + y;
28+
2829
carry = sum / 10;
29-
prev.next = cur;
30-
prev = cur;
30+
curr.next = new ListNode(sum % 10);
31+
curr = curr.next;
32+
33+
if (p != null) {
34+
p = p.next;
35+
}
36+
if (q != null) {
37+
q = q.next;
38+
}
39+
}
3140

32-
l1 = (l1 == null) ? l1 : l1.next;
33-
l2 = (l2 == null) ? l2 : l2.next;
41+
if (carry > 0) {
42+
curr.next = newListNode(carry);
3443
}
35-
return head.next;
44+
45+
return dummyHead.next;
3646
}
3747
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /