Skip to content

Commit 81321b1

Browse files
authored
Merge pull request gzc426#7 from gzc426/master
1
2 parents d2e2413 + ec565a2 commit 81321b1

Some content is hidden

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

50 files changed

+1883
-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.
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+
```

2018.11.25-leetcode80/tonythecyclist.md renamed to 2018.11.25-leetcode80/Tony the Cyclist.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
```
2+
23
class Solution {
34
public int removeDuplicates(int[] nums) {
45
if (nums.length < 3){

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
```
2+
class Solution {
3+
public:
4+
int max(int a, int b){return a>b?a:b;}
5+
int min(int a, int b){return a<b?a:b;}
6+
int maxArea(vector<int>& height) {
7+
int ans = 0;
8+
int l = 0, r = height.size() - 1;
9+
ans = max(ans,min(height[l],height[r])*(r-l));
10+
while(l<r){
11+
if(height[l]>height[r]){
12+
r--;
13+
ans = max(ans,min(height[l],height[r])*(r-l));
14+
}else{
15+
l++;
16+
ans = max(ans,min(height[l],height[r])*(r-l));
17+
}
18+
}
19+
return ans;
20+
}
21+
};
22+
```
File renamed without changes.
File renamed without changes.

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

0 commit comments

Comments
 (0)