1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "1"Output: "100"Example 2:…
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 解题思路: JAVA实现如下: static public String addBinary(String a, String b) { if (a.length() < b.length()) { String temp…
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 详细一位一位地加即可了,考虑进位的问题.还有最后记得把生成的string反过来再返回,由于我们是从最低位開始加的. public class Solution { public String addBinary(String a…
题目说明 Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 题目分析 第一感觉是前序遍历,顺便打算在这题练习一下昨天学到的二级指针的写法XD,调的时候bug挺多的,可读性贼差,指针还是慎用啊-- 以下为个人实现(C++,12ms):…
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解: 题意比较清楚, 找到从root出发最长的一条路径的长度. 采用DFS即可. 相似的一道题: Minimux Depth of Binary Tree 解法: http://…
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题意很清楚,找到二叉树中深度最短的一条路径,DFS(貌似是面试宝典上的一道题) 类似的一道题:http://www.cnblogs.com/double-win/p/3737262…
给定两个二进制字符串,返回他们的和(用二进制表示).案例:a = "11"b = "1"返回 "100" .详见:https://leetcode.com/problems/add-binary/description/ Java实现: class Solution { public String addBinary(String a, String b) { String s=""; int c=0; int i=a.len…
题目 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 分析 一个简单的字符串相加,该题目要注意两点: 字符位的求和计算,必须转换为整型,即: 如下利用'0'字符作为中间转换,才得到正确结果. int temp = (a[i]-'0') + (b[i]-'0'); char c…
1.题目描述 2.分析 深度优先. 3.代码 int ans; int diameterOfBinaryTree(TreeNode* root) { ans = ; depth(root); ; } int depth(TreeNode *root){ if (root == NULL) ; int L = depth(root->left); int R = depth(root->right); ans = max(ans, L+R+); ; }…
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. Example 1: Input…