Leetcode随便做做

63. Unique Paths II

https://leetcode.com/problems/unique-paths-ii/description/

class Solution(object):
    def uniquePathsWithObstacles(self, obstacleGrid):
        """
        :type obstacleGrid: List[List[int]]
        :rtype: int
        """
        m = len(obstacleGrid)
        n = len(obstacleGrid[0])
        path = [[0 for a in range(n)] for b in range(m)]
        for i in range(m):
            if (obstacleGrid[i][0] == 1):
                break
            path[i][0] = 1
        for i in range(n):
            if (obstacleGrid[0][i] == 1):
                break
            path[0][i] = 1
        for i in range(1, m):
            for j in range(1, n):
                if (obstacleGrid[i][j] == 1):
                    path[i][j] = 0
                else:
                    path[i][j] = path[i - 1][j] + path[i][j - 1]
        return path[m - 1][n - 1]

104. Maximum Depth of Binary Tree

https://leetcode.com/problems/maximum-depth-of-binary-tree/description/

# 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 maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root == None:
            return 0
        else:
            return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))

222. Count Complete Tree Nodes

https://leetcode.com/problems/count-complete-tree-nodes/description/

# 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 countNodes(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if (root == None):
            return 0
        path = []
        while (root != None):
            if root.left == None and root.right == None:
                break
            if self.depth(root.left) > self.depth(root.right):
                root = root.left
                path.append(0)
            else:
                root = root.right
                path.append(1)
        l = len(path)
        index = 0
        for i, p in enumerate(path):
            if p == 1:
                index += 2 ** (l - i -1)
        num = 2 ** len(path) + index
        return num
        
    def depth(self, root):
        tmp = root
        max_depth = 0
        while (tmp != None):
            max_depth += 1
            tmp = tmp.left
        return max_depth

476. Number Complement

https://leetcode.com/problems/number-complement/#/description

public class Solution {
    public int findComplement(int num) {
        String s = Integer.toBinaryString(num);
        int ret = 0;
        for (int i = 0; i < s.length(); i ++) {
            if (s.charAt(i) == '0') {
                ret += Math.pow(2, s.length() - i - 1);
            }
        }       
        return ret;
    }
}

492. Construct the Rectangle

https://leetcode.com/problems/construct-the-rectangle/#/description

public class Solution {
    public int[] constructRectangle(int area) {
        int []ret = {0, 0};
        int L, W;
        for (W = 1; W <= Math.pow(area, 0.5); W ++) {
            L = area / W;
            if (L * W == area) {
                ret[0] = L;
                ret[1] = W;
            }
        }
        return ret;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,779评论 0 33
  • 初开的十月 沉静 喧嚣 忘却惆怅 唯有菊花执意添寒 岁月澄明处 如阳光一般 菊叶婆娑 于深秋里阑珊 归鸿几...
    _老猫_阅读 402评论 0 1
  • 文/魏千洛 昨天狠狠吐槽了团贷网,作为半个互联网相关的从业者,真的无法忍受一个用户体验如此差的平台,但是,看在收益...
    千洛读书阅读 4,707评论 8 8
  • 我去给iPad换屏,正等着,有个女生急匆匆地跑过来,拿着一个碎了屏的手机,她满脸青白,紧张不已。 柜台是夫妻档,老...
    卢璐说阅读 968评论 4 20