-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
112 changed files
with
2,473 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode(int x) : val(x), next(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { | ||
ListNode* res = new ListNode(-1); | ||
ListNode* cur = res; | ||
int carry = 0; | ||
while(l1 || l2){ | ||
int t1 = l1? l1->val : 0; | ||
int t2 = l2? l2->val : 0; | ||
int sum = t1 + t2 + carry; | ||
carry = sum / 10; | ||
cur->next = new ListNode(sum % 10); | ||
cur = cur->next; | ||
if (l1) l1 = l1->next; | ||
if (l2) l2 = l2->next; | ||
} | ||
if (carry) cur->next =new ListNode(1); | ||
return res->next; | ||
|
||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution { | ||
public: | ||
int lengthOfLongestSubstring(string s) { | ||
|
||
string dic = ""; | ||
int ans = 0; | ||
int i = 0; | ||
int j = 0; | ||
int l = s.size(); | ||
while( i < l && j < l ){ | ||
char a = s[i]; | ||
char b = s[j]; | ||
// cout<<dic.find(b)<<endl; | ||
if (dic.find(b) >= 0 && dic.find(b) < 99999999){ | ||
i += 1; | ||
dic.erase(0, 1); | ||
} | ||
else{ | ||
j += 1; | ||
dic.push_back(b); | ||
// cout << "Dasdas" << endl; | ||
} | ||
ans = ans < j-i ? j-i : ans; | ||
// cout <<i<<" "<<j << endl; | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution(object): | ||
def isPalindrome(self, x): | ||
|
||
s = str(x) | ||
if len(s) <= 1: | ||
return True | ||
print len(s) / 2 | ||
for count in range(0, len(s)/2): | ||
if s[count] != s[len(s)-1 - count]: | ||
return False | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode(object): | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.next = None | ||
|
||
class Solution(object): | ||
def removeNthFromEnd(self, head, n): | ||
""" | ||
:type head: ListNode | ||
:type n: int | ||
:rtype: ListNode | ||
""" | ||
if not head.next: | ||
return [] | ||
p = head | ||
if n == 1: | ||
while(p.next.next): | ||
p = p.next | ||
p.next = None | ||
else: | ||
fast = head | ||
slow = head | ||
for i in range(n-1): | ||
fast = fast.next | ||
print fast.val,slow.val | ||
while(fast.next): | ||
fast = fast.next | ||
slow = slow.next | ||
print fast.val,slow.val | ||
slow.val = slow.next.val | ||
slow.next = slow.next.next | ||
return head |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution(object): | ||
def isValid(self, s): | ||
""" | ||
:type s: str | ||
:rtype: bool | ||
""" | ||
if len(s)%2: | ||
return False | ||
dic = {")":"(","]":"[","}":"{"} | ||
stack = [None] | ||
for item in s: | ||
if item in dic and stack[-1] == dic[item]: | ||
stack.pop() | ||
else: | ||
stack.append(item) | ||
return len(stack) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode(int x) : val(x), next(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { | ||
ListNode temp_head(0); | ||
ListNode* pre = &temp_head; | ||
while(l1 && l2){ | ||
if (l1->val<= l2->val){ | ||
pre->next = l1; | ||
l1 = l1->next; | ||
} | ||
else{ | ||
pre->next = l2; | ||
l2 = l2->next; | ||
} | ||
pre = pre->next; | ||
} | ||
if(l1) pre->next = l1; | ||
if(l2) pre->next = l2; | ||
return temp_head.next; | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution(object): | ||
def generate(self, temp, left, right, result): | ||
if (left == 0 and right == 0): | ||
result.append(temp) | ||
return | ||
if (left > 0): | ||
self.generate(temp + "(", left-1, right, result) | ||
if (left < right): | ||
self.generate(temp + ")", left, right - 1, result) | ||
|
||
def generateParenthesis(self, n): | ||
""" | ||
:type n: int | ||
:rtype: List[str] | ||
""" | ||
result = [] | ||
self.generate("", n, n, result) | ||
return result | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode(int x) : val(x), next(NULL) {} | ||
* }; | ||
*/ | ||
bool cmp(const ListNode* a, const ListNode* b){ | ||
return a->val < b->val; | ||
} | ||
class Solution { | ||
public: | ||
ListNode* mergeKLists(vector<ListNode*>& lists) { | ||
std::vector<ListNode *> value; | ||
for(int i = 0; i< lists.size(); ++i){ | ||
ListNode* head = lists[i]; | ||
while(head){ | ||
value.push_back(head); | ||
head = head->next; | ||
} | ||
} | ||
|
||
std::sort(value.begin(),value.end(), cmp); | ||
if (value.size() == 0) | ||
return NULL; | ||
|
||
for(int i = 1; i<value.size(); ++i){ | ||
value[i-1]->next = value[i]; | ||
} | ||
value[value.size()-1]->next = NULL; | ||
return value[0]; | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode(int x) : val(x), next(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
ListNode* swapPairs(ListNode* head) { | ||
if (!head || !head->next) | ||
return head; | ||
ListNode n_head(0); | ||
ListNode* pre = &n_head; | ||
n_head.next = head; | ||
ListNode* ptr = head; | ||
while(ptr && ptr->next){ | ||
pre->next = ptr->next; | ||
// if (!ptr->next) | ||
ptr->next = ptr->next->next; | ||
// else | ||
// ptr->next = NULL; | ||
pre->next->next = ptr; | ||
pre = pre->next->next; | ||
ptr = ptr->next; | ||
} | ||
return n_head.next; | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution { | ||
public: | ||
int strStr(string haystack, string needle) { | ||
if (needle.size() == 0){ | ||
return 0; | ||
} | ||
int lh = haystack.size(); | ||
int ln = needle.size(); | ||
bool flag = 0; | ||
for (int i = 0; i <=lh - ln ; ++i){ | ||
flag = 1; | ||
for (int j = i; j < i + ln; ++ j){ | ||
if (haystack[j] != needle[j-i]){ | ||
flag = 0; | ||
break; | ||
} | ||
} | ||
if (flag) return i; | ||
} | ||
if (!flag) return -1; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Solution { | ||
public: | ||
string countAndSay(int n) { | ||
if (n == 1) return "1"; | ||
if (n == 2) return "11"; | ||
int i = 2, j = 0; | ||
string temp = ""; | ||
string res = "11"; | ||
|
||
for (i; i <n; ++ i ){ // No. i | ||
j = 0; | ||
temp = ""; | ||
for (j; j < res.size(); ){ | ||
int k = 0; | ||
while(j + k < res.size() && res[j] == res[j+k]) | ||
k += 1; | ||
temp += to_string(k) + res[j]; | ||
// cout << res[j]<<endl; | ||
// cout << to_string(res[j] - '0')<<endl; | ||
// cout <<temp<<endl; | ||
j+= k; | ||
} | ||
res = temp; | ||
// cout <<res<<endl; | ||
} | ||
return res; | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution { | ||
public: | ||
void rotate(vector<vector<int>>& matrix) { | ||
int l = matrix[0].size(); | ||
for (int i = 0 ; i < matrix.size(); ++i){ | ||
for (int j = i + 1; j < l; ++j) | ||
swap(matrix[i][j], matrix[j][i]); | ||
reverse(matrix[i].begin(), matrix[i].end()); | ||
} | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution(object): | ||
def canJump(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: bool | ||
""" | ||
|
||
max_jump = 0 | ||
for index, num in enumerate(nums): | ||
if index > max_jump: | ||
return False | ||
max_jump = max(max_jump, index + num) | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution(object): | ||
def lengthOfLastWord(self, s): | ||
""" | ||
:type s: str | ||
:rtype: int | ||
""" | ||
if len(s) <= 0 : | ||
return 0 | ||
s = s.rstrip(" ") | ||
print s,s.split(" ") | ||
return len(s.split(" ")[-1]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode(object): | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.next = None | ||
|
||
class Solution(object): | ||
def rotateRight(self, head, k): | ||
""" | ||
:type head: ListNode | ||
:type k: int | ||
:rtype: ListNode | ||
""" | ||
if k <= 0: | ||
return head | ||
l = 0 | ||
ptr = head | ||
while(ptr != None): | ||
ptr = ptr.next | ||
l += 1 | ||
# print l | ||
if l <= 1: | ||
return head | ||
k = k % l | ||
if k == 0: | ||
return head | ||
|
||
fast = head | ||
slow = head | ||
while(k != 0): | ||
fast = fast.next | ||
k-= 1 | ||
while(fast.next != None): | ||
fast = fast.next | ||
slow = slow.next | ||
# print fast.val, slow.val | ||
ptr = slow.next | ||
fast.next = head | ||
slow.next = None | ||
return ptr |
Oops, something went wrong.