From b258f1557ddd90271153b47215c1c100d8ac3bfa Mon Sep 17 00:00:00 2001 From: ping Date: Tue, 8 Oct 2019 13:49:34 -0400 Subject: [PATCH] done 001-003 --- python/001_Two_Sum.py | 4 +- python/002_Add_Two_Numbers.py | 62 +++++++++++++++++++ ..._Substring_Without_Repeating_Characters.py | 43 +++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/python/001_Two_Sum.py b/python/001_Two_Sum.py index e4bd58a..0ed3335 100644 --- a/python/001_Two_Sum.py +++ b/python/001_Two_Sum.py @@ -48,7 +48,9 @@ class Solution(object): def twoSum(self, nums, target): # two point nums_index = [(v, index) for index, v in enumerate(nums)] + print "list of (value, index) for %s are: \n%s" % (nums, nums_index) nums_index.sort() + print "sorted to:\n%s" % nums_index begin, end = 0, len(nums) - 1 while begin < end: curr = nums_index[begin][0] + nums_index[end][0] @@ -63,4 +65,4 @@ def twoSum(self, nums, target): if __name__ == '__main__': # begin s = Solution() - print s.twoSum([3, 2, 4], 6) + print s.twoSum([3, 2, 4, 5], 9) diff --git a/python/002_Add_Two_Numbers.py b/python/002_Add_Two_Numbers.py index e452aa7..63f754f 100644 --- a/python/002_Add_Two_Numbers.py +++ b/python/002_Add_Two_Numbers.py @@ -1,9 +1,24 @@ +""" +You are given two non-empty linked lists representing two non-negative integers. +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. + +You may assume the two numbers do not contain any leading zero, except the +number 0 itself. +""" +##https://medium.com/@kojinoshiba/data-structures-in-python-series-1-linked-lists-d9f848537b4d + # Definition for singly-linked list. class ListNode(object): def __init__(self, x): self.val = x self.next = None + def traverse(self): + node = self # start from the head node + while node != None: + print "%s->" % node.val, # access the node value + node = node.next # move on to the next node class Solution(object): # def addTwoNumbers(self, l1, l2): @@ -37,21 +52,68 @@ class Solution(object): # prev = current # return head + + """ + create dummy node as head of the result linked list. + if any linked list is not iterated to the end, repeat: + take carry as the init val + if any linked list is not iterated to the end yet: + add its value into val + move (pointer) to the next node + use the remainder of val as a new node in result, + append (save) it to result linked list + move (pointer) to the new node, so next time + the pointer to new result node can be added to it + get the carry of current value, which will be the init value when doing + addition on the next iteration + once both linked list is iterated, if there is a carry in the last step + use it as a new node and append to the result linked list + + return the result linked list using the pointer saved in the dummy head node + """ + def addTwoNumbers(self, l1, l2): carry = 0 # dummy head + ##to save the linked table head = curr = ListNode(0) + ##repeat as long as any linked list is not iterated to the end: while l1 or l2: + ##take the carry as init value val = carry + ##if any linked list is not iterated to the end, work on it if l1: + ##add its value and mv to the next val += l1.val l1 = l1.next if l2: val += l2.val l2 = l2.next + ##take the remainder as a new node in the result curr.next = ListNode(val % 10) curr = curr.next + ##check the possible carry carry = val / 10 if carry > 0: curr.next = ListNode(carry) return head.next + + +if __name__ == '__main__': + s = Solution() + + ##create 2 linked list for 2 nums + ##num1=243: 3->4->2 + node_1, node_2, node_3=ListNode(2), ListNode(4), ListNode(3) + node_3.next,node_2.next=node_2,node_1 + ##num2=564: 4->6->5 + node_a, node_b, node_c=ListNode(5), ListNode(6), ListNode(4) + node_c.next,node_b.next=node_b,node_a + + ##show each linked list and the result + print node_3.traverse() + print '+' + print node_c.traverse() + print '=' + print s.addTwoNumbers(node_3, node_c).traverse() + diff --git a/python/003_Longest_Substring_Without_Repeating_Characters.py b/python/003_Longest_Substring_Without_Repeating_Characters.py index e6977c5..50d1972 100644 --- a/python/003_Longest_Substring_Without_Repeating_Characters.py +++ b/python/003_Longest_Substring_Without_Repeating_Characters.py @@ -1,3 +1,29 @@ +""" +Given a string, find the length of the longest substring without repeating +characters. + +Example 1: + +Input: "abcabcbb" +Output: 3 +Explanation: The answer is "abc", with the length of 3. + +Example 2: + +Input: "bbbbb" +Output: 1 +Explanation: The answer is "b", with the length of 1. + +Example 3: + +Input: "pwwkew" +Output: 3 +Explanation: The answer is "wke", with the length of 3. + +Note that the answer must be a substring, "pwke" is a subsequence and not a +substring. +""" + class Solution(object): # def lengthOfLongestSubstring(self, s): # """ @@ -56,11 +82,22 @@ class Solution(object): def lengthOfLongestSubstring(self, s): # https://leetcode.com/articles/longest-substring-without-repeating-characters/ + + ##build a char map: for every chars (max 256 ansi code), give a counter + ##default to -1. charMap = {} for i in range(256): charMap[i] = -1 + + ##get length of string to iterate ls = len(s) i = max_len = 0 + + ##for each chars in the string, update its corresponding counter in char + ##map with the index number in the string, until a duplicate char is met. + ##in that case: + ## record the index of duplication + ## deduct it from the max calculation for j in range(ls): # Note that when charMap[ord(s[j])] >= i, it means that there are # duplicate character in current i,j. So we need to update i. @@ -69,3 +106,9 @@ def lengthOfLongestSubstring(self, s): charMap[ord(s[j])] = j max_len = max(max_len, j - i + 1) return max_len + +if __name__ == '__main__': + # begin + s = Solution() + print s.lengthOfLongestSubstring("abcdaef") +