Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7e4ab5b

Browse files
committed
solve problems with rotated array
1 parent6157edc commit7e4ab5b

File tree

6 files changed

+109
-2
lines changed

6 files changed

+109
-2
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Similar to 153-findMinimumInRotatedSortedArray, and 81-searchRotatedSortedArrayII,
3+
* combile the solutions from these two.
4+
*
5+
*@param {number[]} nums
6+
*@return {number}
7+
*/
8+
varfindMin=function(nums){
9+
varlo=0;
10+
varhi=nums.length-1;
11+
while(lo<=hi){
12+
if(nums[lo]<nums[hi])returnnums[lo];
13+
varmid=lo+Math.floor((hi-lo)/2);
14+
if(nums[lo]<nums[mid])lo=mid+1;
15+
elseif(nums[lo]>nums[mid])hi=mid;
16+
elselo++;
17+
}
18+
19+
returnnums[hi];
20+
};

‎Hard/33-searchRotatedSortedArray.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/**
2-
* Binary search
2+
* Binary search. In the rotated array, there must be a half of the array in sorted order.
3+
*@see the recursive method. If the target is in the sorted half, binary search the target in
4+
* this half. Otherwise, search the target in the other half. And recursively do this process until
5+
* the target is found.
36
*
47
*@param {number[]} nums
58
*@param {number} target
@@ -31,7 +34,7 @@ var searchHelper = function(nums, left, right, target) {
3134
}
3235
};
3336

34-
// Interative, to be continued
37+
// Interative
3538
varsearch=function(nums,target){
3639
varleft=0;
3740
varright=nums.length-1;

‎Medium/153-findMinimumInRotatedSortedArray.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,24 @@ var findMin = function(nums) {
2121

2222
returnnums[0];
2323
};
24+
25+
// second try
26+
varfindMin=function(nums){
27+
varlo=0;
28+
varhi=nums.length-1;
29+
30+
while(lo<=hi){
31+
if(nums[lo]<nums[hi])returnnums[lo];
32+
varmid=lo+Math.floor((hi-lo)/2);
33+
if(nums[lo]<=nums[mid]){
34+
// this case, minimun must exist in the second half
35+
lo=mid+1;
36+
}else{
37+
// this case, mininum can be mid itself or must exist in the first half.
38+
hi=mid;
39+
}
40+
}
41+
42+
// if hi < lo, then nums[hi] is the minum.
43+
returnnums[hi];
44+
};

‎Medium/71-simplifyPath.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Key: use stack
3+
*@param {string} path
4+
*@return {string}
5+
*/
6+
varsimplifyPath=function(path){
7+
varstack=[];
8+
varpaths=path.split('/');
9+
for(vari=0,length=paths.length;i<length;i++){
10+
if(paths[i]==='..'){
11+
stack.pop();
12+
}elseif(paths[i]==='.'){
13+
continue;
14+
}elseif(paths[i]!==''){
15+
stack.push(paths[i]);
16+
}
17+
}
18+
// if '/../'
19+
returnstack.length===0 ?'/' :'/'+stack.join('/');
20+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Because duplicates are allowed in the array, consider the following two scenarios
3+
* 1. [1,1,3,1,1,1,1,1,1,1,1,1] 2. [1,1,1,1,1,1,1,1,1,1,1,1,3]
4+
* if nums[mid] === nums[lo], that means:
5+
* 1) all lefts are 1
6+
* 2) there is a different number exists in this half.
7+
* So, we need to increase lo one. --> worst case is the second array.O(n)
8+
*
9+
*@param {number[]} nums
10+
*@param {number} target
11+
*@return {boolean}
12+
*/
13+
varsearch=function(nums,target){
14+
varlo=0;
15+
varhi=nums.length-1;
16+
17+
while(lo<=hi){
18+
varmid=lo+Math.floor((hi-lo)/2);
19+
if(nums[mid]===target)returntrue;
20+
21+
if(nums[lo]<nums[mid]){
22+
if(target>=nums[lo]&&target<nums[mid]){
23+
hi=mid-1;
24+
}else{
25+
lo=mid+1;
26+
}
27+
}elseif(nums[lo]>nums[mid]){
28+
if(target<=nums[hi]&&target>nums[mid]){
29+
lo=mid+1;
30+
}else{
31+
hi=mid-1;
32+
}
33+
}else{
34+
lo+=1;
35+
}
36+
}
37+
38+
returnfalse;
39+
40+
};

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,15 @@
9999
*[63.Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) -[Solution](./Medium/63-uniquePathsII.js)
100100
*[64.Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) -[Solution](./Medium/64-minimumPathSum.js)
101101
*[69. Sqrt(x)](https://leetcode.com/problems/sqrtx/) -[Solution](./Medium/69-sqrtx.js)
102+
*[71. Simplify Path](https://leetcode.com/problems/simplify-path/) -[Solution](./Medium/71-simplifyPath.js)
102103
*[73. Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) -[Solution](./Medium/73-setMatrixZeroes.js)
103104
*[74. Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) -[Solution](./Medium/74-search2DMatrix.js)
104105
*[75. Sort Colors](https://leetcode.com/problems/sort-colors/) -[Solution](./Medium/75-sortColors.js)
105106
*[77.Combinations](https://leetcode.com/problems/combinations/) -[Solution](./Medium/77-combinations.js)
106107
*[78. Subsets](https://leetcode.com/problems/subsets/) -[Solution](./Medium/78-subsets.js)
107108
*[79. Word Search](https://leetcode.com/problems/word-search/) -[Solution](./Medium/79-wordSearch.js)
108109
*[80. Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) -[Solution](./Medium/80-removeDuplicatesII.js)
110+
*[81. Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/) -[Solution](./Medium/81-searchRotatedSortedArrayII.js)
109111
*[82. Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) -[Solution](./Medium/82-removeDuplicatesListII.js)
110112
*[89. Gray Code](https://leetcode.com/problems/gray-code/) -[Solution](./Medium/89-grayCode.js)
111113
*[90. Subsets II](https://leetcode.com/problems/subsets-ii/) -[Solution](./Medium/90-subsetsII.js)
@@ -151,3 +153,4 @@
151153
*[32. Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) -[Solution](./Hard/32-longestValidParentheses.js)
152154
*[33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) -[Solution](./Hard/33-searchRotatedSortedArray.js)
153155
*[123. Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/) -[Solution](./Hard/123-bestTimeBuySellStockIII.js)
156+
*[154.Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/) -[Solution](./Hard/154-findMinimumInRotatedSortedArray-II.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp