Skip to content

Commit 265cfee

Browse files
authored
Merge pull request #1 from gzc426/master
Merge pull request gzc426#12 from Syuan-Cheng/master
2 parents e4dddc0 + 6f66a0b commit 265cfee

File tree

118 files changed

+3322
-18
lines changed

Some content is hidden

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

118 files changed

+3322
-18
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.15/暮成雪.md

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

2018.11.19-leetcode15/Felix.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
```
2+
import java.util.Arrays;
3+
4+
/**
5+
* @author Felix
6+
* @date 2018年11月2日下午7:27:55
7+
@version 给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。
8+
返回这三个数的和。假定每组输入只存在唯一答案。例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.
9+
与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).
10+
*/
11+
public class ThreeSumClosest {
12+
public int threeSumClosest(int[] nums,int target){
13+
int res = nums[0] + nums[1] +nums[nums.length-1];
14+
Arrays.sort(nums);//将数组排序
15+
for(int first=0; first<nums.length-2; first++){//从0开始固定第一个数,第二三个数分别为剩下的边界
16+
int second = first+1,third = nums.length-1;
17+
while(second < third){
18+
int sum = nums[first] + nums[second] + nums[third];
19+
if(sum < target){//数组已经从小到大排序,若sum<target,则第二个数向右走,相当于让sum值变大,更接近target
20+
second++;
21+
}else if(sum > target){//若小于则第三个数向左走,相当于让sum值变小,更接近target
22+
third--;
23+
}else{//相等则最接近target,直接返回
24+
return sum;
25+
}
26+
res = Math.abs(sum-target)<Math.abs(res-target) ? sum : res;
27+
}
28+
}
29+
return res;
30+
}
31+
}
32+
```
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+
```

2018.11.24-leetcode28/ELF

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int strStr(String haystack, String needle) {
3+
if (needle.length() ==0 )
4+
return 0;
5+
for (int i = 0;i<haystack.length();i++){
6+
if (haystack.charAt(i) == needle.charAt(0)){
7+
int j =i+1;
8+
int k =1;
9+
for (k =1;k<needle.length()&&j<haystack.length();k++){
10+
if (haystack.charAt(j) == needle.charAt(k))
11+
j++;
12+
else
13+
break;
14+
15+
}
16+
if (k == needle.length())
17+
return i;
18+
}
19+
}
20+
return -1;
21+
}
22+
}

2018.11.24-leetcode28/TheRocket

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
```java
2+
class Solution {
3+
public int strStr(String haystack, String needle) {
4+
if (needle.length() == 0) {
5+
return 0;
6+
}
7+
int max = haystack.length() - needle.length();
8+
for (int i = 0; i <= max; ++i) {
9+
// 找到第一个相等的字符
10+
if (haystack.charAt(i) == needle.charAt(0)) {
11+
int j = i + 1;
12+
for (int k = 1; k < needle.length()
13+
&& haystack.charAt(j) == needle.charAt(k); ++k, ++j);
14+
// 找到整个字符串就返回
15+
if (j == i + needle.length()) {
16+
return i;
17+
}
18+
}
19+
}
20+
return -1;
21+
}
22+
}
23+
```
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/WhiteNight.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
>```java
2+
>/**
3+
> * 实现strStr()
4+
> *
5+
> */
6+
>public class S1 {
7+
>// public int strStr(String haystack, String needle) {
8+
>// //直接调用indexof()
9+
>// return haystack.indexOf(needle);
10+
>// }
11+
>
12+
> public int strStr(String haystack, String needle) {
13+
> if (needle.length() == 0)
14+
> return 0;
15+
>
16+
> char[] charArrayHaystack = haystack.toCharArray();
17+
> char[] charArrayNeedle = needle.toCharArray();
18+
>
19+
> for (int i = 0; i < charArrayHaystack.length; i++) {
20+
> if (charArrayHaystack[i] == charArrayNeedle[0]){
21+
> int j = i + 1;
22+
> int k = 1;
23+
> for (;k < charArrayNeedle.length && j < charArrayHaystack.length; k++) {
24+
> if (charArrayHaystack[j] == charArrayNeedle[k])
25+
> j++;
26+
> else
27+
> break;
28+
> }
29+
> if (k == charArrayNeedle.length)
30+
> return i;
31+
> }
32+
> }
33+
>
34+
> return -1;
35+
> }
36+
>
37+
> public static void main(String[] args) {
38+
> S1 s = new S1();
39+
> int res = s.strStr("hello","ll");
40+
> System.out.println(res);
41+
> }
42+
>}
43+
>```
44+
File renamed without changes.

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+
}

0 commit comments

Comments
 (0)