题目:

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

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

题意及分析:求两个用字符串表示 的二进制数的和。主要是判断每次相加的和是否大于2,大于2便进1取余。

代码:

class Solution {public String addBinary(String a, String b) {String temp = "";int i = a.length()-1,j=b.length()-1;int count = 0;while(i>=0&& j>=0){count = count + (a.charAt(i)-'0') + (b.charAt(j)-'0');if(count > 1){temp = ((count)%2) + temp;count = 1;}else{temp = count + temp;count = 0;}i--;j--;}//a长一些while(i>=0){count += a.charAt(i)-'0';if(count > 1){temp = ((count)%2) + temp;count = 1;}else{temp = count + temp;count = 0;}i--;}//b长一些while(j>=0){count += b.charAt(j)-'0';if(count > 1){temp = ((count)%2) + temp;count = 1;}else{temp = count + temp;count = 0;}j--;}if(count == 1)temp = count + temp;return temp;}
}

  

转载于:https://www.cnblogs.com/271934Liao/p/8249700.html

[LeetCode] 67. Add Binary Java相关推荐

  1. [LeetCode]67.Add Binary

    [题目] Given two binary strings, return their sum (also a binary string). For example, a = "11&qu ...

  2. LeetCode 67. Add Binary

    67. Add Binary My Submissions QuestionEditorial Solution Total Accepted: 81992 Total Submissions: 29 ...

  3. leetcode 67 Add Binary

    Add Binary Total Accepted: 46815 Total Submissions: 189215 My Submissions Given two binary strings, ...

  4. LeetCode刷题记录4——67. Add Binary(easy)

    LeetCode刷题记录4--67. Add Binary(easy) 目录 LeetCode刷题记录4--67. Add Binary(easy) 题目 语言 思路 后记 题目 今天这题是与字符串相 ...

  5. 67. Add Binary 二进制加法leetcode

    67 Add Binary 二进制加法leetcode 题目描述 方法1: 方法2: 题目描述 Given two binary strings a and b, return their sum a ...

  6. Java for LeetCode 067 Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  7. leetcode python3 简单题67. Add Binary

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第六十七题 (1)题目 英文: Given two binary strings, ...

  8. 67. Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  9. 67. Add Binary

最新文章

  1. java的byte php_java_浅谈java的byte数组的不同写法,(由于篇幅原因阐述的不够详 - phpStudy...
  2. Eclipse安装软件长时间停留在calculating requirements and dependencies
  3. 排列组合算法的实现代码
  4. 百度 Serverless 函数计算引擎 EasyFaaS 正式开源
  5. php文件包含绕过,文件包含漏洞(绕过姿势) | nmask's Blog
  6. Java基础—8大数据类型
  7. 操作系统服务:time时间模块+datetime模块
  8. 数据库出货加权平均成本计算问题
  9. office2019卸载组件_禁止电脑随意安装和卸载软件,用这个方法就够了
  10. 中国“脑计划”研究正在悄然布局
  11. 基于Spring Boot的宠物猫店管理系统的设计与实现毕业设计源码140909
  12. 与现代教育技术有关的计算机知识,基于计算机的现代教育技术手段的利用与开发...
  13. 抖音很火的公众号表白,每天定时发送给自己的女朋友
  14. stm32f4定时器时钟频率/选择
  15. 小游戏正在毁灭微信群聊(文中有福利)
  16. 自定義 ForkJoinPool 線程池,并消除classLoader加载失败的问题
  17. 话题 | 手机充电越充越少,90%的人都遇过这些囧事,有你吗?
  18. [Web安全]信息收集
  19. 十位世界级顶尖投资大师的投资哲学:忍耐、聚焦、坚定
  20. 阿里P6架构师的成长之路,我只用了5个月.....

热门文章

  1. 231 Power of Two 2的幂
  2. 20162309单步追踪数组及查找课下补分博客
  3. 关于如何准备一份制胜简历的清单
  4. GNU C和ANSI C的区别
  5. TechEd China 2009 课程幻灯片和代码下载 - MBL311 SQL Server Compact 终极性能调校
  6. jQuery 1.3 for Asp.Net、 CSLA.Net 3.6 for Windows 及 MOSS/WSS 一步一步从入门到精通三个系列的目录...
  7. JAVA中文字符串编码--GBK转UTF-8
  8. scala unapply
  9. 编写一个求和函数sum,使输入sum(2)(3)或输入sum(2,3),输出结果都为5
  10. node实战学习纪录