zoukankan      html  css  js  c++  java
  • 67. Add Binary

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

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

    此题主要考察数学知识,这里面注意,如果String的值要不断的改变的话,就最好用StringBuilder,我用了String结果没有通过,代码如下:

    public class Solution {

        public String addBinary(String a, String b) {

            int i = a.length()-1;

            int j = b.length()-1;

            int c = 0;

            String s = "";

            while(i>=0||j>=0||c==1){

                if(i>=0) c+=a.charAt(i--)-'0';

                if(j>=0) c+=b.charAt(j--)-'0';

                sb.append(c%2);

                c/=2;

            }

            return sb.reverse().toString();

        }

    }

  • 相关阅读:
    装饰器和表达生成式
    函数
    字符编码
    函数基础
    列表,字典与集合
    Linux Semaphore
    tp5安装easyWeChat
    wx.request
    小程序设计规范
    小程序的概念和特点
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6358600.html
Copyright © 2011-2022 走看看