• 上面是以前搞的 iis 发布。

  • class Solution:
        def findContentChildren(self, g: List[int], s: List[int]) -> int:
            # 贪心算法
            g.sort()
            s.sort()
            i = 0
            j = 0
            while i < len(g) and j < len(s):
                if g[i] <= s[j]:
                    i += 1
                j += 1
            return i
    
  • class Solution:
        def repeatedSubstringPattern(self, s: str) -> bool:
            return s in (s + s)[1: len(s)*2 -1]
    
    
  • class Solution:
        def minMoves(self, nums: List[int]) -> int:
            sum = 0
            minmum = min(nums)
            for i in nums:
                sum += i-minmum
            return sum
    
  • class Solution:
        def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
            # 将所有正数作为数组下标,置对应数组值为负值。那么,仍为正数的位置即为(未出现过)消失的数字。
            for num in nums:
                nums[abs(num)-1] = -abs(nums[abs(num)-1])
            print(nums)
            return [i+1 for i,num in enumerate(nums) if num>0]
    
    
  • class Solution:
        def numberOfBoomerangs(self, points: List[List[int]]) -> int:
            result=0
            for m in points:
                dic={}
                for j in points:
                    distance=(m[0]-j[0])**2+(m[1]-j[1])**2
                    if distance not in dic:
                        dic[distance]=1
                    else:
                        dic[distance]+=1
                for val in dic.values():
                    if val>=2:
                        result+=val*(val-1)
            return result
    
  • class Solution:
        def compress(self, chars: List[str]) -> int:
            count = 1
    
            length = len(chars)
            for index in range(length-1, -1, -1):
                if index > 0 and chars[index] == chars[index-1]:
                    count += 1
                else:
                    end = index+count
                    chars[index: end] = [chars[index]] if count == 1 else [chars[index]] +  list(str(count))
                    count = 1
    
            return len(chars)
    
  • class Solution:
        def arrangeCoins(self, n: int) -> int:
            c = 0
            while n > c:
                c = c + 1
                n = n - c
            return c
    
  • # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def pathSum(self, root, sum):
            if root == None:
                return 0
            return self.count(root,sum)+self.pathSum(root.left,sum)+self.pathSum(root.right,sum)
        def count(self,root,sum):
            if root ==None:
                return 0
            return int(root.val == sum) + self.count(root.left,sum-root.val) + self.count(root.right,sum-root.val)
    
  • class Solution:
        def countSegments(self, s: str) -> int:
            return len(s.split())
    
  • class Solution:
        def addStrings(self, num1: str, num2: str) -> str:
            res = ""
            i, j, carry = len(num1) - 1, len(num2) - 1, 0
            while i >= 0 or j >= 0:
                n1 = int(num1[i]) if i >= 0 else 0
                n2 = int(num2[j]) if j >= 0 else 0
                tmp = n1 + n2 + carry
                carry = tmp // 10
                res = str(tmp % 10) + res
                i, j = i - 1, j - 1
            return "1" + res if carry else res
    
  • class Solution:
        def thirdMax(self, nums: List[int]) -> int:
            a = b = c = float('-inf')
    
            for num in nums:
                if a < num:
                    a , b , c = num , a , b 
                elif b < num < a:
                    b , c = num , b
                elif c < num < b:
                    c = num
            return a if c == float('-inf') else c
    
  • 412. Fizz Buzz[简单] at 2020年06月09日
    class Solution:
        def fizzBuzz(self, n: int) -> List[str]:
            res = []
            for i in range(1,n+1):
                res.append('Fizz'[i%3*len('Fizz')::]+'Buzz'[i%5*len('Buzz')::] or str(i))
            return res
    
  • class Solution:
        def longestPalindrome(self, s: str) -> int:
            d = {}
            for i in range(len(s)):
                if s[i] not in d:
                    d[s[i]] = 1
                else:
                    d[s[i]] += 1
            count = 0
            for j in d:
                count += d[j] // 2 * 2
                if d[j] % 2 == 1 and count % 2 == 0:
                    count += 1
            return count
    
  • class Solution:
        def toHex(self, num: int) -> str:
            return hex(num&0xFFFFFFFF)[2:]
    
  • # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def sumOfLeftLeaves(self, root: TreeNode) -> int:
            if root==None:
                return 0
            if root.left and root.left.left==None and root.left.right==None:
                return root.left.val+self.sumOfLeftLeaves(root.right)
            else:
                return self.sumOfLeftLeaves(root.left)+self.sumOfLeftLeaves(root.right)
    
  • class Solution:
        def readBinaryWatch(self, num: int) -> List[str]:
            bins = [str(bin(i))[2:].count('1') for i in range(60)]
            results = []
            for hour in range(12):
                for minute in range(60):
                    if bins[hour] + bins[minute] == num:
                        results.append('%d:%02d'%(hour, minute))
            return results
    
  • class Solution:
        def isSubsequence(self, s: str, t: str) -> bool:
            t = iter(t)
            return all(i in t for i in s) 
    
  • class Solution:
        def firstUniqChar(self, s: str) -> int:
            """
            遍历字符串中的字符,如果满足以下两个条件,则返回idx
            1. 该字符不存在于 s[:idx]
            2. 该字符不存在于 s[idx + 1:]
            :param s:
            :return:
            """
            for idx, c in enumerate(s):
                if c not in s[idx + 1:] and c not in s[:idx]:
                    return idx
            return -1
    
  • class Solution:
        def canConstruct(self, ransomNote: str, magazine: str) -> bool:
            a = list(ransomNote)
            b = list(magazine)
    
            try:
                for i in a:
                    b.remove(i)
                return True
            except:
                return False
    
  • # The guess API is already defined for you.
    # @param num, your guess
    # @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
    # def guess(num: int) -> int:
    
    class Solution:
        def guessNumber(self, n: int) -> int:
            lo, hi = 1, n
            while lo < hi:
                mid = lo + ((hi - lo) >> 1)
                if guess(mid) > 0:
                    lo = mid + 1
                else:
                    hi = mid 
            return lo
    
  • class Solution:
        def getSum(self, a: int, b: int) -> int:
            # 位运算
            MAX = 0x7fffffff
            MIN = 0x80000000
            mask = 0xFFFFFFFF
            while b != 0:
                a, b = (a ^ b) & mask, ((a & b) << 1)
            return a if a <= MAX else ~(a ^ mask)
    
  • class Solution:
        def isPerfectSquare(self, num: int) -> bool:
             return num**0.5 == int(num**0.5)
    
  • class Solution:
        def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
            inter = set(nums1) & set(nums2)
            l = []
            for i in inter:
                l += [i] * min(nums1.count(i), nums2.count(i))  
            return l
    
  • class Solution:
        def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
             return list(set(nums1) & set(nums2))