Skip to content

Commit

Permalink
2019-8-23
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunyuPCY committed Aug 23, 2019
0 parents commit d0ead92
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 0 deletions.
12 changes: 12 additions & 0 deletions 1. Two Sum/Document.md
Original file line number Diff line number Diff line change
@@ -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].
```
35 changes: 35 additions & 0 deletions 1. Two Sum/code.py
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions 2. Add Two Numbers/Document.md
Original file line number Diff line number Diff line change
@@ -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.
```
113 changes: 113 additions & 0 deletions 2. Add Two Numbers/code.py
Original file line number Diff line number Diff line change
@@ -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()
24 changes: 24 additions & 0 deletions 3. Longest Substring Without Repeating Characters/Document.md
Original file line number Diff line number Diff line change
@@ -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.
```
28 changes: 28 additions & 0 deletions 3. Longest Substring Without Repeating Characters/code.py
Original file line number Diff line number Diff line change
@@ -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)))

22 changes: 22 additions & 0 deletions 4. Median of Two Sorted Arrays/Document.md
Original file line number Diff line number Diff line change
@@ -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
```

24 changes: 24 additions & 0 deletions 4. Median of Two Sorted Arrays/code.py
Original file line number Diff line number Diff line change
@@ -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)))

0 comments on commit d0ead92

Please sign in to comment.