【Leetcode】【Easy】Add Binary
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
解题思路:
从两个子字符尾部遍历字符;
每次得到新的字符插入结果字符串的头部;
解题步骤:
1、建立返回string、从后向前遍历的idx:idx_a / idx_b、进位量
2、循环开始,当idx_a或者idx_b任意不为0时,循环继续:
(1)临时整形sum = 进位值;
(2)如果idx_a不为0,则加上a[idx_a - 1],idx_a--;同理对idx_b;
(3)更新进位值和sum,并将sum以字符的形式插入返回string的头部;
3、如果循环结束时进位值不为0,则在返回string头部添加一位。
代码:
class Solution {
public:
string addBinary(string a, string b) {
int len_a = a.size();
int len_b = b.size();
int sig_flag = ;
string ret;
while (len_a || len_b) {
int curd = sig_flag;
if (len_a) {
curd += a[len_a - ] - '';
len_a--;
}
if (len_b) {
curd += b[len_b - ] - '';
len_b--;
}
sig_flag = curd / ;
curd = curd % ;
ret.insert(, , '' + curd);
}
if (sig_flag)
ret.insert(, , '');
return ret;
}
};
附录:
string操作
int char string
【Leetcode】【Easy】Add Binary的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode】66 & 67- Plus One & Add Binary
66 - Plus One Given a non-negative number represented as an array of digits, plus one to the number. ...
- 【LeetCode每天一题】Add Binary(二进制加法)
Given two binary strings, return their sum (also a binary string).The input strings are both non-emp ...
- 【leetcode刷题笔记】Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
- 【leetcode刷题笔记】Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
随机推荐
- [转] Spring Boot 揭秘与实战(二) 数据存储篇 - ElasticSearch
[From] http://www.tuicool.com/articles/JBvQrmj 本文讲解Spring Boot基础下,如何使用 ElasticSearch,实现全文搜索. 版本须知 sp ...
- XFire创建WebService实例应用
[转自] http://clq9761.iteye.com/blog/1261963 XFire创建WebService实例应用 XFire使得在JavaEE应用中发布Web服务变得轻而易举.和其他W ...
- 【Python】urlopen小结
0X00 简介 urlopen是urllib的的一个方法,它属于类文件对象,具有文件对象的方法,如read()等,同时也具有自身的一些方法: 1.info() 返回响应包的头信息 2.info() ...
- 2019.3.22 SQL语句(基础篇)
SQL语句 创建一个数据库: create database+数据库名; 使用数据库: use+数据库名; 查看mySQL中有哪些数据库: show databases; 删除数据库 drop dat ...
- 使用Maven运行Java main的3种方式使用Maven运行Java main的3种方式
maven使用exec插件运行java main方法,以下是3种不同的操作方式. 一.从命令行运行 1.运行前先编译代码,exec:java不会自动编译代码,你需要手动执行mvn compile来完成 ...
- js的apply和call
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- AppInventor2笔记
将视觉化的 块语言 翻译为 Android上的实现 的编译器使用了Kawa语言框架,而Kawa是Scheme编程语言的方言,由Per Bothner开发,并由自由软件基金会发布,它是GNU操作系统的一 ...
- python-几种快速了解函数及模块功能的方式
背景 在进行编程的时候经常要导入各种包的各种函数,但是很多包一下又不知道为什么要导入这个模块,所以想总结下有哪些方法可以让我们快速熟悉其中函数的作用. import numpy as np impor ...
- unity5.5 5.6 使用c#6.0或7.0
http://blog.csdn.net/davied9/article/details/77281393
- Unity3d Attribute 总结
举两个例子,在变量上使用[SerializeFiled]属性,可以强制让变量进行序列化,可以在Unity的Editor上进行赋值. 在Class上使用[RequireComponent]属性,就会在C ...