Skip to content

Commit 81a1522

Browse files
committed
Initial commit
0 parents  commit 81a1522

8 files changed

+277
-0
lines changed

.idea/kotlinc.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

leetcode.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package easy;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* Created by Huangsky on 2018/5/25.
9+
*/
10+
public class NO438_FindAllAnagramsInAString {
11+
public static void main(String[] args){
12+
String s=new String("cbaebabacd");
13+
String p=new String("abc");
14+
List<Integer> list=findAnagrams(s,p);
15+
System.out.println(list);
16+
return ;
17+
}
18+
public static List<Integer> findAnagrams(String s, String p) {
19+
List<Integer> list=new ArrayList<Integer>();
20+
int len=p.length();
21+
for(int i=0;i<=s.length()-len;i++){
22+
String str=s.substring(i,i+len);
23+
if(compareStr(str,p))
24+
list.add(i);
25+
}
26+
return list;
27+
}
28+
public static boolean compareStr(String a,String b){
29+
char[] strA=a.toCharArray();
30+
char[] strB=b.toCharArray();
31+
Arrays.sort(strA);
32+
Arrays.sort(strB);
33+
int i=0;
34+
for(;i< strA.length;i++){
35+
if (strA[i]!=strB[i])
36+
break;
37+
}
38+
return i==strA.length;
39+
40+
}
41+
}
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package easy;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import java.util.Queue;
6+
7+
/**
8+
* Created by LXM on 2018/4/30.
9+
*/
10+
11+
/**
12+
* Description:
13+
* Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string.
14+
* Return a list of all possible strings we could create.
15+
*
16+
* Example:
17+
*
18+
Input: S = "a1b2"
19+
Output: ["a1b2", "a1B2", "A1b2", "A1B2"]
20+
21+
Input: S = "3z4"
22+
Output: ["3z4", "3Z4"]
23+
24+
Input: S = "12345"
25+
Output: ["12345"]
26+
*
27+
*
28+
* Note:
29+
30+
S will be a string with length at most 12.
31+
S will consist only of letters or digits.
32+
*/
33+
public class NO784_LetterCasePermutation {
34+
35+
public static void main(String[] args){
36+
String s=new String("a1b2");
37+
System.out.println("Result:::"+letterCasePermutation(s));
38+
}
39+
40+
/**
41+
* BFS-广度优先算法
42+
*/
43+
public static List<String> letterCasePermutation(String S) {
44+
Queue<String> queue=new LinkedList<>();
45+
queue.add(S);
46+
for(int i=0;i<S.length();i++){
47+
int size=queue.size();
48+
if(Character.isDigit(S.charAt(i)))
49+
continue;
50+
for(int j=0;j<size;j++){
51+
String str=queue.poll();
52+
char[] arrCh=str.toCharArray();
53+
54+
arrCh[i]=Character.toUpperCase(arrCh[i]);
55+
queue.add(new String(arrCh));
56+
57+
arrCh[i]=Character.toLowerCase(arrCh[i]);
58+
queue.add(new String(arrCh));
59+
}
60+
61+
}
62+
return new LinkedList<>(queue);
63+
}
64+
65+
}

src/easy/NO788_RotatedDigits.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package easy;
2+
3+
/**
4+
* Created by Hsky on 2018/4/30.
5+
*/
6+
public class NO788_RotatedDigits {
7+
/**
8+
* Description:
9+
* X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X.
10+
* Each digit must be rotated - we cannot choose to leave it alone.
11+
* A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other;
12+
* 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number and become invalid.
13+
* Now given a positive number N, how many numbers X from 1 to N are good?
14+
*
15+
* Example:
16+
*
17+
Input: 10
18+
Output: 4
19+
Explanation:
20+
There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
21+
Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
22+
23+
24+
* Note:
25+
N will be in range [1, 10000].
26+
*/
27+
28+
public static void main(String[] args){
29+
30+
System.out.println("result:"+rotatedDigits(100));
31+
}
32+
public static int rotatedDigits(int N) {
33+
int count = 0;
34+
for (int i = 1; i <= N; i ++) {
35+
if (isValid(i)) count ++;
36+
}
37+
return count;
38+
}
39+
40+
public static boolean isValid(int N) {
41+
/*
42+
Valid if N contains ATLEAST ONE 2, 5, 6, 9
43+
AND NO 3, 4 or 7s
44+
*/
45+
boolean validFound = false;
46+
while (N > 0) {
47+
if (N % 10 == 2) validFound = true;
48+
if (N % 10 == 5) validFound = true;
49+
if (N % 10 == 6) validFound = true;
50+
if (N % 10 == 9) validFound = true;
51+
if (N % 10 == 3) return false;
52+
if (N % 10 == 4) return false;
53+
if (N % 10 == 7) return false;
54+
N = N / 10;
55+
}
56+
return validFound;
57+
}
58+
}

src/easy/NO819_MostCommonWord.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package easy;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by Hsky on 2018/4/30.
7+
*/
8+
public class NO819_MostCommonWord {
9+
/**
10+
* Description:
11+
* Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words.
12+
* It is guaranteed there is at least one word that isn't banned, and that the answer is unique.
13+
* Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive.
14+
* The answer is in lowercase.
15+
* <p>
16+
* Example:
17+
* Example:
18+
* Input:
19+
* paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
20+
* banned = ["hit"]
21+
* Output: "ball"
22+
* Explanation:
23+
* "hit" occurs 3 times, but it is a banned word.
24+
* "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
25+
* Note that words in the paragraph are not case sensitive,
26+
* that punctuation is ignored (even if adjacent to words, such as "ball,"),
27+
* and that "hit" isn't the answer even though it occurs more because it is banned.
28+
* <p>
29+
* <p>
30+
* Note:
31+
* <p>
32+
* 1 <= paragraph.length <= 1000.
33+
* 1 <= banned.length <= 100.
34+
* 1 <= banned[i].length <= 10.
35+
* The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.)
36+
* paragraph only consists of letters, spaces, or the punctuation symbols !?',;.
37+
* Different words in paragraph are always separated by a space.
38+
* There are no hyphens or hyphenated words.
39+
* Words only consist of letters, never apostrophes or other punctuation symbols.
40+
*/
41+
//
42+
public static void main(String[] args) {
43+
String paragraph=new String("Bob!");//hit a ball, the hit BALL flew far after it was hit.
44+
String[] banned=new String[]{"hit"};
45+
String res=mostCommonWord(paragraph,banned);
46+
System.out.println(res);
47+
48+
}
49+
50+
public static String mostCommonWord(String p, String[] banned) {
51+
Set<String> ban = new HashSet<>(Arrays.asList(banned));
52+
Map<String, Integer> count = new HashMap<>();
53+
String[] words = p.replaceAll("\\pP" , "").toLowerCase().split("\\s+");
54+
String res = "";
55+
int max = 0;
56+
for (String w : words) {
57+
if (!ban.contains(w)) {
58+
count.put(w, count.getOrDefault(w, 0) + 1);
59+
if (count.get(w) > max) {
60+
res = w;
61+
max = count.get(w);
62+
}}}
63+
return res;
64+
}
65+
}

0 commit comments

Comments
 (0)