Skip to content

Commit bf327b1

Browse files
committedAug 13, 2017
refactor 35
1 parent 3ffad7e commit bf327b1

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ Your ideas/fixes/algorithms are more than welcome!
564564
|38|[Count and Say](https://leetcode.com/problems/count-and-say/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_38.java)|O(n*2^n)|O(2^n)|Easy| Recursion, LinkedList
565565
|37|[Sudoku Solver](https://leetcode.com/problems/sudoku-solver/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_37.java)|O((9!)^9)|O(1)|Hard|
566566
|36|[Valid Sudoku](https://leetcode.com/problems/valid-sudoku/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_36.java)|O(1)|O(1)|Medium|
567-
|35|[Search Insert Position](https://leetcode.com/problems/search-insert-position/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_35.java)|O(n)|O(1)|Medium|Array
567+
|35|[Search Insert Position](https://leetcode.com/problems/search-insert-position/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_35.java)|O(n)|O(1)|Easy|Array
568568
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_34.java)|O(logn)|O(1)|Medium|Array, Binary Search
569569
|33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_33.java)|O(logn)|O(1)|Medium|Binary Search
570570
|32|[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_32.java)|O(n)|O(n)|Hard|Stack, DP

‎src/main/java/com/fishercoder/solutions/_35.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
* Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
4+
* 35. Search Insert Position
5+
*
6+
* Given a sorted array and a target value,
7+
* return the index if the target is found.
8+
* If not, return the index where it would be if it were inserted in order.
59
610
You may assume no duplicates in the array.
711
@@ -11,22 +15,24 @@
1115
[1,3,5,6], 7 → 4
1216
[1,3,5,6], 0 → 0
1317
*/
18+
1419
public class _35 {
1520

1621
public int searchInsert(int[] A, int target) {
1722
int len = A.length;
18-
if (len == 0)
23+
if (len == 0) {
1924
return 0;
20-
else {
25+
} else {
2126
for (int i = 0; i < len; i++) {
22-
if (A[0] > target)
27+
if (A[0] > target) {
2328
return 0;
24-
else if (A[len - 1] < target)
29+
} else if (A[len - 1] < target) {
2530
return len;
26-
else if (A[i] == target)
31+
} else if (A[i] == target) {
2732
return i;
28-
else if (A[i] < target && A[i + 1] > target)
33+
} else if (A[i] < target && A[i + 1] > target) {
2934
return i + 1;
35+
}
3036
}
3137
return len;
3238
}

0 commit comments

Comments
 (0)