Skip to content

feat: add solution to leetcode No.0033 No.0081 #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions solution/0000-0099/0033.Search in Rotated Sorted Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,46 @@

```

### **C++**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```cpp
class Solution {
public:
int search(vector<int>& nums, int target) {
if(nums.size() < 1) {
return -1;
}
if(nums.size() == 1) {
return nums[0] == target ? 0 : -1;
}
int n = nums.size();
int l = 0, r = n - 1;
while(l <= r) {
int mid = (l + r) / 2;
if(nums[mid] == target) {
return mid;
}
if(nums[l] <= nums[mid]) {
if(nums[l] <= target && target < nums[mid]) {
r = mid - 1;
}else {
l = mid + 1;
}
}else {
if(nums[mid] < target && target <= nums[r]) {
l = mid + 1;
}else {
r = mid - 1;
}
}
}
return -1;
}
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,46 @@

```

### **C++**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```cpp
class Solution {
public:
int search(vector<int>& nums, int target) {
if(nums.size() < 1) {
return -1;
}
if(nums.size() == 1) {
return nums[0] == target ? 0 : -1;
}
int n = nums.size();
int l = 0, r = n - 1;
while(l <= r) {
int mid = (l + r) / 2;
if(nums[mid] == target) {
return mid;
}
if(nums[l] <= nums[mid]) {
if(nums[l] <= target && target < nums[mid]) {
r = mid - 1;
}else {
l = mid + 1;
}
}else {
if(nums[mid] < target && target <= nums[r]) {
l = mid + 1;
}else {
r = mid - 1;
}
}
}
return -1;
}
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Solution {
while(left <= right) {
mid = (left + right) / 2;
if(nums[mid] == target)return mid;

if(nums[mid] < nums[right]) {
if(nums[right] >= target && nums[mid] < target)left = mid + 1;
else right = mid - 1;
Expand All @@ -18,7 +18,7 @@ class Solution {
else left = mid + 1;
}
}
return -1;
return -1;
}
};

Expand All @@ -30,7 +30,7 @@ class Solution {
for(int i = 0;i<len;i++) {
if(target == nums[i])return i;
}

return -1;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,51 @@

```

### **C++**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```cpp
class Solution {
public:
bool search(vector<int>& nums, int target) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里代码格式有问题,缩进异常。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我先帮你合一下吧,然后做下格式调整,下次注意一下格式问题哈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • okok

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之后的代码提交不注明 author 哈,git 已经记录了作者了

if(nums.size() < 1) {
return false;
}
if(nums.size() == 1) {
return nums[0] == target;
}
int n = nums.size();
int l = 0, r = n - 1;
while(l <= r) {
int mid = (l + r) / 2;
if(nums[mid] == target) {
return true;
}
if(nums[l] == nums[mid] && nums[mid] == nums[r]) {
l++;
r--;
}else if(nums[l] <= nums[mid]) {
//
if(nums[l] <= target && target < nums[mid]) {
r = mid - 1;
}else {
l = mid + 1;
}
}else {
//
if(nums[mid] < target && target <= nums[r]) {
l = mid + 1;
}else {
r = mid - 1;
}
}
}
return false;
}
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,51 @@

```

### **C++**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```cpp
class Solution {
public:
bool search(vector<int>& nums, int target) {
if(nums.size() < 1) {
return false;
}
if(nums.size() == 1) {
return nums[0] == target;
}
int n = nums.size();
int l = 0, r = n - 1;
while(l <= r) {
int mid = (l + r) / 2;
if(nums[mid] == target) {
return true;
}
if(nums[l] == nums[mid] && nums[mid] == nums[r]) {
l++;
r--;
}else if(nums[l] <= nums[mid]) {
//
if(nums[l] <= target && target < nums[mid]) {
r = mid - 1;
}else {
l = mid + 1;
}
}else {
//
if(nums[mid] < target && target <= nums[r]) {
l = mid + 1;
}else {
r = mid - 1;
}
}
}
return false;
}
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Author: Moriarty12138
*/
class Solution {
public:
bool search(vector<int>& nums, int target) {
if(nums.size() < 1) {
return false;
}
if(nums.size() == 1) {
return nums[0] == target;
}
int n = nums.size();
int l = 0, r = n - 1;
while(l <= r) {
int mid = (l + r) / 2;
if(nums[mid] == target) {
return true;
}
if(nums[l] == nums[mid] && nums[mid] == nums[r]) {
l++;
r--;
}else if(nums[l] <= nums[mid]) {
//
if(nums[l] <= target && target < nums[mid]) {
r = mid - 1;
}else {
l = mid + 1;
}
}else {
//
if(nums[mid] < target && target <= nums[r]) {
l = mid + 1;
}else {
r = mid - 1;
}
}
}
return false;
}
};