Skip to content

Commit 7a5ebc8

Browse files
Merge pull request gzc426#10 from gzc426/master
1
2 parents c6b7596 + 4eaff4d commit 7a5ebc8

File tree

115 files changed

+3227
-0
lines changed

Some content is hidden

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

115 files changed

+3227
-0
lines changed

2018.11.18/大腩肚.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public List<List<Integer>> threeSum(int[] nums) {
2+
final int length = nums.length;
3+
List<List<Integer>> result = new ArrayList<>();
4+
HashMap<Integer, int[]> hashMap = new HashMap<Integer, int[]>();
5+
6+
if (length < 3) return result;
7+
8+
Arrays.sort(nums);
9+
10+
for (int i = 0; i < length - 2; i++) {
11+
hashMap.clear();
12+
13+
if (i == 0 || nums[i] > nums[i - 1]) {
14+
for (int j = i + 1; j < length; j++) {
15+
16+
if (hashMap.containsKey(nums[j])) {
17+
ArrayList<Integer> elem = new ArrayList<Integer>(3);
18+
19+
elem.add(hashMap.get(nums[j])[0]);
20+
elem.add(hashMap.get(nums[j])[1]);
21+
elem.add(nums[j]);
22+
23+
result.add(elem);
24+
25+
while (j < (length - 1) && nums[j] == nums[j + 1]) j++;
26+
} else {
27+
int[] temp = new int[2];
28+
temp[0] = nums[i];
29+
temp[1] = nums[j];
30+
hashMap.put(0 - (nums[i] + nums[j]), temp);
31+
}
32+
}
33+
}
34+
}
35+
return result;
36+
}

2018.11.24-leetcode28/雪辰.markdown

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def strStr(self, haystack, needle):
3+
"""
4+
:type haystack: str
5+
:type needle: str
6+
:rtype: int
7+
"""
8+
lenNeedle=len(needle)
9+
if not needle.strip():
10+
return (0)
11+
if len(needle)>len(haystack):
12+
return(-1)
13+
match=0
14+
for a in range(len(haystack)-lenNeedle+1):
15+
m = a
16+
match = 0
17+
for i in range(lenNeedle):
18+
if haystack[m]==needle[i]:
19+
match+=1
20+
m+=1
21+
if match == lenNeedle:
22+
break
23+
if match == lenNeedle:
24+
break
25+
if match == lenNeedle:
26+
return(a)
27+
else:
28+
return(-1)
29+

2018.11.26-leetcode11/zjukk.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
```
2+
#include <iostream>
3+
#include <vector>
4+
using namespace std;
5+
6+
//mysol:
7+
int removeDuplicates(vector<int>& nums) {
8+
for (int i = 1; i < nums.size(); i++) {
9+
if (nums[i] == nums[i-1])
10+
nums.erase(nums.begin() + i);
11+
}
12+
return nums.size();
13+
}
14+
//better sol:
15+
int removeDuplicates(vector<int>& nums) {
16+
int i = 0;
17+
for (int j = 1; j < nums.size(); j++) {
18+
if (nums[i] != nums[j]) {
19+
i++;
20+
nums[i] = nums[j];
21+
}
22+
}
23+
return i + 1;
24+
}
25+
```

2018.11.26-leetcode11/。。。.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public int maxArea(int[] height) {
2+
int left=0,right=height.length-1,area = 0;
3+
while (left < right){
4+
area = Math.max(area,(right-left)*Math.min(height[left],height[right]));
5+
if (height[left] < height[right]){
6+
left++;
7+
}else {
8+
right--;
9+
}
10+
}
11+
return area;
12+
}

2018.11.26-leetcode11/暮成雪.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
if( height.length < 2 ){
4+
return 0;
5+
}
6+
//双指针,复杂度O(n),5ms 91%
7+
int max=0;
8+
int i=0;
9+
int j= height.length-1;
10+
while(i < j){
11+
int areaTmp= (j-i) * Math.min(height[i] , height[j]);
12+
Boolean flag = height[i] > height[j] ? true : false;
13+
if(max < areaTmp){
14+
max = areaTmp;
15+
}
16+
if(flag == true){
17+
j--;
18+
}else{
19+
i++;
20+
}
21+
}
22+
}

2018.11.27-leetcode125/zjukk.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
```
2+
#include <iostream>
3+
#include <string>
4+
5+
using namespace std;
6+
7+
class Solution {
8+
public:
9+
bool isPalindrome(string s) {
10+
int l = 0, r = s.size() - 1;
11+
bool flag = false;
12+
while (l <= r) {
13+
if (!isAlphaNum(s[l])) {
14+
++l; continue;
15+
}
16+
if (!isAlphaNum(s[r])) {
17+
--r; continue;
18+
}
19+
if ((s[r] + 32 - 'a') % 32 != (s[l] + 32 - 'a') % 32) {flag = true; break;}
20+
++l; --r;
21+
}
22+
return !flag;
23+
}
24+
bool isAlphaNum(char c) {
25+
if (c >= 'a' && c <= 'z') return true;
26+
if (c >= 'A' && c <= 'Z') return true;
27+
if (c >= '0' && c <= '9') return true;
28+
return false;
29+
}
30+
};
31+
32+
int main() {
33+
Solution s;
34+
cout << s.isPalindrome("race a car");
35+
//A man, a plan, a canal: Panama
36+
}
37+
```

2018.11.27-leetcode125/。。。.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
```
2+
public boolean isPalindrome(String s){
3+
if (s.length() >= 2){
4+
int left = 0,right = s.length()-1;
5+
char leftChar,rightChar;
6+
while (left <= right){
7+
leftChar = s.charAt(left);rightChar = s.charAt(right);
8+
if (leftChar>='A'&&leftChar<='Z'){//转换大写字母为小写
9+
leftChar+=32;
10+
}else if (rightChar>='A'&&rightChar<='Z'){
11+
rightChar+=32;
12+
}
13+
if (leftChar == rightChar){
14+
left++;right--;
15+
continue;
16+
}
17+
//跳过非字母非数字的字符
18+
if (!((leftChar>='0'&& leftChar<='9') || (leftChar>='a'&&leftChar<='z') || (leftChar>='A'&&leftChar<='Z'))){
19+
left++;
20+
continue;
21+
}else if (!((rightChar>='0'&& rightChar<='9') || (rightChar>='a'&&rightChar<='z') || (rightChar>='A'&&rightChar<='Z'))){
22+
right--;
23+
continue;
24+
}
25+
if (leftChar != rightChar)return false;
26+
}
27+
}
28+
return true;
29+
}
30+
```

2018.11.27-leetcode125/暮成雪.md

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

2018.11.28-leetcode151/ieei.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
public class Solution {
2+
public String reverseWords(String s) {
3+
int length=s.length();
4+
//当字符串为空
5+
if(s == null){ return null;}
6+
//当字符串为" "时 直接返回null
7+
if(s.trim().equals("")){
8+
return s.trim();
9+
}
10+
/**
11+
* 先将字符串按照空格分裂为字符串数组
12+
* 然后收尾两个指针来交换即可
13+
*/
14+
//0.掐头去尾
15+
String allRevStr=s.trim();
16+
//1.将字符串按照空格分裂为字符串数组
17+
String[] strChar=allRevStr.split(" ");
18+
19+
//2.使用首尾两个指针来交换
20+
int left=0;
21+
int right=strChar.length-1;
22+
String temStr=null;
23+
while(left<right){
24+
//交换
25+
temStr=strChar[left];
26+
strChar[left]=strChar[right];
27+
strChar[right]=temStr;
28+
//移动指针
29+
left++;
30+
right--;
31+
}
32+
//3.拼接好新的字符串
33+
String reverse="";
34+
for (int i=0; i<strChar.length; ++i) {
35+
//对每一个单词掐头去尾
36+
String temp=strChar[i].trim();
37+
38+
if (temp.equals("")) {
39+
continue;
40+
}
41+
42+
if (i==strChar.length-1) {
43+
reverse=reverse+temp;
44+
}else{
45+
reverse=reverse+temp+" ";
46+
}
47+
}
48+
return reverse;
49+
}
50+
}

2018.11.28-leetcode151/zjukk.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
```
2+
#include <iostream>
3+
#include <string>
4+
#include <sstream>
5+
#include <stack>
6+
using namespace std;
7+
8+
//using stack space O(n)
9+
class Solution {
10+
public:
11+
void reverseWords(string &s) {
12+
if (s.empty()) return;
13+
stack<string> st;
14+
for (int i = 0; i < s.size(); ++i) {
15+
string sub;
16+
while (s[i] != ' ' && i < s.size()) {
17+
sub.push_back(s[i++]);
18+
}
19+
if (!sub.empty()) st.push(sub);//空字符串因为有‘\0’的存在也会被当做一个元素放到stack中
20+
}
21+
string res;
22+
while (!st.empty()) {
23+
res += st.top();
24+
res += " ";
25+
st.pop();
26+
}
27+
if (!res.empty()) res.pop_back();
28+
s = res;
29+
}
30+
};
31+
32+
class Solution2 {
33+
public:
34+
void reverseWords(string &s) {
35+
istringstream is(s);
36+
is >> s;
37+
cout << s.size();
38+
string tmp;
39+
while (is >> tmp) s = tmp + " " + s;
40+
// if (!s.empty() && s[0] == ' ') s = "";
41+
}
42+
};
43+
int main() {
44+
Solution3 s;
45+
// string str{"The sky is blue"};
46+
// string str{" "};
47+
string str{" The sky is blue"};
48+
// string str{"The sky is blue "};
49+
// string str{"The sky is blue"};
50+
s.reverseWords(str);
51+
cout << str << "*";
52+
}
53+
```

0 commit comments

Comments
 (0)