Skip to content

Commit 334115e

Browse files
[N-0] refactor 540
1 parent 93e6712 commit 334115e

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Your ideas/fixes/algorithms are more than welcome!
136136
|542|[01 Matrix](https://leetcode.com/problems/01-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_542.java) | O(m*n) |O(n) | Medium | BFS
137137
|541|[Reverse String II](https://leetcode.com/problems/reverse-string-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_541.java) | O(n) |O(1) | Easy | String
138138
|540|[Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_540.java) | O(n) |O(1) | Medium |
139-
|539|[Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/MinimumTimeDifference.java) | O(n) |O(1) | Medium | String
139+
|539|[Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/MinimumTimeDifference.java) | O(logn) |O(1) | Medium | String
140140
|538|[Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_538.java) | O(n) |O(h) | Easy | Tree
141141
|537|[Complex Number Multiplication](https://leetcode.com/problems/complex-number-multiplication/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_537.java) | O(1) |O(1) | Medium | Math, String
142142
|536|[Construct Binary Tree from String](https://leetcode.com/problems/construct-binary-tree-from-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_536.java) | O(n) |O(h) | Medium | Recursion, Stack
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
* Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once.
4+
* 540. Single Element in a Sorted Array
5+
*
6+
* Given a sorted array consisting of only integers where every
7+
* element appears twice except for one element which appears once.
8+
* Find this single element that appears only once.
59
610
Example 1:
711
Input: [1,1,2,3,3,4,4,8,8]
@@ -12,13 +16,33 @@
1216
Note: Your solution should run in O(log n) time and O(1) space.
1317
*/
1418
public class _540 {
15-
// TODO: Could be optimized to O(logn) by using binary search
16-
public int singleNonDuplicate(int[] nums) {
17-
int result = 0;
18-
for (int i = 0; i < nums.length; i++) {
19-
result ^= nums[i];
19+
public static class Solution1 {
20+
public int singleNonDuplicate(int[] nums) {
21+
int result = 0;
22+
for (int i = 0; i < nums.length; i++) {
23+
result ^= nums[i];
24+
}
25+
return result;
2026
}
21-
return result;
2227
}
2328

29+
public static class Solution2 {
30+
public int singleNonDuplicate(int[] nums) {
31+
int start = 0;
32+
int end = nums.length - 1;
33+
while (start < end) {
34+
int mid = start + (end - start) / 2;
35+
if (nums[mid] != nums[mid + 1] && nums[mid] != nums[mid - 1]) {
36+
return nums[mid];
37+
} else if (nums[mid] == nums[mid + 1] && mid % 2 == 0) {
38+
start = mid + 1;
39+
} else if (nums[mid] == nums[mid - 1] && mid % 2 == 1) {
40+
start = mid + 1;
41+
} else {
42+
end = mid - 1;
43+
}
44+
}
45+
return nums[start];
46+
}
47+
}
2448
}
+10-21
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,40 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._540;
4-
import org.junit.Before;
54
import org.junit.BeforeClass;
65
import org.junit.Test;
76

87
import static junit.framework.Assert.assertEquals;
98

109
public class _540Test {
11-
private static _540 test;
12-
private static int expected;
13-
private static int actual;
10+
private static _540.Solution1 solution1;
11+
private static _540.Solution2 solution2;
1412
private static int[] nums;
1513

1614
@BeforeClass
1715
public static void setup() {
18-
test = new _540();
19-
}
20-
21-
@Before
22-
public void setupForEachTest() {
23-
expected = 0;
24-
actual = 0;
25-
nums = new int[1000];
16+
solution1 = new _540.Solution1();
17+
solution2 = new _540.Solution2();
2618
}
2719

2820
@Test
2921
public void test1() {
3022
nums = new int[]{1, 1, 2, 3, 3, 4, 4, 8, 8};
31-
expected = 2;
32-
actual = test.singleNonDuplicate(nums);
33-
assertEquals(expected, actual);
23+
assertEquals(2, solution1.singleNonDuplicate(nums));
24+
assertEquals(2, solution2.singleNonDuplicate(nums));
3425
}
3526

3627
@Test
3728
public void test2() {
3829
nums = new int[]{3, 3, 7, 7, 10, 11, 11};
39-
expected = 10;
40-
actual = test.singleNonDuplicate(nums);
41-
assertEquals(expected, actual);
30+
assertEquals(10, solution1.singleNonDuplicate(nums));
31+
assertEquals(10, solution2.singleNonDuplicate(nums));
4232
}
4333

4434
@Test
4535
public void test3() {
4636
nums = new int[]{1, 1, 2};
47-
expected = 2;
48-
actual = test.singleNonDuplicate(nums);
49-
assertEquals(expected, actual);
37+
assertEquals(2, solution1.singleNonDuplicate(nums));
38+
assertEquals(2, solution2.singleNonDuplicate(nums));
5039
}
5140
}

0 commit comments

Comments
 (0)