Skip to content

Commit 2e48046

Browse files
Merge pull request #1 from algorithm001/master
pull
2 parents 500acc3 + dea8b17 commit 2e48046

File tree

684 files changed

+27672
-43
lines changed

Some content is hidden

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

684 files changed

+27672
-43
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.DS_Store
2+
.idea
3+
.vscode
4+
.vs

Leetcode_262_115_java.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package data.leetcode.hash;
2+
3+
import java.util.HashMap;
4+
5+
public class Leetcode262 {
6+
/**
7+
* 执行时间过长
8+
* 执行用时 : 57 ms, 在Valid Anagram的Java提交中击败了15.39% 的用户
9+
* 内存消耗 : 41.9 MB, 在Valid Anagram的Java提交中击败了25.98% 的用户
10+
*
11+
* @param s
12+
* @param t
13+
* @return
14+
*/
15+
public static boolean isAnagram(String s, String t) {
16+
if (s.length() != t.length()) return false;
17+
HashMap<Character, Integer> sMap = new HashMap<Character, Integer>();
18+
for (int i = 0; i < s.length(); i++) {
19+
char c = s.charAt(i);
20+
Integer integer = sMap.get(c);
21+
if (integer == null) {
22+
integer = 0;
23+
}
24+
sMap.put(c, ++integer);
25+
}
26+
for (int i = 0; i < t.length(); i++) {
27+
if (sMap.containsKey(t.charAt(i))) {
28+
Integer integer = sMap.get(t.charAt(i));
29+
integer--;
30+
if (integer == 0) {
31+
sMap.remove(t.charAt(i));
32+
} else {
33+
sMap.put(t.charAt(i), integer);
34+
}
35+
} else {
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
42+
/**
43+
* 第二种解法,是将数据进行一个因射到26位字母
44+
* <p>
45+
* 执行用时 : 4 ms, 在Valid Anagram的Java提交中击败了96.25% 的用户
46+
* 内存消耗 : 38.1 MB, 在Valid Anagram的Java提交中击败了95.44% 的用户
47+
* 进行下一个挑战:
48+
*/
49+
public static boolean isAnagram2(String s, String t) {
50+
if (s.length() != t.length())
51+
return false;
52+
int[] freq1 = new int[26];
53+
int[] freq2 = new int[26];
54+
for (int i = 0; i < s.length(); i++)
55+
freq1[s.charAt(i) - 'a']++;
56+
for (int i = 0; i < t.length(); i++)
57+
freq2[t.charAt(i) - 'a']++;
58+
for (int i = 0; i < 26; i++) {
59+
if (freq1[i] != freq2[i])
60+
return false;
61+
}
62+
return true;
63+
}
64+
65+
public static void main(String[] args) {
66+
67+
68+
isAnagram2("aacc", "ccac");//false
69+
70+
isAnagram2("aacc", "caac");//true
71+
72+
}
73+
}

Leetcode_609_115_java.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package data.leetcode.hash;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class Leetcode609 {
10+
/**
11+
* 给定一个目录信息列表,包括目录路径,以及该目录中的所有包含内容的文件,您需要找到文件系统中的所有重复文件组的路径。
12+
* 一组重复的文件至少包括二个具有完全相同内容的文件。
13+
* <p>
14+
* 输入列表中的单个目录信息字符串的格式如下:
15+
* <p>
16+
* "root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"
17+
* <p>
18+
* 这意味着有 n 个文件(f1.txt, f2.txt ... fn.txt 的内容分别是 f1_content, f2_content ... fn_content)在目录 root/d1/d2/.../dm 下。注意:n>=1 且 m>=0。如果 m=0,则表示该目录是根目录。
19+
* <p>
20+
* 该输出是重复文件路径组的列表。对于每个组,它包含具有相同内容的文件的所有文件路径。文件路径是具有下列格式的字符串:
21+
* <p>
22+
* "directory_path/file_name.txt"
23+
* <p>
24+
* 示例 1:
25+
* <p>
26+
* 输入:
27+
* ["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"]
28+
* 输出:
29+
* [["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]
30+
*
31+
* @param paths
32+
* @return 最终输出不需要顺序。
33+
* 您可以假设目录名、文件名和文件内容只有字母和数字,并且文件内容的长度在 [1,50] 的范围内。
34+
* 给定的文件数量在 [1,20000] 个范围内。
35+
* 您可以假设在同一目录中没有任何文件或目录共享相同的名称。
36+
* 您可以假设每个给定的目录信息代表一个唯一的目录。目录路径和文件信息用一个空格分隔。
37+
*/
38+
39+
/**
40+
* 执行用时 : 61 ms, 在Find Duplicate File in System的Java提交中击败了73.58% 的用户
41+
* 内存消耗 : 75 MB, 在Find Duplicate File in System的Java提交中击败了6.90% 的用户
42+
* @param paths
43+
* @return
44+
*/
45+
public static List<List<String>> findDuplicate(String[] paths) {
46+
47+
HashMap<String, List<String>> contentMap = new HashMap<>();
48+
for (int i = 0; i < paths.length; i++) {
49+
String[] split = paths[i].split(" ");
50+
for (int j = 1; j < split.length; j++) {
51+
int contentStartSplit = split[j].indexOf("(");
52+
String newPath = split[0]+"/" +split[j].substring(0,contentStartSplit).trim();
53+
String content = split[j].substring(contentStartSplit+1, split[j].length()-1);
54+
List<String> contentList = contentMap.get(content);
55+
if (contentList == null) {
56+
contentList = new ArrayList<>();
57+
contentMap.put(content, contentList);
58+
}
59+
contentList.add(newPath);
60+
}
61+
}
62+
List<List<String>> result = new ArrayList<>();
63+
for (Map.Entry<String, List<String>> entry : contentMap.entrySet()) {
64+
if(entry.getValue().size()>1){
65+
result.add(entry.getValue());
66+
}
67+
}
68+
return result;
69+
}
70+
71+
public static void main(String[] args) {
72+
String[] path = {"root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"};
73+
List<List<String>> duplicate = findDuplicate(path);
74+
}
75+
}

Leetcode_692_115_java.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package data.leetcode.hash;
2+
3+
import java.util.*;
4+
5+
public class Leetcode692 {
6+
/**
7+
* 给一非空的单词列表,返回前 k 个出现次数最多的单词。
8+
* <p>
9+
* 返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。
10+
* <p>
11+
* 示例 1:
12+
* <p>
13+
* 输入: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
14+
* 输出: ["i", "love"]
15+
* 解析: "i" 和 "love" 为出现次数最多的两个单词,均为2次。
16+
* 注意,按字母顺序 "i" 在 "love" 之前。
17+
*/
18+
19+
/**
20+
* 执行用时 : 17 ms, 在Top K Frequent Words的Java提交中击败了83.42% 的用户
21+
* 内存消耗 : 41.2 MB, 在Top K Frequent Words的Java提交中击败了74.16% 的用户
22+
* @param words
23+
* @param k
24+
* @return
25+
*/
26+
27+
public static List<String> topKFrequent(String[] words, int k) {
28+
HashMap<String, Integer> wordMap = new HashMap<String, Integer>();
29+
List<String> wordList = new ArrayList<>();
30+
for (int i = 0; i < words.length; i++) {
31+
Integer node = wordMap.get(words[i]);
32+
if (node == null) node = 0;
33+
node += 1;
34+
wordMap.put(words[i], node);
35+
}
36+
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(wordMap.entrySet());
37+
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
38+
@Override
39+
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
40+
int code = o1.getValue() - o2.getValue();
41+
if (code != 0) {
42+
return -code;
43+
} else {
44+
return o1.getKey().compareTo(o2.getKey());
45+
}
46+
}
47+
});
48+
List<Map.Entry<String, Integer>> entries = list.subList(0, k);
49+
for (Map.Entry<String, Integer> entry : entries) {
50+
wordList.add(entry.getKey());
51+
}
52+
return wordList;
53+
}
54+
55+
56+
public static void main(String[] args) {
57+
58+
String words[] = {"i", "love", "leetcode", "i", "love", "coding"};
59+
topKFrequent(words, 3);
60+
}
61+
62+
}

Week_01/.DS_Store

6 KB
Binary file not shown.

Week_01/id_0/TestPush.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class TestPush {
2+
public static void main(String[] args) {
3+
System.out.println("Hello Geekbang~~");
4+
}
5+
}

Week_01/id_1/LeetCode_153_1.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int findMin(int[] nums) {
3+
int len = nums.length;
4+
if (len == 1) {
5+
return nums[0];
6+
}
7+
8+
int min = 0;
9+
int max = len - 1;
10+
11+
if (nums[min] < nums[max]) {
12+
return nums[min];
13+
}
14+
15+
int mid = (min + max) / 2;
16+
while (min <= max) {
17+
if (nums[min] < nums[mid]) {
18+
min = mid;
19+
mid = (min + max) / 2;
20+
}else if (nums[min] > nums[mid]){
21+
max = mid;
22+
mid = (min + max) / 2;
23+
}else {
24+
break;
25+
}
26+
}
27+
28+
if (min < len -1) {
29+
if (nums[min] > nums[min + 1]) {
30+
return nums[min + 1];
31+
}
32+
}
33+
return nums[min];
34+
}
35+
}

Week_01/id_1/LeetCode_24_1.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
3+
ListNode prePairTail = null;
4+
5+
public ListNode swapPairs(ListNode head) {
6+
7+
if (head == null) {
8+
return null;
9+
}
10+
11+
if (head.next == null) {
12+
return head;
13+
}
14+
15+
ListNode result = head.next;
16+
17+
ListNode node = head;
18+
ListNode next = head.next;
19+
while (node != null && next != null) {
20+
21+
node = swap(node, next);
22+
23+
if (node == null) {
24+
break;
25+
}
26+
next = node.next;
27+
}
28+
return result;
29+
30+
}
31+
32+
private ListNode swap(ListNode node, ListNode next) {
33+
34+
ListNode tmpNext = next.next;
35+
next.next = node;
36+
node.next = tmpNext;
37+
if (prePairTail != null) {
38+
prePairTail.next = next;
39+
}
40+
prePairTail = node;
41+
return tmpNext;
42+
}
43+
44+
}

Week_01/id_1/LeetCode_25_1.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
class Solution {
3+
4+
ListNode preGroupTail = null;
5+
ListNode result = null;
6+
7+
public ListNode reverseKGroup(ListNode head, int k) {
8+
9+
if (head == null || head.next == null || k == 1) {
10+
return head;
11+
}
12+
13+
int index = 1;
14+
ListNode groupHead = head;
15+
ListNode groupTail = head;
16+
17+
while (true) {
18+
19+
if (index != k) { // 查找 k 个节点中的 head 和 tail 节点
20+
groupTail = groupTail.next;
21+
if (groupTail != null) {
22+
index ++;
23+
continue;
24+
}else {
25+
// 链表长度小于 K 的情况
26+
if (this.preGroupTail != null) {
27+
this.preGroupTail.next = groupHead;
28+
break;
29+
}else {
30+
return head;
31+
}
32+
}
33+
}else {
34+
// 个数达到 k 时执行交换,返回下一组的 head 节点
35+
ListNode node = this.swap(groupHead, groupTail);
36+
if (node == null) {
37+
break;
38+
}
39+
groupHead = node;
40+
groupTail = node;
41+
index = 1;
42+
}
43+
}
44+
return result;
45+
}
46+
47+
private ListNode swap(ListNode head, ListNode tail) {
48+
49+
// 反转链表
50+
ListNode preNode = null;
51+
ListNode currentNode = head;
52+
while (currentNode != tail) {
53+
ListNode next = currentNode.next;
54+
currentNode.next = preNode;
55+
preNode = currentNode;
56+
currentNode = next;
57+
}
58+
59+
// 当前组的 tail 节点执行 swap
60+
ListNode node= currentNode.next;
61+
currentNode.next = preNode;
62+
63+
// 反转完成,将上一组的 tail 节点指向当前组的 head 节点
64+
// 第一次反转时上一组 tail 为 null,因此要执行判空操作
65+
if (this.preGroupTail != null) {
66+
this.preGroupTail.next = currentNode;
67+
}else {
68+
// 上一组的 tail 为空说明是第一组执行交换
69+
// 结果一定是第一组交换后的 head 节点
70+
result = currentNode;
71+
}
72+
this.preGroupTail = head;
73+
return node;
74+
}
75+
}

0 commit comments

Comments
 (0)