[leedcode 67] Add Binary

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

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

public class Solution {
    public String addBinary(String a, String b) {
        //时间复杂度O(n)。空间复杂度O(1)
        //注意:1.心细,注意循环的写法,简化的方式如非注释代码所示,即使一个遍历完,仍然可以继续相加
        //2.注意最后一个进位的处理
        //3.注意向stringBuilder头部添加字符的方法
        StringBuilder seq=new StringBuilder();
        int carry=0;
        int i=a.length()-1;
        int j=b.length()-1;
       /* for(;j>=0&&i>=0;i--,j--){
            int a1=a.charAt(i)-‘0‘;
            int b1=b.charAt(j)-‘0‘;
            int temp=a1+b1+carry;
            seq.insert(0,temp%2);
            carry=temp/2;
        }
        while(i>=0){
            int temp=a.charAt(i)-‘0‘+carry;
            seq.insert(0,temp%2);
            carry=temp/2;
            i--;
        }
        while(j>=0){
            int temp=b.charAt(j)-‘0‘+carry;
            seq.insert(0,temp%2);
            carry=temp/2;
            j--;
        }*/
        for(;j>=0||i>=0;i--,j--){
            int a1=i>=0?a.charAt(i)-‘0‘:0;
            int b1=j>=0?b.charAt(j)-‘0‘:0;
            int temp=a1+b1+carry;
            seq.insert(0,temp%2);
            carry=temp/2;
        }
        if(carry>0) seq.insert(0,carry);
        return seq.toString();
    }
}
时间: 2024-10-10 06:35:10

[leedcode 67] Add Binary的相关文章

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". 1 public class Solution { 2 public String addBinary(String a, String b) { 3 String res ="";

leetCode 67. Add Binary 字符串

67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路: 1.将两个字符串按数组相加得到新数组. 2.将新数组转换成结果. 代码如下: class Solution { public:     string addBinary(string a, str

No.67 Add Binary

No.67 Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 与No.2 Add Two Numbers的思路非常相似,不同在:对其进行运算时,其实是要逆向相加的!!!因为边界控制的原因,可先将字符串逆序reverse( ). 1 #include "

LeeCode from 0 —— 67. Add Binary

67. Add Binary 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,相加. 2)若两个字符串所有对应位相加结

67. Add Binary

1. 问题描述 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". Tags: Math String 2. 解答思路 3. 代码 1 #include <stack> 2 #include <string> 3 #include <cctype&g

LeetCode 67. Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 这个题目只要注意各种情况你就成功了一大半,特别要注意的是对进位赋值后可能产生的变化,以及最后一位进位为1时, 我们要把这个1插进来.同时注意字符串的低位是我们数值的高位 class Solution { public: string

【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.siz

LeetCode 67 Add Binary(二进制相加)(*)

翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 分析 我一開始写了这个算法,尽管实现

【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