【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, ...
随机推荐
- 关于strcmp函数的用法
strcmp函数是在string.h库下的han函数, 具体用法如下: strcmp函数是用来比较2个字符串的函数,如srcmp(字如果符串1,字符串2),从第一个字符开始比较,如果到最后两个字符串完 ...
- matlab实用教程
苏金明.2005.电子工业 1 语句末尾加 : 可以不显示到屏. who 查看变量 whos 列出变量信息 exist t 判断变量是否在空间中. help 函数 doc 函数 : doc f ...
- [转] javascript 判断对象是否存在的10种方法总结
[From] http://www.jb51.net/article/44726.htm Javascript语言的设计不够严谨,很多地方一不小心就会出错.举例来说,请考虑以下情况.现在,我们要判断一 ...
- D15 模块
模块
- Jmeter4.0----HTTP Cookie管理器(9)
1.说明 在脚本编写的过程中,我们常常会遇到用户登录之后的相关操作,但是又不想去通过脚本先模拟用户登录,再使用cookie值保持登录,做后续的操作的情况下,我们就会用到HTTP Cookie管理. H ...
- RabbitMQ原理——exchange、route、queue的关系
从AMQP协议可以看出,MessageQueue.Exchange和Binding构成了AMQP协议的核心,下面我们就围绕这三个主要组件 从应用使用的角度全面的介绍如何利用Rabbit MQ构建 ...
- Python编程:基础学习常见错误整理
# Python学习之错误整理: # 错误一:# TypeError: cannot concatenate 'str' and 'int' objects# 不能连接str和int对象age = 2 ...
- zabbix 另一种方式取 zabbix-sender
一,zabbix-sender介绍 这种模式是两主机并没有agent互联 使用zabbix-serder的话适用那种没有固定公网IP的,实时系统数据监控操作 还一个实用为零延迟数据监控, 本省zabb ...
- 通过JavaScript动态生成html控件
示例代码 <html> <head> <meta http-equiv="Content-Type" content="text/html& ...
- 深入理解JavaScript系列(12):变量对象(Variable Object)
介绍 JavaScript编程的时候总避免不了声明函数和变量,以成功构建我们的系统,但是解释器是如何并且在什么地方去查找这些函数和变量呢?我们引用这些对象的时候究竟发生了什么? 原始发布:Dmitry ...