Skip to content

Commit eb0d465

Browse files
author
Botao Xiao
committed
[Function add]
1. Add leetcode solutions.
1 parent 40e48c3 commit eb0d465

File tree

3 files changed

+141
-2
lines changed

3 files changed

+141
-2
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@
407407

408408
[231. Power of Two](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/231.%20Power%20of%20Two.md)
409409

410+
[232. Implement Queue using Stacks](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/232.%20Implement%20Queue%20using%20Stacks.md)
411+
412+
[234. Palindrome Linked List](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/234.%20Palindrome%20Linked%20List.md)
413+
410414
## Algorithm(4th_Edition)
411415
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
412416
All java realization codes are placed in different packages.

leetcode/232. Implement Queue using Stacks.md

+59-1
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,62 @@ class MyQueue {
115115
* int param_3 = obj.peek();
116116
* boolean param_4 = obj.empty();
117117
*/
118-
```
118+
```
119+
120+
### 二刷
121+
1. Implement the queue using two stacks.
122+
2. We have a stack for save values and another one for temperory saving, when we want to use peek or poll, we pop all values saved in one stack to another and read(or pop) the peek values at the top of another stack. After we finished the action, we need to pop the values back to the original stack.
123+
```Java
124+
class MyQueue {
125+
Stack<Integer> save = null;
126+
Stack<Integer> temp = null;
127+
/** Initialize your data structure here. */
128+
public MyQueue() {
129+
this.save = new Stack<>();
130+
this.temp = new Stack<>();
131+
}
132+
133+
/** Push element x to the back of queue. */
134+
public void push(int x) {
135+
this.save.push(x);
136+
}
137+
138+
/** Removes the element from in front of queue and returns that element. */
139+
public int pop() {
140+
while(!this.save.empty()){
141+
this.temp.push(this.save.pop());
142+
}
143+
int result = this.temp.pop();
144+
while(!this.temp.empty()){
145+
this.save.push(this.temp.pop());
146+
}
147+
return result;
148+
}
149+
150+
/** Get the front element. */
151+
public int peek() {
152+
while(!this.save.empty()){
153+
this.temp.push(this.save.pop());
154+
}
155+
int result = this.temp.peek();
156+
while(!this.temp.empty()){
157+
this.save.push(this.temp.pop());
158+
}
159+
return result;
160+
}
161+
162+
/** Returns whether the queue is empty. */
163+
public boolean empty() {
164+
return this.save.empty();
165+
}
166+
}
167+
168+
/**
169+
* Your MyQueue object will be instantiated and called as such:
170+
* MyQueue obj = new MyQueue();
171+
* obj.push(x);
172+
* int param_2 = obj.pop();
173+
* int param_3 = obj.peek();
174+
* boolean param_4 = obj.empty();
175+
*/
176+
```

leetcode/234. Palindrome Linked List.md

+78-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,81 @@ class Solution {
5656
return true;
5757
}
5858
}
59-
```
59+
```
60+
61+
### 二刷
62+
1. Method 1: I first save the list as array and then checked the palindrome of the array.
63+
```Java
64+
/**
65+
* Definition for singly-linked list.
66+
* public class ListNode {
67+
* int val;
68+
* ListNode next;
69+
* ListNode(int x) { val = x; }
70+
* }
71+
*/
72+
class Solution {
73+
public boolean isPalindrome(ListNode head) {
74+
ListNode cur = head;
75+
List<Integer> temp = new LinkedList<>();
76+
while(cur != null){
77+
temp.add(cur.val);
78+
cur = cur.next;
79+
}
80+
Integer[] arr = new Integer[temp.size()];
81+
for(int i = 0; i < temp.size(); i++){
82+
arr[i] = (Integer)temp.get(i);
83+
}
84+
int start = 0, end = arr.length - 1;
85+
while(start < end){
86+
if(!arr[start++].equals(arr[end--])){
87+
return false;
88+
}
89+
}
90+
return true;
91+
}
92+
}
93+
```
94+
95+
2. Method 2, I reversed the first half of the list, and compare the first half and second half.
96+
```Java
97+
/**
98+
* Definition for singly-linked list.
99+
* public class ListNode {
100+
* int val;
101+
* ListNode next;
102+
* ListNode(int x) { val = x; }
103+
* }
104+
*/
105+
class Solution {
106+
public boolean isPalindrome(ListNode head) {
107+
if(head == null) return true;
108+
ListNode dummy = new ListNode(0);;
109+
dummy.next = head;
110+
ListNode slow = dummy;
111+
ListNode fast = dummy;
112+
while(fast != null && fast.next != null){
113+
slow = slow.next;
114+
fast = fast.next.next;
115+
}
116+
ListNode temp = slow.next;
117+
slow.next = null;
118+
ListNode pre = null;
119+
ListNode cur = head;
120+
ListNode next = null;
121+
while(cur != null){
122+
next = cur.next;
123+
cur.next = pre;
124+
pre = cur;
125+
cur = next;
126+
}
127+
cur = fast == null ? pre.next : pre;
128+
while(cur != null && temp != null){
129+
if(cur.val != temp.val) return false;
130+
cur = cur.next;
131+
temp = temp.next;
132+
}
133+
return true;
134+
}
135+
}
136+
```

0 commit comments

Comments
 (0)