Skip to content

Commit 69aee1d

Browse files
author
Longerhaha
authored
Merge pull request gzc426#24 from gzc426/master
请求同步更新乔戈里leetcode每日一题
2 parents 8239cb0 + 1a09a80 commit 69aee1d

Some content is hidden

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

60 files changed

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

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/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+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
101. 对称二叉树
2+
3+
给定一个二叉树,检查它是否是镜像对称的。
4+
5+
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
6+
7+
1
8+
/ \
9+
2 2
10+
/ \ / \
11+
3 4 4 3
12+
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
13+
14+
1
15+
/ \
16+
2 2
17+
\ \
18+
3 3
19+
20+
```
21+
/**
22+
* Definition for a binary tree node.
23+
* public class TreeNode {
24+
* int val;
25+
* TreeNode left;
26+
* TreeNode right;
27+
* TreeNode(int x) { val = x; }
28+
* }
29+
*/
30+
class Solution {
31+
public boolean isSymmetric(TreeNode root) {
32+
if(root == null) return true;
33+
return symmetricCheck(root.left,root.right);
34+
}
35+
36+
public boolean symmetricCheck(TreeNode node1,TreeNode node2){
37+
if(node1 == null && node2 == null) return true;
38+
if(node1 == null || node2 == null) return false;
39+
return (node1.val != node2.val) ? false : symmetricCheck(node1.left,node2.right) && symmetricCheck(node1.right,node2.left);
40+
41+
}
42+
}
43+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
```
2+
/**
3+
* Definition for a binary tree node.
4+
* public class TreeNode {
5+
* int val;
6+
* TreeNode left;
7+
* TreeNode right;
8+
* TreeNode(int x) { val = x; }
9+
* }
10+
*/
11+
class Solution {//递归方式
12+
public boolean isSymmetric(TreeNode root) {
13+
if(root==null){
14+
return true;
15+
16+
}
17+
return isSymmetric(root.left,root.right);
18+
19+
}
20+
public boolean isSymmetric(TreeNode left,TreeNode right){
21+
if(left==null&&right==null){//无左右子树,则节点本身是对称的
22+
return true;
23+
}else if(left==null||right==null){//只含有一个左子树或者右子树,不对称
24+
return false;
25+
}
26+
if(left.val!=right.val){
27+
return false;
28+
}
29+
30+
31+
return isSymmetric(left.right,right.left)&&isSymmetric(left.left,right.right);//左子树的右子树和右子树的左子树是否对称。
32+
}
33+
}
34+
```

0 commit comments

Comments
 (0)