Commit c1aef7e0 authored by misterbooo's avatar misterbooo
Browse files

Remove Code

parent 5871d9af
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})
\ No newline at end of file
/// 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;
}
\ No newline at end of file
/// 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;
}
\ No newline at end of file
/// 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;
}
\ No newline at end of file
/// 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));
}
}
/// 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));
}
}
/// 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));
}
}
cmake_minimum_required(VERSION 3.5)
project(cpp_0002)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main2.cpp)
add_executable(cpp_0002 ${SOURCE_FILES})
\ No newline at end of file
/// Source : https://leetcode.com/problems/add-two-numbers/description/
/// Author : liuyubobobo
/// Time : 2018-08-09
#include <iostream>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// 时间复杂度: O(n)
/// 空间复杂度: O(n)
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *p1 = l1, *p2 = l2;
ListNode *dummyHead = new ListNode(-1);
ListNode* cur = dummyHead;
int carried = 0;
while(p1 || p2 ){
int a = p1 ? p1->val : 0;
int b = p2 ? p2->val : 0;
cur->next = new ListNode((a + b + carried) % 10);
carried = (a + b + carried) / 10;
cur = cur->next;
p1 = p1 ? p1->next : NULL;
p2 = p2 ? p2->next : NULL;
}
cur->next = carried ? new ListNode(1) : NULL;
ListNode* ret = dummyHead->next;
delete dummyHead;
return ret;
}
};
int main() {
return 0;
}
/// Source : https://leetcode.com/problems/add-two-numbers/description/
/// Author : liuyubobobo
/// Time : 2018-08-09
#include <iostream>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// Using l1 as the result list
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *p1 = l1, *p2 = l2;
ListNode* pre = NULL;
int carried = 0;
while(p1 || p2){
int a = p1 ? p1->val : 0;
int b = p2 ? p2->val : 0;
if(p1)
p1->val = (a + b + carried) % 10;
else{
pre->next = new ListNode((a + b + carried) % 10);
p1 = pre->next;
}
carried = (a + b + carried) / 10;
pre = p1;
p1 = p1->next;
if(p2) p2 = p2->next;
}
pre->next = carried ? new ListNode(1) : NULL;
return l1;
}
};
int main() {
return 0;
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/add-two-numbers/description/
/// Author : liuyubobobo
/// Time : 2018-08-09
#include <iostream>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// Using the longest list in l1 and l2 as the result list
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int len1 = getLen(l1), len2 = getLen(l2);
ListNode *p1 = len1 > len2 ? l1 : l2;
ListNode *p2 = len1 > len2 ? l2 : l1;
ListNode* pre = NULL;
int carried = 0;
while(p1){
int a = p1->val;
int b = p2 ? p2->val : 0;
p1->val = (a + b + carried) % 10;
carried = (a + b + carried) / 10;
pre = p1;
p1 = p1->next;
p2 = p2 ? p2->next : NULL;
}
pre->next = carried ? new ListNode(1) : NULL;
return len1 > len2 ? l1 : l2;
}
private:
int getLen(ListNode* l){
int res = 0;
while(l)
res ++, l = l -> next;
return res;
}
};
int main() {
return 0;
}
\ No newline at end of file
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})
\ No newline at end of file
/// 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;
}
\ No newline at end of file
/// 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;
}
\ No newline at end of file
// 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();
}
}
\ No newline at end of file
/// 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);
}
}
/// 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);
}
}
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})
\ No newline at end of file
/// 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;
}
\ No newline at end of file
public class Main {
public static void main(String[] args) {
// write your code here
}
}
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