leetcode 二进制求和 python】的更多相关文章

class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ a, b = int(a, 2), int(b, 2) return bin(a + b)[2:] i = Solution() print(i.addBinary('1010', '1011'))…
Leetcode 67:Add Binary(二进制求和) (python.java) 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. 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. Example 1: Input…
LeetCode:二进制求和[67] 题目描述 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: "100" 示例 2: 输入: a = "1010", b = "1011" 输出: "10101" 题目分析 分三部分分别运算.考虑进位值: Java题解 class Solut…
67. 二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: "100" 示例 2: 输入: a = "1010", b = "1011" 输出: "10101" class Solution { public String addBinary(String a, St…
/* * @lc app=leetcode.cn id=67 lang=c * * [67] 二进制求和 * * https://leetcode-cn.com/problems/add-binary/description/ * * algorithms * Easy (46.67%) * Total Accepted: 17.6K * Total Submissions: 37.8K * Testcase Example: '"11"\n"1"' * * 给定两…
题目: 二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 解题: 和求两个链表的和很类似 考虑进位,考虑最后一项的进位 0+0 = 0 不需要进位 0+1 = 1 不需要进位 1+1 =0  进位 1 同时注意 低位进1,高位时1+1的情况,直接加就是3了,这个需要进位1 ,原位的结果也是1的情况 Java程序: public class Solution { /** * @param a a number * @param b a…
leetcode-67.二进制求和 Points 数组 数学 题意 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: "100" 示例 2: 输入: a = "1010", b = "1011" 输出: "10101" 示例 3:(自己加的) 输入: a = "101…
二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 细节出了好多问题,提交了好多次... public class Solution { /** * @param a a number * @param b a number * @return the result */ public String addBinary(String a, String b) { int c = 0; int al = a.length() - 1; i…
leetcode -- 二进制 在学习编程语言的运算符时,大部分语言都会有与,或等二进制运算符,我在初期学习这些运算符的时候,并没有重点留意这些运算符,并且在后续的业务代码中也没有频繁的使用过,直到后来的一些算法题目和源码中经常遇到它们的身影,这些二进制运算符相比普通的运算符具有更快的效率,比如hashMap的源码就是将%替换成了&.我们在日常的很多需求都是可以转化为二进制运算符来完成的,这篇文章整理我在刷leetcode的时候遇到的一些二进制题目,对于很多问题,其实我们是没有意识来用这些二进制…
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee: https://gitee.com/inwsy/LeetCode 题目:数组加一 题目来源:https://leetcode-cn.com/problems/add-binary/ 给你两个二进制字符串,返回它们的和(用二进制表示). 输入为 非空 字符串且只包含数字 1 和 0. 示例 1:…
Easy! 题目描述: 给定两个二进制字符串,返回它们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: "100" 示例 2: 输入: a = "1010", b = "1011" 输出: "10101" 解题思路: 二进制数相加,并且保存在string中,要注意的是如何将string和int之间互相转换,…
给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: "100" 示例 2: 输入: a = "1010", b = "1011" 输出: "10101" Java版 class Solution { public String addBinary(String a, String…
给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: "100" 示例 2: 输入: a = "1010", b = "1011" 输出: "10101" class Solution: def addBinary(self, a, b): """ :…
给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: "100" 思路:这种写法既巧妙又简洁,用了两个指针分别指向a和b的末尾,然后每次取出一个字符,转为数字,若无法取出字符则按0处理,然后定义进位carry,初始化为0,将三者加起来,对2取余即为当前位的数字,对2取商即为当前进位的值,记得最后还要判断下carry,如果为1的话,要在结果最…
给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: "100" 示例 2: 输入: a = "1010", b = "1011" 输出: "10101" 思路比较清晰,模拟汇编中二进制加法的做法,对缺位补0后,然后依次相加两数和CF进位标志位. 最后结束时,判断CF是否为1,为1…
开源地址:https://github.com/jiauzhang/algorithms 题目描述 * https://leetcode-cn.com/problems/add-binary * 给定两个二进制字符串,返回他们的和(用二进制表示). * 输入为非空字符串且只包含数字 1 和 0. * * 示例 1: * 输入: a = "11", b = "1" * 输出: "100" * * 示例 2: * 输入: a = "1010…
题目 给你两个二进制字符串,返回它们的和(用二进制表示). 输入为 非空 字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1"输出: "100" 思路 位运算: 以13的二进制为0000 1101,9的二进制为0000 1001为例子 不考虑进位,分别对各位数进行相加:sum = 0000 1101 + 0000 1001 = 0000 0100 考虑进位:有两处进位,第0位和第3位,只考虑进位的结果为:car…
题目 给你两个二进制字符串,返回它们的和(用二进制表示). 输入为 非空 字符串且只包含数字 1 和 0. 题解 两个字符串从低位开始加,前面位不够补0.维护进位,最后加上最后一个进位,最后反转结果字符串. 代码 class Solution { public String addBinary(String a, String b) { StringBuilder sb = new StringBuilder(); int ca = 0; for(int i = a.length() - 1,…
解题思路: 1. 首先在两个字符串前面补‘0’,使它们等长.否则要一直监督操作是否超出两个字符串的索引. 2. 从后向前遍历所有位数,同位相加.字符相加,利用ASCII码,字符在内部都用数字表示,我们不需要知道具体数值,但可知 ‘0’ - ‘0’ = 0, ‘0’ + 1 = ‘1’,以此类推.字符的加减.比较大小,实际上都是内部数字的加减.比较大小. 3. 判断位数相加的结果,若大于等于字符‘2’,则需要向前一位进1. class Solution { public: string addBi…
题目来源: https://leetcode.com/problems/maximal-rectangle/ 题意分析: 给定一个二维的二进制矩阵,也就是只包括0 和 1的,找出只包括1的最大的矩阵的面积. 题目思路: 这道题目可以用84题的解决方法去解决.如果选定了一行为起点,这一行向上或者向下从这行开始连续1的个数是bar的高度,那么就可以求出以这一行起得到的最大的矩阵面积.遍历所有的行,得到最大的结果为这题的最后结果. 代码(python): class Solution(object):…
题目来源: https://leetcode.com/problems/add-binary/ 题意分析: 这题是要将二进制相加,比如“11”,“1”,那么就返回“100”. 题目思路: 模拟加法的过程,直接模拟,大于等于2就进位. 代码(Python): class Solution(object): def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """…
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car"…
题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [], [,4], [6,,7], [4,,8,3] ] The min…
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. 题意分析 Input:integer Output:kth row of the Pascal's triangle Conditions:只返回第n行 题目思路 同118,不…
题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意分析 Input:integer Output:帕斯卡三角 Conditions:帕斯卡三…
题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If th…
题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten it to a linked list in-place. 题意分析 Input: binary tree Output: flattened tree Conditions:将一个二叉树压平为一个flatten 树,也就是一条斜线 题目思路 先将左右子树压平,然后将左子树嵌入到本节点与右子树之间.…
题目来源 https://leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. 题意分析 Input: a binary tree, sum Output: list of list. Conditions: 给定一个二叉树,将所有root-leaf的路径值和等于sum的路径返…
题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. 题意分析 Input: a binary tree, sum Output: True or False Con…
题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题意分析 Input: a binary tree…