Skip to content

Commit 33a51c4

Browse files
author
Longerhaha
authored
Merge pull request gzc426#22 from gzc426/master
请求同步更新乔戈里leetcode每日一题
2 parents d628347 + e7c6761 commit 33a51c4

File tree

51 files changed

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

51 files changed

+1886
-0
lines changed

018429.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
int firstUniqChar(char* s) {
2+
int ascii[256]={0};
3+
int i=0,len;
4+
while(s[i]!='\0')
5+
{
6+
i++;
7+
}
8+
len=i;
9+
for(i=0;i<len;i++)
10+
{
11+
ascii[s[i]]++;
12+
}
13+
i=0;
14+
while(ascii[s[i]]!=1&&i<len) i++;
15+
if(i==len)
16+
return -1;
17+
return i;
18+
}

2018.11.28-leetcode151/018429.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
int top=0;
2+
void push(char c,char* stack)
3+
{
4+
stack[top++]=c;
5+
}
6+
char pop(char* stack)
7+
{
8+
return stack[--top];//写入s1
9+
}
10+
void reverseWords(char *s) {
11+
int i=0,j=0,flag=0;
12+
while(s[i]!='\0') i++;
13+
char* stack = (char*)malloc(i);
14+
char* s1 = (char*)malloc(i+1);
15+
i--;
16+
while(i>=0)
17+
{
18+
if(s[i]==' ')
19+
{
20+
while(top!=0)//如果栈不为空
21+
{
22+
flag=1;
23+
s1[j++]=pop(stack);
24+
}
25+
if(flag==1)
26+
{
27+
s1[j++]=' ';
28+
flag=0;//重置flag
29+
}
30+
i--;
31+
}
32+
else
33+
{
34+
push(s[i--],stack);
35+
}
36+
}
37+
if(s[++i]!=' ')
38+
{
39+
while(top!=0)//如果栈不为空
40+
s1[j++]=pop(stack);
41+
s[j]=' ';
42+
}
43+
else
44+
j--;
45+
s1[j]='\0';
46+
s = strcpy(s,s1);
47+
free(s1);
48+
}

2018.11.29-leetcode443/kiritocly.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
private static int solution(char[] charArray) {
2+
if (charArray.length == 0) {
3+
return 0;
4+
}
5+
//遍历整个字符数组
6+
//压缩后的字符长度
7+
int resultCount = 0;
8+
//当前重复字符计数
9+
int count = 1;
10+
for (int i = 0; i < charArray.length; i++) {
11+
//如果遇到某个字符与当前字符不相等或者已经遍历到最后一个字符
12+
if (i + 1 == charArray.length || charArray[i] != charArray[i + 1]) {
13+
//记录一下当前字符,并且resultCount加1,比如a,a,b,b,b,扫面到第二个a时
14+
//a!=b,如果a的个数大于1,先将a后面所有的a替换为数字,如a,a替换为a,2
15+
charArray[resultCount++] = charArray[i];
16+
if (count > 1) {
17+
String temp = String.valueOf(count);
18+
for (int k = 0; k < temp.length(); k++) {
19+
charArray[resultCount++] = temp.charAt(k);
20+
}
21+
}
22+
//重新统计下一个字符
23+
count = 1;
24+
} else {
25+
//统计重复的字符个数
26+
count++;
27+
}
28+
29+
}
30+
return resultCount;
31+
}

2018.11.29-leetcode443/妮可.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
```java
2+
package sy181203;
3+
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
7+
public class leetcode_443压缩字符串
8+
{
9+
10+
public static void main(String[] args)
11+
{
12+
char[] chars = {'a','b','b','b','c','c','b','b','b','b','b','b','b','b','b','b','b','b'};
13+
System.out.println(Arrays.toString(chars));
14+
15+
System.out.println(compress(chars));
16+
17+
}
18+
19+
public static int compress(char[] chars)
20+
{
21+
if(chars.length==0)
22+
return 0;
23+
int m=0,i=0,j;
24+
while(i<chars.length)//外层循环控制总次数
25+
{
26+
j=i;
27+
chars[m++]=chars[i];
28+
int k=0;//统计重复字母数
29+
while(j<chars.length && chars[i]==chars[j])//内层循环判断是不是同一个字母
30+
{
31+
k++;
32+
j++;
33+
}
34+
StringBuilder sb=new StringBuilder();
35+
if(k!=1)//k=1不用再加数字
36+
{
37+
while(k!=0)//除来减小,到0出循环
38+
{
39+
System.out.println("k%10 "+(k%10));
40+
sb.append((char)(k%10+48));
41+
k/=10;
42+
}
43+
}
44+
System.out.println("sb ==="+sb.toString());
45+
sb.reverse();//
46+
System.out.println("sbre ==="+sb.toString());
47+
for(char c:sb.toString().toCharArray())
48+
49+
{
50+
System.out.println("c="+c);
51+
chars[m++]=c;
52+
}
53+
i=j;//置位
54+
}
55+
System.out.println(Arrays.toString(chars));
56+
return m;
57+
}
58+
59+
}
60+
```

2018.11.30-leetcode890/妮可.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
```java
2+
package sy181203;
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* @author suyuan
9+
*
10+
你有一个单词列表 words 和一个模式 pattern,你想知道 words 中的哪些单词与模式匹配。
11+
12+
如果存在字母的排列 p ,使得将模式中的每个字母 x 替换为 p(x) 之后,
13+
我们就得到了所需的单词,那么单词与模式是匹配的。
14+
15+
(回想一下,字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。)
16+
17+
返回 words 中与给定模式匹配的单词列表。
18+
19+
你可以按任何顺序返回答案。
20+
21+
22+
23+
示例:
24+
25+
输入:words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
26+
输出:["mee","aqq"]
27+
解释:
28+
"mee" 与模式匹配,因为存在排列 {a -> m, b -> e, ...}。
29+
"ccc" 与模式不匹配,因为 {a -> c, b -> c, ...} 不是排列。
30+
因为 a 和 b 映射到同一个字母。
31+
32+
33+
提示:
34+
35+
1 <= words.length <= 50
36+
1 <= pattern.length = words[i].length <= 20
37+
38+
*/
39+
public class leetcode_890查找和替换模式
40+
{
41+
42+
public static void main(String[] args)
43+
{
44+
String[] wordsStrings= {"ddd","abc","deq","mee","aqq","dkd","ccc"};
45+
String pattern = "abb";
46+
System.out.println(findAndReplacePattern(wordsStrings, pattern));
47+
48+
}
49+
50+
public static List<String> findAndReplacePattern(String[] words, String pattern)
51+
{
52+
List<String> list=new ArrayList<String>();
53+
for(int i=0;i<words.length;i++)
54+
{
55+
if(words[i].length()==pattern.length())
56+
{
57+
if(isMatch(words[i], pattern))
58+
list.add(words[i]);
59+
}
60+
}
61+
return list;
62+
}
63+
64+
public static boolean isMatch(String word,String pattern)
65+
{
66+
//不用map,弄个布隆过滤器,双射
67+
//直接映射char对应的int值,就不用存数值了
68+
int [] map=new int[128];
69+
int [] isUse=new int[128];
70+
for(int i=0;i<word.length();i++)
71+
{
72+
int p=pattern.charAt(i);
73+
int w=word.charAt(i);
74+
if(map[p]==0 && isUse[w]==0)//没有匹配过
75+
{
76+
map[p]=w;
77+
isUse[w]=1;
78+
}
79+
else if (map[p]!=w) {
80+
return false;
81+
}
82+
}
83+
return true;
84+
}
85+
86+
}
87+
```

2018.12.04-leetcode101/Despacito.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# leetcode 101 Symmetric Tree
2+
## 1. Description
3+
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
4+
5+
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
6+
7+
1
8+
/ \
9+
2 2
10+
/ \ / \
11+
3 4 4 3
12+
13+
But the following [1,2,2,null,3,null,3] is not:
14+
15+
1
16+
/ \
17+
2 2
18+
\ \
19+
3 3
20+
21+
**Note:**
22+
23+
Bonus points if you could solve it both recursively and iteratively.
24+
25+
## 2. Solution
26+
```python
27+
# Definition for a binary tree node.
28+
# class TreeNode:
29+
# def __init__(self, x):
30+
# self.val = x
31+
# self.left = None
32+
# self.right = None
33+
34+
class Solution:
35+
def isSymmetric(self, root):
36+
"""
37+
:type root: TreeNode
38+
:rtype: bool
39+
"""
40+
if root == None:
41+
return True
42+
return self.comroot(root.left, root.right)
43+
44+
def comroot(self, left, right):
45+
if left == None and right == None:
46+
return True
47+
elif left == None or right == None:
48+
return False
49+
elif left.val != right.val:
50+
return False
51+
return self.comroot(left.left, right.right) and self.comroot(left.right, right.left)
52+
```
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
递归
2+
```java
3+
class Solution {
4+
public boolean isSymmetric(TreeNode root) {
5+
if (root == null){
6+
return true;
7+
}
8+
else {
9+
if (check(root.left, root.right)){
10+
return true;
11+
}
12+
else {
13+
return false;
14+
}
15+
}
16+
}
17+
18+
public static boolean check(TreeNode node1, TreeNode node2){
19+
if (node1 == null && node2 == null){
20+
return true;
21+
}
22+
else if (node1 == null || node2 == null){
23+
return false;
24+
}
25+
return node1.val == node2.val && check(node1.right, node2.left) && check(node1.left, node2.right);
26+
}
27+
}
28+
```
29+
非递归
30+
```java
31+
import java.util.*;
32+
class Solution {
33+
public boolean isSymmetric(TreeNode root) {
34+
if ((root == null) || (root.left == null && root.right == null)){
35+
return true;
36+
}
37+
else if (root.left == null || root.right == null){
38+
return false;
39+
}
40+
else {
41+
ArrayDeque<TreeNode> d = new ArrayDeque<TreeNode>();
42+
d.addFirst(root.left);
43+
d.addLast(root.right);
44+
while (d.size() != 0){
45+
TreeNode leftnode = d.pollFirst();
46+
TreeNode rightnode = d.pollLast();
47+
if (leftnode == null && rightnode == null){
48+
return true;
49+
}
50+
else if (leftnode == null || rightnode == null){
51+
return false;
52+
}
53+
else if (leftnode.val != rightnode.val){
54+
return false;
55+
}
56+
else {
57+
if (rightnode.right != null && leftnode.left != null){
58+
d.addLast(rightnode.right);
59+
d.addFirst(leftnode.left);
60+
}
61+
else if (rightnode.right == null && leftnode.left == null){
62+
63+
}
64+
else {
65+
return false;
66+
}
67+
if (rightnode.left != null && leftnode.right != null){
68+
d.addFirst(rightnode.left);
69+
d.addLast(leftnode.right);
70+
}
71+
else if (rightnode.left == null && leftnode.right == null){
72+
73+
}
74+
else {
75+
return false;
76+
}
77+
}
78+
}
79+
return true;
80+
}
81+
}
82+
}
83+
```

0 commit comments

Comments
 (0)