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

Commit282e5e3

Browse files
committed
add: Search in Rotated Sorted Array
1 parent01ccda9 commit282e5e3

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

‎README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#LeetCodeOJ
22

3-
This is the solution collection of my LeetCode problems, most of them are programmed in JavaScript.
3+
This is the solution collection of my LeetCode problems, most of them are programmed in JavaScript. All JavaScript codes are wrote in ECMAScript 6 standard, each solution file will contain a problem description in the beginning.
44

5-
Progress:20/
5+
**Progress:28/**
66

77
| ID| Title| Solution| Difficulty|
88
|---| -----| --------| ----------|
@@ -13,6 +13,7 @@ Progress: 20/
1313
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)|[JavaScript](./src/remove-nth-node-from-end-of-list/res.js)|Medium|
1414
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)|[JavaScript](./src/generate-parentheses/res.js)|Medium|
1515
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)|[JavaScript](./src/remove-duplicates-from-sorted-array/res.js)|Easy|
16+
|33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)|[JavaScript](./src/search-in-rotated-sorted-array/res.js)|Medium|
1617
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/)|[JavaScript](./src/search-for-a-range/res.js)|Medium|
1718
|48|[Rotate Image](https://leetcode.com/problems/rotate-image/)|[JavaScript](./src/rotate-image/res.js)|Medium|
1819
|66|[Plus One](https://leetcode.com/problems/plus-one/)|[JavaScript](./src/plus-one/res.js)|Easy|
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
3+
*
4+
* You are given a target value to search. If found in the array return its index, otherwise return -1.
5+
*
6+
* You may assume no duplicate exists in the array.
7+
*
8+
* res.js
9+
*@authors Joe Jiang (hijiangtao@gmail.com)
10+
*@date 2017-02-26 23:34:54
11+
*@version $Id$
12+
*
13+
*@param {number[]} nums
14+
*@param {number} target
15+
*@return {number}
16+
*/
17+
letsearch=function(nums,target){
18+
letcount=0,
19+
curIndex=0,
20+
preIndex=0,
21+
larger=true,
22+
numslen=nums.length;
23+
24+
if(numslen===0){
25+
return-1;
26+
}
27+
if(nums[0]<target){
28+
larger=false;
29+
}
30+
31+
// 需要考虑两点: 到了原始排序数组边缘跳转, 或者 target 不存在对比出现大小反转的情况
32+
while(count<numslen){
33+
// Found the target
34+
if(nums[curIndex]===target){
35+
returncurIndex;
36+
}elseif(nums[curIndex]>target){
37+
if(!larger||nums[curIndex]>nums[preIndex]){
38+
return-1;
39+
}
40+
41+
preIndex=curIndex;
42+
curIndex=(curIndex-1+numslen)%numslen;
43+
}else{
44+
if(larger||nums[curIndex]<nums[preIndex]){
45+
return-1;
46+
}
47+
48+
preIndex=curIndex;
49+
curIndex=(curIndex+1)%numslen;
50+
}
51+
52+
count+=1;
53+
}
54+
55+
return-1;
56+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp