forked from MisterBooo/LeetCodeAnimation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e5b464
commit 921cc79
Showing
137 changed files
with
5,727 additions
and
2 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 7 additions & 0 deletions
7
0019-Remove-Nth-Node-From-End-of-List/cpp-0019/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}) |
Oops, something went wrong.