zoukankan      html  css  js  c++  java
  • leetcode--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 Solution {
        public int searchInsert(int[] A, int target) {
            int position = 0, length = A.length;
    		if(length > 0){
    			int index = Arrays.binarySearch(A, target);
    			position = (index < 0) ? (index + 1)*(-1) : index; 
    		}
    		return position;    
        }
    }
    

      

  • 相关阅读:
    用 Sqlmap 识别 WAF
    OD 实验(九)
    跳转指令及其跳转条件
    Python
    Python 模块
    OD 实验(八)
    OD 实验(七)
    OD 实验(六)
    OD 实验(五)
    OD 实验(四)
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3771657.html
Copyright © 2011-2022 走看看