File tree Expand file tree Collapse file tree 4 files changed +118
-0
lines changed
question_linked_list_cycle_lcci
question_three_in_one_lcci Expand file tree Collapse file tree 4 files changed +118
-0
lines changed Original file line number Diff line number Diff line change
1
+ package question_linked_list_cycle_lcci ;
2
+
3
+ public class ListNode {
4
+ int val ;
5
+
6
+ ListNode next ;
7
+
8
+ ListNode (int x ) {
9
+ val = x ;
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ package question_linked_list_cycle_lcci ;
2
+
3
+ /**
4
+ * https://leetcode-cn.com/problems/linked-list-cycle-lcci/
5
+ *
6
+ * 时间复杂度是O(n),其中n为链表中的节点个数。空间复杂度是O(1)。
7
+ *
8
+ * 执行用时:0ms,击败100.00%。消耗内存:39.7MB,击败100.00%。
9
+ */
10
+ public class Solution {
11
+ public ListNode detectCycle (ListNode head ) {
12
+ ListNode cur1 = head , cur2 = head ;
13
+ do {
14
+ if (cur2 == null || cur2 .next == null ) {
15
+ return null ;
16
+ }
17
+ cur1 = cur1 .next ;
18
+ cur2 = cur2 .next .next ;
19
+ } while (cur1 != cur2 );
20
+ cur1 = head ;
21
+ while (cur1 != cur2 ) {
22
+ cur1 = cur1 .next ;
23
+ cur2 = cur2 .next ;
24
+ }
25
+ return cur1 ;
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ package question_min_stack_lcci ;
2
+
3
+ import java .util .Stack ;
4
+
5
+ /**
6
+ * https://leetcode-cn.com/problems/min-stack-lcci/
7
+ *
8
+ * 执行用时:19ms,击败77.11%。消耗内存:41.7MB,击败100.00%。
9
+ */
10
+ public class MinStack {
11
+ private Stack <Integer > stack1 , stack2 ;
12
+
13
+ /** initialize your data structure here. */
14
+ public MinStack () {
15
+ stack1 = new Stack <>();
16
+ stack2 = new Stack <>();
17
+ }
18
+
19
+ public void push (int x ) {
20
+ stack1 .push (x );
21
+ if (stack2 .isEmpty ()) {
22
+ stack2 .push (x );
23
+ } else {
24
+ stack2 .push (Math .min (x , stack2 .peek ()));
25
+ }
26
+ }
27
+
28
+ public void pop () {
29
+ stack1 .pop ();
30
+ stack2 .pop ();
31
+ }
32
+
33
+ public int top () {
34
+ return stack1 .peek ();
35
+ }
36
+
37
+ public int getMin () {
38
+ return stack2 .peek ();
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ package question_three_in_one_lcci ;
2
+
3
+ /**
4
+ * https://leetcode-cn.com/problems/three-in-one-lcci/
5
+ *
6
+ * 执行用时:12ms,击败99.73%。消耗内存:48.9MB,击败100.00%。
7
+ */
8
+ public class TripleInOne {
9
+ private int [] nums , curs ;
10
+
11
+ public TripleInOne (int stackSize ) {
12
+ nums = new int [3 * stackSize ];
13
+ curs = new int [] {0 , stackSize , 2 * stackSize };
14
+ }
15
+
16
+ public void push (int stackNum , int value ) {
17
+ if (curs [stackNum ] == (stackNum + 1 ) * nums .length / 3 ) {
18
+ return ;
19
+ }
20
+ nums [curs [stackNum ]++] = value ;
21
+ }
22
+
23
+ public int pop (int stackNum ) {
24
+ if (isEmpty (stackNum )) {
25
+ return -1 ;
26
+ }
27
+ return nums [--curs [stackNum ]];
28
+ }
29
+
30
+ public int peek (int stackNum ) {
31
+ if (isEmpty (stackNum )) {
32
+ return -1 ;
33
+ }
34
+ return nums [curs [stackNum ] - 1 ];
35
+ }
36
+
37
+ public boolean isEmpty (int stackNum ) {
38
+ return curs [stackNum ] == stackNum * nums .length / 3 ;
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments