From be11a901af3969c9b2ce64cdeed85d333d1ea8f2 Mon Sep 17 00:00:00 2001 From: Mrzhudky <1183260441@qq.com> Date: Sat, 27 Oct 2018 10:16:03 +0800 Subject: [PATCH 1/8] correct the format mistake for 030 README.md --- .../README.md | 35 +++++++++++-------- .../Solution.java | 12 +++---- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/solution/030.Substring with Concatenation of All Words/README.md b/solution/030.Substring with Concatenation of All Words/README.md index 595fbe0b0d54f..e6ebaf7e67c40 100644 --- a/solution/030.Substring with Concatenation of All Words/README.md +++ b/solution/030.Substring with Concatenation of All Words/README.md @@ -4,18 +4,23 @@ 注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。 -``` -示例 1: +**示例** 1: +``` 输入: + s = "barfoothefoobarman", words = ["foo","bar"] + 输出: [0,9] 解释: 从索引 0 和 9 开始的子串分别是 "barfoor" 和 "foobar" 。 输出的顺序不重要, [9,0] 也是有效答案。 -示例 2: +``` +**示例** 2: +``` 输入: + s = "wordgoodstudentgoodword", words = ["word","good","good"] (ps:原题的例子为 words = ["word","student"] 和题目描述不符,这里私自改了一下) @@ -44,8 +49,8 @@ class Solution { } if(s.length() == 0 || words[0].length() == 0 || s.length() < words.length * words[0].length()) { return re; - } - // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 + } + // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 HashMap map = new HashMap(); for (String string : words) { map.put(string, map.getOrDefault(string,0) + 1); @@ -58,12 +63,12 @@ class Solution { for (int j = i; j <= strLen - len - lastStart; j += len) { String tempStr = s.substring(j, j + len); if(map.containsKey(tempStr)) { - HashMap searched = new HashMap<>(); - // 从后向前依次对比 + HashMap searched = new HashMap<>(); + // 从后向前依次对比 int tempIndex = j + lastStart; - String matchedStr = s.substring(tempIndex, tempIndex + len); - while (tempIndex >= j && map.containsKey(matchedStr)) { - // 正确匹配到单词 + String matchedStr = s.substring(tempIndex, tempIndex + len); + while (tempIndex >= j && map.containsKey(matchedStr)) { + // 正确匹配到单词 if(searched.getOrDefault(matchedStr,0) < map.get(matchedStr)) { searched.put(matchedStr, searched.getOrDefault(matchedStr,0) + 1); } @@ -75,12 +80,12 @@ class Solution { break; } matchedStr = s.substring(tempIndex, tempIndex + len); - } - // 完全匹配所以单词 - if(j > tempIndex) { + } + // 完全匹配所以单词 + if(j > tempIndex) { re.add(j); - } - // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 + } + // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 else { j = tempIndex; } diff --git a/solution/030.Substring with Concatenation of All Words/Solution.java b/solution/030.Substring with Concatenation of All Words/Solution.java index 0aa20a07bb569..6b3580ffb737e 100644 --- a/solution/030.Substring with Concatenation of All Words/Solution.java +++ b/solution/030.Substring with Concatenation of All Words/Solution.java @@ -8,7 +8,7 @@ public List findSubstring(String s, String[] words) { } if(s.length() == 0 || words[0].length() == 0 || s.length() < words.length * words[0].length()) { return re; - } + } // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 HashMap map = new HashMap(); for (String string : words) { @@ -22,8 +22,8 @@ public List findSubstring(String s, String[] words) { for (int j = i; j <= strLen - len - lastStart; j += len) { String tempStr = s.substring(j, j + len); if(map.containsKey(tempStr)) { - HashMap searched = new HashMap<>(); - // 从后向前依次对比 + HashMap searched = new HashMap<>(); + // 从后向前依次对比 int tempIndex = j + lastStart; String matchedStr = s.substring(tempIndex, tempIndex + len); while (tempIndex >= j && map.containsKey(matchedStr)) { @@ -39,11 +39,11 @@ public List findSubstring(String s, String[] words) { break; } matchedStr = s.substring(tempIndex, tempIndex + len); - } - // 完全匹配所以单词 + } + // 完全匹配所以单词 if(j > tempIndex) { re.add(j); - } + } // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 else { j = tempIndex; From 02a7e68520373ebeb1beac39a68ae4bd0582297b Mon Sep 17 00:00:00 2001 From: Mrzhudky <1183260441@qq.com> Date: Sat, 27 Oct 2018 10:21:34 +0800 Subject: [PATCH 2/8] Update Readme.md --- .../README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/solution/030.Substring with Concatenation of All Words/README.md b/solution/030.Substring with Concatenation of All Words/README.md index e6ebaf7e67c40..b014c825ae4ba 100644 --- a/solution/030.Substring with Concatenation of All Words/README.md +++ b/solution/030.Substring with Concatenation of All Words/README.md @@ -50,7 +50,7 @@ class Solution { if(s.length() == 0 || words[0].length() == 0 || s.length() < words.length * words[0].length()) { return re; } - // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 + // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 HashMap map = new HashMap(); for (String string : words) { map.put(string, map.getOrDefault(string,0) + 1); @@ -64,8 +64,8 @@ class Solution { String tempStr = s.substring(j, j + len); if(map.containsKey(tempStr)) { HashMap searched = new HashMap<>(); - // 从后向前依次对比 - int tempIndex = j + lastStart; + // 从后向前依次对比 + int tempIndex = j + lastStart; String matchedStr = s.substring(tempIndex, tempIndex + len); while (tempIndex >= j && map.containsKey(matchedStr)) { // 正确匹配到单词 @@ -81,11 +81,11 @@ class Solution { } matchedStr = s.substring(tempIndex, tempIndex + len); } - // 完全匹配所以单词 + // 完全匹配所以单词 if(j > tempIndex) { re.add(j); } - // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 + // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 else { j = tempIndex; } @@ -95,4 +95,4 @@ class Solution { return re; } } -``` \ No newline at end of file +``` From 6c9f9588b643aed5122501b8644cbe2edd05d56b Mon Sep 17 00:00:00 2001 From: Mrzhudky <1183260441@qq.com> Date: Sat, 27 Oct 2018 10:22:50 +0800 Subject: [PATCH 3/8] Update Solution.java --- .../Solution.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/solution/030.Substring with Concatenation of All Words/Solution.java b/solution/030.Substring with Concatenation of All Words/Solution.java index 6b3580ffb737e..f2fccc4bc31f4 100644 --- a/solution/030.Substring with Concatenation of All Words/Solution.java +++ b/solution/030.Substring with Concatenation of All Words/Solution.java @@ -8,8 +8,8 @@ public List findSubstring(String s, String[] words) { } if(s.length() == 0 || words[0].length() == 0 || s.length() < words.length * words[0].length()) { return re; - } - // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 + } + // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 HashMap map = new HashMap(); for (String string : words) { map.put(string, map.getOrDefault(string,0) + 1); @@ -23,8 +23,8 @@ public List findSubstring(String s, String[] words) { String tempStr = s.substring(j, j + len); if(map.containsKey(tempStr)) { HashMap searched = new HashMap<>(); - // 从后向前依次对比 - int tempIndex = j + lastStart; + // 从后向前依次对比 + int tempIndex = j + lastStart; String matchedStr = s.substring(tempIndex, tempIndex + len); while (tempIndex >= j && map.containsKey(matchedStr)) { // 正确匹配到单词 @@ -40,11 +40,11 @@ public List findSubstring(String s, String[] words) { } matchedStr = s.substring(tempIndex, tempIndex + len); } - // 完全匹配所以单词 + // 完全匹配所以单词 if(j > tempIndex) { re.add(j); } - // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 + // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 else { j = tempIndex; } @@ -53,4 +53,4 @@ public List findSubstring(String s, String[] words) { } return re; } -} \ No newline at end of file +} From 542f73c3ba980d5c19933ef3112b40fc21fa64e0 Mon Sep 17 00:00:00 2001 From: Mrzhudky <1183260441@qq.com> Date: Sat, 27 Oct 2018 10:29:26 +0800 Subject: [PATCH 4/8] update format --- .../README.md | 10 +++++----- .../Solution.java | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/solution/030.Substring with Concatenation of All Words/README.md b/solution/030.Substring with Concatenation of All Words/README.md index b014c825ae4ba..f1fe822301a43 100644 --- a/solution/030.Substring with Concatenation of All Words/README.md +++ b/solution/030.Substring with Concatenation of All Words/README.md @@ -50,7 +50,7 @@ class Solution { if(s.length() == 0 || words[0].length() == 0 || s.length() < words.length * words[0].length()) { return re; } - // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 + // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 HashMap map = new HashMap(); for (String string : words) { map.put(string, map.getOrDefault(string,0) + 1); @@ -64,8 +64,8 @@ class Solution { String tempStr = s.substring(j, j + len); if(map.containsKey(tempStr)) { HashMap searched = new HashMap<>(); - // 从后向前依次对比 - int tempIndex = j + lastStart; + // 从后向前依次对比 + int tempIndex = j + lastStart; String matchedStr = s.substring(tempIndex, tempIndex + len); while (tempIndex >= j && map.containsKey(matchedStr)) { // 正确匹配到单词 @@ -81,11 +81,11 @@ class Solution { } matchedStr = s.substring(tempIndex, tempIndex + len); } - // 完全匹配所以单词 + // 完全匹配所以单词 if(j > tempIndex) { re.add(j); } - // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 + // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 else { j = tempIndex; } diff --git a/solution/030.Substring with Concatenation of All Words/Solution.java b/solution/030.Substring with Concatenation of All Words/Solution.java index f2fccc4bc31f4..4960f50bb3607 100644 --- a/solution/030.Substring with Concatenation of All Words/Solution.java +++ b/solution/030.Substring with Concatenation of All Words/Solution.java @@ -8,8 +8,8 @@ public List findSubstring(String s, String[] words) { } if(s.length() == 0 || words[0].length() == 0 || s.length() < words.length * words[0].length()) { return re; - } - // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 + } + // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 HashMap map = new HashMap(); for (String string : words) { map.put(string, map.getOrDefault(string,0) + 1); @@ -23,8 +23,8 @@ public List findSubstring(String s, String[] words) { String tempStr = s.substring(j, j + len); if(map.containsKey(tempStr)) { HashMap searched = new HashMap<>(); - // 从后向前依次对比 - int tempIndex = j + lastStart; + // 从后向前依次对比 + int tempIndex = j + lastStart; String matchedStr = s.substring(tempIndex, tempIndex + len); while (tempIndex >= j && map.containsKey(matchedStr)) { // 正确匹配到单词 @@ -40,11 +40,11 @@ public List findSubstring(String s, String[] words) { } matchedStr = s.substring(tempIndex, tempIndex + len); } - // 完全匹配所以单词 + // 完全匹配所以单词 if(j > tempIndex) { re.add(j); } - // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 + // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 else { j = tempIndex; } From c779695b497a13f6fbf88e92906bcf674ccfbfd0 Mon Sep 17 00:00:00 2001 From: Mrzhudky <1183260441@qq.com> Date: Sat, 27 Oct 2018 10:42:54 +0800 Subject: [PATCH 5/8] Update README.md --- .../README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/solution/030.Substring with Concatenation of All Words/README.md b/solution/030.Substring with Concatenation of All Words/README.md index f1fe822301a43..b014c825ae4ba 100644 --- a/solution/030.Substring with Concatenation of All Words/README.md +++ b/solution/030.Substring with Concatenation of All Words/README.md @@ -50,7 +50,7 @@ class Solution { if(s.length() == 0 || words[0].length() == 0 || s.length() < words.length * words[0].length()) { return re; } - // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 + // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 HashMap map = new HashMap(); for (String string : words) { map.put(string, map.getOrDefault(string,0) + 1); @@ -64,8 +64,8 @@ class Solution { String tempStr = s.substring(j, j + len); if(map.containsKey(tempStr)) { HashMap searched = new HashMap<>(); - // 从后向前依次对比 - int tempIndex = j + lastStart; + // 从后向前依次对比 + int tempIndex = j + lastStart; String matchedStr = s.substring(tempIndex, tempIndex + len); while (tempIndex >= j && map.containsKey(matchedStr)) { // 正确匹配到单词 @@ -81,11 +81,11 @@ class Solution { } matchedStr = s.substring(tempIndex, tempIndex + len); } - // 完全匹配所以单词 + // 完全匹配所以单词 if(j > tempIndex) { re.add(j); } - // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 + // 从tempIndex 到 tempIndex + len 这个单词不能正确匹配 else { j = tempIndex; } From 41ea68f8c5f021cb9a0a28118a053cbff7a7e312 Mon Sep 17 00:00:00 2001 From: Mrzhudky <1183260441@qq.com> Date: Sat, 27 Oct 2018 10:46:36 +0800 Subject: [PATCH 6/8] Update Solution.java --- .../Solution.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solution/030.Substring with Concatenation of All Words/Solution.java b/solution/030.Substring with Concatenation of All Words/Solution.java index 4960f50bb3607..e87ac8255a1fd 100644 --- a/solution/030.Substring with Concatenation of All Words/Solution.java +++ b/solution/030.Substring with Concatenation of All Words/Solution.java @@ -8,7 +8,7 @@ public List findSubstring(String s, String[] words) { } if(s.length() == 0 || words[0].length() == 0 || s.length() < words.length * words[0].length()) { return re; - } + } // 用< 单词,出现次数 > 来存储 words 中的元素,方便查找 HashMap map = new HashMap(); for (String string : words) { @@ -22,7 +22,7 @@ public List findSubstring(String s, String[] words) { for (int j = i; j <= strLen - len - lastStart; j += len) { String tempStr = s.substring(j, j + len); if(map.containsKey(tempStr)) { - HashMap searched = new HashMap<>(); + HashMap searched = new HashMap<>(); // 从后向前依次对比 int tempIndex = j + lastStart; String matchedStr = s.substring(tempIndex, tempIndex + len); From 1d14f7ed05a25d9195b963b30118cbf4c84e4ef2 Mon Sep 17 00:00:00 2001 From: Mrzhudky <1183260441@qq.com> Date: Sat, 27 Oct 2018 11:05:07 +0800 Subject: [PATCH 7/8] try .editorconfig --- .editorconfig | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000000..7f0970a24d83d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ + +root = true + +[*] +indent_style=space +indent_size=4 +end_of_line=crlf +trim_trailing_whitespace=true \ No newline at end of file From 1d20bc0594f83948ec8a09d4ae7430e6a1e7dd98 Mon Sep 17 00:00:00 2001 From: Mrzhudky <1183260441@qq.com> Date: Mon, 11 Mar 2019 16:52:01 +0800 Subject: [PATCH 8/8] Add 147 Solution.java and README.md --- solution/0147.Insertion Sort List/README.md | 81 +++++++++++++++++++ .../0147.Insertion Sort List/Solution.java | 46 +++++++++++ 2 files changed, 127 insertions(+) create mode 100644 solution/0147.Insertion Sort List/README.md create mode 100644 solution/0147.Insertion Sort List/Solution.java diff --git a/solution/0147.Insertion Sort List/README.md b/solution/0147.Insertion Sort List/README.md new file mode 100644 index 0000000000000..0babe035b4f76 --- /dev/null +++ b/solution/0147.Insertion Sort List/README.md @@ -0,0 +1,81 @@ +## 对链表进行插入排序 +### 题目描述 + +对链表进行插入排序。 + +插入排序的动画演示如上。从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。 +每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中。 + +插入排序算法: +1. 插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。 +2. 每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。 +3. 重复直到所有输入数据插入完为止。 + +示例1: +``` +输入: 4->2->1->3 +输出: 1->2->3->4 +``` + +示例2: +``` +输入: -1->5->3->4->0 +输出: -1->0->3->4->5 +``` +### 解法 +从链表头部开始遍历,记录当前已完成插入排序的最后一个节点。然后进行以下操作: +- 获得要插入排序的节点 curNode 、其上一个节点 perNode 、其下一个节点 nextNode; +- 判断 curNode 是否应插入在 perNode 之后,若否,将 curNode 从链表中移除准备插入,若是,无需进一步操作,此时已排序的最后一个节点为 curNode; +- 在链表头节点前增加一个节点,应对 curNode 插入位置在 头节点之前的情况; +- 从头节点开始遍历,找到curNode 的插入位置,进行插入; +- 此时已排序的最后一个节点仍为 perNode ,重复以上操作直至链表末尾。 + +```java +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public ListNode insertionSortList(ListNode head) { + if(head == null) { + return null; + } + return insertionOneNode(head, head); + } + + private ListNode insertionOneNode(ListNode head, ListNode node) { + if(head == null || node == null || node.next == null) { + return head; + } + + ListNode perNode = node; + ListNode curNode = node.next; + ListNode nextNode = curNode.next; + + if(node.val <= curNode.val) { + return insertionOneNode(head, curNode); + } + else { + node.next = nextNode; + } + + ListNode pNode = new ListNode(0); + pNode.next = head; + head = pNode; + while(pNode.next.val <= curNode.val) { + pNode = pNode.next; + } + ListNode nNode = pNode.next; + pNode.next = curNode; + curNode.next = nNode; + + return insertionOneNode(head.next, perNode); + } +} + + +``` \ No newline at end of file diff --git a/solution/0147.Insertion Sort List/Solution.java b/solution/0147.Insertion Sort List/Solution.java new file mode 100644 index 0000000000000..591713fb8deb1 --- /dev/null +++ b/solution/0147.Insertion Sort List/Solution.java @@ -0,0 +1,46 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public ListNode insertionSortList(ListNode head) { + if(head == null) { + return null; + } + return insertionOneNode(head, head); + } + + private ListNode insertionOneNode(ListNode head, ListNode node) { + if(head == null || node == null || node.next == null) { + return head; + } + + ListNode perNode = node; + ListNode curNode = node.next; + ListNode nextNode = curNode.next; + + if(node.val <= curNode.val) { + return insertionOneNode(head, curNode); + } + else { + node.next = nextNode; + } + + ListNode pNode = new ListNode(0); + pNode.next = head; + head = pNode; + while(pNode.next.val <= curNode.val) { + pNode = pNode.next; + } + ListNode nNode = pNode.next; + pNode.next = curNode; + curNode.next = nNode; + + return insertionOneNode(head.next, perNode); + } +} +