Skip to content

Commit 35c6f04

Browse files
Merge pull request #97 from chenjiajuntime/master
SZ001-1906019
2 parents 2aef401 + 903750b commit 35c6f04

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

Week_01/id_19/LeetCode_1047_19.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public String removeDuplicates(String S) {
3+
4+
/**
5+
* 去重可利用栈来解
6+
* 与栈顶对比,重复移除,否则入栈
7+
*/
8+
// 将字符串转成字符
9+
char[] chars = S.toCharArray();
10+
Stack<Character> stack = new Stack<>();
11+
12+
for (int i = 0; i<chars.length; i++) {
13+
if (stack.isEmpty() || stack.peek() != chars[i]) {
14+
stack.push(chars[i]);
15+
}else {
16+
stack.pop();
17+
}
18+
}
19+
20+
StringBuilder sb = new StringBuilder();
21+
for (char c: stack) {
22+
sb.append(c);
23+
}
24+
25+
return sb.toString();
26+
}
27+
}

Week_01/id_19/LeetCode_24_19.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public ListNode swapPairs(ListNode head) {
3+
// 终止条件只有一个结点或者无结点
4+
if (head == null || head.next == null) {
5+
return head;
6+
}
7+
8+
ListNode nextNode = head.next;
9+
// 递归条件 f(n) = f(n+1)
10+
head.next = swapPairs(nextNode.next);
11+
nextNode.next = head;
12+
return nextNode;
13+
}
14+
}

Week_01/id_19/LeetCode_26_19.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int removeDuplicates(int[] nums) {
3+
4+
// 时间复杂度为O(n)
5+
// 数组判断
6+
if (nums.length == 0) {
7+
return 0;
8+
}
9+
// 假设不相等的位置为1
10+
int uniquePos = 1;
11+
for(int i = 1;i < nums.length; i++) {
12+
// 这里要注意数组越界问题
13+
if (nums[i] != nums[i-1]) {
14+
// 数组往前移动
15+
nums[uniquePos] = nums[i];
16+
uniquePos++;
17+
}
18+
}
19+
return uniquePos;
20+
21+
}
22+
}

Week_01/id_19/NOTE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# 学习笔记
22

33
week_01
4+
5+
本周主要是学习了[线性表数据结构](http://note.youdao.com/noteshare?id=1a02479cdcbb9ffc7bb6dc587cbee0fc)。学习中基础知识发现都懂了,但在刷题时却没什么思路,最后回想起覃超老师传授的五毒神掌法及常见算法解题步骤一步步实践、反复练习及总结。
Loading

0 commit comments

Comments
 (0)