class Solution(object):
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
newgrid = [list(x) for x in grid]
ans = 0
if not len(newgrid):
return ans
m = len(newgrid)
n = len(newgrid[0])
for i in range(m):
for j in range(n):
if newgrid[i][j ...Other articles
- read more
199. Binary Tree Right Side View
read more# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def rightSideView(self, root): """ :type root: TreeNode :rtype: List[int] """ if not root: return [] ans = [] queue = [root] while queue: for i in range(len(queue ...
198. House Robber
read moreclass Solution(object): def rob(self, nums): """ :type nums: List[int] :rtype: int """ #dp[i] = max(dp[i-1], dp[i-2] + nums[i]) if not nums: return 0 if len(nums) <= 2: return max(nums) dp = [0] * len(nums) dp[0] = nums[0] dp[1] = max(nums[0], nums[1]) i ...
191. Number of 1 Bits
read moreclass Solution(object): def hammingWeight(self, n): """ :type n: int :rtype: int """ count = 0 for i in range(32): if (n>>i)%2 == 1: count += 1 return count
190. Reverse Bits
read more# -*- coding:utf8 -*- class Solution: # @param n, an integer # @return an integer def reverseBits(self, n): new = 0 for i in range(32): new = (new << 1) | ((n >> i) % 2) # n右移并%2得到二进制表达下的每位值。然后,new左移得到xxx0,'|' n的每位值,即xxx0可能变成xxx1。 return new if __name__ == "__main__": print Solution().reverseBits(43261596)
189. Rotate Array
read moreclass Solution(object): def rotate(self, nums, k): """ :type nums: List[int] :type k: int :rtype: void Do not return anything, modify nums in-place instead. """ # san bu fanzhuan fa k %= len(nums) if k == 0: return start1= 0 end1 = len(nums)-k-1 for i in range((end1-start1)/2 ...
187. Repeated DNA Sequences
read moreclass Solution(object): def findRepeatedDnaSequences(self, s): """ :type s: str :rtype: List[str] """ n = len(s) d = {} res = [] for i in range(n): substr = s[i:i + 10] d[substr] = d.get(substr, 0) + 1 for key, val in d.items(): if val > 1: res.append(key) return res if ...
186. Reverse Words in a String II
read more# Time: O(n) # Space:O(1) # # Given an input string, reverse the string word by word. # A word is defined as a sequence of non-space characters. # # The input string does not contain leading or trailing spaces # and the words are always separated by a single space. # # For example, # Given s ...
179. Largest Number
read more# -*- coding:utf8 -*- class Solution: # @param {integer[]} nums # @return {string} def largestNumber(self, nums): res = '' while nums: item = nums.pop(self.help(nums)) res += str(item) if (res[0]=='0'): return '0' return res def help(self, nums): # 如果a+b串大于b+a串,那么a比较大,反之b比较大。 strnums = [str(x) for x in nums] index ...
Page 1 / 21 »