Commit c1aef7e0 authored by misterbooo's avatar misterbooo
Browse files

Remove Code

parent 5871d9af
/// 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("([)]"));
}
}
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})
\ No newline at end of file
/// 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;
}
\ No newline at end of file
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();
}
}
\ No newline at end of file
/// 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);
}
}
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})
\ No newline at end of file
/// 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;
}
\ No newline at end of file
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})
\ No newline at end of file
/// 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;
}
/// 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;
}
/// 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);
}
}
/// 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);
}
}
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})
\ No newline at end of file
/// 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;
}
\ No newline at end of file
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})
\ No newline at end of file
/// 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;
}
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})
\ No newline at end of file
/// 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;
}
/// 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;
}
/// 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;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment