题目

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) {String lon= a.length()-b.length()>=0 ? a:b;String sho= a.length()-b.length()<0 ? a:b;char[] lon_char=lon.toCharArray();char[] sho_char=sho.toCharArray();char temp='0';int final_length=lon.length()-1;int index=0;String[] str=new String[lon.length()+1];String addBinary_str="";for(int i=0;i<sho.length();i++){String temp_s=String.valueOf(temp);String sho_char_s=String.valueOf(sho_char[sho.length()-1-i]);String lon_char_s=String.valueOf(lon_char[lon.length()-1-i]);switch(Integer.parseInt(temp_s)+Integer.parseInt(sho_char_s)+Integer.parseInt(lon_char_s)){ case 0:temp='0';str[index++]="0";break;case 1:temp='0';str[index++]="1";break;case 2:temp='1';str[index++]="0";break;case 3:temp='1';str[index++]="1";break;}}while(index<lon.length()){String temp_s=String.valueOf(temp);String lon_char_s=String.valueOf(lon_char[lon.length()-index-1]);switch(Integer.parseInt(temp_s)+Integer.parseInt(lon_char_s)){ case 0:temp='0';str[index++]="0";break;case 1:temp='0';str[index++]="1";break;case 2:temp='1';str[index++]="0";break;}}if(temp=='1'){str[lon.length()]="1";final_length=lon.length();}for(int k=final_length;k>=0;k--){addBinary_str+=str[k];}return addBinary_str;}}
代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

【LeetCode从零单排】No67.AddBinary相关推荐

  1. 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)

    1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...

  2. 【LeetCode从零单排】No70.ClimbingStairs

    题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each ...

  3. 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array

    题目      题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...

  4. 【LeetCode从零单排】No.7 Reverse Integer

    前话       今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目        没想到做的第一道题目,虽然看似 ...

  5. 【LeetCode从零单排】No.135Candy(双向动态规划)

    1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. 【LeetCode从零单排】No22.Generate Parentheses

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  7. 【LeetCode从零单排】No221.Maximal Square

    题目 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)

    背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...

  9. 【LeetCode从零单排】No121 Best Time to Buy and Sell Stock

    题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...

最新文章

  1. 请问:hive中avg聚合函数会使用到combiner功能吗?
  2. js自定义类,混合的构造函数/原型方式
  3. scrapy爬虫代理——利用crawlera神器,无需再寻找代理IP
  4. Java 的工厂方法及代理模式
  5. STM32基于AD5663的UV灯电压控制
  6. 无法远程连接mssqlserver2005服务器的解决
  7. Covariance and Contravariance in C#, Part One
  8. SuseLinux详解(5)——安装上传下载工具lrzsz
  9. php radio用法,JavaScript_JQuery radio(单选按钮)操作方法汇总,随着Jquery的作用越来越大,使 - phpStudy...
  10. 程序员应该具备的12种能力
  11. Angular JS 学习笔记(二)
  12. (14)Python_SimpleImputer缺失值处理
  13. 16_多易教育之《yiee数据运营系统》用户画像-标签体系设计篇
  14. IAR编译器问题的总结
  15. java 有序map_java 有序Map之TreeMap的使用
  16. MATLAB GUI编程总结
  17. python aic准则_赤池信息准则AIC,BIC
  18. SAS硬盘安装Windows 7操作方法
  19. 一文带你了解什么是CDN?
  20. Jmeter01 -mac下的安装

热门文章

  1. ubuntu 使用ccache加快linux内核编译速度
  2. mysql主从同步默认延迟_减少mysql主从数据同步延迟问题的详解
  3. php获得前一天,php 获取前一天、后一天等日期的方法
  4. oracle回收ddl权限,oracle禁止指定用户DDL操作触发器
  5. android edittext不可复制_Android EditText禁止复制粘贴
  6. python 嵌入键值数据库_PupDB 一个用Python编写基于文件的简单键值数据库
  7. java字符串10_十个最常见的Java字符串问题
  8. 主键由数据库mysql 映射native_Hibernate主键生成策略详解
  9. mysql当前时间加一天_MySQL 的加锁处理,你都了解的一清二楚了吗?
  10. 建立能够持续请求的CS网络程序