package com.fishercoder.solutions;

/**
 * 35. Search Insert Position
 *
 * 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.

 You may assume no duplicates in the array.

 Here are few examples.
 [1,3,5,6], 5 → 2
 [1,3,5,6], 2 → 1
 [1,3,5,6], 7 → 4
 [1,3,5,6], 0 → 0
 */

public class _35 {

    public int searchInsert(int[] A, int target) {
        int len = A.length;
        if (len == 0) {
            return 0;
        } else {
            for (int i = 0; i < len; i++) {
                if (A[0] > target) {
                    return 0;
                } else if (A[len - 1] < target) {
                    return len;
                } else if (A[i] == target) {
                    return i;
                } else if (A[i] < target && A[i + 1] > target) {
                    return i + 1;
                }
            }
            return len;
        }
    }

}

@@ -431,17 +431,20 @@
 t(int%5B%5D 
-A
+nums
 , int ta
@@ -469,17 +469,20 @@
 t len = 
-A
+nums
 .length;
@@ -494,400 +494,77 @@
     
-if (len == 0) %7B%0A            return 0;%0A        %7D else %7B%0A            for (int i = 0; i %3C len; i++) %7B%0A                if (A%5B0%5D %3E target) %7B%0A                    return 0;%0A                %7D else if (A%5Blen - 1%5D %3C target) %7B%0A                    return len;%0A                %7D else if (A%5Bi%5D == target) %7B%0A                    return i;%0A                %7D else if (A%5Bi%5D %3C target && A%5Bi + 1%5D %3E target) %7B%0A    
+for (int i = 0; i %3C len; i++) %7B%0A            if (target %3C= nums%5Bi%5D) %7B%0A
     
@@ -587,12 +587,8 @@
 rn i
- + 1
 ;%0A  
@@ -597,32 +597,26 @@
         
-    %7D%0A  
+%7D%0A
         
   %7D%0A    
@@ -611,23 +611,18 @@
     
-  %7D%0A   
+%7D%0A
         
  ret
@@ -613,25 +613,24 @@
   %7D%0A        
- 
 return len;%0A
@@ -629,26 +629,16 @@
 rn len;%0A
-        %7D%0A
     %7D%0A%0A%7D

Update _35.java (#36)