Skip to content

Commit b2300cd

Browse files
authored
Merge pull request gzc426#8 from gzc426/master
merge
2 parents aed3792 + 6049105 commit b2300cd

Some content is hidden

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

46 files changed

+1109
-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.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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#leetcode 443
2+
、、、
3+
class Solution {
4+
public int compress(char[] chars) {
5+
int count =1;
6+
int index= 0;
7+
for(int i=0;i<chars.length;i++){
8+
9+
//判断是否最后一个 或者 判断是否重复的最后一个
10+
if(i+1 == chars.length || chars[i]!=chars[i+1]){
11+
12+
13+
14+
//将字母赋值到结果字符数组中
15+
chars[index++]=chars[i];
16+
17+
//判断是否需要将数量赋值到数组中
18+
if(count>1){
19+
//int转为String
20+
String temp =String.valueOf(count);
21+
22+
//将数量赋值到结果数组中
23+
for(int z=0;z<temp.length();z++){
24+
//获取字符串中某个下标的字符
25+
chars[index++] =temp.charAt(z);
26+
}
27+
//初始化数量
28+
count=1;
29+
}
30+
}else{ //字符重复中
31+
count++;
32+
}
33+
}
34+
return index;
35+
}
36+
}
37+
、、、

2018.11.30-leetcode890/Avalon.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public List<String> findAndReplacePattern(String[] words, String pattern) {
3+
List<String> wlist = new ArrayList<>();
4+
int len = words.length;
5+
String template = getTemplate(pattern);
6+
for (int i=0;i<len;i++){
7+
if (words[i].length()==pattern.length()){
8+
String temp = getTemplate(words[i]);
9+
if (temp.equals(template))
10+
wlist.add(words[i]);
11+
}
12+
}
13+
return wlist;
14+
}
15+
16+
private static String getTemplate(String pattern){
17+
char[] cs = pattern.toCharArray();
18+
List<Character> cl = new ArrayList<>();
19+
String result="";
20+
int len = cs.length;
21+
for (int i=0;i<len;i++){
22+
if (cl.contains(cs[i]))
23+
result+=String.valueOf(cl.indexOf(cs[i])+1);
24+
else{
25+
cl.add(cs[i]);
26+
result += String.valueOf(cl.size());
27+
}
28+
}
29+
return result;
30+
}
31+
}

2018.11.30-leetcode890/Saul.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
```java
2+
public class Solution {
3+
public static List<String> findAndReplacePattern(String[] words, String pattern) {
4+
List<String> result = new ArrayList<>();
5+
boolean flag = false;
6+
for (String word : words) {
7+
if (word.length() != pattern.length()) {
8+
continue;
9+
}
10+
flag = false;
11+
char[] wordChars = word.toCharArray();
12+
char[] patternChars = pattern.toCharArray();
13+
//比较wordChars第i个位置与第j个位置的值,如果相同,那么patternChars第i个位置与第j个位置的值也要相同
14+
//如果wordChars第i个位置与第j个位置的值不同,那么patternChars第i个位置与第j个位置的值也要不同
15+
for (int i = 0; i < wordChars.length ; i++) {
16+
for (int j = i + 1; j < wordChars.length; j++) {
17+
if (wordChars[i] == wordChars[j]) {
18+
if (patternChars[i] != patternChars[j]) {
19+
flag = true;
20+
break;
21+
}
22+
}
23+
24+
if (wordChars[i] != wordChars[j]) {
25+
if (patternChars[i] == patternChars[j]) {
26+
flag = true;
27+
break;
28+
}
29+
}
30+
}
31+
}
32+
33+
if (!flag) {
34+
result.add(word);
35+
}
36+
}
37+
return result;
38+
}
39+
}
40+
41+
```

2018.11.30-leetcode890/kiritocly.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
private static List<String> solution(String[] words, String pattern) {
2+
//定义一个list存放结果字符串
3+
List<String> result = new ArrayList<>();
4+
char[] patternArray = pattern.toCharArray();
5+
//遍历字符串
6+
for (int i = 0; i < words.length; i++) {
7+
//匹配字符串
8+
char[] wordArray = words[i].toCharArray();
9+
//是否匹配
10+
boolean flag = true;
11+
//长度相同才能匹配
12+
if (wordArray.length == patternArray.length) {
13+
//定义两个hashMap存放映射:words中的单词与pattern
14+
Map<Character, Character> hashMap1 = new HashMap<>();
15+
Map<Character, Character> hashMap2 = new HashMap<>();
16+
//逐个字符匹配
17+
for (int j = 0; j < patternArray.length; j++) {
18+
if (!hashMap1.containsKey(patternArray[j])) {
19+
//如果不存在字符则向hashMap1存放映射关系对
20+
hashMap1.put(patternArray[j], wordArray[j]);
21+
//判断hashMap2中是否存在反向映射关系对
22+
if (!hashMap2.containsKey(wordArray[j])) {
23+
hashMap2.put(wordArray[j], patternArray[j]);
24+
} else {
25+
if (hashMap2.get(wordArray[j]) != patternArray[j]) {
26+
//存在反向字符且当前判断的字符不相同
27+
flag = false;
28+
break;
29+
}
30+
31+
}
32+
} else {
33+
if (hashMap1.get(patternArray[j]) != wordArray[j]) {
34+
flag = false;
35+
break;
36+
}
37+
}
38+
}
39+
}
40+
if (flag) {
41+
result.add(words[i]);
42+
}
43+
}
44+
return result;
45+
}

2018.11.30-leetcode890/家

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
```
2+
func findAndReplacePattern(words []string, pattern string) []string {
3+
l := len(pattern)
4+
a := map[string]int{}
5+
arr := []int{}
6+
for i:=0;i<=l-1;i++{
7+
if _,ok := a[string(pattern[i])];!ok {
8+
a[string(pattern[i])] = len(a)
9+
}
10+
arr = append(arr,a[string(pattern[i])])
11+
}
12+
newstr := []string{}
13+
for _,v := range words {
14+
a = map[string]int{}
15+
flag := true
16+
for i:=0;i<len(v);i++{
17+
if _,ok := a[string(v[i])];!ok {
18+
a[string(v[i])] = len(a)
19+
}
20+
if a[string(v[i])] != arr[i]{
21+
flag = false
22+
break;
23+
}
24+
}
25+
if flag {newstr = append(newstr,v)}
26+
27+
}
28+
return newstr
29+
}
30+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#leetcode 890
2+
```
3+
//思路是保证对应下标的权重是一样的,只需要比较权重就可以判断是否符合规则
4+
class Solution {
5+
public static List<String> findAndReplacePattern(String[] words, String pattern) {
6+
List<String> result = new ArrayList<>();//返回结果
7+
char [] patternArray = pattern.toCharArray();//字符串转字符数组
8+
for(int i=0;i<words.length;i++)//遍历每一个words中的字符串
9+
{
10+
char [] wordArray = words[i].toCharArray();
11+
boolean flag = true;//标记,为true说明匹配
12+
if(wordArray.length == patternArray.length)//匹配肯定长度要相等
13+
{
14+
HashMap<Character,Character> judge = new HashMap<>();//hashmap1
15+
HashMap<Character,Character> judge2 = new HashMap<>();//反向比较hashmap2
16+
for(int j=0;j<pattern.length();j++)//遍历模式串中的每一个字符
17+
{
18+
if(judge.containsKey(patternArray[j]) == false)
19+
{//判断key中是否存在这个字符,不存在hashmap1就添加key-value
20+
judge.put(patternArray[j],wordArray[j]);
21+
if(judge2.containsKey(wordArray[j]) == false)
22+
{//这个if else的作用就是思路中的hashmap2的作用了
23+
judge2.put(wordArray[j],patternArray[j]);//不存在就添加
24+
}else {
25+
if(judge2.get(wordArray[j]) != patternArray[j]){
26+
flag = false;//存在的话就去判断是不是相等的,不相等就不匹配
27+
break;
28+
}
29+
}
30+
}else {//存在的话 去hashmap中找这个字符与现有的字符是否相等
31+
//不相等,说明存在了(a-c a-b 一个a对应两个字符)直接返回false,不匹配
32+
if(judge.get(patternArray[j]) != wordArray[j])
33+
{
34+
flag = false;
35+
break;
36+
}
37+
}
38+
}
39+
if(flag)
40+
{
41+
result.add(words[i]);
42+
}
43+
}
44+
45+
}
46+
return result;
47+
}
48+
}
49+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
给定一个二叉树,检查它是否是镜像对称的。
2+
3+
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
4+
5+
1
6+
/ \
7+
2 2
8+
/ \ / \
9+
3 4 4 3
10+
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
11+
12+
1
13+
/ \
14+
2 2
15+
\ \
16+
3 3
17+
说明:
18+
19+
如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
2+
3+
例如:
4+
给定二叉树: [3,9,20,null,null,15,7],
5+
6+
3
7+
/ \
8+
9 20
9+
/ \
10+
15 7
11+
返回其层次遍历结果:
12+
13+
[
14+
[3],
15+
[9,20],
16+
[15,7]
17+
]

0 commit comments

Comments
 (0)