Skip to content

Commit 0cb7e00

Browse files
committed
add 001 cpp, 019 cpp, 059 folder & cpp
1 parent 36b4fc7 commit 0cb7e00

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

solution/001.Two Sum/Solution.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target)
4+
{
5+
vector<int> res ;
6+
unordered_map<int, int> hash ;
7+
8+
for (int i = 0; i < nums.size(); ++i)
9+
{
10+
int aim = target - nums[i] ;
11+
int local = hash[aim] ;
12+
if (local != NULL)
13+
{
14+
res.push_back(local-1) ;
15+
res.push_back(i) ;
16+
return res ;
17+
}
18+
else
19+
hash[nums[i]] = i+1 ;
20+
}
21+
22+
return res ;
23+
}
24+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
ListNode* removeNthFromEnd(ListNode* head, int n) {
4+
ListNode *p, *q ;
5+
q = head ;
6+
p = head ;
7+
8+
for (int i = 0; i < n; ++i)
9+
q = q->next ;
10+
11+
if (!q)
12+
{
13+
return head->next ;
14+
}
15+
16+
while (q->next)
17+
{
18+
q = q->next ;
19+
p = p->next ;
20+
}
21+
22+
p->next = p->next->next ;
23+
return head ;
24+
}
25+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> generateMatrix(int n) {
4+
if (0 == n)
5+
return vector<vector<int>>() ;
6+
vector<vector<int>> res(n, vector<int>(n, 0)) ;
7+
8+
int i = 0, j = 0 ;
9+
10+
int dir = 0 ;
11+
12+
int n2 = n*n ;
13+
14+
for (int d = 1; d <= n2; ++d)
15+
{
16+
res[i][j] = d ;
17+
//cout << d << endl ;
18+
switch (dir)
19+
{
20+
case 0:
21+
++j ;
22+
if (j >= n || res[i][j])
23+
{
24+
dir++ ;
25+
--j ;
26+
++i ;
27+
}
28+
break ;
29+
case 1:
30+
++i ;
31+
if (i >= n || res[i][j])
32+
{
33+
dir++ ;
34+
--i ;
35+
--j ;
36+
}
37+
break ;
38+
case 2:
39+
--j ;
40+
if (j < 0 || res[i][j])
41+
{
42+
dir++ ;
43+
++j ;
44+
--i ;
45+
}
46+
break ;
47+
case 3:
48+
--i ;
49+
if (i < 0 || res[i][j])
50+
{
51+
dir = 0 ;
52+
++i ;
53+
++j ;
54+
}
55+
56+
break ;
57+
default:
58+
break ;
59+
}
60+
61+
}
62+
63+
return res ;
64+
}
65+
};

0 commit comments

Comments
 (0)