leetcode 매일 펀치
: 갱스터 단어가 자신에게 동기를 부여 kuangbin 부착
절대 포기하지, 누가 나 백, 십명 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