1.Two Sum --Easy
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].
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
add_one=target-nums[i] # temp+nums[i]=target
if nums.count(add_one) !=0: #判斷temp 是否存在
add_one_index=nums.index(add_one)
if add_one_index!=i: # 判斷是否是同一個數字同一個位置,例如6=3+3
return [i,add_one_index]
2. Add Two Numbers
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.
class ListNode:
''' __init__ 創建實例時,將一些屬性綁定,第一個參數self,表示創建的實例本身,
在創建實例的時候,需要把x等屬性綁定上去,不能傳入空的參數,必須傳入與__init__方法
匹配的參數,但是self不需要傳
'''
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
carry=0 # carry 表示相加的進位
phead=result=ListNode(0)
while l1!=None or l2!=None:
# l1 或 l2 確定加數add_one add_two
if l1!=None:
add_one=l1.val
else:
add_one=0
if l2!=None:
add_two=l2.val
else:
add_two=0
temp=add_one+add_two+carry
if temp>=10:
carry=1
sum=temp%10
else:
sum=temp
carry=0
temp_node=ListNode(sum)
result.next=temp_node
result=result.next
if l1!=None:
l1=l1.next
if l2!=None:
l2=l2.next
# 最後一位的進位
if carry>0:
temp_node=ListNode(carry)
result.next=temp_node
return phead.next
references:
class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
carry = 0
root = n = ListNode(0)
while l1 or l2 or carry:
v1 = v2 = 0
if l1:
v1 = l1.val
l1 = l1.next
if l2:
v2 = l2.val
l2 = l2.next
#divmod(a,b) 返回一個包含商和餘數的元組(a//b,a%b)
carry, val = divmod(v1+v2+carry, 10)
n.next = ListNode(val)
n = n.next
return root.next
def addTwoNumbers(self, l1, l2):
dummy = cur = ListNode(0)
carry = 0
while l1 or l2 or carry:
if l1:
carry += l1.val
l1 = l1.next
if l2:
carry += l2.val
l2 = l2.next
cur.next = ListNode(carry%10)
cur = cur.next
carry //= 10
return dummy.next
3.Longest Substring Without Repeating Characters
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.
references:
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
start=maxLength=0
usedChar={} # 字典
for i in range(len(s)): # 遍歷
if s[i] in usedChar and start<=usedChar[s[i]]: # 當前字符s[i]出現過,並且當前字符s[i]出現過的位置在start之前
start=usedChar[s[i]]+1 # start位置移動到字典中出現s[i]字符後移一位
else:
maxLength=max(maxLength,i-start+1)
usedChar[s[i]]=i # 建立並更新字典
return maxLength