-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Add 147 Solution (java) #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6e7d386
Merge branch 'master' into work
Mrzhudky be11a90
correct the format mistake for 030 README.md
Mrzhudky 02a7e68
Update Readme.md
Mrzhudky 6c9f958
Update Solution.java
Mrzhudky 542f73c
update format
Mrzhudky c779695
Update README.md
Mrzhudky 41ea68f
Update Solution.java
Mrzhudky 1d14f7e
try .editorconfig
Mrzhudky 3a5299b
Merge branch 'work' of https://github.com/Mrzhudky/leetcode into work
Mrzhudky 307d8ee
Merge branch 'work'
Mrzhudky c430836
Merge remote-tracking branch 'upstream1/master'
Mrzhudky 3782264
Merge remote-tracking branch 'upstream1/master'
Mrzhudky 5eecf74
Merge remote-tracking branch 'upstream1/master'
Mrzhudky ed95e77
Merge remote-tracking branch 'upstream1/master'
Mrzhudky 1d20bc0
Add 147 Solution.java and README.md
Mrzhudky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
root = true | ||
|
||
[*] | ||
indent_style=space | ||
indent_size=4 | ||
end_of_line=crlf | ||
trim_trailing_whitespace=true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<String,Integer> 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<String,Integer> searched = new HashMap<>(); | ||
// 从后向前依次对比 | ||
int tempIndex = j + lastStart; | ||
String matchedStr = s.substring(tempIndex, tempIndex + len); | ||
while (tempIndex >= j && map.containsKey(matchedStr)) { | ||
// 正确匹配到单词 | ||
HashMap<String,Integer> searched = new HashMap<>(); | ||
// 从后向前依次对比 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里及以下的代码缩进不对噢 |
||
int tempIndex = j + lastStart; | ||
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; | ||
} | ||
|
@@ -90,4 +95,4 @@ class Solution { | |
return re; | ||
} | ||
} | ||
``` | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 所有的 if/else 的书写都不规范噢,希望可以改一下。
if (condition) {
// statement here
} else 跟在上个 if 的 if (condition) {
// statement here
} else {
// statement here
}
|
||
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); | ||
} | ||
} | ||
|
||
|
||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要添加多余文件噢