# -*- coding: utf-8 -*-
"""@created on 2018/6/3 17:06
@author: zhifengfang
"""# 排列陣列刪除重複項
def removeduplicates(nums):
if len(nums) <= 1:
return len(nums)
i = 1
while len(nums) != i:
if nums[i] == nums[i - 1]:
del nums[i]
i -= 1
i += 1
return len(nums)
# 買賣**最佳時機2
def maxprofit(prices):
max = 0
if len(prices) <= 1:
return 0
for i in range(len(prices) - 1):
if prices[i] < prices[i + 1]:
max += prices[i + 1] - prices[i]
return max
# 旋轉陣列
def rotate(nums, k):
# nums = nums[-k:] + nums[:k + 1]
# print(nums)
if len(nums) > 1:
k = k % len(nums)
if k != 0:
temp = nums[-k:]
nums[k:] = nums[:len(nums) - k]
nums[0:k] = temp
print(nums)
# 判斷陣列中是否有重複元素
def containsduplicate(nums):
# if len(nums)>len(set(nums)):
# return true
# return false
for num in nums:
if nums.count(num) > 1:
return true
return false
# 獲得裡面只出現一次的數字
def singlenumber(nums):
numcounts = {}
result =
for num in nums:
numcounts[num] = numcounts.get(num, 0) + 1
for key in numcounts.keys():
if numcounts.get(key) == 1:
break
return result[0]
# 兩個陣列的交集 ii
def intersect(nums1, nums2):
if len(nums2) < len(nums1):
nums1, nums2 = nums2, nums1
newnums =
i = 0
while i < len(nums1):
j = 0
while j < len(nums2):
if nums1[i] == nums2[j]:
del nums1[i], nums2[j]
i -= 1
j -= 1
break
j += 1
i += 1
return newnums
# print(intersect([9],[7,8,3,9,0,0,9,1,5]))
# 加1
def plusone(digits):
strdigits = ''
for example in digits:
strdigits += str(example)
strdigits = int(strdigits) + 1
listdigits = [int(str) for str in str(strdigits)]
return listdigits
# print(plusone([1, 2, 3]))
# 移動0
def movezeroes(nums):
# for i in range(len(nums)):
i = 0
zeroescount = 0
while i + zeroescount < len(nums):
if nums[i] == 0:
nums[i:] = nums[i + 1:] + [0]
i -= 1
zeroescount += 1
i += 1
return nums
# 兩數和
def twosum(nums, target):
d = {}
for x in range(len(nums)):
a = target - nums[x]
if nums[x] in d:
return d[nums[x]], x
else:
d[a] = x
nums = [3, 2, 4]
target = 6
# print(twosum(nums, target))
def isxt(strs):
strset = set(strs)
for s in strset:
if s != ".":
if strs.count(s) > 1:
return false
return true
# 有效的數獨
def isvalidsudoku(board):
for i in range(9):
boardlie = [example[i] for example in board]
key1 = int(i / 3) * 3 + 1
key2 = 1 + (i % 3) * 3
boardge = [board[key1 - 1][key2 - 1], board[key1 - 1][key2], board[key1 - 1][key2 + 1],
board[key1][key2 - 1], board[key1][key2], board[key1][key2 + 1],
board[key1 + 1][key2 - 1], board[key1 + 1][key2], board[key1 + 1][key2 + 1]]
if isxt(board[i]) == false:
return false
if isxt(boardlie) == false:
return false
if isxt(boardge) == false:
return false
return true
board = [[".", ".", "4", ".", ".", ".", "6", "3", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."],
["5", ".", ".", ".", ".", ".", ".", "9", "."],
[".", ".", ".", "5", "6", ".", ".", ".", "."],
["4", ".", "3", ".", ".", ".", ".", ".", "1"],
[".", ".", ".", "7", ".", ".", ".", ".", "."],
[".", ".", ".", "5", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."]]
# print(isvalidsudoku(board))
# 旋轉影象
def rotate(matrix):
for i in range(len(matrix)):
for j in range(i+1,len(matrix)):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
matrix[i].reverse()
print(matrix)
ma = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]rotate(ma)
Leetcode初級演算法
不是很難的一道動態規劃的題,感覺做多了就記住了。class solution return dp n 此題想法就是,只要後面買的減去前面買的能大於0,就算在內,每次買完和max比較,大於max就記錄為max,如果買的sum小於0了,重新開始買,sum記為0 class solution if sum...
Leetcode 初級演算法02
了解的知識 1.空間複雜度 空間複雜度 space complexity 是對乙個演算法在執行過程中臨時占用儲存空間大小的量度。這樣子理解起來有點困難,我們又了解到當乙個演算法的空間複雜度為乙個常量,即不隨被處理資料量n的大小而改變時,可表示為o 1 舉兩個例子 a.陣列的隨機訪問就是o 1 b.鍊...
LeetCode初級演算法C
class solution temp len s i else if s i 0 s i 9 int i 0,j len 1 while i j else break if i j return true else return false 暴力求解,開闢了100010的陣列。class solu...