Skip to content

add 122 cpp version(4ms) #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions solution/001.Two Sum/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> res ;
unordered_map<int, int> hash ;

for (int i = 0; i < nums.size(); ++i)
{
int aim = target - nums[i] ;
int local = hash[aim] ;
if (local != NULL)
{
res.push_back(local-1) ;
res.push_back(i) ;
return res ;
}
else
hash[nums[i]] = i+1 ;
}

return res ;
}
};
25 changes: 25 additions & 0 deletions solution/019.Remove Nth Node From End of List/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *p, *q ;
q = head ;
p = head ;

for (int i = 0; i < n; ++i)
q = q->next ;

if (!q)
{
return head->next ;
}

while (q->next)
{
q = q->next ;
p = p->next ;
}

p->next = p->next->next ;
return head ;
}
};
65 changes: 65 additions & 0 deletions solution/059.Spiral Matrix II/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
if (0 == n)
return vector<vector<int>>() ;
vector<vector<int>> res(n, vector<int>(n, 0)) ;

int i = 0, j = 0 ;

int dir = 0 ;

int n2 = n*n ;

for (int d = 1; d <= n2; ++d)
{
res[i][j] = d ;
//cout << d << endl ;
switch (dir)
{
case 0:
++j ;
if (j >= n || res[i][j])
{
dir++ ;
--j ;
++i ;
}
break ;
case 1:
++i ;
if (i >= n || res[i][j])
{
dir++ ;
--i ;
--j ;
}
break ;
case 2:
--j ;
if (j < 0 || res[i][j])
{
dir++ ;
++j ;
--i ;
}
break ;
case 3:
--i ;
if (i < 0 || res[i][j])
{
dir = 0 ;
++i ;
++j ;
}

break ;
default:
break ;
}

}

return res ;
}
};
15 changes: 15 additions & 0 deletions solution/122.Best Time to Buy and Sell Stock II/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public:
int maxProfit(vector<int>& prices)
{
if (0 == prices.size())
return 0 ;
int M = 0 ;
for (int i = 1; i < prices.size(); ++i)
{
int d = prices[i] - prices[i-1] ;
if (d > 0)
M += d ;
}
return M ;
}
};
40 changes: 40 additions & 0 deletions solution/581.Shortest Unsorted Continuous Subarray/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// i自左向右找到第一个逆序,
// j自右向左找到第一个逆序
// 找到nums[i, j]的上下界
// ij向外扩展找到满足上下界的最小[i, j]
class Solution {
public:
int findUnsortedSubarray(vector<int>& nums) {
if (1 == nums.size())
return 0 ;

int i = 0, j = nums.size()-1 ;
while (i < j && nums[i] <= nums[i+1])
++i ;
if (i >= j)
return 0 ;

while (nums[j-1] <= nums[j])
--j ;

//cout << i << ' ' << j << endl ;
int m = nums[i] ;
int M = m ;
for (int k = i; k <= j; ++k)
{
if (m > nums[k])
m = nums[k] ;
if (M < nums[k])
M = nums[k] ;
}
//cout << m << ' ' << M << endl ;

while (i >= 0 && m < nums[i])
--i ;
while (j < nums.size() && M > nums[j])
++j ;

//cout << i << ' ' << j << endl ;
return j-i-1 ;
}
};