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 1 commit
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
Prev Previous commit
Next Next commit
add 001 cpp, 019 cpp, 059 folder & cpp
  • Loading branch information
zouwx2cs committed Oct 17, 2018
commit 0cb7e00e2362a4c739eadd7b7b1f3fe44ce471b6
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 ;
}
};