Skip to content

Commit aa159df

Browse files
author
YILING
committed
[1. TwoSum] solution by using hash map
1 parent 4a96bd3 commit aa159df

21 files changed

+156
-2
lines changed

LeetCode.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
6DEF8F5F1E75A0EC0028351C /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6DEF8F5E1E75A0EC0028351C /* main.cpp */; };
11+
6DEF8F671E75A1780028351C /* 1_two_sum.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6DEF8F651E75A1780028351C /* 1_two_sum.cpp */; };
1112
/* End PBXBuildFile section */
1213

1314
/* Begin PBXCopyFilesBuildPhase section */
@@ -25,6 +26,8 @@
2526
/* Begin PBXFileReference section */
2627
6DEF8F5B1E75A0EC0028351C /* LeetCode */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = LeetCode; sourceTree = BUILT_PRODUCTS_DIR; };
2728
6DEF8F5E1E75A0EC0028351C /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
29+
6DEF8F651E75A1780028351C /* 1_two_sum.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = 1_two_sum.cpp; sourceTree = "<group>"; };
30+
6DEF8F661E75A1780028351C /* 1_two_sum.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = 1_two_sum.hpp; sourceTree = "<group>"; };
2831
/* End PBXFileReference section */
2932

3033
/* Begin PBXFrameworksBuildPhase section */
@@ -58,6 +61,8 @@
5861
isa = PBXGroup;
5962
children = (
6063
6DEF8F5E1E75A0EC0028351C /* main.cpp */,
64+
6DEF8F651E75A1780028351C /* 1_two_sum.cpp */,
65+
6DEF8F661E75A1780028351C /* 1_two_sum.hpp */,
6166
);
6267
path = LeetCode;
6368
sourceTree = "<group>";
@@ -119,6 +124,7 @@
119124
isa = PBXSourcesBuildPhase;
120125
buildActionMask = 2147483647;
121126
files = (
127+
6DEF8F671E75A1780028351C /* 1_two_sum.cpp in Sources */,
122128
6DEF8F5F1E75A0EC0028351C /* main.cpp in Sources */,
123129
);
124130
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
type = "1"
4+
version = "2.0">
5+
<Breakpoints>
6+
<BreakpointProxy
7+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
8+
<BreakpointContent
9+
shouldBeEnabled = "Yes"
10+
ignoreCount = "0"
11+
continueAfterRunningActions = "No"
12+
filePath = "LeetCode/main.cpp"
13+
timestampString = "511032595.904409"
14+
startingColumnNumber = "9223372036854775807"
15+
endingColumnNumber = "9223372036854775807"
16+
startingLineNumber = "19"
17+
endingLineNumber = "19"
18+
landmarkName = "main(int argc, const char * argv[])"
19+
landmarkType = "9">
20+
</BreakpointContent>
21+
</BreakpointProxy>
22+
<BreakpointProxy
23+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
24+
<BreakpointContent
25+
shouldBeEnabled = "Yes"
26+
ignoreCount = "0"
27+
continueAfterRunningActions = "No"
28+
filePath = "LeetCode/1_two_sum.cpp"
29+
timestampString = "511032909.632054"
30+
startingColumnNumber = "9223372036854775807"
31+
endingColumnNumber = "9223372036854775807"
32+
startingLineNumber = "30"
33+
endingLineNumber = "30"
34+
landmarkName = "TwoSum::twoSum(std::vector&lt;int&gt; &amp;nums, int target)"
35+
landmarkType = "7">
36+
</BreakpointContent>
37+
</BreakpointProxy>
38+
<BreakpointProxy
39+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
40+
<BreakpointContent
41+
shouldBeEnabled = "Yes"
42+
ignoreCount = "0"
43+
continueAfterRunningActions = "No"
44+
filePath = "LeetCode/1_two_sum.cpp"
45+
timestampString = "511032910.636003"
46+
startingColumnNumber = "9223372036854775807"
47+
endingColumnNumber = "9223372036854775807"
48+
startingLineNumber = "35"
49+
endingLineNumber = "35"
50+
landmarkName = "TwoSum::twoSum(std::vector&lt;int&gt; &amp;nums, int target)"
51+
landmarkType = "7">
52+
</BreakpointContent>
53+
</BreakpointProxy>
54+
<BreakpointProxy
55+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
56+
<BreakpointContent
57+
shouldBeEnabled = "Yes"
58+
ignoreCount = "0"
59+
continueAfterRunningActions = "No"
60+
filePath = "LeetCode/1_two_sum.cpp"
61+
timestampString = "511032961.840229"
62+
startingColumnNumber = "9223372036854775807"
63+
endingColumnNumber = "9223372036854775807"
64+
startingLineNumber = "27"
65+
endingLineNumber = "27"
66+
landmarkName = "TwoSum::twoSum(std::vector&lt;int&gt; &amp;nums, int target)"
67+
landmarkType = "7">
68+
</BreakpointContent>
69+
</BreakpointProxy>
70+
</Breakpoints>
71+
</Bucket>

LeetCode/1_two_sum.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// 1_two_sum.cpp
3+
// LeetCode
4+
//
5+
// Created by Yi-Ling Wu on 12/03/2017.
6+
// Copyright © 2017 YILING. All rights reserved.
7+
//
8+
9+
#include "iostream"
10+
#include "1_two_sum.hpp"
11+
#include <unordered_map>
12+
13+
std::vector<int> TwoSum::twoSum(std::vector<int> &nums, int target) {
14+
std::unordered_map<int, int> hash;
15+
std::vector<int> result;
16+
for (int i = 0; i < nums.size(); i++) {
17+
if (hash.find(target - nums[i]) != hash.end()) {
18+
result.push_back(hash[target - nums[i]]);
19+
result.push_back(i);
20+
std::cout << i << std::endl;
21+
std::cout << result[i] << std::endl;
22+
return result;
23+
}
24+
25+
hash[nums[i]] = i;
26+
std::cout << hash[i] << std::endl;
27+
}
28+
29+
// 无解的情况
30+
result.push_back(-1);
31+
result.push_back(-1);
32+
return result;
33+
}

LeetCode/1_two_sum.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// 1_two_sum.hpp
3+
// LeetCode
4+
//
5+
// Created by Yi-Ling Wu on 12/03/2017.
6+
// Copyright © 2017 YILING. All rights reserved.
7+
//
8+
9+
// Question: Given an array of integers, return indices of the two numbers such that they add up to a specific target. \
10+
You may assume that each input would have exactly one solution, and you may not use the same element twice. \
11+
12+
/*Example:
13+
Given nums = [2, 7, 11, 15], target = 9,
14+
15+
Because nums[0] + nums[1] = 2 + 7 = 9,
16+
return [0, 1]. */
17+
18+
#ifndef __two_sum_hpp
19+
#define __two_sum_hpp
20+
21+
#include <stdio.h>
22+
#include <vector>
23+
24+
class TwoSum {
25+
public:
26+
std::vector<int> twoSum(std::vector<int> &nums, int target);
27+
};
28+
29+
30+
#endif /* __two_sum_hpp */

LeetCode/main.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
//
88

99
#include <iostream>
10+
#include "1_two_sum.hpp"
1011

1112
int main(int argc, const char * argv[]) {
12-
// insert code here...
13-
std::cout << "Hello, World!\n";
13+
14+
// Test_1: TwoSum
15+
TwoSum *twosum = new TwoSum;
16+
std::vector<int> nums = {3,2,4};
17+
int target = 6;
18+
std::vector<int> result = twosum->twoSum(nums, target);
1419
return 0;
20+
1521
}

build/Debug/LeetCode

60.3 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies: \
2+
/Users/YLibrarian/Developer/Leetcode/Algorithm_1/LeetCode/LeetCode/1_two_sum.cpp \
3+
/Users/YLibrarian/Developer/Leetcode/Algorithm_1/LeetCode/LeetCode/1_two_sum.hpp
Binary file not shown.
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/Users/YLibrarian/Developer/Leetcode/Algorithm_1/LeetCode/build/LeetCode.build/Debug/LeetCode.build/Objects-normal/x86_64/1_two_sum.o
2+
/Users/YLibrarian/Developer/Leetcode/Algorithm_1/LeetCode/build/LeetCode.build/Debug/LeetCode.build/Objects-normal/x86_64/main.o
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies: \
2+
/Users/YLibrarian/Developer/Leetcode/Algorithm_1/LeetCode/LeetCode/main.cpp \
3+
/Users/YLibrarian/Developer/Leetcode/Algorithm_1/LeetCode/LeetCode/1_two_sum.hpp
Binary file not shown.
Binary file not shown.
21.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)