首页 > 代码库 > Leetcode: Search Insert Position

Leetcode: Search Insert Position

很简单的题目,一次过,注意为数组空的时候,应返回0而非null

 1 public class Solution {
 2     public int searchInsert(int[] A, int target) {
 3         int i;
 4         if (A.length == 0) return 0;
 5         for (i=0; i<A.length; i++){
 6             if (target <= A[i]){
 7                 return i;
 8             }
 9         }
10         return A.length;
11     }
12 }