Skip to content

Commit 0adf28d

Browse files
author
Longerhaha
authored
Merge pull request gzc426#14 from gzc426/master
更新乔戈里Leetcode每日一题
2 parents ac05ff1 + 917ca1b commit 0adf28d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+2779
-6
lines changed

2018.11.24-leetcode28/雪辰.markdown

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def strStr(self, haystack, needle):
3+
"""
4+
:type haystack: str
5+
:type needle: str
6+
:rtype: int
7+
"""
8+
lenNeedle=len(needle)
9+
if not needle.strip():
10+
return (0)
11+
if len(needle)>len(haystack):
12+
return(-1)
13+
match=0
14+
for a in range(len(haystack)-lenNeedle+1):
15+
m = a
16+
match = 0
17+
for i in range(lenNeedle):
18+
if haystack[m]==needle[i]:
19+
match+=1
20+
m+=1
21+
if match == lenNeedle:
22+
break
23+
if match == lenNeedle:
24+
break
25+
if match == lenNeedle:
26+
return(a)
27+
else:
28+
return(-1)
29+

2018.11.26-leetcode11/zjukk.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
```
2+
#include <iostream>
3+
#include <vector>
4+
using namespace std;
5+
6+
//mysol:
7+
int removeDuplicates(vector<int>& nums) {
8+
for (int i = 1; i < nums.size(); i++) {
9+
if (nums[i] == nums[i-1])
10+
nums.erase(nums.begin() + i);
11+
}
12+
return nums.size();
13+
}
14+
//better sol:
15+
int removeDuplicates(vector<int>& nums) {
16+
int i = 0;
17+
for (int j = 1; j < nums.size(); j++) {
18+
if (nums[i] != nums[j]) {
19+
i++;
20+
nums[i] = nums[j];
21+
}
22+
}
23+
return i + 1;
24+
}
25+
```

2018.11.26-leetcode11/。。。.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public int maxArea(int[] height) {
2+
int left=0,right=height.length-1,area = 0;
3+
while (left < right){
4+
area = Math.max(area,(right-left)*Math.min(height[left],height[right]));
5+
if (height[left] < height[right]){
6+
left++;
7+
}else {
8+
right--;
9+
}
10+
}
11+
return area;
12+
}

2018.11.26-leetcode11/暮成雪.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
if( height.length < 2 ){
4+
return 0;
5+
}
6+
//双指针,复杂度O(n),5ms 91%
7+
int max=0;
8+
int i=0;
9+
int j= height.length-1;
10+
while(i < j){
11+
int areaTmp= (j-i) * Math.min(height[i] , height[j]);
12+
Boolean flag = height[i] > height[j] ? true : false;
13+
if(max < areaTmp){
14+
max = areaTmp;
15+
}
16+
if(flag == true){
17+
j--;
18+
}else{
19+
i++;
20+
}
21+
}
22+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package JZoffer;
2+
3+
/**
4+
* @program: Algorithm
5+
* @description: 11盛最多水的容器
6+
* @author: liu yan
7+
* @create: 2018-11-28 11:11
8+
*/
9+
public class Solution1 {
10+
public int MaxArea(int[] array){
11+
if(array.length<=1){
12+
return 0;
13+
}
14+
int left=0;
15+
int right = array.length-1;
16+
int result=0;
17+
int temparea=0;
18+
while (left<right){
19+
int len= right-left;
20+
if(array[left]>array[right]){
21+
temparea=array[right]*len;
22+
right--;
23+
}else {
24+
temparea=array[left]*len;
25+
left++;
26+
}
27+
if(temparea>result){
28+
result=temparea;
29+
}
30+
}
31+
return result;
32+
}
33+
34+
public static void main(String[] args) {
35+
Solution1 s1 = new Solution1();
36+
int[] a = {1,8,6,2,5,4,8,3,7};
37+
int b = s1.MaxArea(a);
38+
System.out.println("盛最多水的容器面积是:"+b);
39+
}
40+
}

2018.11.27-leetcode125/caroline.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
题目详述
2+
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
3+
n<=39
4+
(这里的字体怎么调呀,要下什么插件吗,另外,斐波那契数列的第0项怎么是0,百科里面是1,这样婶儿的,斐波那契数列指的是这样一个
5+
数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368........)
6+
7+
public class Solution{
8+
public int Fibonacci(int n){
9+
int a = 0;
10+
int b =1;
11+
if(n == 0)
12+
return 1;
13+
if(n == 1)
14+
return 1;
15+
int i=2;
16+
int sum=0;
17+
while(i<=n){
18+
sum = a+b;
19+
a=b;
20+
b=sum;
21+
i++;
22+
}
23+
return sum;
24+
}
25+
}

2018.11.27-leetcode125/tongLuoWan.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
>leetcode 125. Valid Palindrome
2+
3+
```
4+
class Solution {
5+
6+
public boolean isPalindrome(String s) {
7+
if(s.length()==0)
8+
return true;
9+
s=s.toLowerCase();
10+
int i = 0, j = s.length()- 1;
11+
while (i<j){
12+
if(!Character.isLetterOrDigit(s.charAt(i)))
13+
i++;
14+
else if(!Character.isLetterOrDigit(s.charAt(j)))
15+
j--;
16+
else if(s.charAt(i) != s.charAt(j))
17+
return false;
18+
else{ i++;j--;}
19+
}
20+
return true;
21+
}
22+
}
23+
```
24+
>2018.11.27号打卡

2018.11.27-leetcode125/zjukk.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
```
2+
#include <iostream>
3+
#include <string>
4+
5+
using namespace std;
6+
7+
class Solution {
8+
public:
9+
bool isPalindrome(string s) {
10+
int l = 0, r = s.size() - 1;
11+
bool flag = false;
12+
while (l <= r) {
13+
if (!isAlphaNum(s[l])) {
14+
++l; continue;
15+
}
16+
if (!isAlphaNum(s[r])) {
17+
--r; continue;
18+
}
19+
if ((s[r] + 32 - 'a') % 32 != (s[l] + 32 - 'a') % 32) {flag = true; break;}
20+
++l; --r;
21+
}
22+
return !flag;
23+
}
24+
bool isAlphaNum(char c) {
25+
if (c >= 'a' && c <= 'z') return true;
26+
if (c >= 'A' && c <= 'Z') return true;
27+
if (c >= '0' && c <= '9') return true;
28+
return false;
29+
}
30+
};
31+
32+
int main() {
33+
Solution s;
34+
cout << s.isPalindrome("race a car");
35+
//A man, a plan, a canal: Panama
36+
}
37+
```

2018.11.27-leetcode125/。。。.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public boolean isPalindrome(String s){
2+
if (s.length() >= 2){
3+
int left = 0,right = s.length()-1;
4+
char leftChar,rightChar;
5+
while (left <= right){
6+
leftChar = s.charAt(left);rightChar = s.charAt(right);
7+
if (leftChar>='A'&&leftChar<='Z'){//转换大写字母为小写
8+
leftChar+=32;
9+
}else if (rightChar>='A'&&rightChar<='Z'){
10+
rightChar+=32;
11+
}
12+
if (leftChar == rightChar){
13+
left++;right--;
14+
continue;
15+
}
16+
//跳过非字母非数字的字符
17+
if (!((leftChar>='0'&& leftChar<='9') || (leftChar>='a'&&leftChar<='z') || (leftChar>='A'&&leftChar<='Z'))){
18+
left++;
19+
continue;
20+
}else if (!((rightChar>='0'&& rightChar<='9') || (rightChar>='a'&&rightChar<='z') || (rightChar>='A'&&rightChar<='Z'))){
21+
right--;
22+
continue;
23+
}
24+
if (leftChar != rightChar)return false;
25+
}
26+
}
27+
return true;
28+
}

2018.11.28

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
/**
3+
* Created by zongjianwei
4+
* DATE: 21:32 2018/11/28
5+
* Desc: 压缩字符数组
6+
*/
7+
public class StringCompression {
8+
public int compress(char[] chars){
9+
int m = 0;
10+
int n = 0;
11+
for(int i=0;i<chars.length;i++){
12+
if(i+1 == chars.length || chars[i+1]!=chars[i]){
13+
chars[m++] = chars[n];
14+
if(i>n){
15+
for(int j=0;j<(""+(i-n+1)).toCharArray().length;j++){
16+
chars[m++] = chars[j];
17+
}
18+
}
19+
n = i +1;
20+
}
21+
}
22+
return m;
23+
}
24+
25+
public static void main(String[] args) {
26+
char[] chars = {'a','a','b','b','c','c','c'};
27+
StringCompression sc = new StringCompression();
28+
int length = sc.compress(chars);
29+
System.out.println(length);
30+
31+
}
32+
}

0 commit comments

Comments
 (0)