From 407a44ae01c9b368e732d02033b2c33d404d45d1 Mon Sep 17 00:00:00 2001 From: simonclouds Date: Sat, 6 Dec 2014 23:32:55 +0800 Subject: [PATCH 1/4] Create findPeakElement.py --- src/findPeakElement/findPeakElement.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/findPeakElement/findPeakElement.py diff --git a/src/findPeakElement/findPeakElement.py b/src/findPeakElement/findPeakElement.py new file mode 100644 index 000000000..6141b1863 --- /dev/null +++ b/src/findPeakElement/findPeakElement.py @@ -0,0 +1,13 @@ +class Solution: + # @param num, a list of integer + # @return an integer + def findPeakElement(self, num): + numlen = len(num) + if numlen == 1: + return 0 + else: + for i in range(0,numlen-1): + if num[i] > num[i+1]: + return i; + return numlen-1; + From 56791bd7d4ba857835e1b28f654fb95013e7e57b Mon Sep 17 00:00:00 2001 From: simonclouds Date: Sat, 6 Dec 2014 23:34:31 +0800 Subject: [PATCH 2/4] Create intersectionOfTwoLinkedLists.py --- .../intersectionOfTwoLinkedLists.py | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/intersectionOfTwoLinkedLists/intersectionOfTwoLinkedLists.py diff --git a/src/intersectionOfTwoLinkedLists/intersectionOfTwoLinkedLists.py b/src/intersectionOfTwoLinkedLists/intersectionOfTwoLinkedLists.py new file mode 100644 index 000000000..dc6e8111c --- /dev/null +++ b/src/intersectionOfTwoLinkedLists/intersectionOfTwoLinkedLists.py @@ -0,0 +1,52 @@ +# Definition for singly-linked list. +#class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + # @param two ListNodes + # @return the intersected ListNode + def getIntersectionNode(self, headA, headB): + heada = headA + headb = headB + + + lengthA = self.getListLen(headA); + lengthB = self.getListLen(headB); + if lengthA * lengthB == 0: + return None; + + lastStatus = '=' + + + if lengthA > lengthB: + while lengthA != lengthB: + heada = heada.next; + lengthA = lengthA - 1; + else: + while lengthA != lengthB: + headb = headb.next; + lengthB = lengthB - 1; + + intersectionNode = heada + + while heada != None: + if heada.val != headb.val: + heada = heada.next; + headb = headb.next; + intersectionNode = heada; + lastStatus = 'x' + else : + heada = heada.next; + headb = headb.next; + lastStatus = '=' + + return intersectionNode; + + def getListLen(self, head): + length = 0; + while head != None: + length = length + 1; + head = head.next; + return length; From ebcafb3d1ab09c356f777cd2d5ad3bdcf5672a8a Mon Sep 17 00:00:00 2001 From: simonclouds Date: Sun, 7 Dec 2014 09:12:09 +0800 Subject: [PATCH 3/4] Create twoSum.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 暴利搜索,结果,超时没通过 --- src/twoSum/twoSum.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/twoSum/twoSum.py diff --git a/src/twoSum/twoSum.py b/src/twoSum/twoSum.py new file mode 100644 index 000000000..2dc90ca55 --- /dev/null +++ b/src/twoSum/twoSum.py @@ -0,0 +1,7 @@ +class Solution: + # @return a tuple, (index1, index2) + def twoSum(self, num, target): + for i in range(0,len(num)): + for j in range(i+1,len(num)): + if num[i] + num[j] == target: + return (i,j); From 83dbe2ab843e94a8a9e37db53af6e14f76a9b5e6 Mon Sep 17 00:00:00 2001 From: simonclouds Date: Sun, 7 Dec 2014 09:13:31 +0800 Subject: [PATCH 4/4] Update twoSum.py Submission Result: Runtime Error Runtime Error Message: Line 35: TypeError: 'NoneType' object has no attribute '__getitem__' Last executed input: [-3,4,3,90], 0 --- src/twoSum/twoSum.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/twoSum/twoSum.py b/src/twoSum/twoSum.py index 2dc90ca55..b7659f857 100644 --- a/src/twoSum/twoSum.py +++ b/src/twoSum/twoSum.py @@ -1,7 +1,18 @@ class Solution: # @return a tuple, (index1, index2) def twoSum(self, num, target): - for i in range(0,len(num)): - for j in range(i+1,len(num)): + letarget = len(num) -1; + + while num[letarget] > target: + letarget = letarget - 1; + + for i in range(0,letarget): + firstnum = num[i] + diff = target - firstnum; + + secondnumlast = letarget; + while num[secondnumlast] > diff: + secondnumlast = secondnumlast-1; + for j in range(i+1, secondnumlast+1): if num[i] + num[j] == target: - return (i,j); + return (i+1,j+1);