def removeElement(self, nums, val):
last_index = len(nums)-1
i = 0
while (i <= last_index):
if nums[i] == val:
nums.remove(val) ## 因為remove 掉 後一個數自動往前
last_index -= 1
else:
i+=1
print(nums,i)
return len(nums)
def removeElement(self, nums, val):
last_index = len(nums)-1
i,j = 0 ,0
while ( j <=last_index ):
if nums[j] == val:
# 把要刪除的丟到最後
nums[j],nums[last_index] = nums[last_index],nums[j]
last_index -=1 # 最後index-1 代表不會在找尋到他
continue
else:
i += 1
j+=1
print(nums[:i])
return i
LeetCode
remove
My Solution O(n^2) def twoSum(self, nums:[int], target: int): # 以 for 寫兩層巢狀 for 去尋找 第一層數字 = i length = len(nums) ans = [int]*2 for i in range(length): # 1.target_num = target 數字 減去 第一層巢狀 for 的值 target_num = target - nums[i] ans[0] = i # 2-1. 如果其數字 等於第一層巢狀 for 值 則 跳下一個數字
Jan 13, 2022[TOC] 台泥 1101 亞泥 1102 食品(傳產) 大成(飼料47%, 肉品20%) 1210 統一 1216 泰山 1218
Jul 19, 2020Contents [TOC] Hot Key in codepen Ctrl+D 可選取同一個字詞 (先用滑鼠選) Ctrl+/ comment Ctrl+L whole line select Tab indent Shift+Tab cancel indent
Feb 28, 2020or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up