Skip to content

Commit 6338041

Browse files
authored
Merge branch 'master' into master
2 parents 806f355 + 5af9c7e commit 6338041

Some content is hidden

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

78 files changed

+2733
-9
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/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+
```
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+
```

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+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public int removeDuplicates(int[] nums) {
3+
int count=0;
4+
if(nums.length==0){
5+
return 0;
6+
}
7+
if(nums.length==0){
8+
return 1;
9+
}
10+
int i=0;
11+
int j=i;//用来遍历数组
12+
int tempCount=0;
13+
while(i<nums.length){
14+
if(j<nums.length&&nums[j]==nums[i]){//固定i,j遍历,满足j位置的值和i相同,则j++;
15+
j++;
16+
tempCount++;
17+
}else{
18+
if(tempCount>=2){//统计每个位置数字出现的次数。
19+
count+=2;
20+
nums[i+1]=nums[i];//保存i的值,前后两个是相同的值,eg 1,1,2,。。
21+
i=i+2;//i来到下一个的下一个位置。
22+
}else{//相同的只有两个
23+
count++;
24+
i++;
25+
}
26+
if(j>=nums.length){
27+
break;
28+
}
29+
nums[i]=nums[j];//
30+
tempCount=0;//重新统计
31+
}
32+
33+
34+
35+
}
36+
37+
38+
39+
40+
41+
return count;
42+
}
43+
}

0 commit comments

Comments
 (0)