Skip to content

Commit 6730cbd

Browse files
committed
OddEventLinkedList,TernarySumClosest
1 parent d6853d6 commit 6730cbd

File tree

5 files changed

+199
-5
lines changed

5 files changed

+199
-5
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package joshua.leetcode.array.twopointers;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 16. 3Sum Closest
7+
* <p/>
8+
* <a href="https://leetcode.com/problems/3sum-closest/">leetcode link</a>
9+
*
10+
* @author Joshua.Jiang on 2016/5/21.
11+
*/
12+
public abstract class TernarySumClosest {
13+
14+
/**
15+
* Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
16+
* <p/>
17+
* For example, given array S = {-1 2 1 -4}, and target = 1.
18+
* <p/>
19+
* The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
20+
*
21+
* @param nums the array
22+
* @param target the target value
23+
* @return the closest sum for three elements from <code>nums</code>
24+
*/
25+
public abstract int threeSumClosest(int[] nums, int target);
26+
27+
28+
/**
29+
* 一个从 {@link joshua.leetcode.array.TernarySum} 的解法的变种。
30+
* 不同的在于判断two pointers中如何移动left pointer和right pointer。
31+
*/
32+
public static class Solution1 extends TernarySumClosest {
33+
34+
@Override
35+
public int threeSumClosest(int[] nums, int target) {
36+
int closestSum = 0;
37+
int closestGap = Integer.MAX_VALUE;
38+
Arrays.sort(nums);
39+
for (int i = 0; i < nums.length - 2; i++) {
40+
int leftPointer = i + 1;
41+
int rightPointer = nums.length - 1;
42+
while (leftPointer < rightPointer) {
43+
int sum = nums[i] + nums[leftPointer] + nums[rightPointer];
44+
if (Math.abs(sum - target) < closestGap) {
45+
closestSum = sum;
46+
closestGap = Math.abs(sum - target);
47+
}
48+
if(sum < target) {
49+
leftPointer ++;
50+
} else {
51+
rightPointer --;
52+
}
53+
}
54+
}
55+
return closestSum;
56+
}
57+
}
58+
}
59+
60+

src/main/java/joshua/leetcode/linkedlist/ListNode.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
11
package joshua.leetcode.linkedlist;
22

33
public class ListNode {
4-
4+
5+
@Override
6+
public boolean equals(Object o) {
7+
if (this == o) return true;
8+
if (o == null || getClass() != o.getClass()) return false;
9+
10+
ListNode listNode = (ListNode) o;
11+
12+
if (val != listNode.val) return false;
13+
if (next != null ? !next.equals(listNode.next) : listNode.next != null) return false;
14+
15+
return true;
16+
}
17+
18+
@Override
19+
public int hashCode() {
20+
int result = val;
21+
result = 31 * result + (next != null ? next.hashCode() : 0);
22+
return result;
23+
}
24+
525
public int val;
626
public ListNode next;
727

828
public ListNode(int x) {
929
val = x;
1030
next = null;
1131
}
12-
13-
1432

1533
public ListNode(int val, ListNode next) {
1634
super();
1735
this.val = val;
1836
this.next = next;
1937
}
2038

21-
22-
2339
public static ListNode buildList(int[] vals){
2440
if(vals==null||vals.length==0)
2541
return null;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package joshua.leetcode.linkedlist;
2+
3+
/**
4+
* 328. Odd Even Linked List<br/>
5+
*
6+
* <a href="https://leetcode.com/problems/odd-even-linked-list/">leetcode link</a>
7+
*
8+
* @author Joshua.Jiang on 2016/5/22.
9+
*/
10+
public abstract class OddEvenLinkedList {
11+
12+
public abstract ListNode oddEvenList(ListNode head);
13+
14+
public static class Solution1 extends OddEvenLinkedList {
15+
16+
@Override
17+
public ListNode oddEvenList(ListNode head) {
18+
if (head == null) {
19+
return head;
20+
}
21+
ListNode lastOddNode = head;
22+
while (lastOddNode.next != null && lastOddNode.next.next != null) {
23+
lastOddNode = lastOddNode.next.next;
24+
}
25+
ListNode nextOddNode = head;
26+
ListNode posToInsert = lastOddNode;
27+
while(nextOddNode != lastOddNode) {
28+
ListNode evenNodeToMove = nextOddNode.next;
29+
nextOddNode.next = nextOddNode.next.next;
30+
nextOddNode = nextOddNode.next;
31+
evenNodeToMove.next = posToInsert.next;
32+
posToInsert.next = evenNodeToMove;
33+
posToInsert = evenNodeToMove;
34+
}
35+
return head;
36+
}
37+
}
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package joshua.leetcode.array.twopointers;
2+
3+
import com.google.common.collect.Maps;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.util.Map;
8+
9+
import static org.junit.Assert.*;
10+
11+
public class TernarySumClosestTest {
12+
13+
private TernarySumClosest solution;
14+
15+
private Map<Param, Integer> cases = Maps.newHashMap();
16+
17+
@Before
18+
public void setUp() {
19+
cases.put(new Param(new int[]{0,0,0},1), 0);
20+
cases.put(new Param(new int[]{-1,2,1,4},1), 2);
21+
cases.put(new Param(new int[]{-1,0,1,2,-1,-4},0), 0);
22+
cases.put(new Param(new int[]{-1,2,1,-4},1), 2);
23+
cases.put(new Param(new int[]{1,1,-1,-1,3},-1), -1);
24+
cases.put(new Param(new int[]{4,0,5,-5,3,3,0,-4,-5},-2), -2);
25+
}
26+
27+
@Test
28+
public void testSolution1() {
29+
solution = new TernarySumClosest.Solution1();
30+
for(Param param : cases.keySet()) {
31+
assertEquals((int)cases.get(param), solution.threeSumClosest(param.nums,param.target));
32+
}
33+
}
34+
35+
class Param {
36+
int[] nums;
37+
int target;
38+
39+
public Param(int[] nums, int target) {
40+
this.nums = nums;
41+
this.target = target;
42+
}
43+
}
44+
45+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package joshua.leetcode.linkedlist;
2+
3+
import com.google.common.collect.Maps;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.util.Map;
8+
9+
import static org.junit.Assert.*;
10+
11+
public class OddEvenLinkedListTest {
12+
13+
private Map<ListNode, ListNode> cases = Maps.newHashMap();
14+
15+
private OddEvenLinkedList solution;
16+
17+
@Before
18+
public void setUp() {
19+
// ListNode origin = ListNode.buildList(new int[]{1,2,3,4,5});
20+
// ListNode transferred = ListNode.buildList(new int[]{1,3,5,2,4});
21+
ListNode origin = ListNode.buildList(new int[]{1, 2});
22+
ListNode transferred = ListNode.buildList(new int[]{1, 2});
23+
cases.put(origin, transferred);
24+
}
25+
26+
@Test
27+
public void testSolution1() {
28+
solution = new OddEvenLinkedList.Solution1();
29+
for (ListNode param : cases.keySet()) {
30+
ListNode result = solution.oddEvenList(param);
31+
assertEquals(cases.get(param), result);
32+
}
33+
}
34+
35+
}

0 commit comments

Comments
 (0)