leetcode-每日打卡-day 6
leetcode 每日打卡
those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out
那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~
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