Skip to content

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
wants to merge 15 commits into from
Closed
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

root = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要添加多余文件噢


[*]
indent_style=space
indent_size=4
end_of_line=crlf
trim_trailing_whitespace=true
39 changes: 22 additions & 17 deletions solution/0030.Substring with Concatenation of All Words/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"] 和题目描述不符,这里私自改了一下)
Expand Down Expand Up @@ -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);
Expand All @@ -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<>();
// 从后向前依次对比
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
Expand All @@ -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;
}
Expand All @@ -90,4 +95,4 @@ class Solution {
return re;
}
}
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public List<Integer> findSubstring(String s, String[] words) {
String tempStr = s.substring(j, j + len);
if(map.containsKey(tempStr)) {
HashMap<String,Integer> searched = new HashMap<>();
// 从后向前依次对比
int tempIndex = j + lastStart;
// 从后向前依次对比
int tempIndex = j + lastStart;
String matchedStr = s.substring(tempIndex, tempIndex + len);
while (tempIndex >= j && map.containsKey(matchedStr)) {
// 正确匹配到单词
Expand All @@ -39,11 +39,11 @@ public List<Integer> findSubstring(String s, String[] words) {
break;
}
matchedStr = s.substring(tempIndex, tempIndex + len);
}
// 完全匹配所以单词
}
// 完全匹配所以单词
if(j > tempIndex) {
re.add(j);
}
}
// 从tempIndex 到 tempIndex + len 这个单词不能正确匹配
else {
j = tempIndex;
Expand All @@ -53,4 +53,4 @@ public List<Integer> findSubstring(String s, String[] words) {
}
return re;
}
}
}
81 changes: 81 additions & 0 deletions solution/0147.Insertion Sort List/README.md
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有的 if/else 的书写都不规范噢,希望可以改一下。

  • if 后面需要加一个空格,然后才开始左括号,比如
if (condition) {
    // statement here
} 

else 跟在上个 if 的 } 后面,用一个空格隔开。如

if (condition) {
    // statement here
} else {
    // statement here
}
  • while 与 if 的写法一样

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);
}
}


```
46 changes: 46 additions & 0 deletions solution/0147.Insertion Sort List/Solution.java
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);
}
}