File tree Expand file tree Collapse file tree 5 files changed +65
-0
lines changed Expand file tree Collapse file tree 5 files changed +65
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
# 学习笔记
2
2
3
3
week_01
4
+
5
+ 本周主要是学习了[ 线性表数据结构] ( http://note.youdao.com/noteshare?id=1a02479cdcbb9ffc7bb6dc587cbee0fc ) 。学习中基础知识发现都懂了,但在刷题时却没什么思路,最后回想起覃超老师传授的五毒神掌法及常见算法解题步骤一步步实践、反复练习及总结。
You can’t perform that action at this time.
0 commit comments