day89(2.18)——leetcode面试经典150

67. 二进制求和

67. 二进制求和

题目:

题解:

java 复制代码
class Solution {
    public String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int ai = a.length()-1;
        int bi = b.length()-1;
       int t = 0;
        while(ai>=0||bi>=0||t>0) {
            int c = t;
            if(ai>=0) {
                c+=a.charAt(ai)-'0';
                ai--;
            }
            if(bi>=0) {
                c+=b.charAt(bi)-'0';
                bi--;
            }
            sb.append((char)c%2);
            t=c/2;
        }
        return sb.reverse().toString();
    }
}
相关推荐
雨白18 分钟前
哈希:以时间换空间的算法实战
算法
Cosolar1 小时前
LlamaIndex 文档解析与分块策略深度解析
人工智能·面试·架构
San813_LDD2 小时前
[数据结构]LeetCode学习
数据结构·算法·图论
x138702859572 小时前
c语言排雷游戏(基础版9*9)
c语言·算法·游戏
kyriewen3 小时前
我读了一遍 Babel 编译后的 async/await,终于搞懂了它的原理(附 20 行手写实现)
前端·javascript·面试
sheeta19983 小时前
LeetCode 每日一题笔记 日期:2026.06.06 题目:2196. 根据描述创建二叉树
笔记·算法·leetcode
小欣加油3 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
QuZero4 小时前
Guava Cache Deep Dive
java·后端·算法·guava
随意起个昵称4 小时前
线性dp-LIS题目4(A Twisty Movement)
算法·动态规划