-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d0ead92
Showing
8 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
3. Longest Substring Without Repeating Characters/Document.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
|