Skip to content

Commit 35995df

Browse files
authored
Merge pull request gzc426#11 from gzc426/master
.
2 parents a7cc00d + 7fb0cf7 commit 35995df

File tree

95 files changed

+4067
-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.

95 files changed

+4067
-0
lines changed

2018.11.25-leetcode80/大魔王爱穿孖烟筒.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
```
12
class Solution {
23
public int removeDuplicates(int[] nums) {
34
int count=0;
@@ -41,3 +42,4 @@ class Solution {
4142
return count;
4243
}
4344
}
45+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
```
2+
class Solution {
3+
public int maxArea(int[] height) {
4+
int left=0;//从左边遍历
5+
int right=height.length-1;//从右边遍历
6+
int tempCha=0;
7+
int sum=0;
8+
int tempsum=0;
9+
while(left<right){//遍历到中间,两指针重合为止,求出沿途的面积。
10+
tempCha=right-left;//每次遍历记录临时长度差,也就是底边的长度
11+
if(height[right]>height[left]){
12+
tempsum=height[left]*tempCha; //短(短板)的边作为高,求出此时的面积
13+
left++;
14+
}else{
15+
tempsum=height[right]*tempCha;
16+
right--;
17+
}
18+
if(tempsum>sum){
19+
sum=tempsum;
20+
}
21+
22+
23+
}
24+
return sum;
25+
}
26+
}
27+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
```
2+
class Solution {
3+
public boolean isPalindrome(String s) {
4+
s = s.toLowerCase();
5+
char [] charArray = s.toCharArray();
6+
7+
int begin = 0;
8+
int end = charArray.length - 1;
9+
while(begin < end)
10+
11+
{
12+
if (!isChar(charArray[begin])){//不是数字和小字母
13+
begin++;
14+
continue;
15+
}
16+
if (!isChar(charArray[end])){//不是数字和小字母
17+
end--;
18+
continue;
19+
}
20+
if(charArray[begin] == charArray[end])
21+
{
22+
begin++;
23+
end--;
24+
}else{
25+
return false;
26+
}
27+
}
28+
return true;
29+
}
30+
public boolean isChar(char c){//判断是否为数字或者字母
31+
if (c<48||(c>57&&c<65)||(c>90&&c<97)||c>122){
32+
return false;
33+
}
34+
return true;
35+
}
36+
}
37+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
```
2+
public class Solution {
3+
public String reverseWords(String s) {
4+
if (s == null || s.length() == 0) {
5+
return "";
6+
}
7+
String[] array = s.split(" ");//按照空格划分字符串
8+
String str = "";
9+
for (int i = array.length - 1; i >= 0; i--) {
10+
if (!array[i].equals("")) {
11+
if (str.length() > 0) {
12+
str += " ";
13+
}
14+
str += array[i];//如果不是空字符串,就把截断的旧字符串添加到新字符串中。
15+
}
16+
}
17+
return str;
18+
}
19+
}
20+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
```
2+
class Solution {
3+
public int compress(char[] chars) {//思路有一点,写的时候就缺胳膊少腿,又来模仿群主代码
4+
int count = 1;
5+
int index = 0;
6+
for (int i = 0; i < chars.length; i++) {
7+
if (i + 1 == chars.length || chars[i] != chars[i+1]) {
8+
chars[index++] = chars[i];//前后不同直接添加
9+
if (count > 1) {
10+
String temp = String.valueOf(count);//转换成字符串,也可以变成字符数组,然后遍历。
11+
for(int k=0;k<temp.length();k++)
12+
chars[index++] = temp.charAt(k);
13+
}
14+
count = 1;//重置
15+
}
16+
else {
17+
count++;//统计重复字符的个数
18+
}
19+
}
20+
return index;
21+
}
22+
}
23+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
```
2+
class Solution {
3+
public List<String> findAndReplacePattern(String[] words, String pattern) {
4+
List<String> list=new ArrayList<>();
5+
char [] pat=pattern.toCharArray();
6+
for(int i=0;i<words.length;i++){
7+
8+
char [] sArray=words[i].toCharArray();
9+
boolean flag=true;
10+
if(sArray.length==pat.length) {
11+
HashMap<Character,Character> hash=new HashMap<>();
12+
HashMap<Character,Character> hash2=new HashMap<>();
13+
for(int j=0;j<pat.length;j++){
14+
15+
if(hash.containsKey(pat[j])==false){//一个模式字符不存在的话,添加key和value
16+
hash.put(pat[j],sArray[j]);
17+
18+
if(hash2.containsKey(sArray[j])==false){//考虑存在key和value重复的情况,使用另外一个map反向添加key和value。
19+
hash2.put(sArray[j],pat[j]);
20+
}else{//如果已经存在了
21+
if(hash2.get(sArray[j])!= pat[j]){
22+
flag=false;
23+
break;
24+
}
25+
}
26+
}else{//已经存在相应的key
27+
if(hash.get(pat[j])!= sArray[j]){
28+
flag=false;
29+
break;
30+
}
31+
}
32+
}
33+
34+
}
35+
36+
if(flag){//如果模式对应,添加到list集合中
37+
list.add(words[i]);
38+
}
39+
40+
}
41+
42+
return list;
43+
}
44+
}
45+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
4+
vector<string> ret;
5+
6+
int size = words.size();
7+
int n = pattern.length();
8+
9+
for(int i=0; i<size; i++)
10+
{
11+
map<char, char> m;
12+
map<char, char> m1;
13+
int j = 0;
14+
for(j=0; j<n; j++)
15+
{
16+
if(m.find(pattern[j])==m.end())
17+
{
18+
m[pattern[j]] = words[i][j];
19+
}
20+
21+
if(m1.find(words[i][j])==m1.end())
22+
{
23+
m1[words[i][j]] = pattern[j];
24+
}
25+
26+
if(m.find(pattern[j])!=m.end() && m[pattern[j]] != words[i][j])
27+
break;
28+
29+
if(m1.find(words[i][j])!=m.end() && m1[words[i][j]]!=pattern[j])
30+
break;
31+
32+
}
33+
34+
if(j == n)
35+
ret.push_back(words[i]);
36+
}
37+
38+
return ret;
39+
}
40+
};

2018.12.04-leetcode101/Avalon.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public boolean isSymmetric(TreeNode root) {
12+
if (root == null ){
13+
return true;
14+
}
15+
16+
return isSymmetric(root.left, root.right);
17+
}
18+
19+
public static boolean isSymmetric(TreeNode nodeA, TreeNode node_A){
20+
if (node_A == null && nodeA == null) {
21+
return true;
22+
}
23+
if (node_A != null && nodeA != null) {
24+
return node_A.val == nodeA.val && isSymmetric(nodeA.left, node_A.right) && isSymmetric(nodeA.right, node_A.left);
25+
}
26+
return false;
27+
}
28+
}

2018.12.04-leetcode101/MQQM.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
题目:
3+
给定一个二叉树,检查它是否是镜像对称的。
4+
5+
做法:
6+
给定对应位置上的两个节点,递归判断这两个节点是否一致。
7+
8+
参考:
9+
https://www.cnblogs.com/xiaozhuyang/p/7376749.html
10+
*/
11+
12+
/**
13+
* Definition for a binary tree node.
14+
* struct TreeNode {
15+
* int val;
16+
* TreeNode *left;
17+
* TreeNode *right;
18+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
19+
* };
20+
*/
21+
class Solution {
22+
public:
23+
bool isSymmetric(TreeNode* root) {
24+
if( root == NULL ){
25+
return true;
26+
}
27+
28+
return isEqual(root->left, root->right);
29+
}
30+
bool isEqual(TreeNode* left, TreeNode* right){//给定的两个节点已经是对应位置上的节点
31+
if( left == NULL && right == NULL ){//两个节点都是空
32+
return true;
33+
}
34+
if( left == NULL || right == NULL ){//有一个节点是空
35+
return false;
36+
}
37+
if( left->val != right->val ){//两个节点都存在,但是不相同
38+
return false;
39+
}
40+
41+
return isEqual(left->left, right->right) && isEqual(left->right, right->left);
42+
}
43+
};

2018.12.04-leetcode101/marguerite.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
101_(对称二叉树)Symmetric Tree
2+
给定一个二叉树,检查它是否是镜像对称的。
3+
4+
输入:
5+
TreeNode* root:二叉树的根节点指针
6+
输出:
7+
bool:是否是对称二叉树
8+
9+
输入: 给定二叉树: [1,2,2,3,4,4,3] ,
10+
1
11+
/ \
12+
2 2
13+
/ \ / \
14+
3 4 4 3
15+
输出: true
16+
17+
solution1
18+
19+
判断是否是镜像对称,重要的就是判断对应位置的2个结点的值是否相等。用双端队列来实现
20+
21+
public boolean isSymmetric(TreeNode root){
22+
if(root == null){
23+
return true;
24+
}
25+
//队列做遍历
26+
Deque<TreeNode> deque = new LinkedList<TreeNode>();
27+
//根节点不用判断,直接加入左右孩子结点
28+
deque.addFirst(root.left);
29+
deque.addLast(root.right);
30+
31+
TreeNode preNode = null;
32+
TreeNode postNode = null;
33+
34+
while(!deque.isEmpty()){
35+
//取出列队两个元素
36+
preNode = deque.pollFirst();
37+
postNode = deque.pollLast();
38+
//如果当前结点是空结点,结束本次循环,即不需要检查后续结点
39+
if(preNode == null && postNode == null){
40+
continue;
41+
}
42+
//如果不对称返回false,
43+
if(preNode == null || postNode == null){
44+
return false;
45+
}
46+
if(preNode.val != postNode.val){
47+
return false;
48+
} else{
49+
deque.addFirst(preNode.right);//preNode右孩子入队
50+
deque.addFirst(preNode.left);//preNode左孩子入队
51+
52+
deque.addLast(postNode.left);//postNode左孩子入队
53+
deque.addLast(postNode.right);//postNode右孩子入队
54+
}
55+
}
56+
return true;//都是对称的返回真
57+
}
58+
59+
60+
solution2
61+
public boolean isSysmmetric2(TreeNode root){
62+
if(root == null){
63+
return true;
64+
}
65+
return checkNodes(root.left,root.right);
66+
}
67+
68+
public boolean checkNodes(TreeNode node1,TreeNode node2){
69+
if(node1 == null && node2 == null){
70+
return true;
71+
}
72+
if(node1 == null || node2 == null){
73+
return false;
74+
}
75+
if(node1.val != node2.val){
76+
return false;
77+
} else{
78+
return checkNodes(node1.left,node2.right) && checkNodes(node1.right,node2.left);
79+
}
80+
}
81+
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+

0 commit comments

Comments
 (0)