Search Insert Position &Search in Rotated Sorted Array
来源:互联网 发布:天津买菜软件 编辑:程序博客网 时间:2024/05/17 23:24
class Solution {public: int searchInsert(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function if (!A) return 0; if (target<A[0]) { return 0; } if (target>A[n-1]) { return n; } for (int i=0; i<n; i++) { if (target==A[i]) { return i; }else if (target>A[i] && target < A[i+1]) { return i+1; } } }};
14/1/30: 二分法
class Solution {public: int searchInsert(int A[], int n, int target) { if (!n) return 0; int i=0, j=n-1; while(i<=j) { int mid=(i+j)/2; if (A[mid]==target) { return mid; } if (A[mid]<target && A[mid+1]>target) { return mid+1; } if (A[mid]<target) { i=mid+1; }else { j=mid-1; } } if (i==j) { if (A[i]<target) { return j+1; }else { return j; } } }};
刚开始写的比较长,后来发现还可以化简:
class Solution {public: int searchInsert(int A[], int n, int target) { if (!n) return 0; int i=0, j=n-1; while(i<=j) { int mid=(i+j)/2; if (A[mid]==target) { return mid; } if (A[mid]<target) { i=mid+1; }else { j=mid-1; } } return i; }};
Search in Rotated Sorted Array
class Solution {public: int search(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function if (A[0]<target) { for (int i=1; i<n; i++) { if (A[i]==target) { return i; } } }else if (A[0]>target) { for (int i=n-1; i>0; i--) { if (A[i]==target) { return i; } } }else if (A[0]==target) { return 0; } return -1; } };
other better solution:
http://blog.unieagle.net/2012/10/23/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Asearch-in-rotated-sorted-array/
class Solution {public: int search(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n <= 0) return -1; int i = 0, j = n-1; while(i <= j) { int mid = (i + j) / 2; int mval = A[mid]; if(target == mval) return mid; if(i == j) return -1; if(A[i] < A[j]) { //we are in the normal sorted array range if(target < mval) j = mid - 1; else i = mid + 1; } else if (A[i] > A[j]) { //we are in the rotated range of array if(mval > A[i]) { //mid is in the ascending part of arr if(target < mval) { if(target < A[i]) { //target should be in the rear of array i = mid + 1; } else if (target == A[i]) { return i; } else { //target should be in the front j = mid - 1; } } else i = mid + 1; } else if (mval < A[i]) { if(target < mval) { j = mid - 1; } else { if(target == A[j]) return j; else if(target < A[j]) i = mid + 1; else j = mid - 1; } } else { i = mid + 1; } } else { //unsure return -3; } } return -1; }};
- Search Insert Position &Search in Rotated Sorted Array
- Search Insert Position &&Search in Rotated Sorted Array &&Search in Rotated Sorted Array II
- search-insert-position/search-in-rotated-sorted-array/search-in-rotated-sorted-array ii
- [LeetCode] Search in Rotated Sorted Array、Search for a Range、Search Insert Position、Search in Rotate
- leetcode Search in Rotated Sorted Array & Search for a Range & Search Insert Position
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array
- LeetCode 11: Container With Most Water
- (step7.2.3)hdu 2554(N对数的排列问题——简单数论)
- poi 一个单元格不同字体
- Putty远程登录VMware虚拟机Linux(Ubuntu12.04)
- Linux 下重新编译Windows下生成的Qt工程
- Search Insert Position &Search in Rotated Sorted Array
- 七步入门CMake之第二步(添加库)
- 华中科技大学Ubuntu12.04网络认证
- 2013,收割知识的一年
- 右侧浮动,可以设置距离右侧的宽度和高度
- 无法嵌入互操作类型"SpeechLib.SpVoiceClass".请改用适用的接口.
- 【批评的入口是激励,是员工成长】
- 关系代数的七个扩充操作-及其深刻内涵
- 【3岁前一定要改掉的毛病】