Skip to content

Commit 44324ae

Browse files
authored
Merge pull request gzc426#13 from gzc426/master
for merging
2 parents 94dd8e5 + ccfe23f commit 44324ae

Some content is hidden

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

52 files changed

+1961
-0
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2018.11.24-leetcode28/Whits.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package AC;
2+
3+
import java.lang.reflect.Array;
4+
5+
public class leetcode_28_AC {
6+
7+
8+
9+
public static void main(String[] args) {
10+
11+
String haystack = "hello";
12+
String needle = "ll";
13+
System.out.println(strStrNew(haystack,needle));
14+
15+
}
16+
public static int strStr(String haystack, String needle) {
17+
if (needle.length()==0)
18+
return 0;
19+
char[]hayStackCharArray =haystack.toCharArray();
20+
char[]needleCharArray = needle.toCharArray();
21+
for (int i = 0; i < hayStackCharArray.length; i++) {
22+
if (hayStackCharArray[i] == needleCharArray[0]) { //第一个指针对比,若相同则进入if语句。
23+
int j = i+1; //把此时的i+1赋值给k,直接比较第二个值
24+
int k = 1; //直接比较第二个值
25+
for (; k<needleCharArray.length && j<hayStackCharArray.length; k++) {
26+
if (hayStackCharArray[j] == needleCharArray[k])
27+
j++;
28+
else {
29+
break;
30+
}
31+
}
32+
if (needleCharArray.length==k){
33+
return i;
34+
}
35+
}
36+
}
37+
return -1;
38+
}
39+
40+
public static int strStrNew(String haystack, String needle){
41+
int hayStackLength = haystack.length();
42+
int needleLength = needle.length();
43+
if (needleLength>hayStackLength) return -1; if (needleLength == 0) return 0;
44+
for (int i = 0; i<hayStackLength-needleLength;i++) {
45+
if (haystack.substring(i,needleLength+i).equals(needle))
46+
return i;
47+
}
48+
return -1;
49+
}
50+
/**
51+
* 还应该尝试下kmp算法
52+
* @author:liuquan
53+
* @date:2018/11/28 22:00
54+
* @param
55+
* @return
56+
*/
57+
58+
}

2018.11.24-leetcode28/syuan.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
```
2+
class Solution {
3+
public int strStr(String haystack, String needle) {
4+
if(needle.length() == 0)
5+
return 0;
6+
char [] charArrayhaystack = haystack.toCharArray();
7+
char [] charArrayneedle = needle.toCharArray();
8+
for(int i =0;i<charArrayhaystack.length;i++)
9+
{
10+
if(charArrayhaystack[i] == charArrayneedle[0])
11+
{
12+
int j = i + 1;
13+
int k = 1;
14+
for(k=1;k<charArrayneedle.length && j < charArrayhaystack.length;k++)
15+
{
16+
if(charArrayhaystack[j] == charArrayneedle[k])
17+
{
18+
j++;
19+
}else{
20+
break;
21+
}
22+
}
23+
if(k == charArrayneedle.length)
24+
return i;
25+
}
26+
}
27+
return -1;
28+
}
29+
}
30+
```

2018.11.24-leetcode28/syuan1.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
##### 题目
2+
3+
```
4+
实现 strStr() 函数。
5+
6+
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
7+
8+
示例 1:
9+
10+
输入: haystack = "hello", needle = "ll"
11+
输出: 2
12+
13+
示例 2:
14+
15+
输入: haystack = "aaaaa", needle = "bba"
16+
输出: -1
17+
18+
说明:
19+
20+
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
21+
22+
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
23+
```
24+
##### 思路
25+
在haystack上找跟needle首字符相等的位置,然后依次与needle剩余的字符进行比较,如果发现不同,将needle的指针归零 将haystack 的指针归到比较遍历的起始加一(j=j-i+1) 然后重复上述操作即可。
26+
27+
##### 代码
28+
```
29+
class Solution {
30+
public int strStr(String haystack, String needle) {
31+
char[] hayArray=haystack.toCharArray();
32+
char[] neeArray=needle.toCharArray();
33+
int lengthHay=hayArray.length;
34+
int lengthNee=neeArray.length;
35+
//如果needle的长度大于haystck的长度 返回-1
36+
if (needle.equals("")) {
37+
return 0;
38+
}
39+
if (lengthHay<lengthNee) {
40+
return -1;
41+
}
42+
//判定
43+
int j=0,i=0;
44+
while(true){
45+
while(j<lengthHay && hayArray[j]!=neeArray[0]){
46+
j++;
47+
}
48+
49+
if (j+lengthNee>lengthHay) {
50+
return -1;
51+
}else{
52+
while(i<lengthNee){
53+
if (hayArray[j]==neeArray[i]) {
54+
j++;
55+
i++;
56+
}else{
57+
j=j-i+1;
58+
i=0;
59+
break;
60+
}
61+
}
62+
}
63+
64+
if (i==lengthNee) {
65+
break;
66+
}
67+
}
68+
69+
return j-lengthNee;
70+
}
71+
}
72+
```

2018.11.25-leetcode80/syuan.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
##### 题目
2+
3+
```
4+
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度。
5+
6+
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
7+
8+
示例 1:
9+
10+
给定 nums = [1,1,1,2,2,3],
11+
12+
函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。
13+
14+
你不需要考虑数组中超出新长度后面的元素。
15+
16+
示例 2:
17+
18+
给定 nums = [0,0,1,1,1,1,2,3,3],
19+
20+
函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。
21+
22+
你不需要考虑数组中超出新长度后面的元素。
23+
24+
说明:
25+
26+
为什么返回数值是整数,但输出的答案是数组呢?
27+
28+
请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
29+
30+
你可以想象内部操作如下:
31+
32+
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
33+
int len = removeDuplicates(nums);
34+
35+
// 在函数里修改输入数组对于调用者是可见的。
36+
// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
37+
for (int i = 0; i < len; i++) {
38+
print(nums[i]);
39+
}
40+
```
41+
##### 代码
42+
43+
```
44+
class Solution {
45+
public int removeDuplicates(int[] nums) {
46+
int count=0;
47+
if (nums.length==1) {
48+
return 1;
49+
}
50+
if (nums.length==0) {
51+
return 0;
52+
}
53+
if (nums.length==2) {
54+
return 2;
55+
}
56+
int i=0;
57+
int j=i;
58+
int tempCount=0;
59+
while(i<nums.length)
60+
{
61+
if (j<nums.length && nums[j]==nums[i]) {
62+
j++;
63+
tempCount++;
64+
}else{
65+
if (tempCount>=2) {
66+
count+=2;
67+
nums[i+1]=nums[i];
68+
i=i+2;
69+
}else{
70+
count++;
71+
i++;
72+
}
73+
if (j>=nums.length) {
74+
break;
75+
}
76+
nums[i]=nums[j];
77+
tempCount=0;
78+
}
79+
}
80+
return count;
81+
}
82+
}
83+
```

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
##### 题目
2+
3+
```
4+
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
5+
6+
说明:你不能倾斜容器,且 n 的值至少为 2。
7+
```
8+
![微信截图_20181127122321](2DF0C29B574D4A30BFAB06ABDF185FA9)
9+
```
10+
图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
11+
示例:
12+
13+
输入: [1,8,6,2,5,4,8,3,7]
14+
输出: 49
15+
```
16+
##### 思路
17+
双指针
18+
19+
##### 代码
20+
21+
```
22+
class Solution {
23+
public int maxArea(int[] height){
24+
int maxarea=0;
25+
int i=0;
26+
int j=height.length-1;
27+
28+
while(i<j){
29+
maxarea=Math.max(maxarea,Math.min(height[i],height[j])*(j-i));
30+
if(height[i]<height[j]){
31+
i++;
32+
}else{
33+
j--;
34+
}
35+
}
36+
return maxarea;
37+
}
38+
}
39+
```

2018.11.27-leetcode125/FFFro.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
s = s.toLowerCase();
4+
char[] charArray = s.toCharArray();
5+
String res = "";
6+
for (int i = 0; i < charArray.length; i++) {
7+
if ((int)charArray[i]>=48&&(int)charArray[i]<=57 || (int)charArray[i]>=97&&(int)charArray[i]<=122)
8+
res+=charArray[i];
9+
}
10+
char[] array = res.toCharArray();
11+
int start = 0;
12+
int end = array.length-1;
13+
while (end>start){
14+
15+
if(array[start] == array[end]){
16+
start++;
17+
end--;
18+
}else {
19+
return false;
20+
}
21+
}
22+
return true;
23+
24+
}
25+
}

0 commit comments

Comments
 (0)