★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-xtrdgpmj-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a sorted (in ascending order) integer array nums
of n
elements and a target
value, write a function to search target
in nums
. If target
exists, then return its index, otherwise return -1
.git
Example 1:github
Input: = [-1,0,3,5,9,12], = 9 Output: 4 Explanation: 9 exists in and its index is 4 numstargetnums
Example 2:數組
Input: = [-1,0,3,5,9,12], = 2 Output: -1 Explanation: 2 does not exist in so return -1 numstargetnums
Note:微信
nums
are unique.n
will be in the range [1, 10000]
.nums
will be in the range [-9999, 9999]
.給定一個 n
個元素有序的(升序)整型數組 nums
和一個目標值 target
,寫一個函數搜索 nums
中的 target
,若是目標值存在返回下標,不然返回 -1
。函數
示例 1:spa
輸入: = [-1,0,3,5,9,12], = 9 輸出: 4 解釋: 9 出如今 中而且下標爲 4 numstargetnums
示例 2:code
輸入: = [-1,0,3,5,9,12], = 2 輸出: -1 解釋: 2 不存在 中所以返回 -1 numstargetnums
提示:htm
nums
中的全部元素是不重複的。n
將在 [1, 10000]
之間。nums
的每一個元素都將在 [-9999, 9999]
之間。292msblog
1 class Solution { 2 func search(_ nums: [Int], _ target: Int) -> Int { 3 4 var left = 0 5 var right = nums.count - 1 6 var mid = (left + right) / 2 7 8 while left <= right { 9 if target == nums[mid] { 10 return mid 11 } else if target == nums[left] { 12 return left 13 } else if target == nums[right] { 14 return right 15 } else if target > nums[left] && target < nums[mid] { 16 right = mid - 1 17 mid = (left + mid) / 2 18 left += 1 19 } else if target > nums[mid] && target < nums[right] { 20 left = mid + 1 21 mid = (mid + right) / 2 22 right -= 1 23 } else { 24 return -1 25 } 26 } 27 28 return -1 29 } 30 }
1 class Solution { 2 func search(_ nums: [Int], _ target: Int) -> Int { 3 var low = 0 4 var high = nums.count - 1 5 var mid = (low + high) >> 1 6 7 while low <= high { 8 let val = nums[mid] 9 if target == val { 10 return mid 11 } else if target < val { 12 high = mid - 1 13 } else { 14 low = mid + 1 15 } 16 mid = (low + high) >> 1 17 } 18 return -1 19 } 20 }
312ms
1 class Solution { 2 func search(_ nums: [Int], _ target: Int) -> Int { 3 if nums.count == 1 {return nums[0] == target ? 0 : -1} 4 else if nums.count == 0 {return -1} 5 6 return binarySearch (nums, target, 0, nums.count-1) 7 } 8 9 func binarySearch(_ nums:[Int], _ target: Int, _ l: Int, _ r: Int) -> Int { 10 if (r >= l) { 11 var mid = l + (r-l)/2 12 if nums[mid] == target {return mid} 13 else if nums[mid] > target {return binarySearch(nums, target, l, mid-1) } 14 else {return binarySearch(nums, target, mid+1, r)} 15 } 16 return -1 17 } 18 }
352ms
1 class Solution { 2 3 var start = 0; 4 var end = 0; 5 6 var center: Int { 7 return (end + start) >> 1 8 } 9 10 func search(_ nums: [Int], _ target: Int) -> Int { 11 end = nums.count 12 while start != end - 1 { 13 if nums[center] == target { 14 return center 15 }else if nums[center] < target { 16 start = center 17 }else { 18 end = center 19 } 20 } 21 return nums[center] == target ? center : -1 22 } 23 }
452ms
1 class Solution { 2 func search(_ nums: [Int], _ target: Int) -> Int { 3 func search(_ l: Int, _ r: Int) -> Int { 4 if l == r && nums[l] != target { 5 return -1 6 } 7 8 let mid = l + (r + 1 - l) / 2 9 10 if nums[mid] == target { 11 return mid 12 } else if nums[mid] > target { 13 return search(l, mid - 1) 14 } else { 15 return search(mid, r) 16 } 17 } 18 19 return search(0, nums.count - 1) 20 } 21 }