Skip to content

Commit cf59a22

Browse files
authored
Merge pull request #24 from zouwx2cs/master
add 122 cpp version(4ms)
2 parents 6df63db + 6e9162b commit cf59a22

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

solution/001.Two Sum/Solution.cpp

+24
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+
};
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+
};
+65
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+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public:
2+
int maxProfit(vector<int>& prices)
3+
{
4+
if (0 == prices.size())
5+
return 0 ;
6+
int M = 0 ;
7+
for (int i = 1; i < prices.size(); ++i)
8+
{
9+
int d = prices[i] - prices[i-1] ;
10+
if (d > 0)
11+
M += d ;
12+
}
13+
return M ;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// i自左向右找到第一个逆序,
2+
// j自右向左找到第一个逆序
3+
// 找到nums[i, j]的上下界
4+
// ij向外扩展找到满足上下界的最小[i, j]
5+
class Solution {
6+
public:
7+
int findUnsortedSubarray(vector<int>& nums) {
8+
if (1 == nums.size())
9+
return 0 ;
10+
11+
int i = 0, j = nums.size()-1 ;
12+
while (i < j && nums[i] <= nums[i+1])
13+
++i ;
14+
if (i >= j)
15+
return 0 ;
16+
17+
while (nums[j-1] <= nums[j])
18+
--j ;
19+
20+
//cout << i << ' ' << j << endl ;
21+
int m = nums[i] ;
22+
int M = m ;
23+
for (int k = i; k <= j; ++k)
24+
{
25+
if (m > nums[k])
26+
m = nums[k] ;
27+
if (M < nums[k])
28+
M = nums[k] ;
29+
}
30+
//cout << m << ' ' << M << endl ;
31+
32+
while (i >= 0 && m < nums[i])
33+
--i ;
34+
while (j < nums.size() && M > nums[j])
35+
++j ;
36+
37+
//cout << i << ' ' << j << endl ;
38+
return j-i-1 ;
39+
}
40+
};

0 commit comments

Comments
 (0)