Skip to content

Commit 4874f8b

Browse files
authored
Merge pull request #2 from gzc426/master
leetcode
2 parents 0a0e5f7 + 07646c4 commit 4874f8b

Some content is hidden

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

81 files changed

+2464
-7
lines changed
File renamed without changes.
File renamed without changes.

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/kiritocly.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
private static int solution(int[] array) {
4+
//从左右两边同时遍历,每次都舍弃最短边
5+
int left = 0;
6+
int right = array.length - 1;
7+
int resultTemp = 0;
8+
int result = 0;
9+
while (left < right) {
10+
if (array[left] <= array[right]) {
11+
//左边柱子低于右边
12+
//计算临时盛水容量
13+
resultTemp = array[left] * (right - left);
14+
//矮柱子向中间移动
15+
left++;
16+
} else if (array[left] >= array[right]) {
17+
//左边柱子高于右边
18+
//计算临时盛水容量
19+
resultTemp = array[right] * (right - left);
20+
//矮柱子向中间移动
21+
right--;
22+
}
23+
//判断临时盛水容量
24+
if (resultTemp > result) {
25+
result = resultTemp;
26+
}
27+
}
28+
return result;
29+
}

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+
}
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/kiritocly.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
private static boolean solution(String input) {
2+
if (input == null || "".equals(input)) {
3+
return true;
4+
}
5+
char[] inputCharArray = input.toCharArray();
6+
//定义两个指针,一个头指针一个尾指针
7+
int head = 0;
8+
int tail = inputCharArray.length - 1;
9+
//判断字母和数字字符的正则表达式
10+
String regex = "^[0-9a-zA-Z]$";
11+
while (head <= tail) {
12+
String headStr = inputCharArray[head] + "";
13+
String tailStr = inputCharArray[tail] + "";
14+
if (headStr.matches(regex) && tailStr.matches(regex)) {
15+
if (headStr.toLowerCase().equals(tailStr.toLowerCase())) {
16+
//比较下一个字符
17+
head++;
18+
tail--;
19+
} else {
20+
//如果未匹配则直接返回
21+
return false;
22+
}
23+
} else {
24+
//如果比较的字符不为字母或数字则不比较
25+
if (!headStr.matches(regex)) {
26+
head++;
27+
}
28+
if (!tailStr.matches(regex)) {
29+
tail--;
30+
}
31+
}
32+
}
33+
return true;
34+
}

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/。。。.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+
}

0 commit comments

Comments
 (0)