声明:本人刚刚入门,算法能力欠佳,所写算法不是最优算法,只提供使用python3的读者以参考。
class Solution:
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
i=1
while iif nums[i]==nums[i-1]:
nums.pop(i)
else:
i+=1
return len(nums)
class Solution:
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
money=0
i=1
l = len(prices)
while iif prices[i]>prices[i-1]:
money += (prices[i]-prices[i-1])
i+=1
return money
class Solution:
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
l = len(nums)
if l>1:
k %= l
if k!=0:
nums[:] = nums[-k:]+nums[:-k]
class Solution:
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
s = set(nums)
if len(s)return True
else:
return False
class Solution:
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
l = int(len(nums)/2)
for i in range(l):
if nums[2*i+1]!=nums[2*i]:
return nums[2*i]
return nums[-1]
class Solution:
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
new_list = []
s_nums1 = set(nums1)
s_nums2 = set(nums2)
nums = s_nums1.intersection(s_nums2)
for i in nums:
c1 = nums1.count(i)
c2 = nums2.count(i)
if c1else:
new_list += [i]*c2
return new_list
class Solution:
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
digits_list = []
str_digits = ""
for i in digits:
str_digits += str(i)
new_digits = int(str_digits)
new_digits += 1
new_str_digit = str(new_digits)
for s in new_str_digit:
digits_list.append(s)
digits_list = list(map(int,digits_list))
return digits_list
class Solution:
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
n = 0
i = 0
while iif nums[i] == 0:
n += 1
nums.pop(i)
else:
i += 1
for j in range(n):
nums.append(0)
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
dic={}
for i in range(len(nums)):
if nums[i] in dic.keys():
return [dic[nums[i]],i]
else:
dic[target-nums[i]] = i
class Solution:
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
for i in range(9):
list1 = []
list2 = []
for j in range(9):
if board[i][j] != ".":
if board[i][j] not in list1:
list1.append(board[i][j])
else:
return False
if board[j][i] != ".":
if board[j][i] not in list2:
list2.append(board[j][i])
else:
return False
for h in range(3):
for l in range(3):
list3 = []
for i in range(3):
for j in range(3):
if board[3*h+i][3*l+j] != ".":
if board[3*h+i][3*l+j] not in list3:
list3.append(board[3*h+i][3*l+j])
else:
return False
return True
class Solution:
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
ele = []
hang = len(matrix)
lie = len(matrix[0])
for l in range(lie):
temp = []
for h in range(hang-1,-1,-1):
temp.append(matrix[h][l])
ele.append(temp)
matrix[:] = ele
print(matrix)