对于第一种解法,可以适当再优化下,大体逻辑一致,不过在使用StringBuilder存储字符串的时候,使用的append方法,所以最后需要将其反转,才是最终我们需要的结果。 public String addBinary2(String a, String b) { StringBuilder sb = new StringBuilder();inti = a.length() -1;intj = b.length() -1;intcarry =...
1publicclassAddBinary {23publicstaticvoidmain(String[] args) {4//TODO Auto-generated method stub5String str1="111";6String str2="1";7System.out.println(addBinary (str1,str2));8}9publicstaticString addBinary(String str1,String str2) {10if(str1==""||str2=="")returnnull;11//int ...
题目地址:https://leetcode.com/problems/add-binary/description/ 题目描述 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" 1...
空间O(1) Java实现 classSolution {publicString addBinary(String a, String b) {inti = a.length() - 1;intj = b.length() - 1;intcarry = 0;intsum = 0; StringBuilder sb=newStringBuilder();while(i >= 0 || j >= 0 || carry > 0) {intx = i >= 0 ? a.charAt(i--) - '0'...
总结:Java 位操作,返回仍是 int, 需要再次进行强制转换为byte。 ** Anyway, Good luck, Richardo! My code: public class Solution { public String addBinary(String a, String b) { if (a == null || a.length() == 0) return b; else if (b == null || b.length() == 0) ...
Leetcode 476. Number Complement 思路 java - int - 32 bit 找到可以开始取反的位置。(highestOneBit(n))其后...
public String addBinary(String a, String b) { int i = a.length() - 1, j = b.length() - 1, carry = 0; StringBuilder sb = new StringBuilder(); while(i >=0 || j >=0){ int m = i >= 0 ? a.charAt(i) - '0' : 0; ...
LeetCode 67. 二进制求和 编程算法 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/add-binary 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 Michael阿明 2020/07/13 2440 【小Y学算法】⚡️每日LeetCode打卡⚡️——20.二进制求和 java编程算法c#腾讯云开发者社区...
【leetcode】AddTwoNumbers 2013-04-13Algrithm, Question: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list....
Leetcode 258. Add Digits JAVA语言 1 2 3 Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it....