-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zhang Yilin
authored and
Zhang Yilin
committed
Jan 3, 2024
1 parent
2f07730
commit 0f8997e
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
int findPeakElement(int* nums, int numsSize) { | ||
int start =0; | ||
int end=numsSize-1; | ||
int mid; | ||
while (start<end) { | ||
// 二分法查找 | ||
mid = (start + end) /2; | ||
// 更新end值 | ||
if (nums[mid] > nums[mid+1]) { | ||
end = mid; | ||
} else { | ||
// 更新start值 | ||
start = mid+1; | ||
} | ||
} | ||
return start; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
bool checkOnesSegment(char* s) { | ||
int len; | ||
len = strlen(s); | ||
int i=0; | ||
int cnt =0; | ||
while (i<len) { | ||
if (s[i]=='1') { | ||
// 连续的1字符个数+1 | ||
cnt++; | ||
// 超过2个直接返回 | ||
if (cnt>=2) { | ||
return false; | ||
} | ||
while (i<len && s[i]=='1') { | ||
i++; | ||
} | ||
}else { | ||
i++; | ||
} | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
bool checkZeroOnes(char* s) { | ||
int len; | ||
len = strlen(s); | ||
char pre=' '; | ||
char cur=' '; | ||
int max1 = 0; | ||
int max0 = 0; | ||
int cnt = 0; | ||
for (int i=0; i<=len; i++) { | ||
if (i==len) { | ||
cur = ' '; | ||
} else { | ||
cur = s[i]; | ||
} | ||
if (pre!= s[i]) { | ||
// 当前字符跟前一个字符不同 | ||
if (pre=='0') { | ||
if (cnt>max0) { | ||
// 更新0字符最大长度 | ||
max0 = cnt; | ||
} | ||
} | ||
if (pre=='1') { | ||
if (cnt>max1) { | ||
// 更新1字符最大长度 | ||
max1 = cnt; | ||
} | ||
} | ||
pre = s[i]; | ||
cnt = 1; | ||
}else { | ||
cnt++; | ||
} | ||
} | ||
return max1>max0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
int countSegments(char * s){ | ||
int len,cnt; | ||
len = strlen(s); | ||
cnt = 0; | ||
//遍历字符串 | ||
for (int i =0; i<len;) { | ||
// 跳过空格字符 | ||
if (s[i] == ' ') { | ||
i++; | ||
continue; | ||
}else { | ||
//第一个非空格字符计数+1 | ||
cnt++; | ||
//跳过后续的非空格字符 | ||
while (s[i] != ' ' && i<len) { | ||
i++; | ||
} | ||
} | ||
} | ||
//返回单词数量 | ||
return cnt; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
int peakIndexInMountainArray(int* arr, int arrSize) { | ||
int mid; | ||
int start = 0; | ||
int end = arrSize-1; | ||
while (true) { | ||
// 二分法查找 | ||
mid = (start + end)/2; | ||
if (arr[mid]>arr[mid-1] && arr[mid]>arr[mid+1]) { | ||
return mid; | ||
} | ||
// 继续查找mid~end范围 | ||
if (arr[mid]>arr[mid-1] && arr[mid]<arr[mid+1]) { | ||
start = mid; | ||
continue; | ||
} | ||
// 继续查找start~mid范围 | ||
if (arr[mid]<arr[mid-1] && arr[mid]>arr[mid+1]) { | ||
end = mid; | ||
continue; | ||
} | ||
} | ||
} |