Skip to content

Commit 8b220f0

Browse files
Merge pull request gzc426#6 from gzc426/master
12.2tongbu
2 parents ace0c72 + 97c4cce commit 8b220f0

File tree

125 files changed

+4310
-6
lines changed

Some content is hidden

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

125 files changed

+4310
-6
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.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
leetcode of 15
2+
```
3+
class Solution {
4+
public:
5+
vector<vector<int>> threeSum(vector<int>& nums) {
6+
7+
vector<vector<int>> res;
8+
if(nums.size()==0) return res;
9+
sort(nums.begin(),nums.end());
10+
int i=0;
11+
int p=0;
12+
int q=0;
13+
while(i<nums.size()-2){
14+
if(i>0 &&nums[i-1]==nums[i]){
15+
i++;
16+
continue;
17+
}
18+
if(nums[i]>0) break;
19+
int diff=0-nums[i];
20+
p=i+1;
21+
q=nums.size()-1;
22+
while(p<q){
23+
if(nums[p]+nums[q]<diff && p<q) p++;
24+
if(nums[p]+nums[q]>diff && p<q) q--;
25+
else if(p<q && nums[p]+nums[q]==diff){
26+
vector<int>temp;
27+
temp.push_back(nums[i]);
28+
temp.push_back(nums[p]);
29+
temp.push_back(nums[q]);
30+
res.push_back(temp);
31+
p++;
32+
q--;
33+
while(p<q &&nums[p]==nums[p-1]){
34+
p++;
35+
}
36+
while(p<q &&nums[q]==nums[q+1]){
37+
q--;
38+
}
39+
}
40+
}
41+
i++;
42+
}
43+
return res;
44+
}
45+
};
46+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
```
2+
class Solution {
3+
public int strStr(String haystack, String needle) {
4+
int len = haystack.length();
5+
int sublen = needle.length();
6+
for (int i = 0; i < len - sublen + 1; i++){
7+
if (haystack.substring(i, i + sublen).equals(needle)){
8+
return i;
9+
}
10+
}
11+
return -1;
12+
}
13+
}
14+
```

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# LeetCode 28
2+
public static int strStr(String haystack, String needle) {
3+
if (needle.length() == 0) {
4+
return 0;
5+
}
6+
if (haystack.length() == 0 || haystack == null || needle.length() > haystack.length()) {
7+
return -1;
8+
}
9+
int hStart,nStart;
10+
for (int i = 0; i < haystack.length(); i++) {
11+
hStart=i;
12+
nStart=0;
13+
while (nStart < needle.length() && hStart < haystack.length()) {
14+
if (haystack.charAt(hStart) == needle.charAt(nStart)) {
15+
hStart++;
16+
nStart++;
17+
} else {
18+
break;
19+
}
20+
if (nStart == needle.length()) {
21+
return i;
22+
}
23+
24+
}
25+
}
26+
return -1;
27+
}

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+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public int strStr(String haystack, String needle) {
3+
if(needle.length()==0){
4+
return 0;
5+
}
6+
char [] hay=haystack.toCharArray();
7+
char [] nde=needle.toCharArray();
8+
9+
for(int i=0;i<hay.length;i++){
10+
11+
if(hay[i]==nde[0]){ //遇到相同的情况
12+
int j=i+1;//指针j比较i的下一个元素与needle是否相同
13+
int k=1; //指针k遍历needle数组,从第二个开始比较,第一个已经判断相同的情况下才进入的。
14+
for(;k<nde.length&&j<hay.length;k++){
15+
if(hay[j]==nde[k]){//如果下一个元素还相同
16+
j++; //则比较haystack的下一个
17+
}else{
18+
break;//很快遇到不同的则退出。
19+
}
20+
}
21+
if(k==nde.length){
22+
return i;
23+
}
24+
}
25+
}
26+
27+
return -1;
28+
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
leetcode number 80
2+
```
3+
class Solution {
4+
public:
5+
void swap(int &a,int &b){
6+
int c = a;
7+
a = b;
8+
b = a;
9+
10+
}
11+
int removeDuplicates(vector<int>& nums) {
12+
int j = -1;
13+
int count = 0;
14+
for(int i = 0;i < nums.size();i++){
15+
if(i != 0 && nums[i] == nums[i-1]) {
16+
if(count >= 1) continue;
17+
++count;
18+
}
19+
else
20+
count = 0;
21+
nums[++j]=nums[i];
22+
}
23+
return j+1;
24+
25+
}
26+
};
27+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
```
2+
3+
class Solution {
4+
public int removeDuplicates(int[] nums) {
5+
if (nums.length < 3){
6+
return nums.length;
7+
}
8+
int pos = 2;
9+
for(int i = 2; i < nums.length; i++){
10+
if(nums[i]!=nums[pos-2])
11+
nums[pos++]=nums[i];
12+
}
13+
return pos;
14+
}
15+
}
16+
```

0 commit comments

Comments
 (0)