mirror of
https://gitee.com/TheAlgorithms/LeetCodeAnimation.git
synced 2024-12-06 15:19:44 +08:00
添加仓库代码
This commit is contained in:
parent
4e5b46437a
commit
921cc799db
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -1 +0,0 @@
|
||||
*.html linguist-language=java
|
7
0001-Two-Sum/cpp-0001/CMakeLists.txt
Executable file
7
0001-Two-Sum/cpp-0001/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0001)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main2.cpp)
|
||||
add_executable(cpp_0001 ${SOURCE_FILES})
|
43
0001-Two-Sum/cpp-0001/main.cpp
Executable file
43
0001-Two-Sum/cpp-0001/main.cpp
Executable file
@ -0,0 +1,43 @@
|
||||
/// Source : https://leetcode.com/problems/two-sum/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Brute Force
|
||||
/// Time Complexity: O(n^2)
|
||||
/// Space Complexity: O(1)
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
|
||||
for(int i = 0 ; i < nums.size() ; i ++)
|
||||
for(int j = i + 1 ; j < nums.size() ; j ++)
|
||||
if(nums[i] + nums[j] == target){
|
||||
int res[] = {i, j};
|
||||
return vector<int>(res, res + 2);
|
||||
}
|
||||
|
||||
throw invalid_argument("the input has no solution");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void printVec(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
const int nums[] = {0,4,3,0};
|
||||
vector<int> nums_vec( nums, nums + sizeof(nums)/sizeof(int) );
|
||||
int target = 0;
|
||||
printVec(Solution().twoSum(nums_vec, target));
|
||||
|
||||
return 0;
|
||||
}
|
50
0001-Two-Sum/cpp-0001/main2.cpp
Executable file
50
0001-Two-Sum/cpp-0001/main2.cpp
Executable file
@ -0,0 +1,50 @@
|
||||
/// Source : https://leetcode.com/problems/two-sum/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Two-Pass Hash Table
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(n)
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
|
||||
unordered_map<int,int> record;
|
||||
for(int i = 0 ; i < nums.size() ; i ++)
|
||||
record[nums[i]] = i;
|
||||
|
||||
for(int i = 0 ; i < nums.size() ; i ++){
|
||||
unordered_map<int,int>::iterator iter = record.find(target - nums[i]);
|
||||
if(iter != record.end() && iter->second != i){
|
||||
int res[] = {i, iter->second};
|
||||
return vector<int>(res, res + 2);
|
||||
}
|
||||
}
|
||||
|
||||
throw invalid_argument("the input has no solution");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void printVec(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
const int nums[] = {0,4,3,0};
|
||||
vector<int> nums_vec( nums, nums + sizeof(nums)/sizeof(int) );
|
||||
int target = 0;
|
||||
printVec(Solution().twoSum(nums_vec, target));
|
||||
|
||||
return 0;
|
||||
}
|
50
0001-Two-Sum/cpp-0001/main3.cpp
Executable file
50
0001-Two-Sum/cpp-0001/main3.cpp
Executable file
@ -0,0 +1,50 @@
|
||||
/// Source : https://leetcode.com/problems/two-sum/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// One-Pass Hash Table
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(n)
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
|
||||
unordered_map<int,int> record;
|
||||
for(int i = 0 ; i < nums.size() ; i ++){
|
||||
|
||||
int complement = target - nums[i];
|
||||
if(record.find(complement) != record.end()){
|
||||
int res[] = {i, record[complement]};
|
||||
return vector<int>(res, res + 2);
|
||||
}
|
||||
|
||||
record[nums[i]] = i;
|
||||
}
|
||||
|
||||
throw invalid_argument("the input has no solution");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void printVec(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
const int nums[] = {0,4,3,0};
|
||||
vector<int> nums_vec( nums, nums + sizeof(nums)/sizeof(int) );
|
||||
int target = 0;
|
||||
printVec(Solution().twoSum(nums_vec, target));
|
||||
|
||||
return 0;
|
||||
}
|
36
0001-Two-Sum/java-0001/src/Solution1.java
Executable file
36
0001-Two-Sum/java-0001/src/Solution1.java
Executable file
@ -0,0 +1,36 @@
|
||||
/// Source : https://leetcode.com/problems/two-sum/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/// Brute Force
|
||||
/// Time Complexity: O(n^2)
|
||||
/// Space Complexity: O(1)
|
||||
public class Solution1 {
|
||||
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
|
||||
for(int i = 0 ; i < nums.length; i ++)
|
||||
for(int j = 0 ; j < nums.length ; j ++)
|
||||
if(nums[i] + nums[j] == target){
|
||||
int[] res = {i, j};
|
||||
return res;
|
||||
}
|
||||
|
||||
throw new IllegalStateException("the input has no solution");
|
||||
}
|
||||
|
||||
private static void printArr(int[] nums){
|
||||
for(int num: nums)
|
||||
System.out.print(num + " ");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] nums = {0, 4, 3, 0};
|
||||
int target = 0;
|
||||
printArr((new Solution1()).twoSum(nums, target));
|
||||
}
|
||||
}
|
44
0001-Two-Sum/java-0001/src/Solution2.java
Executable file
44
0001-Two-Sum/java-0001/src/Solution2.java
Executable file
@ -0,0 +1,44 @@
|
||||
/// Source : https://leetcode.com/problems/two-sum/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/// Two-Pass Hash Table
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(n)
|
||||
public class Solution2 {
|
||||
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
|
||||
HashMap<Integer, Integer> record = new HashMap<Integer, Integer>();
|
||||
for(int i = 0 ; i < nums.length ; i ++)
|
||||
record.put(nums[i], i);
|
||||
|
||||
for(int i = 0 ; i < nums.length; i ++){
|
||||
|
||||
Integer index = record.get(target - nums[i]);
|
||||
if(index != null && index != i){
|
||||
int[] res = {i, index};
|
||||
return res;
|
||||
}
|
||||
|
||||
record.put(nums[i], i);
|
||||
}
|
||||
|
||||
throw new IllegalStateException("the input has no solution");
|
||||
}
|
||||
|
||||
private static void printArr(int[] nums){
|
||||
for(int num: nums)
|
||||
System.out.print(num + " ");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] nums = {0, 4, 3, 0};
|
||||
int target = 0;
|
||||
printArr((new Solution2()).twoSum(nums, target));
|
||||
}
|
||||
}
|
41
0001-Two-Sum/java-0001/src/Solution3.java
Executable file
41
0001-Two-Sum/java-0001/src/Solution3.java
Executable file
@ -0,0 +1,41 @@
|
||||
/// Source : https://leetcode.com/problems/two-sum/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/// One-Pass Hash Table
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(n)
|
||||
public class Solution3 {
|
||||
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
|
||||
HashMap<Integer, Integer> record = new HashMap<Integer, Integer>();
|
||||
for(int i = 0 ; i < nums.length; i ++){
|
||||
|
||||
int complement = target - nums[i];
|
||||
if(record.containsKey(complement)){
|
||||
int[] res = {i, record.get(complement)};
|
||||
return res;
|
||||
}
|
||||
|
||||
record.put(nums[i], i);
|
||||
}
|
||||
|
||||
throw new IllegalStateException("the input has no solution");
|
||||
}
|
||||
|
||||
private static void printArr(int[] nums){
|
||||
for(int num: nums)
|
||||
System.out.print(num + " ");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] nums = {0, 4, 3, 0};
|
||||
int target = 0;
|
||||
printArr((new Solution3()).twoSum(nums, target));
|
||||
}
|
||||
}
|
7
0019-Remove-Nth-Node-From-End-of-List/cpp-0019/CMakeLists.txt
Executable file
7
0019-Remove-Nth-Node-From-End-of-List/cpp-0019/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0019)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main.cpp)
|
||||
add_executable(cpp_0019 ${SOURCE_FILES})
|
111
0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main.cpp
Executable file
111
0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main.cpp
Executable file
@ -0,0 +1,111 @@
|
||||
/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
///Definition for singly-linked list.
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(NULL) {}
|
||||
};
|
||||
|
||||
/// LinkedList Test Helper Functions
|
||||
ListNode* createLinkedList(int arr[], int n){
|
||||
|
||||
if(n == 0)
|
||||
return NULL;
|
||||
|
||||
ListNode* head = new ListNode(arr[0]);
|
||||
ListNode* curNode = head;
|
||||
for(int i = 1 ; i < n ; i ++){
|
||||
curNode->next = new ListNode(arr[i]);
|
||||
curNode = curNode->next;
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
void printLinkedList(ListNode* head){
|
||||
|
||||
if(head == NULL){
|
||||
cout<<"NULL"<<endl;
|
||||
return;
|
||||
}
|
||||
|
||||
ListNode* curNode = head;
|
||||
while(curNode != NULL){
|
||||
cout << curNode->val;
|
||||
if(curNode->next != NULL)
|
||||
cout << " -> ";
|
||||
curNode = curNode->next;
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void deleteLinkedList(ListNode* head){
|
||||
|
||||
ListNode* curNode = head;
|
||||
while(curNode != NULL){
|
||||
ListNode* delNode = curNode;
|
||||
curNode = curNode->next;
|
||||
delete delNode;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/// Get the total length and remove the nth node
|
||||
/// Two Pass Algorithm
|
||||
///
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(1)
|
||||
class Solution {
|
||||
public:
|
||||
ListNode* removeNthFromEnd(ListNode* head, int n) {
|
||||
|
||||
ListNode* dummyHead = new ListNode(0);
|
||||
dummyHead->next = head;
|
||||
|
||||
int length = 0;
|
||||
for(ListNode* cur = dummyHead->next ; cur != NULL ; cur = cur->next)
|
||||
length ++;
|
||||
|
||||
int k = length - n;
|
||||
assert(k >= 0);
|
||||
ListNode* cur = dummyHead;
|
||||
for(int i = 0 ; i < k ; i ++)
|
||||
cur = cur->next;
|
||||
|
||||
ListNode* delNode = cur->next;
|
||||
cur->next = delNode->next;
|
||||
delete delNode;
|
||||
|
||||
ListNode* retNode = dummyHead->next;
|
||||
delete dummyHead;
|
||||
return retNode;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
int arr[] = {1, 2, 3, 4, 5};
|
||||
int n = sizeof(arr)/sizeof(int);
|
||||
|
||||
ListNode* head = createLinkedList(arr, n);
|
||||
printLinkedList(head);
|
||||
|
||||
head = Solution().removeNthFromEnd(head, 2);
|
||||
printLinkedList(head);
|
||||
|
||||
deleteLinkedList(head);
|
||||
|
||||
return 0;
|
||||
}
|
112
0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main2.cpp
Executable file
112
0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main2.cpp
Executable file
@ -0,0 +1,112 @@
|
||||
/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
///Definition for singly-linked list.
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(NULL) {}
|
||||
};
|
||||
|
||||
/// LinkedList Test Helper Functions
|
||||
ListNode* createLinkedList(int arr[], int n){
|
||||
|
||||
if(n == 0)
|
||||
return NULL;
|
||||
|
||||
ListNode* head = new ListNode(arr[0]);
|
||||
ListNode* curNode = head;
|
||||
for(int i = 1 ; i < n ; i ++){
|
||||
curNode->next = new ListNode(arr[i]);
|
||||
curNode = curNode->next;
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
void printLinkedList(ListNode* head){
|
||||
|
||||
if( head == NULL ){
|
||||
cout << "NULL" << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
ListNode* curNode = head;
|
||||
while(curNode != NULL){
|
||||
cout << curNode->val;
|
||||
if( curNode->next != NULL )
|
||||
cout << " -> ";
|
||||
curNode = curNode->next;
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void deleteLinkedList(ListNode* head){
|
||||
|
||||
ListNode* curNode = head;
|
||||
while(curNode != NULL){
|
||||
ListNode* delNode = curNode;
|
||||
curNode = curNode->next;
|
||||
delete delNode;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/// Two Pointers - One Pass Algorithm
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(1)
|
||||
class Solution {
|
||||
public:
|
||||
ListNode* removeNthFromEnd(ListNode* head, int n) {
|
||||
|
||||
ListNode* dummyHead = new ListNode(0);
|
||||
dummyHead->next = head;
|
||||
|
||||
ListNode* p = dummyHead;
|
||||
ListNode* q = dummyHead;
|
||||
for(int i = 0 ; i < n + 1 ; i ++){
|
||||
assert(q);
|
||||
q = q->next;
|
||||
}
|
||||
|
||||
while(q){
|
||||
p = p->next;
|
||||
q = q->next;
|
||||
}
|
||||
|
||||
ListNode* delNode = p->next;
|
||||
p->next = delNode->next;
|
||||
delete delNode;
|
||||
|
||||
ListNode* retNode = dummyHead->next;
|
||||
delete dummyHead;
|
||||
|
||||
return retNode;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
int arr[] = {1, 2, 3, 4, 5};
|
||||
int n = sizeof(arr)/sizeof(int);
|
||||
|
||||
ListNode* head = createLinkedList(arr, n);
|
||||
printLinkedList(head);
|
||||
|
||||
head = Solution().removeNthFromEnd(head, 2);
|
||||
printLinkedList(head);
|
||||
|
||||
deleteLinkedList(head);
|
||||
|
||||
return 0;
|
||||
}
|
52
0019-Remove-Nth-Node-From-End-of-List/java-0019/src/ListNode.java
Executable file
52
0019-Remove-Nth-Node-From-End-of-List/java-0019/src/ListNode.java
Executable file
@ -0,0 +1,52 @@
|
||||
// Definition for singly-linked list.
|
||||
// 在Java版本中,我们将LinkedList相关的测试辅助函数写在ListNode里
|
||||
public class ListNode {
|
||||
|
||||
public int val;
|
||||
public ListNode next = null;
|
||||
|
||||
public ListNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
// 根据n个元素的数组arr创建一个链表
|
||||
// 使用arr为参数,创建另外一个ListNode的构造函数
|
||||
public ListNode (int[] arr){
|
||||
|
||||
if(arr == null || arr.length == 0)
|
||||
throw new IllegalArgumentException("arr can not be empty");
|
||||
|
||||
this.val = arr[0];
|
||||
ListNode curNode = this;
|
||||
for(int i = 1 ; i < arr.length ; i ++){
|
||||
curNode.next = new ListNode(arr[i]);
|
||||
curNode = curNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
ListNode findNode(int x){
|
||||
|
||||
ListNode curNode = this;
|
||||
while(curNode != null){
|
||||
if(curNode.val == x)
|
||||
return curNode;
|
||||
curNode = curNode.next;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 返回以当前ListNode为头结点的链表信息字符串
|
||||
@Override
|
||||
public String toString(){
|
||||
|
||||
StringBuilder s = new StringBuilder("");
|
||||
ListNode curNode = this;
|
||||
while(curNode != null){
|
||||
s.append(Integer.toString(curNode.val));
|
||||
s.append(" -> ");
|
||||
curNode = curNode.next;
|
||||
}
|
||||
s.append("NULL");
|
||||
return s.toString();
|
||||
}
|
||||
}
|
38
0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution1.java
Executable file
38
0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution1.java
Executable file
@ -0,0 +1,38 @@
|
||||
/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(1)
|
||||
public class Solution1 {
|
||||
|
||||
public ListNode removeNthFromEnd(ListNode head, int n) {
|
||||
|
||||
ListNode dummyHead = new ListNode(0);
|
||||
dummyHead.next = head;
|
||||
|
||||
int length = 0;
|
||||
for(ListNode cur = dummyHead.next ; cur != null ; cur = cur.next)
|
||||
length ++;
|
||||
|
||||
int k = length - n;
|
||||
assert k >= 0;
|
||||
ListNode cur = dummyHead;
|
||||
for(int i = 0 ; i < k ; i ++)
|
||||
cur = cur.next;
|
||||
|
||||
cur.next = cur.next.next;
|
||||
|
||||
return dummyHead.next;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int arr[] = {1, 2, 3, 4, 5};
|
||||
ListNode head = new ListNode(arr);
|
||||
System.out.println(head);
|
||||
|
||||
head = (new Solution1()).removeNthFromEnd(head, 2);
|
||||
System.out.println(head);
|
||||
}
|
||||
}
|
41
0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution2.java
Executable file
41
0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution2.java
Executable file
@ -0,0 +1,41 @@
|
||||
/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
//
|
||||
// Two Pointers - One Pass Algorithm
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(1)
|
||||
public class Solution2 {
|
||||
|
||||
public ListNode removeNthFromEnd(ListNode head, int n) {
|
||||
|
||||
ListNode dummyHead = new ListNode(0);
|
||||
dummyHead.next = head;
|
||||
|
||||
ListNode p = dummyHead;
|
||||
ListNode q = dummyHead;
|
||||
for( int i = 0 ; i < n + 1 ; i ++ ){
|
||||
assert q != null;
|
||||
q = q.next;
|
||||
}
|
||||
|
||||
while(q != null){
|
||||
p = p.next;
|
||||
q = q.next;
|
||||
}
|
||||
|
||||
p.next = p.next.next;
|
||||
|
||||
return dummyHead.next;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int arr[] = {1, 2, 3, 4, 5};
|
||||
ListNode head = new ListNode(arr);
|
||||
System.out.println(head);
|
||||
|
||||
head = (new Solution2()).removeNthFromEnd(head, 2);
|
||||
System.out.println(head);
|
||||
}
|
||||
}
|
7
0020-Valid-Parentheses/cpp-0020/CMakeLists.txt
Executable file
7
0020-Valid-Parentheses/cpp-0020/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0020)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main.cpp)
|
||||
add_executable(cpp_0020 ${SOURCE_FILES})
|
64
0020-Valid-Parentheses/cpp-0020/main.cpp
Executable file
64
0020-Valid-Parentheses/cpp-0020/main.cpp
Executable file
@ -0,0 +1,64 @@
|
||||
/// Source : https://leetcode.com/problems/valid-parentheses/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Using Stack
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(n)
|
||||
class Solution {
|
||||
public:
|
||||
bool isValid(string s) {
|
||||
|
||||
stack<char> stack;
|
||||
for( int i = 0 ; i < s.size() ; i ++ )
|
||||
if( s[i] == '(' || s[i] == '{' || s[i] == '[')
|
||||
stack.push(s[i]);
|
||||
else{
|
||||
|
||||
if( stack.size() == 0 )
|
||||
return false;
|
||||
|
||||
char c = stack.top();
|
||||
stack.pop();
|
||||
|
||||
char match;
|
||||
if( s[i] == ')' )
|
||||
match = '(';
|
||||
else if( s[i] == ']' )
|
||||
match = '[';
|
||||
else{
|
||||
assert( s[i] == '}' );
|
||||
match = '{';
|
||||
}
|
||||
|
||||
if(c != match)
|
||||
return false;
|
||||
}
|
||||
|
||||
if( stack.size() != 0 )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void printBool(bool res){
|
||||
cout << (res ? "True" : "False") << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
printBool(Solution().isValid("()"));
|
||||
printBool(Solution().isValid("()[]{}"));
|
||||
printBool(Solution().isValid("(]"));
|
||||
printBool(Solution().isValid("([)]"));
|
||||
|
||||
return 0;
|
||||
}
|
6
0020-Valid-Parentheses/java-0020/src/Main.java
Executable file
6
0020-Valid-Parentheses/java-0020/src/Main.java
Executable file
@ -0,0 +1,6 @@
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// write your code here
|
||||
}
|
||||
}
|
56
0020-Valid-Parentheses/java-0020/src/Solution.java
Executable file
56
0020-Valid-Parentheses/java-0020/src/Solution.java
Executable file
@ -0,0 +1,56 @@
|
||||
/// Source : https://leetcode.com/problems/valid-parentheses/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
// Using Stack
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(n)
|
||||
public class Solution {
|
||||
|
||||
public boolean isValid(String s) {
|
||||
|
||||
Stack<Character> stack = new Stack<Character>();
|
||||
for( int i = 0 ; i < s.length() ; i ++ )
|
||||
if( s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[')
|
||||
stack.push(s.charAt(i));
|
||||
else{
|
||||
|
||||
if( stack.size() == 0 )
|
||||
return false;
|
||||
|
||||
Character c = stack.pop();
|
||||
|
||||
Character match;
|
||||
if( s.charAt(i) == ')' )
|
||||
match = '(';
|
||||
else if( s.charAt(i) == ']' )
|
||||
match = '[';
|
||||
else{
|
||||
assert s.charAt(i) == '}';
|
||||
match = '{';
|
||||
}
|
||||
|
||||
if(c != match)
|
||||
return false;
|
||||
}
|
||||
|
||||
if( stack.size() != 0 )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void printBool(boolean b){
|
||||
System.out.println(b ? "True" : "False");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
printBool((new Solution()).isValid("()"));
|
||||
printBool((new Solution()).isValid("()[]{}"));
|
||||
printBool((new Solution()).isValid("(]"));
|
||||
printBool((new Solution()).isValid("([)]"));
|
||||
}
|
||||
}
|
7
0024-Swap-Nodes-in-Pairs/cpp-0024/CMakeLists.txt
Executable file
7
0024-Swap-Nodes-in-Pairs/cpp-0024/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0024)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main.cpp)
|
||||
add_executable(cpp_0024 ${SOURCE_FILES})
|
105
0024-Swap-Nodes-in-Pairs/cpp-0024/main.cpp
Executable file
105
0024-Swap-Nodes-in-Pairs/cpp-0024/main.cpp
Executable file
@ -0,0 +1,105 @@
|
||||
/// Source : https://leetcode.com/problems/swap-nodes-in-pairs/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for singly-linked list.
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(NULL) {}
|
||||
};
|
||||
|
||||
/// LinkedList Test Helper Functions
|
||||
ListNode* createLinkedList(int arr[], int n){
|
||||
|
||||
if(n == 0)
|
||||
return NULL;
|
||||
|
||||
ListNode* head = new ListNode(arr[0]);
|
||||
ListNode* curNode = head;
|
||||
for(int i = 1 ; i < n ; i ++){
|
||||
curNode->next = new ListNode(arr[i]);
|
||||
curNode = curNode->next;
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
void printLinkedList(ListNode* head){
|
||||
|
||||
if(head == NULL){
|
||||
cout<<"NULL"<<endl;
|
||||
return;
|
||||
}
|
||||
|
||||
ListNode* curNode = head;
|
||||
while(curNode != NULL){
|
||||
cout << curNode->val;
|
||||
if(curNode->next != NULL)
|
||||
cout << " -> ";
|
||||
curNode = curNode->next;
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void deleteLinkedList(ListNode* head){
|
||||
|
||||
ListNode* curNode = head;
|
||||
while(curNode != NULL){
|
||||
ListNode* delNode = curNode;
|
||||
curNode = curNode->next;
|
||||
delete delNode;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(1)
|
||||
class Solution {
|
||||
public:
|
||||
ListNode* swapPairs(ListNode* head) {
|
||||
|
||||
ListNode* dummyHead = new ListNode(0);
|
||||
dummyHead->next = head;
|
||||
|
||||
ListNode* p = dummyHead;
|
||||
while(p->next && p->next->next){
|
||||
ListNode* node1 = p->next;
|
||||
ListNode* node2 = node1->next;
|
||||
ListNode* next = node2->next;
|
||||
node2->next = node1;
|
||||
node1->next = next;
|
||||
p->next = node2;
|
||||
p = node1;
|
||||
}
|
||||
|
||||
ListNode* retHead = dummyHead->next;
|
||||
delete dummyHead;
|
||||
|
||||
return retHead;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
int arr[] = {1, 2, 3, 4};
|
||||
int n = sizeof(arr) / sizeof(int);
|
||||
|
||||
ListNode* head = createLinkedList(arr, n);
|
||||
printLinkedList(head);
|
||||
|
||||
head = Solution().swapPairs(head);
|
||||
printLinkedList(head);
|
||||
|
||||
deleteLinkedList(head);
|
||||
|
||||
return 0;
|
||||
}
|
36
0024-Swap-Nodes-in-Pairs/java-0024/src/ListNode.java
Executable file
36
0024-Swap-Nodes-in-Pairs/java-0024/src/ListNode.java
Executable file
@ -0,0 +1,36 @@
|
||||
public class ListNode {
|
||||
|
||||
public int val;
|
||||
public ListNode next = null;
|
||||
|
||||
public ListNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
public ListNode (int[] arr){
|
||||
|
||||
if(arr == null || arr.length == 0)
|
||||
throw new IllegalArgumentException("arr can not be empty");
|
||||
|
||||
this.val = arr[0];
|
||||
ListNode curNode = this;
|
||||
for(int i = 1 ; i < arr.length ; i ++){
|
||||
curNode.next = new ListNode(arr[i]);
|
||||
curNode = curNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
|
||||
StringBuilder s = new StringBuilder("");
|
||||
ListNode curNode = this;
|
||||
while(curNode != null){
|
||||
s.append(Integer.toString(curNode.val));
|
||||
s.append(" -> ");
|
||||
curNode = curNode.next;
|
||||
}
|
||||
s.append("NULL");
|
||||
return s.toString();
|
||||
}
|
||||
}
|
38
0024-Swap-Nodes-in-Pairs/java-0024/src/Solution.java
Executable file
38
0024-Swap-Nodes-in-Pairs/java-0024/src/Solution.java
Executable file
@ -0,0 +1,38 @@
|
||||
/// Source : https://leetcode.com/problems/swap-nodes-in-pairs/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(1)
|
||||
public class Solution {
|
||||
|
||||
public ListNode swapPairs(ListNode head) {
|
||||
|
||||
ListNode dummyHead = new ListNode(0);
|
||||
dummyHead.next = head;
|
||||
|
||||
ListNode p = dummyHead;
|
||||
while(p.next != null && p.next.next != null ){
|
||||
ListNode node1 = p.next;
|
||||
ListNode node2 = node1.next;
|
||||
ListNode next = node2.next;
|
||||
node2.next = node1;
|
||||
node1.next = next;
|
||||
p.next = node2;
|
||||
p = node1;
|
||||
}
|
||||
|
||||
return dummyHead.next;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] arr = {1, 2, 3, 4};
|
||||
|
||||
ListNode head = new ListNode(arr);
|
||||
System.out.println(head);
|
||||
|
||||
head = (new Solution()).swapPairs(head);
|
||||
System.out.println(head);
|
||||
}
|
||||
}
|
7
0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/CMakeLists.txt
Executable file
7
0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_Remove_Duplicates_from_Sorted_Array)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main.cpp)
|
||||
add_executable(cpp_Remove_Duplicates_from_Sorted_Array ${SOURCE_FILES})
|
51
0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/main.cpp
Executable file
51
0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/main.cpp
Executable file
@ -0,0 +1,51 @@
|
||||
/// Source : https://leetcode.com/problems/remove-duplicates-from-sorted-array/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2016-12-06
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Two pointers
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(1)
|
||||
class Solution {
|
||||
public:
|
||||
int removeDuplicates(vector<int>& nums) {
|
||||
|
||||
if(nums.size() == 0)
|
||||
return 0;
|
||||
|
||||
int res = 1;
|
||||
int index = nextDifferentCharacterIndex(nums, 1);
|
||||
int i = 1;
|
||||
while(index < nums.size()){
|
||||
res ++;
|
||||
nums[i++] = nums[index];
|
||||
index = nextDifferentCharacterIndex(nums, index + 1);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
int nextDifferentCharacterIndex(const vector<int> &nums, int p){
|
||||
for( ; p < nums.size() ; p ++ )
|
||||
if( nums[p] != nums[p - 1] )
|
||||
break;
|
||||
return p;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
vector<int> nums1 = {1, 1, 2};
|
||||
cout << Solution().removeDuplicates(nums1) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
7
0075-Sort-Colors/cpp-0075/CMakeLists.txt
Executable file
7
0075-Sort-Colors/cpp-0075/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0075)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main2.cpp)
|
||||
add_executable(cpp_0075 ${SOURCE_FILES})
|
52
0075-Sort-Colors/cpp-0075/main.cpp
Executable file
52
0075-Sort-Colors/cpp-0075/main.cpp
Executable file
@ -0,0 +1,52 @@
|
||||
/// Source : https://leetcode.com/problems/sort-colors/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-13
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Counting
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(3)
|
||||
class Solution {
|
||||
public:
|
||||
void sortColors(vector<int> &nums) {
|
||||
|
||||
int count[3] = {0};
|
||||
for(int i = 0 ; i < nums.size() ; i ++){
|
||||
assert(nums[i] >= 0 && nums[i] <= 2);
|
||||
count[nums[i]] ++;
|
||||
}
|
||||
|
||||
int index = 0;
|
||||
for(int i = 0 ; i < count[0] ; i ++)
|
||||
nums[index++] = 0;
|
||||
for(int i = 0 ; i < count[1] ; i ++)
|
||||
nums[index++] = 1;
|
||||
for(int i = 0 ; i < count[2] ; i ++)
|
||||
nums[index++] = 2;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void printArr(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
vector<int> vec1 = {2, 2, 2, 1, 1, 0};
|
||||
Solution().sortColors(vec1);
|
||||
printArr(vec1);
|
||||
|
||||
vector<int> vec2 = {2};
|
||||
Solution().sortColors(vec2);
|
||||
printArr(vec2);
|
||||
|
||||
return 0;
|
||||
}
|
51
0075-Sort-Colors/cpp-0075/main2.cpp
Executable file
51
0075-Sort-Colors/cpp-0075/main2.cpp
Executable file
@ -0,0 +1,51 @@
|
||||
/// Source : https://leetcode.com/problems/sort-colors/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-13
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Three Way Quick Sort
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(1)
|
||||
class Solution {
|
||||
public:
|
||||
void sortColors(vector<int> &nums) {
|
||||
|
||||
int zero = -1; // [0...zero] == 0
|
||||
int two = nums.size(); // [two...n-1] == 2
|
||||
for(int i = 0 ; i < two ; ){
|
||||
if(nums[i] == 1)
|
||||
i ++;
|
||||
else if (nums[i] == 2)
|
||||
swap( nums[i] , nums[--two]);
|
||||
else{ // nums[i] == 0
|
||||
assert(nums[i] == 0);
|
||||
swap(nums[++zero] , nums[i++]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void printArr(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
vector<int> vec1 = {2, 2, 2, 1, 1, 0};
|
||||
Solution().sortColors(vec1);
|
||||
printArr(vec1);
|
||||
|
||||
vector<int> vec2 = {2};
|
||||
Solution().sortColors(vec2);
|
||||
printArr(vec2);
|
||||
|
||||
return 0;
|
||||
}
|
39
0075-Sort-Colors/java-0075/src/Solution1.java
Executable file
39
0075-Sort-Colors/java-0075/src/Solution1.java
Executable file
@ -0,0 +1,39 @@
|
||||
/// Source : https://leetcode.com/problems/sort-colors/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-13
|
||||
|
||||
// Counting
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(3)
|
||||
public class Solution1 {
|
||||
|
||||
public void sortColors(int[] nums) {
|
||||
|
||||
int[] count = {0, 0, 0};
|
||||
for(int i = 0 ; i < nums.length ; i ++){
|
||||
assert nums[i] >= 0 && nums[i] <= 2;
|
||||
count[nums[i]] ++;
|
||||
}
|
||||
|
||||
int index = 0;
|
||||
for(int i = 0 ; i < count[0] ; i ++)
|
||||
nums[index++] = 0;
|
||||
for(int i = 0 ; i < count[1] ; i ++)
|
||||
nums[index++] = 1;
|
||||
for(int i = 0 ; i < count[2] ; i ++)
|
||||
nums[index++] = 2;
|
||||
}
|
||||
|
||||
public static void printArr(int[] nums){
|
||||
for(int num: nums)
|
||||
System.out.print(num + " ");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] nums = {2, 2, 2, 1, 1, 0};
|
||||
(new Solution1()).sortColors(nums);
|
||||
printArr(nums);
|
||||
}
|
||||
}
|
44
0075-Sort-Colors/java-0075/src/Solution2.java
Executable file
44
0075-Sort-Colors/java-0075/src/Solution2.java
Executable file
@ -0,0 +1,44 @@
|
||||
/// Source : https://leetcode.com/problems/sort-colors/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-13
|
||||
|
||||
// Three Way Quick Sort
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(1)
|
||||
public class Solution2 {
|
||||
|
||||
public void sortColors(int[] nums) {
|
||||
|
||||
int zero = -1; // [0...zero] == 0
|
||||
int two = nums.length; // [two...n-1] == 2
|
||||
for(int i = 0 ; i < two ; ){
|
||||
if(nums[i] == 1)
|
||||
i ++;
|
||||
else if (nums[i] == 2)
|
||||
swap(nums, i, --two);
|
||||
else{ // nums[i] == 0
|
||||
assert nums[i] == 0;
|
||||
swap(nums, ++zero, i++);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void swap(int[] nums, int i, int j){
|
||||
int t = nums[i];
|
||||
nums[i]= nums[j];
|
||||
nums[j] = t;
|
||||
}
|
||||
|
||||
public static void printArr(int[] nums){
|
||||
for(int num: nums)
|
||||
System.out.print(num + " ");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] nums = {2, 2, 2, 1, 1, 0};
|
||||
(new Solution2()).sortColors(nums);
|
||||
printArr(nums);
|
||||
}
|
||||
}
|
7
0086-Partition-List/cpp-0086/CMakeLists.txt
Executable file
7
0086-Partition-List/cpp-0086/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0086)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main.cpp)
|
||||
add_executable(cpp_0086 ${SOURCE_FILES})
|
56
0086-Partition-List/cpp-0086/main.cpp
Executable file
56
0086-Partition-List/cpp-0086/main.cpp
Executable file
@ -0,0 +1,56 @@
|
||||
/// Source : https://leetcode.com/problems/partition-list/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-07-07
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for singly-linked list.
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(NULL) {}
|
||||
};
|
||||
|
||||
/// Linear Scan
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(1)
|
||||
class Solution {
|
||||
public:
|
||||
ListNode* partition(ListNode* head, int x) {
|
||||
|
||||
ListNode* dummyHead1 = new ListNode(-1);
|
||||
ListNode* dummyHead2 = new ListNode(-1);
|
||||
ListNode* prev1 = dummyHead1;
|
||||
ListNode* prev2 = dummyHead2;
|
||||
|
||||
for(ListNode* cur = head ; cur != NULL ;){
|
||||
if(cur->val < x){
|
||||
prev1->next = cur;
|
||||
cur = cur->next;
|
||||
prev1 = prev1->next;
|
||||
prev1->next = NULL;
|
||||
}
|
||||
else{
|
||||
prev2->next = cur;
|
||||
cur = cur->next;
|
||||
prev2 = prev2->next;
|
||||
prev2->next = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
prev1->next = dummyHead2->next;
|
||||
ListNode* ret = dummyHead1->next;
|
||||
|
||||
delete dummyHead1;
|
||||
delete dummyHead2;
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
7
0092-Reverse-Linked-List-II/cpp-0092/CMakeLists.txt
Executable file
7
0092-Reverse-Linked-List-II/cpp-0092/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0092)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main.cpp)
|
||||
add_executable(cpp_0092 ${SOURCE_FILES})
|
62
0092-Reverse-Linked-List-II/cpp-0092/main.cpp
Executable file
62
0092-Reverse-Linked-List-II/cpp-0092/main.cpp
Executable file
@ -0,0 +1,62 @@
|
||||
/// Source : https://leetcode.com/problems/reverse-linked-list-ii/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-10-02
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
/// Recursive
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(n)
|
||||
|
||||
/// Definition for singly-linked list.
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(NULL) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
ListNode* reverseBetween(ListNode* head, int m, int n) {
|
||||
|
||||
ListNode* dummyHead = new ListNode(-1);
|
||||
dummyHead->next = head;
|
||||
|
||||
ListNode* pre = dummyHead;
|
||||
for(int i = 0; i < m - 1; i ++){
|
||||
pre = pre->next
|
||||
}
|
||||
|
||||
ListNode* tail = pre->next;
|
||||
ListNode* left;
|
||||
pre->next = reverse(pre->next, n - m, left);
|
||||
tail->next = left;
|
||||
|
||||
ListNode* ret = dummyHead->next;
|
||||
delete dummyHead;
|
||||
return ret;
|
||||
}
|
||||
|
||||
private:
|
||||
ListNode* reverse(ListNode* head, int index, ListNode* &left){
|
||||
|
||||
if(index == 0){
|
||||
left = head->next;
|
||||
return head;
|
||||
}
|
||||
|
||||
ListNode* tail = head->next;
|
||||
ListNode* ret = reverse(head->next, index - 1, left);
|
||||
tail->next = head;
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
7
0094-Binary-Tree-Inorder-Traversal/cpp-0094/CMakeLists.txt
Executable file
7
0094-Binary-Tree-Inorder-Traversal/cpp-0094/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0094)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main3.cpp)
|
||||
add_executable(cpp_0094 ${SOURCE_FILES})
|
45
0094-Binary-Tree-Inorder-Traversal/cpp-0094/main.cpp
Executable file
45
0094-Binary-Tree-Inorder-Traversal/cpp-0094/main.cpp
Executable file
@ -0,0 +1,45 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
// Recursive
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> inorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
__inorderTraversal(root, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
void __inorderTraversal(TreeNode* node, vector<int> &res){
|
||||
|
||||
if( node ){
|
||||
__inorderTraversal(node->left, res);
|
||||
res.push_back( node->val );
|
||||
__inorderTraversal(node->right, res);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
64
0094-Binary-Tree-Inorder-Traversal/cpp-0094/main2.cpp
Executable file
64
0094-Binary-Tree-Inorder-Traversal/cpp-0094/main2.cpp
Executable file
@ -0,0 +1,64 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
// My Non-Recursive
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
class Solution {
|
||||
|
||||
private:
|
||||
struct Command{
|
||||
string s; // go, print
|
||||
TreeNode* node;
|
||||
Command(string s, TreeNode* node): s(s), node(node){}
|
||||
};
|
||||
|
||||
public:
|
||||
vector<int> inorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if( root == NULL )
|
||||
return res;
|
||||
|
||||
stack<Command> stack;
|
||||
stack.push(Command("go", root));
|
||||
while( !stack.empty() ){
|
||||
Command command = stack.top();
|
||||
stack.pop();
|
||||
|
||||
if(command.s == "print")
|
||||
res.push_back(command.node->val);
|
||||
else{
|
||||
assert(command.s == "go");
|
||||
if(command.node->right)
|
||||
stack.push(Command("go",command.node->right));
|
||||
stack.push(Command("print", command.node));
|
||||
if(command.node->left)
|
||||
stack.push(Command("go",command.node->left));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
55
0094-Binary-Tree-Inorder-Traversal/cpp-0094/main3.cpp
Executable file
55
0094-Binary-Tree-Inorder-Traversal/cpp-0094/main3.cpp
Executable file
@ -0,0 +1,55 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-05-30
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
// Classic Non-Recursive algorithm for inorder traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> inorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if( root == NULL )
|
||||
return res;
|
||||
|
||||
stack<TreeNode*> stack;
|
||||
TreeNode* cur = root;
|
||||
while(cur != NULL || !stack.empty()){
|
||||
|
||||
while(cur != NULL){
|
||||
stack.push(cur);
|
||||
cur = cur->left;
|
||||
}
|
||||
|
||||
cur = stack.top();
|
||||
stack.pop();
|
||||
res.push_back(cur->val);
|
||||
cur = cur->right;
|
||||
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
55
0094-Binary-Tree-Inorder-Traversal/cpp-0094/main4.cpp
Executable file
55
0094-Binary-Tree-Inorder-Traversal/cpp-0094/main4.cpp
Executable file
@ -0,0 +1,55 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-05-30
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
// Another Classic Non-Recursive algorithm for inorder traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> inorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if( root == NULL )
|
||||
return res;
|
||||
|
||||
stack<TreeNode*> stack;
|
||||
TreeNode* cur = root;
|
||||
while(cur != NULL || !stack.empty()){
|
||||
|
||||
if(cur != NULL){
|
||||
stack.push(cur);
|
||||
cur = cur->left;
|
||||
}
|
||||
else {
|
||||
cur = stack.top();
|
||||
stack.pop();
|
||||
res.push_back(cur->val);
|
||||
cur = cur->right;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
64
0094-Binary-Tree-Inorder-Traversal/cpp-0094/main5.cpp
Executable file
64
0094-Binary-Tree-Inorder-Traversal/cpp-0094/main5.cpp
Executable file
@ -0,0 +1,64 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
// InOrder Morris Traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(1)
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> inorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if( root == NULL )
|
||||
return res;
|
||||
|
||||
TreeNode* cur = root;
|
||||
while(cur != NULL){
|
||||
|
||||
if(cur->left == NULL){
|
||||
res.push_back(cur->val);
|
||||
cur = cur->right;
|
||||
}
|
||||
else{
|
||||
TreeNode* prev = cur->left;
|
||||
while(prev->right != NULL && prev->right != cur)
|
||||
prev = prev->right;
|
||||
|
||||
if(prev->right == NULL){
|
||||
prev->right = cur;
|
||||
cur = cur->left;
|
||||
}
|
||||
else{
|
||||
prev->right = NULL;
|
||||
res.push_back(cur->val);
|
||||
cur = cur->right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
27
0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution1.java
Executable file
27
0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution1.java
Executable file
@ -0,0 +1,27 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
// Recursive
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
public class Solution1 {
|
||||
|
||||
public List<Integer> inorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
inorderTraversal(root, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void inorderTraversal(TreeNode node, List<Integer> list){
|
||||
if(node != null){
|
||||
inorderTraversal(node.left, list);
|
||||
list.add(node.val);
|
||||
inorderTraversal(node.right, list);
|
||||
}
|
||||
}
|
||||
}
|
48
0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution2.java
Executable file
48
0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution2.java
Executable file
@ -0,0 +1,48 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// My Non-Recursive
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
public class Solution2 {
|
||||
|
||||
private class Command{
|
||||
String s; // go, print
|
||||
TreeNode node;
|
||||
Command(String s, TreeNode node){
|
||||
this.s = s;
|
||||
this.node = node;
|
||||
}
|
||||
};
|
||||
|
||||
public List<Integer> inorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<Command> stack = new Stack<Command>();
|
||||
stack.push(new Command("go", root));
|
||||
while(!stack.empty()){
|
||||
Command command = stack.pop();
|
||||
|
||||
if(command.s.equals("print"))
|
||||
res.add(command.node.val);
|
||||
else{
|
||||
assert command.s.equals("go");
|
||||
if(command.node.right != null)
|
||||
stack.push(new Command("go",command.node.right));
|
||||
stack.push(new Command("print", command.node));
|
||||
if(command.node.left != null)
|
||||
stack.push(new Command("go",command.node.left));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
35
0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution3.java
Executable file
35
0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution3.java
Executable file
@ -0,0 +1,35 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// Classic Non-Recursive algorithm for inorder traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
public class Solution3 {
|
||||
|
||||
public List<Integer> inorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
TreeNode cur = root;
|
||||
while(cur != null || !stack.empty()){
|
||||
|
||||
while(cur != null){
|
||||
stack.push(cur);
|
||||
cur = cur.left;
|
||||
}
|
||||
|
||||
cur = stack.pop();
|
||||
res.add(cur.val);
|
||||
cur = cur.right;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
36
0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution4.java
Executable file
36
0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution4.java
Executable file
@ -0,0 +1,36 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// Another Classic Non-Recursive algorithm for inorder traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
public class Solution4 {
|
||||
|
||||
public List<Integer> inorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
TreeNode cur = root;
|
||||
while(cur != null || !stack.empty()){
|
||||
|
||||
if(cur != null){
|
||||
stack.push(cur);
|
||||
cur = cur.left;
|
||||
}
|
||||
else{
|
||||
cur = stack.pop();
|
||||
res.add(cur.val);
|
||||
cur = cur.right;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
45
0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution5.java
Executable file
45
0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution5.java
Executable file
@ -0,0 +1,45 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// Inorder Morris Traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(1)
|
||||
public class Solution5 {
|
||||
|
||||
public List<Integer> inorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
TreeNode cur = root;
|
||||
while(cur != null){
|
||||
|
||||
if(cur.left == null){
|
||||
res.add(cur.val);
|
||||
cur = cur.right;
|
||||
}
|
||||
else{
|
||||
TreeNode prev = cur.left;
|
||||
while(prev.right != null && prev.right != cur)
|
||||
prev = prev.right;
|
||||
|
||||
if(prev.right == null){
|
||||
prev.right = cur;
|
||||
cur = cur.left;
|
||||
}
|
||||
else{
|
||||
prev.right = null;
|
||||
res.add(cur.val);
|
||||
cur = cur.right;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
7
0094-Binary-Tree-Inorder-Traversal/java-0094/src/TreeNode.java
Executable file
7
0094-Binary-Tree-Inorder-Traversal/java-0094/src/TreeNode.java
Executable file
@ -0,0 +1,7 @@
|
||||
// Definition for a binary tree node.
|
||||
public class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
TreeNode(int x) { val = x; }
|
||||
}
|
7
0102-Binary-Tree-Level-Order-Traversal/cpp-0102/CMakeLists.txt
Executable file
7
0102-Binary-Tree-Level-Order-Traversal/cpp-0102/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0102)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main2.cpp)
|
||||
add_executable(cpp_0102 ${SOURCE_FILES})
|
59
0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main.cpp
Executable file
59
0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main.cpp
Executable file
@ -0,0 +1,59 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
/// BFS
|
||||
/// Time Complexity: O(n), where n is the number of nodes in the tree
|
||||
/// Space Complexity: O(n)
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> levelOrder(TreeNode* root) {
|
||||
|
||||
vector<vector<int>> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
queue<pair<TreeNode*,int>> q;
|
||||
q.push(make_pair(root, 0));
|
||||
|
||||
while(!q.empty()){
|
||||
|
||||
TreeNode* node = q.front().first;
|
||||
int level = q.front().second;
|
||||
q.pop();
|
||||
|
||||
if(level == res.size())
|
||||
res.push_back(vector<int>());
|
||||
assert( level < res.size() );
|
||||
|
||||
res[level].push_back(node->val);
|
||||
if(node->left)
|
||||
q.push(make_pair(node->left, level + 1 ));
|
||||
if(node->right)
|
||||
q.push(make_pair(node->right, level + 1 ));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
68
0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main2.cpp
Executable file
68
0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main2.cpp
Executable file
@ -0,0 +1,68 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-10-16
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
/// BFS
|
||||
/// No need to store level information in the queue :-)
|
||||
///
|
||||
/// Time Complexity: O(n), where n is the number of nodes in the tree
|
||||
/// Space Complexity: O(n)
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> levelOrder(TreeNode* root) {
|
||||
|
||||
vector<vector<int>> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
queue<TreeNode*> q;
|
||||
q.push(root);
|
||||
int level_num = 1;
|
||||
|
||||
while(!q.empty()){
|
||||
|
||||
int new_level_num = 0;
|
||||
vector<int> level;
|
||||
for(int i = 0; i < level_num; i ++){
|
||||
TreeNode* node = q.front();
|
||||
q.pop();
|
||||
level.push_back(node->val);
|
||||
|
||||
if(node->left){
|
||||
q.push(node->left);
|
||||
new_level_num ++;
|
||||
}
|
||||
if(node->right){
|
||||
q.push(node->right);
|
||||
new_level_num ++;
|
||||
}
|
||||
}
|
||||
|
||||
res.push_back(level);
|
||||
level_num = new_level_num;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
43
0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution.java
Executable file
43
0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution.java
Executable file
@ -0,0 +1,43 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
import javafx.util.Pair;
|
||||
|
||||
/// BFS
|
||||
/// Time Complexity: O(n), where n is the number of nodes in the tree
|
||||
/// Space Complexity: O(n)
|
||||
class Solution {
|
||||
|
||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||
|
||||
ArrayList<List<Integer>> res = new ArrayList<>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
LinkedList<Pair<TreeNode, Integer>> queue = new LinkedList<>();
|
||||
queue.addLast(new Pair<>(root, 0));
|
||||
|
||||
while(!queue.isEmpty()){
|
||||
|
||||
Pair<TreeNode, Integer> front = queue.removeFirst();
|
||||
TreeNode node = front.getKey();
|
||||
int level = front.getValue();
|
||||
|
||||
if(level == res.size())
|
||||
res.add(new ArrayList<>());
|
||||
assert level < res.size();
|
||||
|
||||
res.get(level).add(node.val);
|
||||
if(node.left != null)
|
||||
queue.addLast(new Pair<>(node.left, level + 1));
|
||||
if(node.right != null)
|
||||
queue.addLast(new Pair<>(node.right, level + 1));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
51
0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution2.java
Executable file
51
0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution2.java
Executable file
@ -0,0 +1,51 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-10-16
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
/// BFS
|
||||
/// No need to store level information in the queue :-)
|
||||
///
|
||||
/// Time Complexity: O(n), where n is the number of nodes in the tree
|
||||
/// Space Complexity: O(n)
|
||||
class Solution2 {
|
||||
|
||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||
|
||||
ArrayList<List<Integer>> res = new ArrayList<>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
int levelNum = 1;
|
||||
|
||||
while(!queue.isEmpty()){
|
||||
|
||||
int newLevelNum = 0;
|
||||
ArrayList<Integer> level = new ArrayList<>();
|
||||
for(int i = 0; i < levelNum; i ++){
|
||||
TreeNode node = queue.remove();
|
||||
level.add(node.val);
|
||||
|
||||
if(node.left != null){
|
||||
queue.add(node.left);
|
||||
newLevelNum ++;
|
||||
}
|
||||
if(node.right != null){
|
||||
queue.add(node.right);
|
||||
newLevelNum ++;
|
||||
}
|
||||
}
|
||||
|
||||
res.add(level);
|
||||
levelNum = newLevelNum;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
7
0102-Binary-Tree-Level-Order-Traversal/java-0102/src/TreeNode.java
Executable file
7
0102-Binary-Tree-Level-Order-Traversal/java-0102/src/TreeNode.java
Executable file
@ -0,0 +1,7 @@
|
||||
// Definition for a binary tree node.
|
||||
public class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
TreeNode(int x) { val = x; }
|
||||
}
|
7
0144-Binary-Tree-Preorder-Traversal/cpp-0144/CMakeLists.txt
Executable file
7
0144-Binary-Tree-Preorder-Traversal/cpp-0144/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0144)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main5.cpp)
|
||||
add_executable(cpp_0144 ${SOURCE_FILES})
|
45
0144-Binary-Tree-Preorder-Traversal/cpp-0144/main.cpp
Executable file
45
0144-Binary-Tree-Preorder-Traversal/cpp-0144/main.cpp
Executable file
@ -0,0 +1,45 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
// Recursive
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> preorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
preorderTraversal(root, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
void preorderTraversal(TreeNode* node, vector<int> &res){
|
||||
|
||||
if(node){
|
||||
res.push_back(node->val);
|
||||
preorderTraversal(node->left, res);
|
||||
preorderTraversal(node->right, res);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
63
0144-Binary-Tree-Preorder-Traversal/cpp-0144/main2.cpp
Executable file
63
0144-Binary-Tree-Preorder-Traversal/cpp-0144/main2.cpp
Executable file
@ -0,0 +1,63 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
// My Non-Recursive
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
class Solution {
|
||||
|
||||
private:
|
||||
struct Command{
|
||||
string s; // go, print
|
||||
TreeNode* node;
|
||||
Command(string s, TreeNode* node): s(s), node(node){}
|
||||
};
|
||||
|
||||
public:
|
||||
vector<int> preorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
stack<Command> stack;
|
||||
stack.push(Command("go", root));
|
||||
while(!stack.empty()){
|
||||
Command command = stack.top();
|
||||
stack.pop();
|
||||
|
||||
if(command.s == "print")
|
||||
res.push_back(command.node->val);
|
||||
else{
|
||||
assert(command.s == "go");
|
||||
if(command.node->right)
|
||||
stack.push(Command("go",command.node->right));
|
||||
if(command.node->left)
|
||||
stack.push(Command("go",command.node->left));
|
||||
stack.push(Command("print", command.node));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
51
0144-Binary-Tree-Preorder-Traversal/cpp-0144/main3.cpp
Executable file
51
0144-Binary-Tree-Preorder-Traversal/cpp-0144/main3.cpp
Executable file
@ -0,0 +1,51 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
// Classic Non-Recursive algorithm for preorder traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> preorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
stack<TreeNode*> stack;
|
||||
stack.push(root);
|
||||
while(!stack.empty()){
|
||||
TreeNode* curNode = stack.top();
|
||||
stack.pop();
|
||||
res.push_back(curNode->val);
|
||||
|
||||
if(curNode->right)
|
||||
stack.push(curNode->right);
|
||||
if(curNode->left)
|
||||
stack.push(curNode->left);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
65
0144-Binary-Tree-Preorder-Traversal/cpp-0144/main4.cpp
Executable file
65
0144-Binary-Tree-Preorder-Traversal/cpp-0144/main4.cpp
Executable file
@ -0,0 +1,65 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
// Another Classic Non-Recursive algorithm for preorder traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> preorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
stack<TreeNode*> stack;
|
||||
TreeNode* cur = root;
|
||||
while(cur != NULL || !stack.empty()){
|
||||
while(cur != NULL){
|
||||
res.push_back(cur->val);
|
||||
stack.push(cur);
|
||||
cur = cur->left;
|
||||
}
|
||||
|
||||
cur = stack.top();
|
||||
stack.pop();
|
||||
cur = cur->right;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void print_vec(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
TreeNode* root = new TreeNode(1);
|
||||
root->right = new TreeNode(2);
|
||||
root->right->left = new TreeNode(3);
|
||||
vector<int> res = Solution().preorderTraversal(root);
|
||||
print_vec(res);
|
||||
|
||||
return 0;
|
||||
}
|
66
0144-Binary-Tree-Preorder-Traversal/cpp-0144/main5.cpp
Executable file
66
0144-Binary-Tree-Preorder-Traversal/cpp-0144/main5.cpp
Executable file
@ -0,0 +1,66 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
// Another Classic Non-Recursive algorithm for preorder traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> preorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
stack<TreeNode*> stack;
|
||||
TreeNode* cur = root;
|
||||
while(cur != NULL || !stack.empty()){
|
||||
if(cur != NULL){
|
||||
res.push_back(cur->val);
|
||||
stack.push(cur);
|
||||
cur = cur->left;
|
||||
}
|
||||
else{
|
||||
cur = stack.top();
|
||||
stack.pop();
|
||||
cur = cur->right;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void print_vec(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
TreeNode* root = new TreeNode(1);
|
||||
root->right = new TreeNode(2);
|
||||
root->right->left = new TreeNode(3);
|
||||
vector<int> res = Solution().preorderTraversal(root);
|
||||
print_vec(res);
|
||||
|
||||
return 0;
|
||||
}
|
62
0144-Binary-Tree-Preorder-Traversal/cpp-0144/main6.cpp
Executable file
62
0144-Binary-Tree-Preorder-Traversal/cpp-0144/main6.cpp
Executable file
@ -0,0 +1,62 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-29
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
// PreOrder Morris Traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(1)
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> preorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
TreeNode* cur = root;
|
||||
while(cur != NULL){
|
||||
if(cur->left == NULL){
|
||||
res.push_back(cur->val);
|
||||
cur = cur->right;
|
||||
}
|
||||
else{
|
||||
TreeNode* prev = cur->left;
|
||||
while(prev->right != NULL && prev->right != cur)
|
||||
prev = prev->right;
|
||||
|
||||
if(prev->right == NULL){
|
||||
res.push_back(cur->val);
|
||||
prev->right = cur;
|
||||
cur = cur->left;
|
||||
}
|
||||
else{
|
||||
prev->right = NULL;
|
||||
cur = cur->right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
26
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution1.java
Executable file
26
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution1.java
Executable file
@ -0,0 +1,26 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
// Recursive
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
public class Solution1 {
|
||||
|
||||
public List<Integer> preorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
preorderTraversal(root, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void preorderTraversal(TreeNode node, List<Integer> list){
|
||||
if(node != null){
|
||||
list.add(node.val);
|
||||
preorderTraversal(node.left, list);
|
||||
preorderTraversal(node.right, list);
|
||||
}
|
||||
}
|
||||
}
|
48
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution2.java
Executable file
48
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution2.java
Executable file
@ -0,0 +1,48 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// My Non-Recursive
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
public class Solution2 {
|
||||
|
||||
private class Command{
|
||||
String s; // go, print
|
||||
TreeNode node;
|
||||
Command(String s, TreeNode node){
|
||||
this.s = s;
|
||||
this.node = node;
|
||||
}
|
||||
};
|
||||
|
||||
public List<Integer> preorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<Command> stack = new Stack<Command>();
|
||||
stack.push(new Command("go", root));
|
||||
while(!stack.empty()){
|
||||
Command command = stack.pop();
|
||||
|
||||
if(command.s.equals("print"))
|
||||
res.add(command.node.val);
|
||||
else{
|
||||
assert command.s.equals("go");
|
||||
if(command.node.right != null)
|
||||
stack.push(new Command("go",command.node.right));
|
||||
if(command.node.left != null)
|
||||
stack.push(new Command("go",command.node.left));
|
||||
stack.push(new Command("print", command.node));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
34
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution3.java
Executable file
34
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution3.java
Executable file
@ -0,0 +1,34 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// Classic Non-Recursive algorithm for preorder traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
public class Solution3 {
|
||||
|
||||
public List<Integer> preorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<TreeNode> stack = new Stack<TreeNode>();
|
||||
stack.push(root);
|
||||
while(!stack.empty()){
|
||||
TreeNode curNode = stack.pop();
|
||||
res.add(curNode.val);
|
||||
|
||||
if(curNode.right != null)
|
||||
stack.push(curNode.right);
|
||||
if(curNode.left != null)
|
||||
stack.push(curNode.left);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
34
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution4.java
Executable file
34
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution4.java
Executable file
@ -0,0 +1,34 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// Another Classic Non-Recursive algorithm for preorder traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
public class Solution4 {
|
||||
|
||||
public List<Integer> preorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<TreeNode> stack = new Stack<TreeNode>();
|
||||
TreeNode cur = root;
|
||||
while(cur != null || !stack.isEmpty()){
|
||||
while(cur != null){
|
||||
res.add(cur.val);
|
||||
stack.push(cur);
|
||||
cur = cur.left;
|
||||
}
|
||||
|
||||
cur = stack.pop();
|
||||
cur = cur.right;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
35
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution5.java
Executable file
35
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution5.java
Executable file
@ -0,0 +1,35 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// Another Classic Non-Recursive algorithm for preorder traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
public class Solution5 {
|
||||
|
||||
public List<Integer> preorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<TreeNode> stack = new Stack<TreeNode>();
|
||||
TreeNode cur = root;
|
||||
while(cur != null || !stack.isEmpty()){
|
||||
if(cur != null){
|
||||
res.add(cur.val);
|
||||
stack.push(cur);
|
||||
cur = cur.left;
|
||||
}
|
||||
else{
|
||||
cur = stack.pop();
|
||||
cur = cur.right;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
45
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution6.java
Executable file
45
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution6.java
Executable file
@ -0,0 +1,45 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-29
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// PreOrder Morris Traversal
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(1)
|
||||
public class Solution6 {
|
||||
|
||||
public List<Integer> preorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
TreeNode cur = root;
|
||||
while(cur != null){
|
||||
if(cur.left == null){
|
||||
res.add(cur.val);
|
||||
cur = cur.right;
|
||||
}
|
||||
else{
|
||||
TreeNode prev = cur.left;
|
||||
while(prev.right != null && prev.right != cur)
|
||||
prev = prev.right;
|
||||
|
||||
if(prev.right == null){
|
||||
res.add(cur.val);
|
||||
prev.right = cur;
|
||||
cur = cur.left;
|
||||
}
|
||||
else{
|
||||
prev.right = null;
|
||||
cur = cur.right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
7
0144-Binary-Tree-Preorder-Traversal/java-0144/src/TreeNode.java
Executable file
7
0144-Binary-Tree-Preorder-Traversal/java-0144/src/TreeNode.java
Executable file
@ -0,0 +1,7 @@
|
||||
// Definition for a binary tree node.
|
||||
public class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
TreeNode(int x) { val = x; }
|
||||
}
|
7
0145-Binary-Tree-Postorder-Traversal/cpp-0145/CMakeLists.txt
Executable file
7
0145-Binary-Tree-Postorder-Traversal/cpp-0145/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0145)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main8.cpp)
|
||||
add_executable(cpp_0145 ${SOURCE_FILES})
|
45
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main.cpp
Executable file
45
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main.cpp
Executable file
@ -0,0 +1,45 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
// Recursive
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
__postorderTraversal(root, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
void __postorderTraversal(TreeNode* node, vector<int> &res){
|
||||
|
||||
if( node ){
|
||||
__postorderTraversal(node->left, res);
|
||||
__postorderTraversal(node->right, res);
|
||||
res.push_back(node->val);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
63
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main2.cpp
Executable file
63
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main2.cpp
Executable file
@ -0,0 +1,63 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
// My Non-Recursive
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
class Solution {
|
||||
|
||||
private:
|
||||
struct Command{
|
||||
string s; // go, print
|
||||
TreeNode* node;
|
||||
Command(string s, TreeNode* node): s(s), node(node){}
|
||||
};
|
||||
|
||||
public:
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
stack<Command> stack;
|
||||
stack.push(Command("go", root) );
|
||||
while(!stack.empty()){
|
||||
Command command = stack.top();
|
||||
stack.pop();
|
||||
|
||||
if(command.s == "print")
|
||||
res.push_back(command.node->val);
|
||||
else{
|
||||
assert(command.s == "go");
|
||||
stack.push(Command("print", command.node));
|
||||
if(command.node->right)
|
||||
stack.push(Command("go",command.node->right));
|
||||
if(command.node->left)
|
||||
stack.push(Command("go",command.node->left));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
71
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main3.cpp
Executable file
71
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main3.cpp
Executable file
@ -0,0 +1,71 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
// Non-Recursive
|
||||
// Using a tag to record whether the node has been visited
|
||||
//
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
class Solution {
|
||||
|
||||
private:
|
||||
struct TagNode{
|
||||
TreeNode* node;
|
||||
bool isFirst;
|
||||
TagNode(TreeNode* node): node(node), isFirst(false){}
|
||||
};
|
||||
|
||||
public:
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
stack<TagNode> stack;
|
||||
TreeNode* cur = root;
|
||||
while(cur != NULL || !stack.empty()){
|
||||
|
||||
while(cur != NULL){
|
||||
stack.push(TagNode(cur));
|
||||
cur = cur->left;
|
||||
}
|
||||
|
||||
TagNode tagNode = stack.top();
|
||||
stack.pop();
|
||||
cur = tagNode.node;
|
||||
if(tagNode.isFirst == false){
|
||||
tagNode.isFirst = true;
|
||||
stack.push(tagNode);
|
||||
cur = cur->right;
|
||||
}
|
||||
else{
|
||||
res.push_back(cur->val);
|
||||
cur = NULL;
|
||||
};
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
62
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main4.cpp
Executable file
62
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main4.cpp
Executable file
@ -0,0 +1,62 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
// Non-Recursive
|
||||
// Using two stacks, Reverse the Preorder Traversal!
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(n)
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
stack<TreeNode*> stack, output;
|
||||
|
||||
stack.push(root);
|
||||
while(!stack.empty()){
|
||||
|
||||
TreeNode* node = stack.top();
|
||||
stack.pop();
|
||||
output.push(node);
|
||||
|
||||
if(node->left != NULL)
|
||||
stack.push(node->left);
|
||||
if(node->right != NULL)
|
||||
stack.push(node->right);
|
||||
}
|
||||
|
||||
while(!output.empty()){
|
||||
res.push_back((output.top())->val);
|
||||
output.pop();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
63
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main5.cpp
Executable file
63
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main5.cpp
Executable file
@ -0,0 +1,63 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
// Non-Recursive
|
||||
// Using two stacks, Reverse the Preorder Traversal!
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(n)
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
stack<TreeNode*> stack, output;
|
||||
|
||||
TreeNode* p = root;
|
||||
while(p != NULL || !stack.empty()){
|
||||
if(p != NULL){
|
||||
stack.push(p);
|
||||
output.push(p);
|
||||
p = p->right;
|
||||
}
|
||||
else{
|
||||
p = stack.top();
|
||||
stack.pop();
|
||||
p = p->left;
|
||||
}
|
||||
}
|
||||
|
||||
while(!output.empty()){
|
||||
res.push_back((output.top())->val);
|
||||
output.pop();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
78
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main6.cpp
Executable file
78
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main6.cpp
Executable file
@ -0,0 +1,78 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-31
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
// Non-Recursive
|
||||
// Using a pre pointer to record the last visted node
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(h)
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
stack<TreeNode*> stack;
|
||||
TreeNode* pre = NULL;
|
||||
|
||||
stack.push(root);
|
||||
while(!stack.empty()){
|
||||
|
||||
TreeNode* node = stack.top();
|
||||
stack.pop();
|
||||
if((node->left == NULL && node->right == NULL) ||
|
||||
(pre != NULL && pre == node->left && node->right == NULL) ||
|
||||
(pre != NULL && pre == node->right)){
|
||||
res.push_back(node->val);
|
||||
pre = node;
|
||||
}
|
||||
else{
|
||||
stack.push(node);
|
||||
|
||||
if(node->right != NULL)
|
||||
stack.push(node->right);
|
||||
if(node->left != NULL)
|
||||
stack.push(node->left);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void print_vec(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
TreeNode* root = new TreeNode(1);
|
||||
root->right = new TreeNode(2);
|
||||
root->right->left = new TreeNode(3);
|
||||
print_vec(Solution().postorderTraversal(root));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
79
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main7.cpp
Executable file
79
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main7.cpp
Executable file
@ -0,0 +1,79 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-31
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
// Classic Non-Recursive
|
||||
// Using a pre pointer to record the last visted node
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(h)
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
stack<TreeNode*> stack;
|
||||
TreeNode* pre = NULL;
|
||||
TreeNode* cur = root;
|
||||
|
||||
while(cur != NULL || !stack.empty()){
|
||||
|
||||
while(cur != NULL){
|
||||
stack.push(cur);
|
||||
cur = cur->left;
|
||||
}
|
||||
|
||||
cur = stack.top();
|
||||
stack.pop();
|
||||
|
||||
if(cur->right == NULL || pre == cur->right){
|
||||
res.push_back(cur->val);
|
||||
pre = cur;
|
||||
cur = NULL;
|
||||
}
|
||||
else{
|
||||
stack.push(cur);
|
||||
cur = cur->right;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void print_vec(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
TreeNode* root = new TreeNode(1);
|
||||
root->right = new TreeNode(2);
|
||||
root->right->left = new TreeNode(3);
|
||||
print_vec(Solution().postorderTraversal(root));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
79
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main8.cpp
Executable file
79
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main8.cpp
Executable file
@ -0,0 +1,79 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-31
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
// Classic Non-Recursive
|
||||
// Using a pre pointer to record the last visted node
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(h)
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
stack<TreeNode*> stack;
|
||||
TreeNode* pre = NULL;
|
||||
TreeNode* cur = root;
|
||||
|
||||
while(cur != NULL || !stack.empty()){
|
||||
if(cur != NULL){
|
||||
stack.push(cur);
|
||||
cur = cur->left;
|
||||
}
|
||||
else{
|
||||
cur = stack.top();
|
||||
stack.pop();
|
||||
|
||||
if(cur->right == NULL || pre == cur->right){
|
||||
res.push_back(cur->val);
|
||||
pre = cur;
|
||||
cur = NULL;
|
||||
}
|
||||
else{
|
||||
stack.push(cur);
|
||||
cur = cur->right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void print_vec(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
TreeNode* root = new TreeNode(1);
|
||||
root->right = new TreeNode(2);
|
||||
root->right->left = new TreeNode(3);
|
||||
print_vec(Solution().postorderTraversal(root));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
89
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main9.cpp
Executable file
89
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main9.cpp
Executable file
@ -0,0 +1,89 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-31
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Definition for a binary tree node.
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
// Morris PostOrder Traversal
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(1)
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
|
||||
vector<int> res;
|
||||
if(root == NULL)
|
||||
return res;
|
||||
|
||||
TreeNode* dummyRoot = new TreeNode(-1);
|
||||
dummyRoot->left = root;
|
||||
|
||||
TreeNode* cur = dummyRoot;
|
||||
while(cur != NULL){
|
||||
if(cur->left == NULL)
|
||||
cur = cur->right;
|
||||
else{
|
||||
TreeNode* prev = cur->left;
|
||||
while(prev->right != NULL && prev->right != cur)
|
||||
prev = prev->right;
|
||||
|
||||
if(prev->right == NULL){
|
||||
prev->right = cur;
|
||||
cur = cur->left;
|
||||
}
|
||||
else{
|
||||
prev->right = NULL;
|
||||
reverseTraversal(cur->left, res);
|
||||
cur = cur->right;
|
||||
}
|
||||
}
|
||||
}
|
||||
delete dummyRoot;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
void reverseTraversal(TreeNode* node, vector<int>& res){
|
||||
|
||||
int start = res.size();
|
||||
while(node != NULL){
|
||||
res.push_back(node->val);
|
||||
node = node->right;
|
||||
}
|
||||
reverse(res.begin() + start, res.end());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void print_vec(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
TreeNode* root = new TreeNode(1);
|
||||
root->right = new TreeNode(2);
|
||||
root->right->left = new TreeNode(3);
|
||||
print_vec(Solution().postorderTraversal(root));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
27
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution1.java
Executable file
27
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution1.java
Executable file
@ -0,0 +1,27 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
// Recursive
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
public class Solution1 {
|
||||
|
||||
public List<Integer> postorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
postorderTraversal(root, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void postorderTraversal(TreeNode node, List<Integer> list){
|
||||
if(node != null){
|
||||
postorderTraversal(node.left, list);
|
||||
postorderTraversal(node.right, list);
|
||||
list.add(node.val);
|
||||
}
|
||||
}
|
||||
}
|
48
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution2.java
Executable file
48
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution2.java
Executable file
@ -0,0 +1,48 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-17
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// My Non-Recursive
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
public class Solution2 {
|
||||
|
||||
private class Command{
|
||||
String s; // go, print
|
||||
TreeNode node;
|
||||
Command(String s, TreeNode node){
|
||||
this.s = s;
|
||||
this.node = node;
|
||||
}
|
||||
};
|
||||
|
||||
public List<Integer> postorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<Command> stack = new Stack<Command>();
|
||||
stack.push(new Command("go", root));
|
||||
while(!stack.empty()){
|
||||
Command command = stack.pop();
|
||||
|
||||
if(command.s.equals("print"))
|
||||
res.add(command.node.val);
|
||||
else{
|
||||
assert command.s.equals("go");
|
||||
stack.push(new Command("print", command.node));
|
||||
if(command.node.right != null)
|
||||
stack.push(new Command("go",command.node.right));
|
||||
if(command.node.left != null)
|
||||
stack.push(new Command("go",command.node.left));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
54
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution3.java
Executable file
54
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution3.java
Executable file
@ -0,0 +1,54 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// Non-Recursive
|
||||
// Using a tag to record whether the node has been visited
|
||||
//
|
||||
// Time Complexity: O(n), n is the node number in the tree
|
||||
// Space Complexity: O(h), h is the height of the tree
|
||||
public class Solution3 {
|
||||
|
||||
private class TagNode{
|
||||
TreeNode node;
|
||||
boolean isFirst;
|
||||
TagNode(TreeNode node){
|
||||
this.node = node;
|
||||
this.isFirst = false;
|
||||
}
|
||||
};
|
||||
|
||||
public List<Integer> postorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<TagNode> stack = new Stack<>();
|
||||
TreeNode cur = root;
|
||||
while(cur != null || !stack.empty()){
|
||||
|
||||
while(cur != null){
|
||||
stack.push(new TagNode(cur));
|
||||
cur = cur.left;
|
||||
}
|
||||
|
||||
TagNode tagNode = stack.pop();
|
||||
cur = tagNode.node;
|
||||
if(tagNode.isFirst == false){
|
||||
tagNode.isFirst = true;
|
||||
stack.push(tagNode);
|
||||
cur = cur.right;
|
||||
}
|
||||
else{
|
||||
res.add(cur.val);
|
||||
cur = null;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
41
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution4.java
Executable file
41
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution4.java
Executable file
@ -0,0 +1,41 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// Non-Recursive
|
||||
// Using two stacks, Reverse the Preorder Traversal!
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(n)
|
||||
public class Solution4 {
|
||||
|
||||
public List<Integer> postorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
Stack<Integer> output = new Stack<>();
|
||||
|
||||
stack.push(root);
|
||||
while(!stack.empty()){
|
||||
|
||||
TreeNode cur = stack.pop();
|
||||
output.push(cur.val);
|
||||
|
||||
if(cur.left != null)
|
||||
stack.push(cur.left);
|
||||
if(cur.right != null)
|
||||
stack.push(cur.right);
|
||||
}
|
||||
|
||||
while(!output.empty())
|
||||
res.add(output.pop());
|
||||
return res;
|
||||
}
|
||||
}
|
40
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution5.java
Executable file
40
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution5.java
Executable file
@ -0,0 +1,40 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-30
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
import java.util.LinkedList;
|
||||
|
||||
// Non-Recursive
|
||||
// Using two stacks, Reverse the Preorder Traversal!
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(n)
|
||||
public class Solution5 {
|
||||
|
||||
public List<Integer> postorderTraversal(TreeNode root){
|
||||
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
LinkedList<TreeNode> output = new LinkedList<>();
|
||||
|
||||
TreeNode p = root;
|
||||
while(p != null || !stack.isEmpty()){
|
||||
if(p != null){
|
||||
stack.push(p);
|
||||
output.push(p);
|
||||
p = p.right;
|
||||
}
|
||||
else{
|
||||
p = stack.pop();
|
||||
p = p.left;
|
||||
}
|
||||
}
|
||||
|
||||
List<Integer> res = new ArrayList<>();
|
||||
while(!output.isEmpty())
|
||||
res.add(output.pop().val);
|
||||
return res;
|
||||
}
|
||||
}
|
45
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution6.java
Executable file
45
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution6.java
Executable file
@ -0,0 +1,45 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-31
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// Non-Recursive
|
||||
// Using a pre pointer to record the last visted node
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(h)
|
||||
public class Solution6 {
|
||||
|
||||
public List<Integer> postorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
TreeNode pre = null;
|
||||
|
||||
stack.push(root);
|
||||
while(!stack.empty()){
|
||||
|
||||
TreeNode cur = stack.pop();
|
||||
if((cur.left == null && cur.right == null) ||
|
||||
(pre != null && pre == cur.left && cur.right == null) ||
|
||||
(pre != null && pre == cur.right)){
|
||||
res.add(cur.val);
|
||||
pre = cur;
|
||||
}
|
||||
else{
|
||||
stack.push(cur);
|
||||
if(cur.right != null)
|
||||
stack.push(cur.right);
|
||||
if(cur.left != null)
|
||||
stack.push(cur.left);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
46
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution7.java
Executable file
46
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution7.java
Executable file
@ -0,0 +1,46 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-31
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// Classic Non-Recursive
|
||||
// Using a pre pointer to record the last visted node
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(h)
|
||||
public class Solution7 {
|
||||
|
||||
public List<Integer> postorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
TreeNode pre = null;
|
||||
TreeNode cur = root;
|
||||
|
||||
while(cur != null || !stack.empty()){
|
||||
|
||||
while(cur != null){
|
||||
stack.push(cur);
|
||||
cur = cur.left;
|
||||
}
|
||||
|
||||
cur = stack.pop();
|
||||
if(cur.right == null || pre == cur.right){
|
||||
res.add(cur.val);
|
||||
pre = cur;
|
||||
cur = null;
|
||||
}
|
||||
else{
|
||||
stack.push(cur);
|
||||
cur = cur.right;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
47
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution8.java
Executable file
47
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution8.java
Executable file
@ -0,0 +1,47 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-31
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// Classic Non-Recursive
|
||||
// Using a pre pointer to record the last visted node
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(h)
|
||||
public class Solution8 {
|
||||
|
||||
public List<Integer> postorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
TreeNode pre = null;
|
||||
TreeNode cur = root;
|
||||
|
||||
while(cur != null || !stack.empty()){
|
||||
|
||||
if(cur != null){
|
||||
stack.push(cur);
|
||||
cur = cur.left;
|
||||
}
|
||||
else{
|
||||
cur = stack.pop();
|
||||
if(cur.right == null || pre == cur.right){
|
||||
res.add(cur.val);
|
||||
pre = cur;
|
||||
cur = null;
|
||||
}
|
||||
else{
|
||||
stack.push(cur);
|
||||
cur = cur.right;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
65
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution9.java
Executable file
65
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution9.java
Executable file
@ -0,0 +1,65 @@
|
||||
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-05-31
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// Morris PostOrder Traversal
|
||||
//
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(1)
|
||||
public class Solution9 {
|
||||
|
||||
public List<Integer> postorderTraversal(TreeNode root) {
|
||||
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
if(root == null)
|
||||
return res;
|
||||
|
||||
TreeNode dummyRoot = new TreeNode(-1);
|
||||
dummyRoot.left = root;
|
||||
|
||||
TreeNode cur = dummyRoot;
|
||||
while(cur != null){
|
||||
if(cur.left == null)
|
||||
cur = cur.right;
|
||||
else{
|
||||
TreeNode pre = cur.left;
|
||||
while(pre.right != null && pre.right != cur)
|
||||
pre = pre.right;
|
||||
|
||||
if(pre.right == null){
|
||||
pre.right = cur;
|
||||
cur = cur.left;
|
||||
}
|
||||
else{
|
||||
pre.right = null;
|
||||
reverseTraversal(cur.left, res);
|
||||
cur = cur.right;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private void reverseTraversal(TreeNode node, ArrayList<Integer> res){
|
||||
int start = res.size();
|
||||
while(node != null){
|
||||
res.add(node.val);
|
||||
node = node.right;
|
||||
}
|
||||
|
||||
int i = start, j = res.size() - 1;
|
||||
while(i < j){
|
||||
Integer t = res.get(i);
|
||||
res.set(i, res.get(j));
|
||||
res.set(j, t);
|
||||
|
||||
i ++;
|
||||
j --;
|
||||
}
|
||||
}
|
||||
}
|
7
0145-Binary-Tree-Postorder-Traversal/java-0145/src/TreeNode.java
Executable file
7
0145-Binary-Tree-Postorder-Traversal/java-0145/src/TreeNode.java
Executable file
@ -0,0 +1,7 @@
|
||||
// Definition for a binary tree node.
|
||||
public class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
TreeNode(int x) { val = x; }
|
||||
}
|
7
0150-Evaluate-Reverse-Polish-Notation/cpp-0150/CMakeLists.txt
Executable file
7
0150-Evaluate-Reverse-Polish-Notation/cpp-0150/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0150)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main.cpp)
|
||||
add_executable(cpp_0150 ${SOURCE_FILES})
|
50
0150-Evaluate-Reverse-Polish-Notation/cpp-0150/main.cpp
Executable file
50
0150-Evaluate-Reverse-Polish-Notation/cpp-0150/main.cpp
Executable file
@ -0,0 +1,50 @@
|
||||
/// Source : https://leetcode.com/problems/evaluate-reverse-polish-notation/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-08-29
|
||||
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
/// Two stacks
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(n)
|
||||
class Solution {
|
||||
public:
|
||||
int evalRPN(vector<string>& tokens) {
|
||||
|
||||
stack<int> nums;
|
||||
stack<char> ops;
|
||||
for(const string& s: tokens){
|
||||
if(s == "+" || s == "-" || s == "*" || s == "/"){
|
||||
int a = nums.top();
|
||||
nums.pop();
|
||||
int b = nums.top();
|
||||
nums.pop();
|
||||
|
||||
if(s == "+"){
|
||||
nums.push(b + a);
|
||||
}else if(s == "-"){
|
||||
nums.push(b - a);
|
||||
} else if(s == "*"){
|
||||
nums.push(b * a);
|
||||
}else if(s == "/"){
|
||||
nums.push(b / a);
|
||||
}
|
||||
}
|
||||
else{
|
||||
nums.push(atoi(s.c_str()));
|
||||
}
|
||||
}
|
||||
return nums.top();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
7
0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/CMakeLists.txt
Executable file
7
0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(04_Two_Sum_II)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main3.cpp)
|
||||
add_executable(04_Two_Sum_II ${SOURCE_FILES})
|
55
0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main.cpp
Executable file
55
0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main.cpp
Executable file
@ -0,0 +1,55 @@
|
||||
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-13
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Brute Force
|
||||
// Time Complexity: O(n^2)
|
||||
// Space Complexity: O(1)
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& numbers, int target) {
|
||||
|
||||
assert(numbers.size() >= 2);
|
||||
// assert(isSorted(numbers));
|
||||
|
||||
for(int i = 0 ; i < numbers.size() ; i ++)
|
||||
for(int j = i+1 ; j < numbers.size() ; j ++)
|
||||
if(numbers[i] + numbers[j] == target){
|
||||
int res[2] = {i+1, j+1};
|
||||
return vector<int>(res, res+2);
|
||||
}
|
||||
|
||||
throw invalid_argument("the input has no solution");
|
||||
}
|
||||
|
||||
private:
|
||||
bool isSorted(const vector<int>& numbers){
|
||||
for(int i = 1 ; i < numbers.size() ; i ++)
|
||||
if(numbers[i] < numbers[i-1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void printVec(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
int nums[] = {2, 7, 11, 15};
|
||||
vector<int> vec(nums, nums + sizeof(nums) / sizeof(int));
|
||||
int target = 9;
|
||||
printVec(Solution().twoSum(vec, target));
|
||||
|
||||
return 0;
|
||||
}
|
74
0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main2.cpp
Executable file
74
0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main2.cpp
Executable file
@ -0,0 +1,74 @@
|
||||
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-13
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Binary Search
|
||||
// Time Complexity: O(nlogn)
|
||||
// Space Complexity: O(1)
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& numbers, int target) {
|
||||
|
||||
assert(numbers.size() >= 2);
|
||||
// assert(isSorted(numbers));
|
||||
|
||||
for(int i = 0 ; i < numbers.size() - 1 ; i ++){
|
||||
int j = binarySearch(numbers, i+1, numbers.size()-1, target - numbers[i]);
|
||||
if(j != -1){
|
||||
int res[2] = {i+1, j+1};
|
||||
return vector<int>(res, res+2);
|
||||
}
|
||||
}
|
||||
|
||||
throw invalid_argument("the input has no solution");
|
||||
}
|
||||
|
||||
private:
|
||||
int binarySearch(const vector<int> &nums, int l, int r, int target){
|
||||
|
||||
assert(l >= 0 && l < nums.size());
|
||||
assert(r >= 0 && r < nums.size());
|
||||
|
||||
while(l <= r){
|
||||
|
||||
int mid = l + (r - l)/2;
|
||||
if(nums[mid] == target)
|
||||
return mid;
|
||||
if(target > nums[mid])
|
||||
l = mid + 1;
|
||||
else
|
||||
r = mid - 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool isSorted(const vector<int>& numbers){
|
||||
for(int i = 1 ; i < numbers.size() ; i ++)
|
||||
if(numbers[i] < numbers[i-1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
void printVec(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
int nums[] = {2, 7, 11, 15};
|
||||
vector<int> vec(nums, nums + sizeof(nums) / sizeof(int));
|
||||
int target = 9;
|
||||
printVec(Solution().twoSum(vec, target));
|
||||
|
||||
return 0;
|
||||
}
|
62
0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main3.cpp
Executable file
62
0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main3.cpp
Executable file
@ -0,0 +1,62 @@
|
||||
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-13
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Two Pointers
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(1)
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& numbers, int target) {
|
||||
|
||||
assert(numbers.size() >= 2);
|
||||
// assert(isSorted(numbers));
|
||||
|
||||
int l = 0, r = numbers.size() - 1;
|
||||
while(l < r){
|
||||
|
||||
if(numbers[l] + numbers[r] == target){
|
||||
int res[2] = {l+1, r+1};
|
||||
return vector<int>(res, res+2);
|
||||
}
|
||||
else if(numbers[l] + numbers[r] < target)
|
||||
l ++;
|
||||
else // numbers[l] + numbers[r] > target
|
||||
r --;
|
||||
}
|
||||
|
||||
throw invalid_argument("the input has no solution");
|
||||
}
|
||||
|
||||
private:
|
||||
bool isSorted(const vector<int>& numbers){
|
||||
for(int i = 1 ; i < numbers.size() ; i ++)
|
||||
if(numbers[i] < numbers[i-1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
void printVec(const vector<int>& vec){
|
||||
for(int e: vec)
|
||||
cout << e << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
int nums[] = {2, 7, 11, 15};
|
||||
vector<int> vec(nums, nums + sizeof(nums) / sizeof(int));
|
||||
int target = 9;
|
||||
printVec(Solution().twoSum(vec, target));
|
||||
|
||||
return 0;
|
||||
}
|
44
0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution1.java
Executable file
44
0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution1.java
Executable file
@ -0,0 +1,44 @@
|
||||
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-13
|
||||
|
||||
// Brute Force
|
||||
// Time Complexity: O(n^2)
|
||||
// Space Complexity: O(1)
|
||||
public class Solution1 {
|
||||
|
||||
public int[] twoSum(int[] numbers, int target) {
|
||||
|
||||
if(numbers.length < 2 /*|| !isSorted(numbers)*/)
|
||||
throw new IllegalArgumentException("Illegal argument numbers");
|
||||
|
||||
for(int i = 0 ; i < numbers.length ; i ++)
|
||||
for(int j = i+1 ; j < numbers.length ; j ++)
|
||||
if(numbers[i] + numbers[j] == target){
|
||||
int[] res = {i+1, j+1};
|
||||
return res;
|
||||
}
|
||||
|
||||
throw new IllegalStateException("The input has no solution");
|
||||
}
|
||||
|
||||
private boolean isSorted(int[] numbers){
|
||||
for(int i = 1 ; i < numbers.length ; i ++)
|
||||
if(numbers[i] < numbers[i-1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void printArr(int[] nums){
|
||||
for(int num: nums)
|
||||
System.out.print(num + " ");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] nums = {2, 7, 11, 15};
|
||||
int target = 9;
|
||||
printArr((new Solution1()).twoSum(nums, target));
|
||||
}
|
||||
}
|
66
0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution2.java
Executable file
66
0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution2.java
Executable file
@ -0,0 +1,66 @@
|
||||
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-13
|
||||
|
||||
// Binary Search
|
||||
// Time Complexity: O(nlogn)
|
||||
// Space Complexity: O(1)
|
||||
public class Solution2 {
|
||||
|
||||
public int[] twoSum(int[] numbers, int target) {
|
||||
|
||||
if(numbers.length < 2 /*|| !isSorted(numbers)*/)
|
||||
throw new IllegalArgumentException("Illegal argument numbers");
|
||||
|
||||
for(int i = 0 ; i < numbers.length - 1 ; i ++){
|
||||
int j = binarySearch(numbers, i+1, numbers.length-1, target - numbers[i]);
|
||||
if(j != -1){
|
||||
int[] res = {i+1, j+1};
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalStateException("The input has no solution");
|
||||
}
|
||||
|
||||
private int binarySearch(int[] nums, int l, int r, int target){
|
||||
|
||||
if(l < 0 || l > nums.length)
|
||||
throw new IllegalArgumentException("l is out of bound");
|
||||
|
||||
if(r < 0 || r > nums.length)
|
||||
throw new IllegalArgumentException("r is out of bound");
|
||||
|
||||
while(l <= r){
|
||||
int mid = l + (r - l)/2;
|
||||
if(nums[mid] == target)
|
||||
return mid;
|
||||
if(target > nums[mid])
|
||||
l = mid + 1;
|
||||
else
|
||||
r = mid - 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private boolean isSorted(int[] numbers){
|
||||
for(int i = 1 ; i < numbers.length ; i ++)
|
||||
if(numbers[i] < numbers[i-1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void printArr(int[] nums){
|
||||
for(int num: nums)
|
||||
System.out.print(num + " ");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] nums = {2, 7, 11, 15};
|
||||
int target = 9;
|
||||
printArr((new Solution2()).twoSum(nums, target));
|
||||
}
|
||||
}
|
50
0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution3.java
Executable file
50
0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution3.java
Executable file
@ -0,0 +1,50 @@
|
||||
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-13
|
||||
|
||||
// Two Pointers
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(1)
|
||||
public class Solution3 {
|
||||
|
||||
public int[] twoSum(int[] numbers, int target) {
|
||||
|
||||
if(numbers.length < 2 /*|| !isSorted(numbers)*/)
|
||||
throw new IllegalArgumentException("Illegal argument numbers");
|
||||
|
||||
int l = 0, r = numbers.length - 1;
|
||||
while(l < r){
|
||||
|
||||
if(numbers[l] + numbers[r] == target){
|
||||
int[] res = {l+1, r+1};
|
||||
return res;
|
||||
}
|
||||
else if(numbers[l] + numbers[r] < target)
|
||||
l ++;
|
||||
else // numbers[l] + numbers[r] > target
|
||||
r --;
|
||||
}
|
||||
|
||||
throw new IllegalStateException("The input has no solution");
|
||||
}
|
||||
|
||||
private boolean isSorted(int[] numbers){
|
||||
for(int i = 1 ; i < numbers.length ; i ++)
|
||||
if(numbers[i] < numbers[i-1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void printArr(int[] nums){
|
||||
for(int num: nums)
|
||||
System.out.print(num + " ");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] nums = {2, 7, 11, 15};
|
||||
int target = 9;
|
||||
printArr((new Solution3()).twoSum(nums, target));
|
||||
}
|
||||
}
|
7
0203-Remove-Linked-List-Elements/cpp-0203/CMakeLists.txt
Executable file
7
0203-Remove-Linked-List-Elements/cpp-0203/CMakeLists.txt
Executable file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(cpp_0203)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main2.cpp)
|
||||
add_executable(cpp_0203 ${SOURCE_FILES})
|
108
0203-Remove-Linked-List-Elements/cpp-0203/main.cpp
Executable file
108
0203-Remove-Linked-List-Elements/cpp-0203/main.cpp
Executable file
@ -0,0 +1,108 @@
|
||||
/// Source : https://leetcode.com/problems/remove-linked-list-elements/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
///Definition for singly-linked list.
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(NULL) {}
|
||||
};
|
||||
|
||||
/// LinkedList Test Helper Functions
|
||||
ListNode* createLinkedList(int arr[], int n){
|
||||
|
||||
if(n == 0)
|
||||
return NULL;
|
||||
|
||||
ListNode* head = new ListNode(arr[0]);
|
||||
ListNode* curNode = head;
|
||||
for(int i = 1 ; i < n ; i ++){
|
||||
curNode->next = new ListNode(arr[i]);
|
||||
curNode = curNode->next;
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
void printLinkedList(ListNode* head){
|
||||
|
||||
if(head == NULL){
|
||||
cout << "NULL" << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
ListNode* curNode = head;
|
||||
while(curNode != NULL){
|
||||
cout << curNode->val;
|
||||
if(curNode->next != NULL)
|
||||
cout << " -> ";
|
||||
curNode = curNode->next;
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void deleteLinkedList(ListNode* head){
|
||||
|
||||
ListNode* curNode = head;
|
||||
while(curNode != NULL){
|
||||
ListNode* delNode = curNode;
|
||||
curNode = curNode->next;
|
||||
delete delNode;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/// Linear Scan with dummy head
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(1)
|
||||
class Solution {
|
||||
public:
|
||||
ListNode* removeElements(ListNode* head, int val) {
|
||||
|
||||
ListNode* dummyHead = new ListNode(0);
|
||||
dummyHead->next = head;
|
||||
|
||||
ListNode* cur = dummyHead;
|
||||
while(cur->next != NULL){
|
||||
if(cur->next->val == val){
|
||||
ListNode* delNode = cur->next;
|
||||
cur->next = delNode->next;
|
||||
delete delNode;
|
||||
}
|
||||
else
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
ListNode* retNode = dummyHead->next;
|
||||
delete dummyHead;
|
||||
|
||||
return retNode;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
int arr[] = {1, 2, 6, 3, 4, 5, 6};
|
||||
int n = sizeof(arr) / sizeof(int);
|
||||
|
||||
ListNode* head = createLinkedList(arr, n);
|
||||
printLinkedList(head);
|
||||
|
||||
Solution().removeElements(head, 6);
|
||||
printLinkedList(head);
|
||||
|
||||
deleteLinkedList(head);
|
||||
|
||||
return 0;
|
||||
}
|
42
0203-Remove-Linked-List-Elements/cpp-0203/main2.cpp
Executable file
42
0203-Remove-Linked-List-Elements/cpp-0203/main2.cpp
Executable file
@ -0,0 +1,42 @@
|
||||
/// Source : https://leetcode.com/problems/remove-linked-list-elements/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2018-09-17
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
///Definition for singly-linked list.
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
/// Recursive
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(n)
|
||||
class Solution {
|
||||
public:
|
||||
ListNode* removeElements(ListNode* head, int val) {
|
||||
|
||||
if(!head)
|
||||
return head;
|
||||
|
||||
if(head->val == val){
|
||||
ListNode* node = head->next;
|
||||
delete head;
|
||||
return removeElements(node, val);
|
||||
}
|
||||
|
||||
head->next = removeElements(head->next, val);
|
||||
return head;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
36
0203-Remove-Linked-List-Elements/java-0203/src/ListNode.java
Executable file
36
0203-Remove-Linked-List-Elements/java-0203/src/ListNode.java
Executable file
@ -0,0 +1,36 @@
|
||||
public class ListNode {
|
||||
|
||||
public int val;
|
||||
public ListNode next = null;
|
||||
|
||||
public ListNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
public ListNode (int[] arr){
|
||||
|
||||
if(arr == null || arr.length == 0)
|
||||
throw new IllegalArgumentException("arr can not be empty");
|
||||
|
||||
this.val = arr[0];
|
||||
ListNode curNode = this;
|
||||
for(int i = 1 ; i < arr.length ; i ++){
|
||||
curNode.next = new ListNode(arr[i]);
|
||||
curNode = curNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
|
||||
StringBuilder s = new StringBuilder("");
|
||||
ListNode curNode = this;
|
||||
while(curNode != null){
|
||||
s.append(Integer.toString(curNode.val));
|
||||
s.append(" -> ");
|
||||
curNode = curNode.next;
|
||||
}
|
||||
s.append("NULL");
|
||||
return s.toString();
|
||||
}
|
||||
}
|
38
0203-Remove-Linked-List-Elements/java-0203/src/Solution.java
Executable file
38
0203-Remove-Linked-List-Elements/java-0203/src/Solution.java
Executable file
@ -0,0 +1,38 @@
|
||||
/// Source : https://leetcode.com/problems/remove-linked-list-elements/description/
|
||||
/// Author : liuyubobobo
|
||||
/// Time : 2017-11-15
|
||||
|
||||
/// Time Complexity: O(n)
|
||||
/// Space Complexity: O(1)
|
||||
public class Solution {
|
||||
|
||||
public ListNode removeElements(ListNode head, int val) {
|
||||
|
||||
ListNode dummyHead = new ListNode(0);
|
||||
dummyHead.next = head;
|
||||
|
||||
ListNode cur = dummyHead;
|
||||
while(cur.next != null){
|
||||
if(cur.next.val == val ){
|
||||
ListNode delNode = cur.next;
|
||||
cur.next = delNode.next;
|
||||
}
|
||||
else
|
||||
cur = cur.next;
|
||||
}
|
||||
|
||||
return dummyHead.next;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] arr = {1, 2, 6, 3, 4, 5, 6};
|
||||
int val = 6;
|
||||
|
||||
ListNode head = new ListNode(arr);
|
||||
System.out.println(head);
|
||||
|
||||
(new Solution()).removeElements(head, val);
|
||||
System.out.println(head);
|
||||
}
|
||||
}
|
24
0203-Remove-Linked-List-Elements/java-0203/src/Solution2.java
Executable file
24
0203-Remove-Linked-List-Elements/java-0203/src/Solution2.java
Executable file
@ -0,0 +1,24 @@
|
||||
public class Solution2 {
|
||||
|
||||
public ListNode removeElements(ListNode head, int val) {
|
||||
|
||||
if(head == null)
|
||||
return head;
|
||||
|
||||
ListNode node = removeElements(head.next, val);
|
||||
head.next = node;
|
||||
return head.val == val ? node : head;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] arr = {1, 2, 6, 3, 4, 5, 6};
|
||||
int val = 6;
|
||||
|
||||
ListNode head = new ListNode(arr);
|
||||
System.out.println(head);
|
||||
|
||||
(new Solution()).removeElements(head, val);
|
||||
System.out.println(head);
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user