leetcode-毎日パンチ日間の6

leetcode毎日パンチ

ギャングの言葉は自分のやる気を引き出すkuangbin取り付け:
誰が私の百、10人I百万、あるいは若者の夢を追いかけては、心の自信を持って、決してあきらめません!
2020年2月13日


自分のアイデアを記録問題、必ずしも最適解を持っています

インタビューの質問58 - IIの文字列は、回転を左に。

class Solution:
    def reverseLeftWords(self, s: str, n: int) -> str:
        return s[n:len(s)]+s[:n]

1108 IPアドレスが無効

解法一
class Solution:
    def defangIPaddr(self, address: str) -> str:
        return address.replace('.','[.]')

解法二
class Solution:
    def defangIPaddr(self, address: str) -> str:
        ans = ''
        for i in address:
            if i == '.':
                ans+='[.]'
            else:
                ans += i
        return ans

709は、小文字に変換しました

内置方法
class Solution:
    def toLowerCase(self, str: str) -> str:
        return str.lower()

class Solution:
    def toLowerCase(self, str: str) -> str:
        ans = ""
        for i in str:
            if i >= 'A' and i <= 'Z':
                ans += chr(ord(i) + 32)
            else:
                ans += i
        return ans

15進数が刻まれて1

class Solution:
    def hammingWeight(self, n: int) -> int:
        ans = 0
        while n > 0:
            ans += n & 1
            n = n >> 1
        return ans

インタビューの質問16.01。デジタル交換

class Solution:
    def swapNumbers(self, numbers: List[int]) -> List[int]:
        # 如何利用位运算交换两个数
        # swqp(a,b): 执行以下三步
        # a = a ^ b ; b = a ^ b;  a = a ^ b;
        numbers[0] = numbers[0] ^ numbers[1]
        numbers[1] = numbers[0] ^ numbers[1]
        numbers[0] = numbers[0] ^ numbers[1]
        return numbers

おすすめ

転載: www.cnblogs.com/wlw-x/p/12305666.html