作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BigInteger类 模拟加法 日期 题目地址:https://leetcode.com/problems/add-binary/description/ 题目描述 Given two binary strings, return their sum (also a binary string). The input strings are bot…
Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". SOLUTION: 指针指到两个字符串的末尾,不断往前推进,用carry表示进位.用stringbuilder来记录结果. 使用insert(0, c)函数将加出的结果不断插入到STRINGBUILDER. pub…
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) { int len = Math.max(a.l…
Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010&q…
题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 解题思路: 使用StringBuilder,且使用进位. 代码如下: public class Solution { public String addBinary(String a, String b) { String…
题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 题目分析及思路 给定一个非负整数,要求将它的各位数相加得到一个新数并对该新数重复进行这样的过程,直到最后的数只有一位.可以使用递归的方法,不断地将给定数的各位数相加,跳出递归的条件是该数只有一位. python代码 class Solution: def addDigits(self,…
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 题目标签:Math 题目给了我们两个string a 和 b,让我们把这两个二进制 相加. 首先把两个string 的长度得到,然后从右向左 取 两个string 的 digit. 增设一个 carry = 0: 每一轮把 digit…
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 这个题目只要注意各种情况你就成功了一大半,特别要注意的是对进位赋值后可能产生的变化,以及最后一位进位为1时, 我们要把这个1插进来.同时注意字符串的低位是我们数值的高位 class Solution { public: string…
Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1or 0. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010&qu…
Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010&q…
Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010&q…
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 简单的二进制高精度加法. class Solution { public: string addBinary(string a, string b) { string ans=""; ,i=a.length()-,…
翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 分析 我一開始写了这个算法,尽管实现…
Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010&q…
题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: string addBinary(string a, string b) { if(a.size() < b.size()){ string t = a; a = b; b = t; } //该步使的能大数加小数,使其能加 reverse(a.begin(),a.end()); reverse(b…
原题链接 思路: 用一个数保存进制,从后往前不断pop出两个数字和进制数相加,放入返回值中. var addBinary = function(a, b) { var arrA = a.split(''); var arrB = b.split(''); var len = Math.max(a.length, b.length), c = 0, result = ''; while(len-- > 0 || c > 0) { let va = arrA.pop(); let vb = arr…
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/problems/add-strings/description/ 67 Add Binary: https://leetcode.com/problems/add-binary/description/ 43 Multiply Strings:https://leetcode.com/problems/…
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址https://leetcode.com/problems/triangle/description/ 题目描述: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac…
67. Add Binary 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 res =""; int l1…
1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "1"Output: "100"Example 2:…
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积累可以对以后在对算法中优化带来好处.Ok,今天是我做的第一题Add Two Sum. 题目要求 Given an array of integers, find two numbers such that they add up to a specific target number. The fu…
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. [解析] 题意:求一个数组的全排列.与[LeetCode]Permutations 解题报告 不同的是…
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Accepted: 16143 Total Submissions: 28552 Difficulty: Easy Question You are given a map in form of a two-dimensional integer grid where 1 represents land…
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/description 题目描述: Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance between two adjacent cells is 1. Example:…
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/#/description 题目描述: Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], t…
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/description 题目描述: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tan…
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spa…
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 接口 String addBinary(String a, String b) 2 思路 处理二进制求和和进位.从低位开始,一直相加并且维护进位.和Add Two Numbers的区别是这个题目低位在后面,所以要从strin…
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solution  Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed fro…
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {…