【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, ...
随机推荐
- Loj 6433. 「PKUSC2018」最大前缀和 (状压dp)
题面 Loj 题解 感觉挺难的啊- 状压\(dp\) 首先,有一个性质 对于一个序列的最大前缀和\(\sum_{i=1}^{p} A[i]\) 显然对于每个\(\sum_{i=p+1}^{x}A[i] ...
- nodejs fs读取静态json文件
let fs = require('fs'),stream = fs.createReadStream('./obd.json'),data = ""; stream.on('da ...
- UVA - 10543 LIS
题意:见大白Page93 真没想到是两边分别设为终点和起点的LIS和..LDS? 注意,要求对称,所以分别取min #include<iostream> #include<algor ...
- Codeforces - 240F 是男人就上26棵线段树
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5+11; typedef long long ll; ch ...
- 剪邮票--蓝桥杯--dfs--思路超清晰
剪邮票 如[图1.jpg], 有12张连在一起的12生肖的邮票. 现在你要从中剪下5张来,要求必须是连着的. (仅仅连接一个角不算相连) 比如,[图2.jpg],[图3.jpg]中,粉红色所示部分就是 ...
- WPF 使用第三方ttf字体
1.将字体文件直接添加到项目中,注意:将文件的“属性”--“生成操作”设置为“Resource” 2.在Xaml中使用,text可以使用文字或直接使用unicode编码,XAML中使用Unicode编 ...
- Python学习 day05
数据类型划分 数据类型可分为:可变数据类型,不可变数据类型 不可变数据类型:bool.int.str.元祖 -- 不可变数据类型又称为可哈希的 可变数据类型:list.dict.se ...
- js中的table导出成Excel表格
首先判断手否是IE,原因在于IE导出我用的是ActiveXObject,判断的方式很简单,只需要拿到window.navigator.userAgent即可进行判断,代码如下 function get ...
- python查看模块版本及所在文件夹
# 以Numpy为例 第一种方法:import numpy as np np.__version__ >>> '1.12.1' np.__file__ >>> '/ ...
- python 爬虫系列02-小说
本爬虫为网络上的.. # # -*- coding:UTF-8 -*- # from bs4 import BeautifulSoup # import requests # if __name__ ...