首页 > 其他分享 >leetcode-67-easy

leetcode-67-easy

时间:2022-10-11 22:25:09浏览次数:50  
标签:insert charAt 67 TWO else easy bs sb leetcode

Add Binary
思路一: 先计算公共部分,最后补充未计算的位置,模拟二进制加法,写的太丑了

public String addBinary(String a, String b) {
    char ONE = '0' + '1';
    char TWO = '1' + '1';

    StringBuilder sb = new StringBuilder();
    int as = a.length() - 1;
    int bs = b.length() - 1;
    char x = '0';
    while (as >= 0 && bs >= 0) {
        if (a.charAt(as) + b.charAt(bs) == TWO) {
            sb.insert(0, x);
            x = '1';
        } else if (a.charAt(as) + b.charAt(bs) == ONE) {
            if (x == '1') {
                sb.insert(0, '0');
                x = '1';
            } else {
                sb.insert(0, '1');
            }
        } else {
            sb.insert(0, x == '1' ? '1' : '0');
            x = '0';
        }

        as--;
        bs--;
    }

    while (as >= 0) {
        if (a.charAt(as) + x == TWO) {
            sb.insert(0, '0');
            x = '1';
        } else  if (a.charAt(as) + x == ONE){
            sb.insert(0, '1');
            x = '0';
        } else {
            sb.insert(0, '0');
        }
        as--;
    }
    while (bs >= 0) {
        if (b.charAt(bs) + x == TWO) {
            sb.insert(0, '0');
            x = '1';
        } else if (b.charAt(bs) + x == ONE) {
            sb.insert(0, '1');
             x = '0';
        } else {
            sb.insert(0, '0');
        }
        bs--;
    }
    if (x == '1') sb.insert(0, '1');

    return sb.toString();
}

标签:insert,charAt,67,TWO,else,easy,bs,sb,leetcode
From: https://www.cnblogs.com/iyiluo/p/16782849.html

相关文章

  • Luogu2167 Bill的挑战 - 容斥 - dp -
    题目链接:https://www.luogu.com.cn/problem/P2167题解:摘录一段描述容斥题目的话:本题中,关于容斥系数,可以先感性理解一下,严格证明可以用即除了自身,自身的超集都计算......
  • LeetCode148. Sort List
    题意链表排序方法递归代码classSolution{public:ListNode*sortList(ListNode*head){returnsortList(head,nullptr);}ListNode*......
  • leetcode-26-easy
    RemoveDuplicatesfromSortedArray思路一:双指针,左指针永远指向有效数组长度+1的位置,左指针只会在出现交换后向右移动。右指针一直向右扫描,遇到不重复的数字就和左指......
  • leetcode-58-easy
    LengthofLastWord思路一:从后面非空格字符开始扫描,记录非空格字符个数。优化:不用char[],直接用charAt()判断publicintlengthOfLastWord(Strings){......
  • leetcode-66-easy
    PlusOne思路一:暴力,方向想错了,不能把digits当做一个整数看publicint[]plusOne(int[]digits){if(digits[digits.length-1]!=9){digits[digit......
  • #yyds干货盘点# LeetCode 热题 HOT 100:最小覆盖子串
    题目:给你一个字符串s、一个字符串t。返回s中涵盖t所有字符的最小子串。如果s中不存在涵盖t所有字符的子串,则返回空字符串""。 注意:对于t中重复字符,我们寻......
  • EasyCVR视频融合平台HLS播放协议配置的细节优化
    EasyCVR视频融合云服务支持海量视频汇聚管理,能兼容多类型的设备接入,平台可对前端接入设备进行统一管理,并能支持采用设备树对设备进行分组、分级、用户与角色权限管理,可支持......
  • LeetCode88.合并两个数组
    1.题目描述给你两个按非递减顺序排列的整数数组nums1和nums2,另有两个整数m和n,分别表示nums1和nums2中的元素数目。请你合并nums2到nums1中,使合并后的......
  • LeetCode 1195. Fizz Buzz Multithreaded
    原题链接在这里:https://leetcode.com/problems/fizz-buzz-multithreaded/题目:Youhavethefourfunctions:printFizz thatprintstheword "fizz" totheconsole......
  • leetcode-394.字符串解码
    394.字符串解码publicStringdecodeString(Strings){Stack<Character>stack=newStack<>();for(charc:s.toCharArray()){if(c......