Content-Length: 1635 | pFad | http://github.com/fishercoder1534/Leetcode/pull/104.patch
thub.com
From c3a1339fc5807df3463d125abf3494ce7d8a8f27 Mon Sep 17 00:00:00 2001
From: MITHILESH
Date: Thu, 1 Oct 2020 12:51:43 +0530
Subject: [PATCH] Create Rotated Sorted Array.cpp
---
cpp/Rotated Sorted Array.cpp | 42 ++++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
create mode 100644 cpp/Rotated Sorted Array.cpp
diff --git a/cpp/Rotated Sorted Array.cpp b/cpp/Rotated Sorted Array.cpp
new file mode 100644
index 0000000000..1fe19904e8
--- /dev/null
+++ b/cpp/Rotated Sorted Array.cpp
@@ -0,0 +1,42 @@
+class Solution {
+public:
+
+int search(vector& nums, int target)
+{
+ int n = nums.size();
+ int index = -1, left = 0, right = n-1;
+ while(left <= right)
+ {
+ int mid = left + (right - left) / 2;
+ if(mid > 0 && nums[mid] < nums[mid-1])
+ {
+ index = mid - 1;
+ break;
+ }
+ if(mid < n-1 && nums[mid] > nums[mid+1])
+ {
+ index = mid;
+ break;
+ }
+ if(nums[mid] > nums[right]) left = mid + 1;
+ else right = mid - 1;
+ }
+ left = 0, right = index;
+ while(left <= right)
+ {
+ int mid = left + (right - left) / 2;
+ if(nums[mid] == target) return mid;
+ else if(nums[mid] > target) right = mid-1;
+ else left = mid + 1;
+ }
+ left = index + 1, right = n - 1;
+ while(left <= right)
+ {
+ int mid = left + (right - left) / 2;
+ if(nums[mid] == target) return mid;
+ else if(nums[mid] > target) right = mid - 1;
+ else left = mid + 1;
+ }
+ return -1;
+}
+};
--- a PPN by Garber Painting Akron. With Image Size Reduction included!Fetched URL: http://github.com/fishercoder1534/Leetcode/pull/104.patch
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy