1. 程式人生 > >LeetCode 35. Search Insert Position 搜尋插入位置

LeetCode 35. Search Insert Position 搜尋插入位置

解法一:class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int i=0;
        for(;i<nums.size();i++)
        {
            if(target==nums[i])
                return i;
            else if(target<nums[i])
                break;
        }
        return i;
    }
};

解法二:class Solution {

public:

int searchInsert(vector<int>& nums, int target) {

int low = 0, high = nums.size()-1;

while (low <= high) {

int mid = low + (high-low)/2;

if (nums[mid] < target)

low = mid+1; else

high = mid-1;

}

return low;

}

};

相關推薦

leetcode-35 search-insert-position(搜尋插入位置

這是我第一道通過自己獨立思考通過的程式碼,雖然題目簡單,但是還是很激動!先看一下題目描述: 題目描述的很清楚,一定要仔細讀題,直接上程式碼: 1 public int searchInsert(int[] nums, int target) { 2 int j = 0

LeetCode 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

LeetCode 35. Search Insert Position 搜尋插入位置

解法一:class Solution { public:     int searchInsert(vector<int>& nums, int target) {         int i=0;         for(;i<nums.size(

leetcode 35. Search Insert Position 搜尋插入位置 python 簡單而高效的方法、二分查詢

class Solution: def searchInsert(self, nums, target): """ :type nums: List[int]

[Leetcode] 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 i

LeetCode: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 or

[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

leetcode 35 Search Insert Position.(搜尋元素插入陣列的位置

題目要求: 給定一個排好順序陣列和一個目標值,搜尋陣列,如果找到目標,那麼返回索引。如果沒有,那麼返回該目標值應該插入陣列的位置索引。 假設陣列中沒有重複項。 Example: 例1 輸入: [1,3,5,6], 5 輸出:2 例2 輸入: [1,3,5,6], 2 輸

[Leetcode] search insert position 尋找插入位置

dex http 應該 nbsp arr 位置 love ray array Given a sorted array and a target value, return the index if the target is found. If not, return t

[leetcode-35-Search Insert Position]

i++ cnblogs insert div sea ins color position amp You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [

LeetCode 35. Search Insert Position (Easy)

assume position 分享 num mar val dex p s eas 上班無聊就在leetCode刷刷題目,有點遺憾的是到現在才去刷算法題,大學那會好好利用該多好啊。 第35道簡單算法題,一次性通過有點開心,分享自己的代碼。 問題描述 Given a sor

leetcode 35. Search Insert Position

question tar assume arch ID index ret pytho turn Given a sorted array and a target value, return the index if the target is found. If n

LeetCode - 35. Search Insert Position(48ms)

lee pre vector duplicate input array osi pub dup Given a sorted array and a target value, return the index if the target is found. If not

[leetcode][35] Search Insert Position

class lee leet public arch tput 找到 位置 target 35 Search Insert Position Given a sorted array and a target value, return the index if the t

#LeetCode# 35. Search Insert Position

https://leetcode.com/problems/search-insert-position/description/   Given a sorted array and a target value, return the index if the target is found

[leetcode]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 i

python leetcode 35. Search Insert Position

二分查詢的變種,注意迴圈退出的條件: 找到此數,返回下標 此數可能位於兩個數字之間,所以while left<right-1: class Solution: def searchInsert(self, nums, target):

LeetCode#35: Search Insert Position

public class Solution { public int searchInsert(int[] nums, int target) { int hi = nums.l

【c/c++】leetcode 35. Search Insert Position(easy)

35. Search Insert Position(easy)  Given a sorted array and a target value, return the index if the target is found. If not, return the in

LeetCode 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 in