leetcode-数组

118. 杨辉三角

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        result = []
        for i in range(numRows):
            now = [1]*(i+1)
            if i > 1:
                for i in range(1, i):
                    now[i] = pre[i - 1] + pre[i]
            result += [now]
            pre = now
        return result        

1122. 数组的相对排序

class Solution:
    def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
        res = []
        for i in arr2:
            while i in arr1:
                res.append(i)
                arr1.remove(i)     
        return res+sorted(arr1)

566重塑矩阵

class Solution:
    def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
        if r * c != len(nums) * len(nums[0]):
            return nums
        if r == len(nums):
            return nums
        
        val = []
        for i in nums:
            val += i
        
        res = []
        for i in range(0, len(val), c):
            res.append(val[i:i + c])
        return res

766. 托普利茨矩阵

class Solution:
    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
        #如何取出对角线元素转化为判断前一行除了最后和后一行除了第一是否相同
        for i in range(1, len(matrix)):
            if matrix[i - 1][:-1] != matrix[i][1:]:
                return False
        return True

1160. 拼写单词

class Solution:
    def countCharacters(self, words: List[str], chars: str) -> int:
        #如何判断一个字符串里面的元素是否在另外一个字符串当中
        
        d = {}
        num = 0
        for i in chars:
            if i not in d.keys():
                d[i] = 1
            else:
                d[i] += 1
        print(d)
        for w in words:
            for t in w:
                if t not in d.keys() or w.count(t) > d[t]:
                    break
            else:
                print(w)
                num += len(w)
        return num

169. 求众数

class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        dic = {c:nums.count(c) for c in set(nums)}
        
        val = max(dic.values())
        for k, v in dic.items():
            if v == val:
                return k

985. 查询后的偶数和

class Solution:
    def sumEvenAfterQueries(self, A: List[int], queries: List[List[int]]) -> List[int]:
        ret = []
        tmp = sum([i for i in A if i % 2 == 0])
        for querie in queries:
            old = A[querie[1]]
            A[querie[1]] += querie[0]
            new = A[querie[1]]
            if old % 2 == 0 and new % 2 == 0:
                tmp += querie[0]
            elif old % 2 == 0 and new % 2 == 1:
                tmp -= old
            elif old % 2 == 1 and new % 2 == 0:
                tmp += new
            else:
                tmp += 0            
            ret.append(tmp)
        return ret

283. 移动零

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        for i in range(nums.count(0)):
            nums.remove(0)
            nums.append(0)        

27. 移除元素

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        for i in range(nums.count(val)):
            nums.remove(val)

122. 买卖股票的最佳时机 II

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        tmp = []
        for i in range(1, len(prices)):
            tmp.append(prices[i] - prices[i - 1])
        return sum([i for i in tmp if i > 0])

1089.复写0

class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        """
        Do not return anything, modify arr in-place instead.
        """
        i = 0 
        while i < len(arr):
            if arr[i] == 0:
                arr.insert(i, 0)
                arr.pop()
                i += 2
            else:
                i += 1

485. 最大连续1的个数

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        s = ''.join([str(x) for x in nums])              
        return max(len(i) for i in s.split('0'))

448. 找到所有数组中消失的数字

class Solution:
    def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
        a = set(nums)
        ret = []
        for i in range(1, len(nums) + 1):
            if i not in a:
                ret.append(i)
        return ret

268. 缺失数字

class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        
        # ret = sum([i for i in range(len(nums) + 1)])
        n = len(nums)
        ret = (n * (n + 1))//2
        return (ret - sum(nums))

217. 存在重复元素

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        return len(set(nums)) != len(nums)

167. 两数之和 II - 输入有序数组

class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        d = {}
        for i, n in enumerate(numbers):
            if target - n in d.keys():
                return [d[target - n] + 1, i + 1]
            d[n] = i

896. 单调数列

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        a = []
        for i in range(1, len(A)):
            a.append(A[i] -A[i - 1])
        b = [0 if i >= 0 else -1 for i in a]
        c = [-1 if i <= 0 else 0 for i in a]
        print(b,c)
        if b.count(0) == len(A) - 1 or c.count(-1) == len(A) - 1:
            return True
        return False

977. 有序数组的平方

class Solution:
    def sortedSquares(self, A: List[int]) -> List[int]:
        return sorted(i * i for i in A)

1051. 高度检查器

class Solution:
    def heightChecker(self, heights: List[int]) -> int:
        b = sorted(heights)       
        cnt = 0
        for i in zip(b,heights):
            if i[0] - i[1] != 0:
                cnt += 1
        return cnt

832. 翻转图像

class Solution:
    def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:
        return [[1-i for i in l][::-1] for l in A]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值