【leetcode刷题笔记】Add Binary

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

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



题解:简单的二进制加法模拟。a,b的最后以为对齐开始进行加法,用carries保存进位,如果加完后最高位还有进位,那么要在结果的最前面加一个1。

代码如下:

 1 public class Solution {
 2     public String addBinary(String a, String b) {
 3         if(a == null || a.length() == 0)
 4             return b;
 5         if(b == null || b.length() == 0)
 6             return a;
 7
 8         String s = new String();
 9         int carries = 0;
10         int a_kepeler = a.length()-1;
11         int b_kepeler = b.length()-1;
12
13         while(a_kepeler >= 0 && b_kepeler >= 0){
14             int sum = carries + a.charAt(a_kepeler) - ‘0‘ + b.charAt(b_kepeler) - ‘0‘;
15             carries = sum / 2;
16             sum = sum % 2;
17             s = String.valueOf(sum) + s;
18             a_kepeler--;
19             b_kepeler--;
20         }
21
22         while(a_kepeler >= 0){
23             int sum = carries + a.charAt(a_kepeler) - ‘0‘;
24             carries = sum / 2;
25             sum = sum % 2;
26             s = String.valueOf(sum) + s;
27             a_kepeler--;
28         }
29
30         while(b_kepeler >= 0){
31             int sum = carries + b.charAt(b_kepeler) - ‘0‘;
32             carries = sum / 2;
33             sum = sum % 2;
34             s = String.valueOf(sum) + s;
35             b_kepeler--;
36         }
37
38         if(carries > 0)
39             s = "1" + s;
40
41         return s;
42     }
43 }

上述代码还可以优化,就是如果a的长度小于b,就把a,b交换,使得a总是较长的那个,那么就可以省略第30~36行的while循环了。

【leetcode刷题笔记】Add Binary

时间: 2024-10-30 07:11:15

【leetcode刷题笔记】Add Binary的相关文章

【leetcode刷题笔记】Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order traver

LeetCode刷题笔录Add Binary

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

【leetcode刷题笔记】Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题解:递归的枚举1~n的每个节点为根节点,然后递归

【leetcode刷题笔记】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 node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 类似http://www.cnblogs.com/sunshineatnoon/p/3854935.html 只是子树的前序和中序遍历序列分别更新为: //左子树: left_prestart = prestart+1 lef

【leetcode刷题笔记】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【leetcode刷题笔记】Maximum Depth of Binary Tree

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. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

【leetcode刷题笔记】Flatten Binary Tree to Linked List

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 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

【leetcode刷题笔记】Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 题解:需要找到二叉搜索树中乱序的两个节点,并把它们交换回来