File tree Expand file tree Collapse file tree 3 files changed +69
-0
lines changed
validate-binary-search-tree Expand file tree Collapse file tree 3 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 2
2
.vscode /
3
3
.DS_Store
4
4
.env
5
+ ** /** -template.md
Original file line number Diff line number Diff line change
1
+ ``` cpp
2
+ class Solution {
3
+ public:
4
+ vector<vector<int >> threeSum(vector<int >& nums) {
5
+ sort(nums.begin(), nums.end());
6
+ set<vector<int >> res;
7
+ for(int i=0;i<nums.size();i++){
8
+ int l = i+1, r = nums.size()-1;
9
+
10
+ while(l<r){
11
+ int sum = nums[i]+nums[l]+nums[r];
12
+
13
+ if(sum<0){
14
+ l++;
15
+ }else if(sum>0){
16
+ r--;
17
+ }else{
18
+ res.insert({nums[i], nums[l], nums[r]});
19
+ l++;
20
+ r--;
21
+ }
22
+ }
23
+ }
24
+
25
+ return vector<vector<int >>(res.begin(), res.end());
26
+ }
27
+ };
28
+ ```
29
+
30
+ - set -> vector ์ฌ์ฉ ์ด์ ๋ ์ค๋ณต ์ ๊ฑฐ๋ฅผ ์ํจ
31
+
32
+ ``` cpp
33
+ class Solution {
34
+ public:
35
+ vector<vector<int >> threeSum(vector<int >& nums) {
36
+ sort(nums.begin(), nums.end());
37
+ set<vector<int >> res;
38
+ for(int i=0;i<nums.size();i++){
39
+ if(i != 0 && nums[ i] == nums[ i-1] ) continue;
40
+
41
+ int l = i+1, r = nums.size()-1;
42
+
43
+ while(l<r){
44
+ int sum = nums[i]+nums[l]+nums[r];
45
+
46
+ if(sum<0){
47
+ l++;
48
+ }else if(sum>0){
49
+ r--;
50
+ }else{
51
+ res.insert({nums[i], nums[l], nums[r]});
52
+ l++;
53
+ r--;
54
+ }
55
+ }
56
+ }
57
+
58
+ return vector<vector<int >>(res.begin(), res.end());
59
+
60
+ }
61
+ };
62
+ ```
63
+
64
+ - ` if(i != 0 && nums[i] == nums[i-1]) continue; ` ๋ฅผ ํตํ ํ์ ๋ฒ์ ์ค์ด๊ธฐ ์ต์ ํ ์ ๋์ ์ฐจ์ด๋ก ์ / ํ์ ๊ฐ๋ฆฌ๋ ์ ๋
65
+ - ๋จ์ 2 pointer๋ก ์ฒ๋ฆฌํด๋ ๋ฌด๋ฐฉ
66
+
67
+
Original file line number Diff line number Diff line change
1
+ - https://leetcode.com/problems/validate-binary-search-tree/
You canโt perform that action at this time.
0 commit comments