From d0ead92b0a974a6769f0a0dec8f5a08ea6b51e9a Mon Sep 17 00:00:00 2001 From: ChunyuPCY <18896534787@163.com> Date: Fri, 23 Aug 2019 23:07:33 +0800 Subject: [PATCH] 2019-8-23 --- 1. Two Sum/Document.md | 12 ++ 1. Two Sum/code.py | 35 ++++++ 2. Add Two Numbers/Document.md | 11 ++ 2. Add Two Numbers/code.py | 113 ++++++++++++++++++ .../Document.md | 24 ++++ .../code.py | 28 +++++ 4. Median of Two Sorted Arrays/Document.md | 22 ++++ 4. Median of Two Sorted Arrays/code.py | 24 ++++ 8 files changed, 269 insertions(+) create mode 100644 1. Two Sum/Document.md create mode 100644 1. Two Sum/code.py create mode 100644 2. Add Two Numbers/Document.md create mode 100644 2. Add Two Numbers/code.py create mode 100644 3. Longest Substring Without Repeating Characters/Document.md create mode 100644 3. Longest Substring Without Repeating Characters/code.py create mode 100644 4. Median of Two Sorted Arrays/Document.md create mode 100644 4. Median of Two Sorted Arrays/code.py diff --git a/1. Two Sum/Document.md b/1. Two Sum/Document.md new file mode 100644 index 0000000..2d0df8d --- /dev/null +++ b/1. Two Sum/Document.md @@ -0,0 +1,12 @@ +## 题目 +Given an array of integers, return **indices** of the two numbers such that they add up to a specific target. + +You may assume that each input would have **exactly** one solution, and you may not use the same element twice. + +**Example:** +``` +Given nums = [2, 7, 11, 15], target = 9, + +Because nums[0] + nums[1] = 2 + 7 = 9, +return [0, 1]. +``` \ No newline at end of file diff --git a/1. Two Sum/code.py b/1. Two Sum/code.py new file mode 100644 index 0000000..6e984fc --- /dev/null +++ b/1. Two Sum/code.py @@ -0,0 +1,35 @@ +# class Solution: +# def twoSum(self, nums: List[int], target: int) -> List[int]: +# for i in range(len(nums)-1): +# for j in range(i + 1, len(nums)): +# if nums[i] + nums[j] == target: +# return [i, j] +from typing import List + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + """ + :param nums: + :param target: + :return: + """ + seen = {} + for i, v in enumerate(nums): + + remaining = target - v + if remaining in seen: + return [seen[remaining], i] + + seen[v] = i + + return [] + + +if __name__ == '__main__': + solution = Solution() + nums = [2, 7, 11, 15] + target = 9 + + result = solution.twoSum(nums, target) + + print(result) diff --git a/2. Add Two Numbers/Document.md b/2. Add Two Numbers/Document.md new file mode 100644 index 0000000..28f2b7a --- /dev/null +++ b/2. Add Two Numbers/Document.md @@ -0,0 +1,11 @@ +## 题目 +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. + +**Example:** +``` +Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) +Output: 7 -> 0 -> 8 +Explanation: 342 + 465 = 807. +``` \ No newline at end of file diff --git a/2. Add Two Numbers/code.py b/2. Add Two Numbers/code.py new file mode 100644 index 0000000..88d5af2 --- /dev/null +++ b/2. Add Two Numbers/code.py @@ -0,0 +1,113 @@ +from typing import List +import json + +class ListNode: + """ Definition for singly-linked list. """ + def __init__(self, x): + self.val = x + self.next = None + + +class Solution: + def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: + head = ListNode(l1.val + l2.val) + ptr = head + flag = 0 + while (l1 is not None) and (l2 is not None): + add_value = l1.val + l2.val + flag + if add_value >= 10: + flag = 1 + ptr.val = add_value - 10 + else: + flag = 0 + ptr.val = add_value + + l1, l2 = l1.next, l2.next + if (l1 is not None) or (l2 is not None): + ptr.next = ListNode(-1) + ptr = ptr.next + + if l1 is None: + l1 = l2 + + if l1 is not None: + if flag == 1: + while l1 is not None: + add_value = l1.val + flag + if add_value >= 10: + flag = 1 + ptr.val = add_value - 10 + else: + flag = 0 + ptr.val = add_value + l1 = l1.next + if l1 is not None: + ptr.next = ListNode(-1) + ptr = ptr.next + else: + ptr.val = l1.val + ptr.next = l1.next + + if flag == 1: + ptr.next = ListNode(1) + + return head + + +def stringToIntegerList(input): + return json.loads(input) # list object + + +def stringToListNode(input): + # Generate list from the input + numbers = stringToIntegerList(input) + + # Now convert that list into linked list + dummyRoot = ListNode(0) + ptr = dummyRoot + for number in numbers: + ptr.next = ListNode(number) + ptr = ptr.next + + ptr = dummyRoot.next + return ptr + + +def listNodeToString(node): + if not node: + return "[]" + + result = "" + while node: + result += str(node.val) + ", " + node = node.next + + # print(result) + return "[" + result[:-2] + "]" + + +def main(): + import sys + import io + def readlines(): + for line in io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8'): + yield line.strip('\n') + + lines = readlines() + while True: + try: + line = next(lines) + l1 = stringToListNode(line) + line = next(lines) + l2 = stringToListNode(line) + + ret = Solution().addTwoNumbers(l1, l2) + + out = listNodeToString(ret) + print(out) + except StopIteration: + break + + +if __name__ == '__main__': + main() diff --git a/3. Longest Substring Without Repeating Characters/Document.md b/3. Longest Substring Without Repeating Characters/Document.md new file mode 100644 index 0000000..e0f2661 --- /dev/null +++ b/3. Longest Substring Without Repeating Characters/Document.md @@ -0,0 +1,24 @@ +## 题目 +Given a string, find the length of the longest substring without repeating characters. + +**Example1:** +``` +Input: "abcabcbb" +Output: 3 +Explanation: The answer is "abc", with the length of 3. +``` + +**Example2:** +``` +Input: "bbbbb" +Output: 1 +Explanation: The answer is "b", with the length of 1. +``` + +**Example3:** +``` +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. +``` diff --git a/3. Longest Substring Without Repeating Characters/code.py b/3. Longest Substring Without Repeating Characters/code.py new file mode 100644 index 0000000..70789c7 --- /dev/null +++ b/3. Longest Substring Without Repeating Characters/code.py @@ -0,0 +1,28 @@ +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + w_size = 1 + start = 0 + + maxlen = 0 + + while w_size <= len(s): + substr = s[start: w_size + start] + if len(set(substr)) < len(substr): + if w_size + start == len(s): + break + start += 1 + else: + maxlen += 1 + w_size += 1 + start = 0 + + return maxlen + + +if __name__ == '__main__': + solution = Solution() + # string = "bbbbbb" + string = "abcabcbb" + # string = "pwwkew" + print((solution.lengthOfLongestSubstring(string))) + diff --git a/4. Median of Two Sorted Arrays/Document.md b/4. Median of Two Sorted Arrays/Document.md new file mode 100644 index 0000000..b0bd846 --- /dev/null +++ b/4. Median of Two Sorted Arrays/Document.md @@ -0,0 +1,22 @@ +## 题目 +There are two sorted arrays nums1 and nums2 of size m and n respectively. + +Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). + +You may assume nums1 and nums2 cannot be both empty. +**Example1:** +``` +nums1 = [1, 3] +nums2 = [2] + +The median is 2.0 +``` + +**Example2:** +``` +nums1 = [1, 2] +nums2 = [3, 4] + +The median is (2 + 3)/2 = 2.5 +``` + diff --git a/4. Median of Two Sorted Arrays/code.py b/4. Median of Two Sorted Arrays/code.py new file mode 100644 index 0000000..bb65c61 --- /dev/null +++ b/4. Median of Two Sorted Arrays/code.py @@ -0,0 +1,24 @@ +from typing import List + + +class Solution: + + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + m_index = [] + l_1, l_2 = len(nums1), len(nums2) + aver_len = l_1 + l_2 // 2 + if aver_len == 0: + m_index.extend([aver_len-1, aver_len]) + else: + m_index.append(aver_len) + + + + +if __name__ == '__main__': + solution = Solution() + nums1 = [1, 2] + nums2 = [3, 4] + + print((solution.findMedianSortedArrays(nums1, nums2))) +