【LeetCode】67 - Add Binary

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

Solution:二进制加法,和为2进1,和为3进1留1;

 1 class Solution {
 2 public:
 3     string addBinary(string a, string b) {
 4         int sizea=a.size(),sizeb=b.size();
 5         if(sizea<sizeb)return addBinary(b,a);
 6         int n=sizea-sizeb;
 7         string helper(n,‘0‘);
 8         b = helper + b;
 9         int carry=0;
10         for(int i=sizea-1;i>=0;i--){
11             int sum=(a[i]-‘0‘)+(b[i]-‘0‘)+carry;
12             if(sum==0);
13             else if(sum==1){
14                 a[i]=‘1‘;
15                 carry=0;
16             }else if(sum==2){
17                 a[i]=‘0‘;
18                 carry=1;
19             }else if(sum==3){
20                 a[i]=‘1‘;
21                 carry=1;
22             }
23         }
24         if(carry==1)a=‘1‘+a;
25         return a;
26     }
27 };
时间: 08-05

【LeetCode】67 - Add Binary的相关文章

【LeetCode】67. Add Binary 解题小结

题目: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". class Solution { public: string addBinary(string a, string b) { string res(a.size()+b.size(),'0'); int i = a.size

【leetcode】998. Maximum Binary Tree II

题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree. Just as in the previous problem, the given tree was constructed from an list A (root = Construct(A)) recursively with

【LeetCode】98. Validate Binary Search Tree

Difficulty:Medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/validate-binary-search-tree/ Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a nod

【LeetCode】98. Validate Binary Search Tree -判断是否为二叉排序树

一.描述: 二.思路: 二叉排序树(BST),中序遍历的结果一定是非递减序列(来自百度百科): 本题中对于BST的定义是要么大于,要么小与,即遍历结果只能是递增序列,故可以通过判断中序遍历的结果序列是否是递增序列,来判断是否为合法BST: 另一种方法是使用递归: 三.代码: 1.非递归,通过中序遍历结果判断: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left;

【LeetCode】226. Invert Binary Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51527554 Subject 出处:https://leetcode.com/problems/invert-binary-tree/ Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Explain 该题目相当简单,就是一个简单的二叉树左右对换结点. Solution solution 1 递归

【LeetCode】002 Add Two Numbers

题目:LeetCode 002 Add Two Numbers 题意:给定表达非负数的两个链表,这些数字按照反向顺序存储,每个节点包含一个单独的数字,将这两个数相加,返回一个新链表. 样例: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 链表每个节点的结构: 1 struct ListNode { 2 int val; 3 ListNode *next; 4 ListNode(int x) : val(

【LeetCode】110. Balanced Binary Tree-判断是否为平衡二叉树

一.描述: 二.思路 平衡二叉树(Balanced Binary Tree):又被称为AVL树(有别于AVL算法),且具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树: 通过每一棵左右子树的深度判断子树是否为平衡二叉树,只有当所有的子树是平衡二叉树时,才能得出整棵树是平衡二叉树: 故函数方法中有两种判断,1是空树?2左右两个子树的高度差的绝对值不超过1? 三.代码: 1 /** 2 * Definition for a binary tr

【LeetCode】258 - Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up:Could you do it without any

【LeetCode】Balanced Tree &amp; Binary Search Tree

整合两道差不多的题目放上来,其中第一题是第二题的基础. 1. Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than one. 所谓