From 55100e4639fb6fda841e4661c2b94d808fd9f180 Mon Sep 17 00:00:00 2001 From: Jack-Lee-Hiter Date: Sun, 21 Aug 2016 21:25:47 +0800 Subject: [PATCH 01/27] add 1 code --- .idea/workspace.xml | 95 +++++++++++++++++--------------- leetcode/13. Roman to Integer.py | 22 ++++++++ 2 files changed, 72 insertions(+), 45 deletions(-) create mode 100644 leetcode/13. Roman to Integer.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 615fb60..0661199 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,9 +2,7 @@ - - - + @@ -28,7 +26,18 @@ - + + + + + + + + + + + + @@ -176,25 +185,25 @@ - - + + - + - + - + - + - - - - - + + + + + - - - - - + + + + + @@ -431,9 +440,9 @@ - + - + @@ -489,13 +498,6 @@ - - - - - - - @@ -728,6 +730,9 @@ + + + @@ -736,9 +741,6 @@ - - - @@ -821,7 +823,6 @@ - @@ -829,7 +830,6 @@ - @@ -837,7 +837,6 @@ - @@ -845,7 +844,6 @@ - @@ -853,6 +851,13 @@ + + + + + + + diff --git a/leetcode/13. Roman to Integer.py b/leetcode/13. Roman to Integer.py new file mode 100644 index 0000000..224c0de --- /dev/null +++ b/leetcode/13. Roman to Integer.py @@ -0,0 +1,22 @@ +# -*- coding:utf-8 -*- +''' +Given a roman numeral, convert it to an integer. + +Input is guaranteed to be within the range from 1 to 3999. + +''' + +class Solution(object): + def romanToInt(self, s): + romanDict = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1} + res, p = 0, 'I' + for ch in s[::-1]: + if romanDict[ch] < romanDict[p]: + res = res - romanDict[ch] + else: + res = res + romanDict[ch] + p = ch + return res + +s = Solution() +print((s.romanToInt("IX"))) \ No newline at end of file From 71989141914b2055890e6e9b943c0cb1095b302f Mon Sep 17 00:00:00 2001 From: "Jack.ht.lee" Date: Mon, 22 Aug 2016 15:22:49 +0800 Subject: [PATCH 02/27] modify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化代码 --- ...40\210\345\256\236\347\216\260\351\230\237\345\210\227.py" | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git "a/Target Offer/\347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.py" "b/Target Offer/\347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.py" index e5659b5..0bf523a 100644 --- "a/Target Offer/\347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.py" +++ "b/Target Offer/\347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.py" @@ -15,9 +15,7 @@ def pop(self): elif len(self.stack2) == 0: while len(self.stack1) > 0: self.stack2.append(self.stack1.pop()) - return self.stack2.pop() - else: - return self.stack2.pop() + return self.stack2.pop() P = Solution() P.push(10) From 49b35b93554369d0676e774e6c08cbd02c2a71fe Mon Sep 17 00:00:00 2001 From: "Jack.ht.lee" Date: Mon, 22 Aug 2016 15:39:52 +0800 Subject: [PATCH 03/27] modify code --- ...\237\345\210\227\345\256\236\347\216\260\346\240\210.py" | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git "a/Target Offer/\347\224\250\344\270\244\344\270\252\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.py" "b/Target Offer/\347\224\250\344\270\244\344\270\252\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.py" index a3e1e3b..a46d001 100644 --- "a/Target Offer/\347\224\250\344\270\244\344\270\252\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.py" +++ "b/Target Offer/\347\224\250\344\270\244\344\270\252\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.py" @@ -7,9 +7,7 @@ def __init__(self): self.queue1 = [] self.queue2 = [] def push(self, x): - if self.queue1 == [] and self.queue2 == []: - self.queue1.append(x) - elif self.queue1 != [] and self.queue2 == []: + if self.queue2 == []: self.queue1.append(x) else: self.queue2.append(x) @@ -36,4 +34,4 @@ def pop(self): print(P.pop()) print(P.pop()) print(P.pop()) -print(P.pop()) \ No newline at end of file +print(P.pop()) From 37f6a3db2029a2f7803d8d4dcbf08cfa4f1b9179 Mon Sep 17 00:00:00 2001 From: "Jack.ht.lee" Date: Mon, 22 Aug 2016 22:12:13 +0800 Subject: [PATCH 04/27] 3 LinkedList codes --- .idea/AlgorithmsByPython.iml | 2 +- .idea/misc.xml | 2 +- .idea/workspace.xml | 127 +++++++++++------- .../19. Remove Nth Node From End of List.py | 28 ++++ leetcode/2. Add Two Numbers.py | 26 ++++ leetcode/203. Remove Linked List Elements.py | 26 ++++ 6 files changed, 158 insertions(+), 53 deletions(-) create mode 100644 leetcode/19. Remove Nth Node From End of List.py create mode 100644 leetcode/2. Add Two Numbers.py create mode 100644 leetcode/203. Remove Linked List Elements.py diff --git a/.idea/AlgorithmsByPython.iml b/.idea/AlgorithmsByPython.iml index 7715f25..e451288 100644 --- a/.idea/AlgorithmsByPython.iml +++ b/.idea/AlgorithmsByPython.iml @@ -2,7 +2,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index ac543ec..6985f4e 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -10,5 +10,5 @@ - + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0661199..287e4d2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,10 @@ - + + + + @@ -26,17 +29,47 @@ - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -47,14 +80,12 @@ + - @@ -137,7 +171,6 @@ - @@ -172,12 +205,13 @@ + - + - + @@ -439,26 +473,26 @@ - + - + - - + + @@ -498,27 +532,6 @@ - - - - - - - - - - - - - - - - - - - - - @@ -729,19 +742,7 @@ - - - - - - - - - - - - - + @@ -856,11 +857,35 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/leetcode/19. Remove Nth Node From End of List.py b/leetcode/19. Remove Nth Node From End of List.py new file mode 100644 index 0000000..18b4249 --- /dev/null +++ b/leetcode/19. Remove Nth Node From End of List.py @@ -0,0 +1,28 @@ +''' +Given a linked list, remove the nth node from the end of list and return its head. +For example, + Given linked list: 1->2->3->4->5, and n = 2. + After removing the second node from the end, the linked list becomes 1->2->3->5. +''' +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None + +class Solution(object): + def removeNthFromEnd(self, head, n): + if not head and n <= 0: + return None + pNode = ListNode(0) + pNode.next = head + first, second = pNode, pNode + for i in range(n): + if first.next: + first = first.next + else: + return None + while first.next: + first = first.next + second = second.next + second.next = second.next.next + return pNode.next \ No newline at end of file diff --git a/leetcode/2. Add Two Numbers.py b/leetcode/2. Add Two Numbers.py new file mode 100644 index 0000000..05f86b9 --- /dev/null +++ b/leetcode/2. Add Two Numbers.py @@ -0,0 +1,26 @@ +''' +You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. +Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) +Output: 7 -> 0 -> 8 +''' +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None + +class Solution(object): + def addTwoNumbers(self, l1, l2): + pNode = ListNode(0) + pHead = pNode + val = 0 + while l1 or l2 or val: + if l1: + val += l1.val + l1 = l1.next + if l2: + val += l2.val + l2 = l2.next + pNode.next = ListNode(val % 10) + val /= 10 + pNode = pNode.next + return pHead.next diff --git a/leetcode/203. Remove Linked List Elements.py b/leetcode/203. Remove Linked List Elements.py new file mode 100644 index 0000000..0bbc214 --- /dev/null +++ b/leetcode/203. Remove Linked List Elements.py @@ -0,0 +1,26 @@ +''' +Remove all elements from a linked list of integers that have value val. +Example +Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 +Return: 1 --> 2 --> 3 --> 4 --> 5 +''' +# Definition for singly-linked list. +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None + +class Solution(object): + + def removeElements(self, head, val): + preNode, res = None, head + while head: + if head.val == val: + if not preNode: + res = res.next + else: + preNode.next = head.next + else: + preNode = head + head = head.next + return res \ No newline at end of file From 09230bf90dbaadac21423e2b57b84fe51be7f490 Mon Sep 17 00:00:00 2001 From: "Jack.ht.lee" Date: Tue, 23 Aug 2016 17:14:58 +0800 Subject: [PATCH 05/27] add 1 leetcode --- .idea/workspace.xml | 179 ++++++------------ ...21\347\232\204\351\225\234\345\203\217.py" | 10 +- leetcode/24. Swap Nodes in Pairs.py | 43 +++++ 3 files changed, 101 insertions(+), 131 deletions(-) create mode 100644 leetcode/24. Swap Nodes in Pairs.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 287e4d2..9570c9f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,11 +2,9 @@ - - - - + + @@ -29,48 +27,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +