线性表基础
链表及经典问题
环形链表-141
LeetCode地址:https://leetcode-cn.com/problems/linked-list-cycle/ 哈希表法:
public boolean hasCycle ( ListNode head) {
Set< ListNode> set = new HashSet < > ( ) ;
while ( head != null) {
if ( ! set. add ( head) ) return true ;
head = head. next;
}
return false ;
}
public boolean hasCycle ( ListNode head) {
if ( head == null) return false ;
ListNode slow = head, fast = head;
do {
if ( fast == null || fast. next == null) return false ;
slow = slow. next;
fast = fast. next. next;
} while ( slow != fast) ;
return true ;
}
环形链表II-142
LeetCode地址:https://leetcode-cn.com/problems/linked-list-cycle-ii/ 哈希表法:
public ListNode detectCycle ( ListNode head) {
Set< ListNode> set = new HashSet < > ( ) ;
while ( head != null) {
if ( ! set. add ( head) ) return head;
head = head. next;
}
return null;
}
public ListNode detectCycle ( ListNode head) {
if ( head == null) return null;
ListNode slow = head, fast = head;
do {
if ( fast == null || fast. next == null) return null;
slow = slow. next;
fast = fast. next. next;
} while ( slow != fast) ;
slow = head;
while ( slow != fast) {
slow = slow. next;
fast = fast. next;
}
return slow;
}
快乐数-202
LeetCode地址:https://leetcode-cn.com/problems/happy-number/
public boolean isHappy ( int n) {
int slow = n, fast = n;
do {
slow = getNext ( slow) ;
fast = getNext ( getNext ( fast) ) ;
} while ( slow != fast && fast != 1 ) ;
return fast == 1 ;
}
private int getNext ( int x) {
int sum = 0 ;
while ( x > 0 ) {
sum += ( x % 10 ) * ( x % 10 ) ;
x /= 10 ;
}
return sum;
}
反转链表-206
LeetCode地址:https://leetcode-cn.com/problems/reverse-linked-list/
public ListNode reverseList ( ListNode head) {
if ( head == null || head. next == null) return head;
ListNode hair = null, cur = head, next = null;
while ( cur != null) {
next = cur. next;
cur. next = hair;
hair = cur;
cur = next;
}
return hair;
}
反转链表II-92
LeetCode地址:https://leetcode-cn.com/problems/reverse-linked-list-ii/
public ListNode reverseBetween ( ListNode head, int left, int right) {
ListNode hair = new ListNode ( 0 , head) , cur = hair;
int n = right - left + 1 ;
while ( left-- > 1 ) cur = cur. next;
cur. next = reverseN ( cur. next, n) ;
return hair. next;
}
private ListNode reverseN ( ListNode head, int n) {
ListNode hair = null, cur = head, next = null;
while ( n-- > 0 ) {
next = cur. next;
cur. next = hair;
hair = cur;
cur = next;
}
head. next = cur;
return hair;
}
K个一组翻转链表-25
LeetCode地址:https://leetcode-cn.com/problems/reverse-nodes-in-k-group/
public ListNode reverseKGroup ( ListNode head, int k) {
ListNode hair = new ListNode ( 0 , head) , cur = hair, pre = null, tail = head;
while ( true ) {
for ( int i = 0 ; i < k; i++ ) {
if ( tail == null) return hair. next;
tail = tail. next;
}
pre = cur. next;
if ( ( cur. next = reverseN ( cur. next, k) ) == pre) break ;
cur = pre;
}
return hair. next;
}
private ListNode reverseN ( ListNode head, int n) {
ListNode hair = null, cur = head, next;
while ( n-- > 0 ) {
next = cur. next;
cur. next = hair;
hair = cur;
cur = next;
}
head. next = cur;
return hair;
}
旋转链表-61
LeetCode地址:https://leetcode-cn.com/problems/rotate-list/
public ListNode rotateRight ( ListNode head, int k) {
if ( head == null) return null;
int cnt = 1 ;
ListNode cur = head;
while ( cur. next != null) {
cur = cur. next;
cnt++ ;
}
k = cnt - k % cnt;
cur. next = head;
while ( k-- > 0 ) {
cur = cur. next;
}
head = cur. next;
cur. next = null;
return head;
}
两两交换链表中的节点-24
LeetCode地址:https://leetcode-cn.com/problems/swap-nodes-in-pairs/
public ListNode swapPairs ( ListNode head) {
ListNode hair = new ListNode ( 0 , head) , cur = hair, one = null, two = null;
while ( cur. next != null && cur. next. next != null) {
one = cur. next;
two = one. next;
one. next = two. next;
two. next = one;
cur. next = two;
cur = one;
}
return hair. next;
}
删除链表中倒数第N个结点-19
LeetCode地址:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
public ListNode removeNthFromEnd ( ListNode head, int n) {
if ( n <= 0 || head == null) return head;
ListNode hair = new ListNode ( 0 , head) , cur = hair, p = head;
while ( n-- > 0 ) p = p. next;
while ( p != null) {
cur = cur. next;
p = p. next;
}
cur. next = cur. next. next;
return hair. next;
}
删除排序链表中的重复元素-83
LeetCode地址:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
public ListNode deleteDuplicates ( ListNode head) {
if ( head == null || head. next == null) return head;
ListNode cur = head;
while ( cur != null && cur. next != null) {
if ( cur. val == cur. next. val) {
cur. next = cur. next. next;
} else {
cur = cur. next;
}
}
return head;
}
删除链表中的重复元素II-82
LeetCode地址:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/
public ListNode deleteDuplicates ( ListNode head) {
ListNode hair = new ListNode ( 0 , head) , cur = head, p = hair;
while ( cur != null) {
while ( cur. next != null && cur. val == cur. next. val) cur = cur. next;
if ( p. next == cur) {
p = p. next;
} else {
p. next = cur. next;
}
cur = cur. next;
}
return hair. next;
}
分隔链表-86
LeetCode地址:https://leetcode-cn.com/problems/partition-list/submissions/
public ListNode partition ( ListNode head, int x) {
ListNode sHair = new ListNode ( 0 ) ;
ListNode bHair = new ListNode ( 0 ) ;
ListNode sp = sHair, bp = bHair, cur = head;
while ( cur != null) {
if ( cur. val < x) {
sp. next = cur;
sp = sp. next;
} else {
bp. next = cur;
bp = bp. next;
}
cur = cur. next;
}
bp. next = null;
sp. next = bHair. next;
return sHair. next;
}
复制带随机指针的链表-138
LeetCode地址:https://leetcode-cn.com/problems/copy-list-with-random-pointer/
public Node copyRandomList ( Node head) {
if ( head == null) return null;
Node cur = head, copy, newHead, next;
while ( cur != null) {
copy = new Node ( cur. val) ;
copy. random = cur. random;
copy. next = cur. next;
cur. next = copy;
cur = copy. next;
}
cur = head. next;
while ( cur != null) {
if ( cur. random != null) cur. random = cur. random. next;
cur = cur. next;
if ( cur != null) cur = cur. next;
}
newHead = head. next;
cur = head;
while ( cur != null) {
next = cur. next;
if ( cur. next != null) cur. next = cur. next. next;
cur = next;
}
return newHead;
}
线程池与任务队列
设计循环队列-622
LeetCode地址:https://leetcode-cn.com/problems/design-circular-queue/
class MyCircularQueue {
private int [ ] arr;
private int head;
private int tail;
private int count;
public MyCircularQueue ( int k) {
arr = new int [ k] ;
head = 0 ;
tail = 0 ;
count = 0 ;
}
public boolean enQueue ( int value) {
if ( isFull ( ) ) return false ;
arr[ tail] = value;
tail = ( tail + 1 ) % arr. length;
count += 1 ;
return true ;
}
public boolean deQueue ( ) {
if ( isEmpty ( ) ) return false ;
head = ( head + 1 ) % arr. length;
count -= 1 ;
return true ;
}
public int Front ( ) {
if ( isEmpty ( ) ) return - 1 ;
return arr[ head] ;
}
public int Rear ( ) {
if ( isEmpty ( ) ) return - 1 ;
return arr[ ( tail - 1 + arr. length) % arr. length] ;
}
public boolean isEmpty ( ) {
return count == 0 ;
}
public boolean isFull ( ) {
return count == arr. length;
}
}
设计循环双端队列-641
LeetCode地址:https://leetcode-cn.com/problems/design-circular-deque/
class MyCircularDeque {
private int [ ] arr;
private int head;
private int tail;
private int count;
public MyCircularDeque ( int k) {
arr = new int [ k] ;
head = 0 ;
tail = 0 ;
count = 0 ;
}
public boolean insertFront ( int value) {
if ( isFull ( ) ) return false ;
head = ( head - 1 + arr. length) % arr. length;
arr[ head] = value;
count++ ;
return true ;
}
public boolean insertLast ( int value) {
if ( isFull ( ) ) return false ;
arr[ tail] = value;
tail = ( tail + 1 ) % arr. length;
count++ ;
return true ;
}
public boolean deleteFront ( ) {
if ( isEmpty ( ) ) return false ;
head = ( head + 1 ) % arr. length;
count-- ;
return true ;
}
public boolean deleteLast ( ) {
if ( isEmpty ( ) ) return false ;
tail = ( tail - 1 + arr. length) % arr. length;
count-- ;
return true ;
}
public int getFront ( ) {
if ( isEmpty ( ) ) return - 1 ;
return arr[ head] ;
}
public int getRear ( ) {
if ( isEmpty ( ) ) return - 1 ;
return arr[ ( tail - 1 + arr. length) % arr. length] ;
}
public boolean isEmpty ( ) {
return count == 0 ;
}
public boolean isFull ( ) {
return count == arr. length;
}
}
设计前中后队列-1670
LeetCode地址:https://leetcode-cn.com/problems/design-front-middle-back-queue/
class Node {
Node pre;
Node next;
int val;
public Node ( ) { }
public Node ( int val) {
this . val = val;
}
public void insertPre ( Node node) {
node. pre = this . pre;
node. next = this ;
if ( this . pre != null) this . pre. next = node;
this . pre = node;
}
public void insertNext ( Node node) {
node. pre = this ;
node. next = this . next;
if ( this . next != null) this . next. pre = node;
this . next = node;
}
public void deletePre ( ) {
if ( this . pre == null) return ;
Node p = this . pre;
this . pre = p. pre;
if ( this . pre != null) this . pre. next = this ;
p. pre = null;
p. next = null;
}
public void deleteNext ( ) {
if ( this . next == null) return ;
Node p = this . next;
this . next = p. next;
if ( this . next != null) this . next. pre = this ;
p. pre = null;
p. next = null;
}
}
class MyQueue {
Node dummyHead = new Node ( ) ;
Node dummyTail = new Node ( ) ;
int count;
public MyQueue ( ) {
dummyHead. next = dummyTail;
dummyHead. pre = null;
dummyTail. pre = dummyHead;
dummyTail. next = null;
count = 0 ;
}
public void pushFront ( int val) {
dummyHead. insertNext ( new Node ( val) ) ;
count++ ;
}
public void pushBack ( int val) {
dummyTail. insertPre ( new Node ( val) ) ;
count++ ;
}
public int popFront ( ) {
if ( isEmpty ( ) ) return - 1 ;
int ret = dummyHead. next. val;
dummyHead. deleteNext ( ) ;
count-- ;
return ret;
}
public int popBack ( ) {
if ( isEmpty ( ) ) return - 1 ;
int ret = dummyTail. pre. val;
dummyTail. deletePre ( ) ;
count-- ;
return ret;
}
public boolean isEmpty ( ) {
return size ( ) == 0 ;
}
public int size ( ) {
return count;
}
}
class FrontMiddleBackQueue {
private MyQueue left = new MyQueue ( ) ;
private MyQueue right = new MyQueue ( ) ;
public FrontMiddleBackQueue ( ) { }
public void pushFront ( int val) {
left. pushFront ( val) ;
reblance ( ) ;
}
public void pushMiddle ( int val) {
if ( left. size ( ) > right. size ( ) ) {
right. pushFront ( left. popBack ( ) ) ;
}
left. pushBack ( val) ;
}
public void pushBack ( int val) {
right. pushBack ( val) ;
reblance ( ) ;
}
public int popFront ( ) {
if ( isEmpty ( ) ) return - 1 ;
int ret = left. popFront ( ) ;
reblance ( ) ;
return ret;
}
public int popMiddle ( ) {
if ( isEmpty ( ) ) return - 1 ;
int ret = left. popBack ( ) ;
reblance ( ) ;
return ret;
}
public int popBack ( ) {
if ( isEmpty ( ) ) return - 1 ;
int ret;
if ( right. isEmpty ( ) ) {
ret = left. popBack ( ) ;
} else {
ret = right. popBack ( ) ;
}
reblance ( ) ;
return ret;
}
public boolean isEmpty ( ) {
return left. size ( ) == 0 ;
}
private void reblance ( ) {
if ( left. size ( ) < right. size ( ) ) {
left. pushBack ( right. popFront ( ) ) ;
}
if ( left. size ( ) == right. size ( ) + 2 ) {
right. pushFront ( left. popBack ( ) ) ;
}
}
}
最近请求次数-933
LeetCode地址:https://leetcode-cn.com/problems/number-of-recent-calls/submissions/
class RecentCounter {
private Queue< Integer> queue;
public RecentCounter ( ) {
queue = new LinkedList < > ( ) ;
}
public int ping ( int t) {
queue. offer ( t) ;
while ( t - queue. peek ( ) > 3000 ) queue. poll ( ) ;
return queue. size ( ) ;
}
}
面试题17.09.第k个数
LeetCode地址:https://leetcode-cn.com/problems/get-kth-magic-number-lcci/
public int getKthMagicNumber ( int k) {
List< Integer> list = new ArrayList < > ( ) ;
list. add ( 1 ) ;
int p3 = 0 , p5 = 0 , p7 = 0 ;
while ( list. size ( ) < k) {
int ans = 3 * list. get ( p3) ;
ans = Math. min ( ans, 5 * list. get ( p5) ) ;
ans = Math. min ( ans, 7 * list. get ( p7) ) ;
if ( 3 * list. get ( p3) == ans) p3++ ;
if ( 5 * list. get ( p5) == ans) p5++ ;
if ( 7 * list. get ( p7) == ans) p7++ ;
list. add ( ans) ;
}
return list. get ( k - 1 ) ;
}
亲密字符串-859
LeetCode地址:https://leetcode-cn.com/problems/buddy-strings/
public boolean buddyStrings ( String a, String b) {
if ( a. length ( ) != b. length ( ) ) return false ;
if ( a. equals ( b) ) return hasRepeat ( a) ;
int i = 0 , j;
while ( a. charAt ( i) == b. charAt ( i) ) ++ i;
j = i + 1 ;
while ( j < a. length ( ) && a. charAt ( j) == b. charAt ( j) ) ++ j;
if ( j == a. length ( ) ) return false ;
if ( a. charAt ( i) != b. charAt ( j) || a. charAt ( j) != b. charAt ( i) ) return false ;
j += 1 ;
if ( j < a. length ( ) ) {
if ( a. charAt ( j) != b. charAt ( j) ) return false ;
j += 1 ;
}
return true ;
}
private boolean hasRepeat ( String a) {
Set< Character> set = new HashSet < > ( ) ;
for ( char c : a. toCharArray ( ) ) {
if ( ! set. add ( c) ) {
return true ;
}
}
return false ;
}
柠檬水找零-860
LeetCode地址:https://leetcode-cn.com/problems/lemonade-change/
public boolean lemonadeChange ( int [ ] bills) {
int [ ] arr = new int [ 3 ] ;
for ( int bill : bills) {
if ( bill == 5 ) arr[ 0 ] += 1 ;
if ( bill == 10 ) {
if ( arr[ 0 ] > 0 ) {
arr[ 0 ] -= 1 ;
arr[ 1 ] += 1 ;
} else {
return false ;
}
}
if ( bill == 20 ) {
if ( arr[ 1 ] > 0 && arr[ 0 ] > 0 ) {
arr[ 0 ] -= 1 ;
arr[ 1 ] -= 1 ;
arr[ 2 ] += 1 ;
} else if ( arr[ 0 ] >= 3 ) {
arr[ 0 ] -= 3 ;
arr[ 2 ] += 1 ;
} else {
return false ;
}
}
}
return true ;
}
煎饼排序-969
LeetCode地址:https://leetcode-cn.com/problems/pancake-sorting/
public List< Integer> pancakeSort ( int [ ] arr) {
int len = arr. length;
List< Integer> result = new ArrayList < > ( ) ;
if ( len < 2 ) return result;
int [ ] idx = new int [ len + 1 ] ;
for ( int i = 0 ; i < len; i++ ) idx[ arr[ i] ] = i;
for ( int i = len; i >= 1 ; i-- ) {
if ( i == idx[ i] + 1 ) continue ;
if ( idx[ i] + 1 != 1 ) {
result. add ( idx[ i] + 1 ) ;
reverse ( arr, idx[ i] + 1 , idx) ;
}
if ( i != 1 ) {
result. add ( i) ;
reverse ( arr, i, idx) ;
}
}
return result;
}
private void reverse ( int [ ] arr, int n, int [ ] idx) {
for ( int i = 0 , j = n - 1 ; i < j; i++ , j-- ) {
int tmp = arr[ i] ;
arr[ i] = arr[ j] ;
arr[ j] = tmp;
idx[ arr[ i] ] = i;
idx[ arr[ j] ] = j;
}
}
任务调度器-621
LeetCode地址:https://leetcode-cn.com/problems/task-scheduler/submissions/
public int leastInterval ( char [ ] tasks, int n) {
int [ ] counter = new int [ 26 ] ;
for ( char task : tasks) {
counter[ task - 'A' ] += 1 ;
}
Arrays. sort ( counter) ;
int m = 0 ;
for ( int i = 25 ; i >= 0 && counter[ i] == counter[ 25 ] ; i-- , m++ ) { }
return Math. max ( tasks. length, ( counter[ 25 ] - 1 ) * ( n + 1 ) + m) ;
}
递归与栈(Stack):解决表达式求值
面试题03.04.化栈为队
LeetCode地址:https://leetcode-cn.com/problems/implement-queue-using-stacks-lcci/
class MyQueue {
private Stack< Integer> s1;
private Stack< Integer> s2;
public MyQueue ( ) {
s1 = new Stack < > ( ) ;
s2 = new Stack < > ( ) ;
}
public void push ( int x) {
s1. push ( x) ;
}
public int pop ( ) {
transfer ( ) ;
return s2. pop ( ) ;
}
private void transfer ( ) {
if ( ! s2. empty ( ) ) return ;
while ( ! s1. empty ( ) ) {
s2. push ( s1. pop ( ) ) ;
}
}
public int peek ( ) {
transfer ( ) ;
return s2. peek ( ) ;
}
public boolean empty ( ) {
return s1. empty ( ) && s2. empty ( ) ;
}
}
棒球比赛-682
LeetCode地址:https://leetcode-cn.com/problems/baseball-game/
public int calPoints ( String[ ] ops) {
if ( ops == null || ops. length == 0 ) return - 1 ;
Stack< Integer> stack = new Stack < > ( ) ;
for ( String op : ops) {
if ( "+" . equals ( op) ) {
int a = stack. pop ( ) ;
int b = stack. peek ( ) ;
stack. push ( a) ;
stack. push ( a + b) ;
} else if ( "D" . equals ( op) ) {
stack. push ( stack. peek ( ) * 2 ) ;
} else if ( "C" . equals ( op) ) {
stack. pop ( ) ;
} else {
stack. push ( Integer. parseInt ( op) ) ;
}
}
int sum = 0 ;
while ( ! stack. empty ( ) ) sum += stack. pop ( ) ;
return sum;
}
比较含退格的字符串-884
LeetCode地址:https://leetcode-cn.com/problems/backspace-string-compare/
public boolean backspaceCompare ( String S, String T) {
Stack< Character> s = convert ( S) ;
Stack< Character> t = convert ( T) ;
if ( s. size ( ) != t. size ( ) ) return false ;
while ( ! s. empty ( ) ) {
if ( s. pop ( ) != t. pop ( ) ) return false ;
}
return true ;
}
private Stack< Character> convert ( String s) {
Stack< Character> stack = new Stack < > ( ) ;
for ( char c : s. toCharArray ( ) ) {
if ( c == '#' && ! stack. empty ( ) ) stack. pop ( ) ;
else if ( c != '#' ) stack. push ( c) ;
}
return stack;
}
验证栈序列-946
LeetCode地址:https://leetcode-cn.com/problems/validate-stack-sequences/
public boolean validateStackSequences ( int [ ] pushed, int [ ] popped) {
Stack< Integer> stack = new Stack < > ( ) ;
for ( int j = 0 , i = 0 ; j < popped. length; j++ ) {
while ( i < pushed. length && ( stack. empty ( ) || stack. peek ( ) != popped[ j] ) ) stack. push ( pushed[ i++ ] ) ;
if ( stack. peek ( ) != popped[ j] ) return false ;
stack. pop ( ) ;
}
return true ;
}
有效的括号-2
LeetCode地址:https://leetcode-cn.com/problems/valid-parentheses/
private static final Map< Character, Character> MAP = new HashMap < > ( 4 ) ;
static {
MAP. put ( ')' , '(' ) ;
MAP. put ( ']' , '[' ) ;
MAP. put ( '}' , '{' ) ;
}
public boolean isValid ( String s) {
if ( s == null || s. length ( ) == 0 ) return true ;
Stack< Character> stack = new Stack < > ( ) ;
for ( char c : s. toCharArray ( ) ) {
switch ( c) {
case '(' :
case '[' :
case '{' : stack. push ( c) ; break ;
case ')' :
case ']' :
case '}' : if ( stack. empty ( ) || stack. peek ( ) != MAP. get ( c) ) return false ; stack. pop ( ) ; break ;
}
}
return stack. empty ( ) ;
}
删除最外层的括号-1021
LeetCode地址:https://leetcode-cn.com/problems/remove-outermost-parentheses/
public String removeOuterParentheses ( String S) {
StringBuilder sb = new StringBuilder ( ) ;
for ( int i = 0 , pre = 0 , cnt = 0 ; i < S. length ( ) ; i++ ) {
if ( S. charAt ( i) == '(' ) cnt++ ;
else cnt-- ;
if ( cnt != 0 ) continue ;
sb. append ( S. substring ( pre + 1 , i) ) ;
pre = i + 1 ;
}
return sb. toString ( ) ;
}
移除无效的括号-1249
LeetCode地址:https://leetcode-cn.com/problems/minimum-remove-to-make-valid-parentheses/ 方法一:
public String minRemoveToMakeValid ( String s) {
Set< Integer> idx = new HashSet < > ( ) ;
for ( int i = 0 , cnt = 0 ; i < s. length ( ) ; i++ ) {
char c = s. charAt ( i) ;
if ( c == '(' ) cnt++ ;
else if ( c == ')' ) cnt-- ;
if ( cnt < 0 ) {
cnt = 0 ;
idx. add ( i) ;
}
}
for ( int i = s. length ( ) - 1 , cnt = 0 ; i >= 0 ; i-- ) {
char c = s. charAt ( i) ;
if ( c == '(' ) cnt++ ;
else if ( c == ')' ) cnt-- ;
if ( cnt > 0 ) {
cnt = 0 ;
idx. add ( i) ;
}
}
StringBuilder sb = new StringBuilder ( ) ;
for ( int i = 0 ; i < s. length ( ) ; i++ ) {
if ( idx. add ( i) ) sb. append ( s. charAt ( i) ) ;
}
return sb. toString ( ) ;
}
public String minRemoveToMakeValid ( String s) {
Stack< Character> s1 = new Stack < > ( ) ;
int cnt = 0 ;
for ( char c : s. toCharArray ( ) ) {
if ( c == '(' ) cnt++ ;
else if ( c == ')' ) cnt-- ;
if ( cnt < 0 ) {
cnt = 0 ;
continue ;
}
s1. push ( c) ;
}
Stack< Character> s2 = new Stack < > ( ) ;
cnt = 0 ;
while ( ! s1. empty ( ) ) {
char c = s1. pop ( ) ;
if ( c == '(' ) cnt++ ;
else if ( c == ')' ) cnt-- ;
if ( cnt > 0 ) {
cnt = 0 ;
continue ;
}
s2. push ( c) ;
}
StringBuilder sb = new StringBuilder ( ) ;
while ( ! s2. empty ( ) ) {
sb. append ( s2. pop ( ) ) ;
}
return sb. toString ( ) ;
}
二叉树的后序遍历-145
LeetCode地址:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
public List< Integer> postorderTraversal ( TreeNode root) {
List< Integer> result = new ArrayList < > ( ) ;
if ( root == null) return result;
Stack< TreeNode> s1 = new Stack < > ( ) ;
Stack< Integer> s2 = new Stack < > ( ) ;
s1. push ( root) ;
s2. push ( 0 ) ;
while ( ! s1. empty ( ) ) {
switch ( s2. pop ( ) ) {
case 0 :
s2. push ( 1 ) ;
if ( s1. peek ( ) . left != null) {
s1. push ( s1. peek ( ) . left) ;
s2. push ( 0 ) ;
}
break ;
case 1 :
s2. push ( 2 ) ;
if ( s1. peek ( ) . right != null) {
s1. push ( s1. peek ( ) . right) ;
s2. push ( 0 ) ;
}
break ;
case 2 :
result. add ( s1. pop ( ) . val) ;
break ;
}
}
return result;
}
验证二叉树的前序序列化-331
LeetCode地址:https://leetcode-cn.com/problems/verify-preorder-serialization-of-a-binary-tree/
public boolean isValidSerialization ( String preorder) {
List< String> list = new ArrayList < > ( ) ;
for ( String s : preorder. split ( "," ) ) {
list. add ( s) ;
int last = list. size ( ) - 1 ;
while ( list. size ( ) >= 3 && "#" . equals ( list. get ( last) ) && "#" . equals ( list. get ( last - 1 ) ) && ! "#" . equals ( list. get ( last - 2 ) ) ) {
list. remove ( last) ;
list. remove ( last - 1 ) ;
list. remove ( last - 2 ) ;
list. add ( "#" ) ;
last = list. size ( ) - 1 ;
}
}
return list. size ( ) == 1 && "#" . equals ( list. get ( 0 ) ) ;
}
基本计算器 II-227
LeetCode地址:https://leetcode-cn.com/problems/basic-calculator-ii/
public int calculate ( String s) {
Stack< Integer> s1 = new Stack < > ( ) ;
Stack< Character> s2 = new Stack < > ( ) ;
int num = 0 ;
s += "@" ;
for ( char c : s. toCharArray ( ) ) {
if ( c == ' ' ) continue ;
if ( c >= '0' && c <= '9' ) {
num = num * 10 + ( c - '0' ) ;
continue ;
}
s1. push ( num) ;
num = 0 ;
while ( ! s2. empty ( ) && level ( c) <= level ( s2. peek ( ) ) ) {
s1. push ( calc ( s1. pop ( ) , s1. pop ( ) , s2. pop ( ) ) ) ;
}
s2. push ( c) ;
}
return s1. pop ( ) ;
}
private int level ( char c) {
switch ( c) {
case '@' : return - 1 ;
case '+' :
case '-' : return 1 ;
case '*' :
case '/' : return 2 ;
default : return 0 ;
}
}
private int calc ( int b, int a, char c) {
switch ( c) {
case '+' : return a + b;
case '-' : return a - b;
case '*' : return a * b;
case '/' : return a / b;
default : return 0 ;
}
}
函数的独占时间-636
LeetCode地址:https://leetcode-cn.com/problems/exclusive-time-of-functions/
public int [ ] exclusiveTime ( int n, List< String> logs) {
int [ ] result = new int [ n] ;
Stack< Integer> stack = new Stack < > ( ) ;
int pre = 0 ;
for ( String log : logs) {
String[ ] slices = log. split ( ":" ) ;
int id = Integer. parseInt ( slices[ 0 ] ) ;
String status = slices[ 1 ] ;
int tickTime = Integer. parseInt ( slices[ 2 ] ) ;
if ( "start" . equals ( status) ) {
if ( ! stack. empty ( ) ) result[ stack. peek ( ) ] += tickTime - pre;
pre = tickTime;
stack. push ( id) ;
} else {
result[ id] += tickTime - pre + 1 ;
pre = tickTime + 1 ;
stack. pop ( ) ;
}
}
return result;
}
表现良好的最长时间段-1124
LeetCode地址:https://leetcode-cn.com/problems/longest-well-performing-interval/
public int longestWPI ( int [ ] hours) {
Map< Integer, Integer> idx = new HashMap < > ( ) ;
idx. put ( 0 , - 1 ) ;
Map< Integer, Integer> f = new HashMap < > ( ) ;
f. put ( 0 , 0 ) ;
int cnt = 0 , ans = 0 ;
for ( int i = 0 ; i < hours. length; i++ ) {
if ( hours[ i] > 8 ) cnt++ ;
else cnt-- ;
if ( ! idx. containsKey ( cnt) ) {
idx. put ( cnt, i) ;
if ( ! f. containsKey ( cnt - 1 ) ) f. put ( cnt, 0 ) ;
else f. put ( cnt, f. get ( cnt - 1 ) + ( i - idx. get ( cnt - 1 ) ) ) ;
}
if ( ! idx. containsKey ( cnt - 1 ) ) continue ;
ans = Math. max ( ans, f. get ( cnt - 1 ) + ( i - idx. get ( cnt - 1 ) ) ) ;
}
return ans;
}