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; + 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; diff --git a/src/twoSum/twoSum.py b/src/twoSum/twoSum.py new file mode 100644 index 000000000..b7659f857 --- /dev/null +++ b/src/twoSum/twoSum.py @@ -0,0 +1,18 @@ +class Solution: + # @return a tuple, (index1, index2) + def twoSum(self, num, target): + 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+1,j+1);