s-3-4

 303

class NumArray:

    def __init__(self, nums: List[int]):
        self.sums = [0]
        _sums = self.sums

        for num in nums:
            _sums.append(_sums[-1] + num)

    def sumRange(self, i: int, j: int) -> int:
        _sums = self.sums
        return _sums[j + 1] - _sums[i]

326

class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        if n <= 0:
            return False
        if n == 1:
            return True
        if n % 3 != 0:
            return False
        # 对余数进行递归处理, 如果递归过程中出现不能整除的情况, 就返回False
        return self.isPowerOfThree(n//3)

338

class Solution(object):
    def countBits(self, num):
        """
        :type num: int
        :rtype: List[int]
        """
        return [bin(i)[2:].count('1') for i in range(num+1)]

class Solution:
    def countBits(self, n: int) -> List[int]:
        def countOnes(x: int) -> int:
            ones = 0
            while x > 0:
                x &= (x - 1)
                ones += 1
            return ones
        
        bits = [countOnes(i) for i in range(n + 1)]
        return bits

342

class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        return n > 0 and (n & (n - 1)) == 0 and (n & 0xaaaaaaaa) == 0

344

class Solution:
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        l,r = 0,len(s)-1
        while l< r:
            s[l],s[r] = s[r],s[l]
            l = l + 1
            r = r - 1

345

class Solution:
    def reverseVowels(self, s: str) -> str:
        def isVowel(ch: str) -> bool:
            return ch in "aeiouAEIOU"
        
        n = len(s)
        s = list(s)
        i, j = 0, n - 1
        while i < j:
            while i < n and not isVowel(s[i]):
                i += 1
            while j > 0 and not isVowel(s[j]):
                j -= 1
            if i < j:
                s[i], s[j] = s[j], s[i]
                i += 1
                j -= 1
        
        return "".join(s)

349

class Solution(object):
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        nums1 = sorted(list(set(nums1)))
        nums2 = sorted(list(set(nums2)))
        l1 = 0
        res = []
        if len(nums1) > len(nums2):
            nums1,nums2 = nums2,nums1
        while l1 < len(nums1):
            if nums1[l1] in nums2:
                res.append(nums1[l1])
            l1 += 1
        return res

350

class Solution:
    def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
        return list((collections.Counter(nums1) & collections.Counter(nums2)).elements())
        '''  way one
        res = []
        for k in nums1:
            if k in nums2:
                res.append(k)
                nums2.remove(k)
        return res
        '''

367

class Solution(object):
    def isPerfectSquare(self, num):
        """
        :type num: int
        :rtype: bool
        """
        if num == 1:
            return True
        start = 1
        end = num
        while start < end:
            middle = (start + end) //2
            val = middle**2
            if val == num:
                return True
            elif val > num:
                end = middle
            else:
                start = middle + 1
        return False

374

class Solution(object):
    def guessNumber(self, n):
        """
        :type n: int
        :rtype: int
        """
        start = 1
        end = n
        while start < end:
            middle = (start + end) // 2
            if guess(middle) == -1:
                end = middle-1
            elif guess(middle) == 1:
                start = middle+1
            else:
                return middle
        return start

383

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        if len(ransomNote) > len(magazine):
            return False
        return not collections.Counter(ransomNote) - collections.Counter(magazine)

387

class Solution:
    def firstUniqChar(self, s: str) -> int:
        frequency = collections.Counter(s)
        for i, ch in enumerate(s):
            if frequency[ch] == 1:
                return i
        return -1

389

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        mask = 0
        for char in s+t:
            mask ^= ord(char)
        return chr(mask)

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        a = "".join([s,t])
        for ch in a:
            if a.count(ch) % 2 == 1:
                return ch

392

class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        j=0
        for i in range(len(s)):

            try:
                res=t.index(s[i],j) #从j开始
                j=res+1
            except:
                return False      
        return True

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值