Skip to content

Commit 921cc79

Browse files
committed
添加仓库代码
1 parent 4e5b464 commit 921cc79

File tree

137 files changed

+5727
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+5727
-2
lines changed

.gitattributes

Lines changed: 0 additions & 1 deletion
This file was deleted.

0001-Two-Sum/cpp-0001/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(cpp_0001)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5+
6+
set(SOURCE_FILES main2.cpp)
7+
add_executable(cpp_0001 ${SOURCE_FILES})

0001-Two-Sum/cpp-0001/main.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// Source : https://leetcode.com/problems/two-sum/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-11-15
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
/// Brute Force
11+
/// Time Complexity: O(n^2)
12+
/// Space Complexity: O(1)
13+
class Solution {
14+
public:
15+
vector<int> twoSum(vector<int>& nums, int target) {
16+
17+
for(int i = 0 ; i < nums.size() ; i ++)
18+
for(int j = i + 1 ; j < nums.size() ; j ++)
19+
if(nums[i] + nums[j] == target){
20+
int res[] = {i, j};
21+
return vector<int>(res, res + 2);
22+
}
23+
24+
throw invalid_argument("the input has no solution");
25+
}
26+
};
27+
28+
29+
void printVec(const vector<int>& vec){
30+
for(int e: vec)
31+
cout << e << " ";
32+
cout << endl;
33+
}
34+
35+
int main() {
36+
37+
const int nums[] = {0,4,3,0};
38+
vector<int> nums_vec( nums, nums + sizeof(nums)/sizeof(int) );
39+
int target = 0;
40+
printVec(Solution().twoSum(nums_vec, target));
41+
42+
return 0;
43+
}

0001-Two-Sum/cpp-0001/main2.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/// Source : https://leetcode.com/problems/two-sum/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-11-15
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <cassert>
8+
#include <unordered_map>
9+
10+
using namespace std;
11+
12+
/// Two-Pass Hash Table
13+
/// Time Complexity: O(n)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
public:
17+
vector<int> twoSum(vector<int>& nums, int target) {
18+
19+
unordered_map<int,int> record;
20+
for(int i = 0 ; i < nums.size() ; i ++)
21+
record[nums[i]] = i;
22+
23+
for(int i = 0 ; i < nums.size() ; i ++){
24+
unordered_map<int,int>::iterator iter = record.find(target - nums[i]);
25+
if(iter != record.end() && iter->second != i){
26+
int res[] = {i, iter->second};
27+
return vector<int>(res, res + 2);
28+
}
29+
}
30+
31+
throw invalid_argument("the input has no solution");
32+
}
33+
};
34+
35+
36+
void printVec(const vector<int>& vec){
37+
for(int e: vec)
38+
cout << e << " ";
39+
cout << endl;
40+
}
41+
42+
int main() {
43+
44+
const int nums[] = {0,4,3,0};
45+
vector<int> nums_vec( nums, nums + sizeof(nums)/sizeof(int) );
46+
int target = 0;
47+
printVec(Solution().twoSum(nums_vec, target));
48+
49+
return 0;
50+
}

0001-Two-Sum/cpp-0001/main3.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/// Source : https://leetcode.com/problems/two-sum/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-11-15
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <cassert>
8+
#include <unordered_map>
9+
10+
using namespace std;
11+
12+
/// One-Pass Hash Table
13+
/// Time Complexity: O(n)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
public:
17+
vector<int> twoSum(vector<int>& nums, int target) {
18+
19+
unordered_map<int,int> record;
20+
for(int i = 0 ; i < nums.size() ; i ++){
21+
22+
int complement = target - nums[i];
23+
if(record.find(complement) != record.end()){
24+
int res[] = {i, record[complement]};
25+
return vector<int>(res, res + 2);
26+
}
27+
28+
record[nums[i]] = i;
29+
}
30+
31+
throw invalid_argument("the input has no solution");
32+
}
33+
};
34+
35+
36+
void printVec(const vector<int>& vec){
37+
for(int e: vec)
38+
cout << e << " ";
39+
cout << endl;
40+
}
41+
42+
int main() {
43+
44+
const int nums[] = {0,4,3,0};
45+
vector<int> nums_vec( nums, nums + sizeof(nums)/sizeof(int) );
46+
int target = 0;
47+
printVec(Solution().twoSum(nums_vec, target));
48+
49+
return 0;
50+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/// Source : https://leetcode.com/problems/two-sum/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-11-15
4+
5+
import java.util.HashMap;
6+
7+
/// Brute Force
8+
/// Time Complexity: O(n^2)
9+
/// Space Complexity: O(1)
10+
public class Solution1 {
11+
12+
public int[] twoSum(int[] nums, int target) {
13+
14+
for(int i = 0 ; i < nums.length; i ++)
15+
for(int j = 0 ; j < nums.length ; j ++)
16+
if(nums[i] + nums[j] == target){
17+
int[] res = {i, j};
18+
return res;
19+
}
20+
21+
throw new IllegalStateException("the input has no solution");
22+
}
23+
24+
private static void printArr(int[] nums){
25+
for(int num: nums)
26+
System.out.print(num + " ");
27+
System.out.println();
28+
}
29+
30+
public static void main(String[] args) {
31+
32+
int[] nums = {0, 4, 3, 0};
33+
int target = 0;
34+
printArr((new Solution1()).twoSum(nums, target));
35+
}
36+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// Source : https://leetcode.com/problems/two-sum/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-11-15
4+
5+
import java.util.HashMap;
6+
7+
/// Two-Pass Hash Table
8+
/// Time Complexity: O(n)
9+
/// Space Complexity: O(n)
10+
public class Solution2 {
11+
12+
public int[] twoSum(int[] nums, int target) {
13+
14+
HashMap<Integer, Integer> record = new HashMap<Integer, Integer>();
15+
for(int i = 0 ; i < nums.length ; i ++)
16+
record.put(nums[i], i);
17+
18+
for(int i = 0 ; i < nums.length; i ++){
19+
20+
Integer index = record.get(target - nums[i]);
21+
if(index != null && index != i){
22+
int[] res = {i, index};
23+
return res;
24+
}
25+
26+
record.put(nums[i], i);
27+
}
28+
29+
throw new IllegalStateException("the input has no solution");
30+
}
31+
32+
private static void printArr(int[] nums){
33+
for(int num: nums)
34+
System.out.print(num + " ");
35+
System.out.println();
36+
}
37+
38+
public static void main(String[] args) {
39+
40+
int[] nums = {0, 4, 3, 0};
41+
int target = 0;
42+
printArr((new Solution2()).twoSum(nums, target));
43+
}
44+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/// Source : https://leetcode.com/problems/two-sum/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-11-15
4+
5+
import java.util.HashMap;
6+
7+
/// One-Pass Hash Table
8+
/// Time Complexity: O(n)
9+
/// Space Complexity: O(n)
10+
public class Solution3 {
11+
12+
public int[] twoSum(int[] nums, int target) {
13+
14+
HashMap<Integer, Integer> record = new HashMap<Integer, Integer>();
15+
for(int i = 0 ; i < nums.length; i ++){
16+
17+
int complement = target - nums[i];
18+
if(record.containsKey(complement)){
19+
int[] res = {i, record.get(complement)};
20+
return res;
21+
}
22+
23+
record.put(nums[i], i);
24+
}
25+
26+
throw new IllegalStateException("the input has no solution");
27+
}
28+
29+
private static void printArr(int[] nums){
30+
for(int num: nums)
31+
System.out.print(num + " ");
32+
System.out.println();
33+
}
34+
35+
public static void main(String[] args) {
36+
37+
int[] nums = {0, 4, 3, 0};
38+
int target = 0;
39+
printArr((new Solution3()).twoSum(nums, target));
40+
}
41+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(cpp_0019)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(cpp_0019 ${SOURCE_FILES})

0 commit comments

Comments
 (0)