s-4-5

 401

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

404

class Solution:
    def sumOfLeftLeaves(self, root: TreeNode) -> int:
        if not root:return 0
        if root.left and not root.left.left and not root.left.right:
            return root.left.val + self.sumOfLeftLeaves(root.right)
        return self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)

405

class Solution:
    def toHex(self, num: int) -> str:
        if num == 0:
            return '0'
        hexdigits = '0123456789abcdef'
        if num < 0:
            num += 2 ** 32
        ans = ''
        while num:
            ans += hexdigits[num % 16]
            num //= 16
        return ans[::-1]

409

class Solution:
    def longestPalindrome(self, s: str) -> int:
        ans = 0
        count = collections.Counter(s)
        for v in count.values():
            ans += v // 2 * 2
            if ans % 2 == 0 and v % 2 == 1:
                ans += 1
        return ans

412

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        l = []
        for i in range(1,n+1):
            tmp = ''
            f1 = i % 3
            f2 = i % 5
            if f1 == 0:
                tmp += "Fizz"
            if f2 == 0:
                tmp += "Buzz"
            if f1 != 0 and f2!=0:
                tmp = str(i)
            l.append(tmp)
        return l

414

class Solution:
    def thirdMax(self, nums: List[int]) -> int:
        nums.sort(reverse=True)
        diff = 1
        for i in range(1, len(nums)):
            if nums[i] != nums[i - 1]:
                diff += 1
                if diff == 3:  # 此时 nums[i] 就是第三大的数
                    return nums[i]
        return nums[0]

415

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

434

class Solution:
    def countSegments(self, s):
        segment_count = 0

        for i in range(len(s)):
            if (i == 0 or s[i - 1] == ' ') and s[i] != ' ':
                segment_count += 1

        return segment_count

441

class Solution:
    def arrangeCoins(self, n: int) -> int:
        left, right = 1, n
        while left < right:
            mid = (left + right + 1) // 2
            if mid * (mid + 1) <= 2 * n:
                left = mid
            else:
                right = mid - 1
        return left

453

class Solution:
    def minMoves(self, nums: List[int]) -> int:
        min_num = min(nums)
        res = 0
        for num in nums:
            res += num - min_num
        return res

455

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        n, m = len(g), len(s)
        i = j = count = 0

        while i < n and j < m:
            while j < m and g[i] > s[j]:
                j += 1
            if j < m:
                count += 1
            i += 1
            j += 1
        
        return count

459

class Solution(object):
    def repeatedSubstringPattern(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if len(s) == 1:
            return False
        for i in range(1,len(s)//2+1):
            if len(s)%i == 0:
                if s[:i]*(len(s)//i) == s:
                    return True
        return False

461

class Solution(object):
    def hammingDistance(self, x, y):
        """
        :type x: int
        :type y: int
        :rtype: int
        """
        #更快更好理解 20ms
        x=bin(x).replace('0b','')
        y=bin(y).replace('0b','')
        m_length = max(len(x),len(y))
        newx= '0'*(m_length-len(x))+x
        newy= '0'*(m_length-len(y))+y
        count=0
        for i in range(m_length):
            if newx[i]!=newy[i]:
                count+=1
        return count
        
        
        
        '''
        #24 ms
        hamming_distance = 0 
        s = str(bin(x^y)) 
        for i in range(2,len(s)): 
            if int(s[i]) is 1: 
                hamming_distance += 1 
        return hamming_distance 
        '''

463

class Solution:
    def islandPerimeter(self, grid):
        ret = 0
        row = len(grid)
        col = len(grid[0])
        distances = [(-1, 0), (1, 0), (0, 1), (0, -1)]
        for i in range(row):
            for j in range(col):
                if grid[i][j] == 0:
                    continue
                nums = 4
                for distance in distances:
                    new_x, new_y = i + distance[0], j + distance[1]
                    if 0 <= new_x < row and 0 <= new_y < col and grid[new_x][new_y] == 1:
                        nums -= 1
                ret += nums
        return ret

476

class Solution:
    def findComplement(self, num: int) -> int:
        highbit = 0
        for i in range(1, 30 + 1):
            if num >= (1 << i):
                highbit = i
            else:
                break
        
        mask = (1 << (highbit + 1)) - 1
        return num ^ mask

class Solution:
    def findComplement(self, num):
        bin_num = bin(num)[2:]
        bin_ans = map(lambda x: '0' if x == '1' else '1', bin_num)
        return int(''.join(bin_ans), 2)

482

class Solution:
    def licenseKeyFormatting(self, s: str, k: int) -> str:
        ans = list()
        cnt = 0

        for i in range(len(s) - 1, -1, -1):
            if s[i] != "-":
                ans.append(s[i].upper())
                cnt += 1
                if cnt % k == 0:
                    ans.append("-")
        
        if ans and ans[-1] == "-":
            ans.pop()
        
        return "".join(ans[::-1])

485

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        maxCount = count = 0

        for i, num in enumerate(nums):
            if num == 1:
                count += 1
            else:
                maxCount = max(maxCount, count)
                count = 0
        
        maxCount = max(maxCount, count)
        return maxCount

492

class Solution:
    def constructRectangle(self, area: int) -> List[int]:
        w = int(sqrt(area))
        while area % w:
            w -= 1
        return [area // w, w]

495

class Solution:
    def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
        ans, expired = 0, 0
        for i in range(len(timeSeries)):
            if timeSeries[i] >= expired:
                ans += duration
            else:
                ans += timeSeries[i] + duration - expired
            expired = timeSeries[i] + duration
        return ans

496

class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        m, n = len(nums1), len(nums2)
        res = [0] * m
        for i in range(m):
            j = nums2.index(nums1[i])
            k = j + 1
            while k < n and nums2[k] < nums2[j]:
                k += 1
            res[i] = nums2[k] if k < n else -1
        return res

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值