Skip to content

Commit 2dd16af

Browse files
committed
2019-5-23
1 parent d9c000b commit 2dd16af

File tree

112 files changed

+2473
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+2473
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
12+
ListNode* res = new ListNode(-1);
13+
ListNode* cur = res;
14+
int carry = 0;
15+
while(l1 || l2){
16+
int t1 = l1? l1->val : 0;
17+
int t2 = l2? l2->val : 0;
18+
int sum = t1 + t2 + carry;
19+
carry = sum / 10;
20+
cur->next = new ListNode(sum % 10);
21+
cur = cur->next;
22+
if (l1) l1 = l1->next;
23+
if (l2) l2 = l2->next;
24+
}
25+
if (carry) cur->next =new ListNode(1);
26+
return res->next;
27+
28+
29+
}
30+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int lengthOfLongestSubstring(string s) {
4+
5+
string dic = "";
6+
int ans = 0;
7+
int i = 0;
8+
int j = 0;
9+
int l = s.size();
10+
while( i < l && j < l ){
11+
char a = s[i];
12+
char b = s[j];
13+
// cout<<dic.find(b)<<endl;
14+
if (dic.find(b) >= 0 && dic.find(b) < 99999999){
15+
i += 1;
16+
dic.erase(0, 1);
17+
}
18+
else{
19+
j += 1;
20+
dic.push_back(b);
21+
// cout << "Dasdas" << endl;
22+
}
23+
ans = ans < j-i ? j-i : ans;
24+
// cout <<i<<" "<<j << endl;
25+
}
26+
return ans;
27+
}
28+
};

0009.回文数/0009-回文数.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def isPalindrome(self, x):
3+
4+
s = str(x)
5+
if len(s) <= 1:
6+
return True
7+
print len(s) / 2
8+
for count in range(0, len(s)/2):
9+
if s[count] != s[len(s)-1 - count]:
10+
return False
11+
return True
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def removeNthFromEnd(self, head, n):
9+
"""
10+
:type head: ListNode
11+
:type n: int
12+
:rtype: ListNode
13+
"""
14+
if not head.next:
15+
return []
16+
p = head
17+
if n == 1:
18+
while(p.next.next):
19+
p = p.next
20+
p.next = None
21+
else:
22+
fast = head
23+
slow = head
24+
for i in range(n-1):
25+
fast = fast.next
26+
print fast.val,slow.val
27+
while(fast.next):
28+
fast = fast.next
29+
slow = slow.next
30+
print fast.val,slow.val
31+
slow.val = slow.next.val
32+
slow.next = slow.next.next
33+
return head
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def isValid(self, s):
3+
"""
4+
:type s: str
5+
:rtype: bool
6+
"""
7+
if len(s)%2:
8+
return False
9+
dic = {")":"(","]":"[","}":"{"}
10+
stack = [None]
11+
for item in s:
12+
if item in dic and stack[-1] == dic[item]:
13+
stack.pop()
14+
else:
15+
stack.append(item)
16+
return len(stack) == 1
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
12+
ListNode temp_head(0);
13+
ListNode* pre = &temp_head;
14+
while(l1 && l2){
15+
if (l1->val<= l2->val){
16+
pre->next = l1;
17+
l1 = l1->next;
18+
}
19+
else{
20+
pre->next = l2;
21+
l2 = l2->next;
22+
}
23+
pre = pre->next;
24+
}
25+
if(l1) pre->next = l1;
26+
if(l2) pre->next = l2;
27+
return temp_head.next;
28+
29+
}
30+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def generate(self, temp, left, right, result):
3+
if (left == 0 and right == 0):
4+
result.append(temp)
5+
return
6+
if (left > 0):
7+
self.generate(temp + "(", left-1, right, result)
8+
if (left < right):
9+
self.generate(temp + ")", left, right - 1, result)
10+
11+
def generateParenthesis(self, n):
12+
"""
13+
:type n: int
14+
:rtype: List[str]
15+
"""
16+
result = []
17+
self.generate("", n, n, result)
18+
return result
19+
20+
21+
22+
23+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
bool cmp(const ListNode* a, const ListNode* b){
10+
return a->val < b->val;
11+
}
12+
class Solution {
13+
public:
14+
ListNode* mergeKLists(vector<ListNode*>& lists) {
15+
std::vector<ListNode *> value;
16+
for(int i = 0; i< lists.size(); ++i){
17+
ListNode* head = lists[i];
18+
while(head){
19+
value.push_back(head);
20+
head = head->next;
21+
}
22+
}
23+
24+
std::sort(value.begin(),value.end(), cmp);
25+
if (value.size() == 0)
26+
return NULL;
27+
28+
for(int i = 1; i<value.size(); ++i){
29+
value[i-1]->next = value[i];
30+
}
31+
value[value.size()-1]->next = NULL;
32+
return value[0];
33+
34+
}
35+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* swapPairs(ListNode* head) {
12+
if (!head || !head->next)
13+
return head;
14+
ListNode n_head(0);
15+
ListNode* pre = &n_head;
16+
n_head.next = head;
17+
ListNode* ptr = head;
18+
while(ptr && ptr->next){
19+
pre->next = ptr->next;
20+
// if (!ptr->next)
21+
ptr->next = ptr->next->next;
22+
// else
23+
// ptr->next = NULL;
24+
pre->next->next = ptr;
25+
pre = pre->next->next;
26+
ptr = ptr->next;
27+
}
28+
return n_head.next;
29+
30+
}
31+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int strStr(string haystack, string needle) {
4+
if (needle.size() == 0){
5+
return 0;
6+
}
7+
int lh = haystack.size();
8+
int ln = needle.size();
9+
bool flag = 0;
10+
for (int i = 0; i <=lh - ln ; ++i){
11+
flag = 1;
12+
for (int j = i; j < i + ln; ++ j){
13+
if (haystack[j] != needle[j-i]){
14+
flag = 0;
15+
break;
16+
}
17+
}
18+
if (flag) return i;
19+
}
20+
if (!flag) return -1;
21+
}
22+
23+
};

0 commit comments

Comments
 (0)