Skip to content

Commit 781b0fc

Browse files
author
xuebu
committed
add note
1 parent 0881d8f commit 781b0fc

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
* 876. 链表的中间结点 快慢指针法
9+
*/
10+
class Solution {
11+
public ListNode middleNode(ListNode head) {
12+
if (head == null ) return head;
13+
ListNode slow = head,fast = head;
14+
while(fast != null && fast.next != null) {
15+
slow = slow.next;
16+
fast = fast.next.next;
17+
}
18+
return slow;
19+
}
20+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* 先拿到第一个断层,然后从断层开始往回减-1直到后面都不大于前面,再对当前的i位后的所有值填充9
3+
*/
4+
class Solution {
5+
public int monotoneIncreasingDigits(int N) {
6+
char[] res = String.valueOf(N).toCharArray();
7+
int i = 1;
8+
while (i < res.length && res[i-1] <= res[i]) i++;
9+
while (0 < i && i < res.length && res[i-1] > res[i]) res[--i]--;
10+
for (int j = i+1; j < res.length; ++j) res[j] = '9';
11+
12+
return Integer.parseInt(String.valueOf(res));
13+
}
14+
}

string/partition-labels-763.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 贪心算法
3+
*/
4+
class Solution {
5+
public List<Integer> partitionLabels(String S) {
6+
int[] last = new int[26];
7+
for(int i = 0 ; i < S.length() ; i++) {
8+
last[S.charAt(i) - 'a'] = i;
9+
}
10+
List<Integer> res = new ArrayList<>();
11+
int pos = 0 , anchor = 0;
12+
for(int j = 0 ; j < S.length() ; j++) {
13+
pos = Math.max(pos,last[S.charAt(j) - 'a']);
14+
if (j == pos) {
15+
res.add(j - anchor + 1);
16+
anchor = j + 1;
17+
}
18+
}
19+
return res;
20+
}
21+
}

0 commit comments

Comments
 (0)