您的位置:首页 > 编程语言 > Python开发

leetcode-python 第十四周

2016-09-27 17:31 225 查看
截至今天,把course上的基础题都做了一遍了。后面估计就要挑战中等的题目了,加油!!

1. Guess Number Higher or Lower [28ms]

class Solution(object):
def guessNumber(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1: return 1

low, high = 1, n + 1
while low < high:
mid = (low + high) >> 1
result = guess(mid)
if result == 0:
return mid
if result < 0:
high = mid
else:
low = mid + 1
return low
# The guess API is already defined for you.
# @param num, your guess
# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
# def guess(num):


2. Binary Tree Leverl Order Traversal [106ms]

# 方法1:队列加上俩个数记录前面和后面的节点数,方便分层
class Solution(object):
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if root == None:
return []
queue = [root]
pre = 1
ans = []
while len(queue) != 0:
tmp = []
now = 0
while pre != 0:
node = queue.pop()
tmp.append(node.val)
pre -= 1
if node.left != None:
queue.insert(0, node.left)
now += 1
if node.right != None:
queue.insert(0, node.right)
now += 1
ans.append(tmp)
pre = now
return ans
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None


3. Valid Perfect Square [45ms]

class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
low, high = 1, num+1
while low < high:
mid = (low + high) >> 1
sq = mid * mid
if sq == num:
return True
if sq < num:
low = mid + 1
else:
high = mid
return False


4. Reverse Bits [66ms]

# 方法1:2的(31-i)次方相加 [66ms]
# 方法2:别人的一行代码,先format成字符串二进制形式
# 然后kfill填充,最后逆序转整形 [52ms]
# 方法3:更简洁
class Solution:
# @param n, an integer
# @return an integer
def reverseBits(self, n):
ans = 0
for i in range(32):
tmp = n - 2 * (n >> 1)
if tmp == 1:
ans += 2 ** (31 - i)
n = n >> 1
return ans

class Solution:
def reverseBits(self, n):
return int(format(n, 'b').zfill(32)[::-1], 2)

class Solution(object):
def reverseBits(self, n):
return int('{0:032b}'.format(n)[::-1], 2)


5. Power Of Two [85ms]

# 方法1:统计1的个数,主要不能为负数而且数目只能是1 [85ms]
# 方法2:别人家的方法1实现
# 方法3:如果一个数是2的指数,那么它肯定是最高位为1,其余位为0,所以n-1就是除了最高位
# 其余都是1,再和自己与运算就可以。
class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
if n < 0 :
return False

nb = format(n, 'b')
count = 0
for i in range(len(nb)):
if nb[i] == '1':
count += 1
if count >= 2:
return False
if count == 1:
return True
else:
return False
class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
cnt = 0
while n > 0:
cnt += (n & 1)
n >>= 1
return cnt == 1
class Solution(object):
def isPowerOfTwo(self, n):
return (n > 0) and ((n & (n - 1)) == 0)


6. Number Of 1 Bits [52ms]

class Solution(object):
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
b = format(n, 'b').zfill(32)
ans = 0
for i in range(len(b)):
if b[i] == '1':
ans += 1
return ans


7. First Bad Version [45ms]

class Solution(object):
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return n
low, high = 1, n
while high - low > 1:
mid = int((low + high) / 2)
if isBadVersion(mid):
high = mid
else:
low = mid + 1
if isBadVersion(low):
return low
else:
return high
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
# def isBadVersion(version):
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python leetcode