【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】

【067-Add Binary(二进制加法)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  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[] ca = new int[a.length()];
        int[] cb = new int[b.length()];

        // 将字符数组中的值转换了数值的0或者1
        for (int i = 0; i < a.length(); i++) {
            ca[i] = a.charAt(i) - ‘0‘;
        }

        // 将字符数组中的值转换了数值的0或者1
        for (int i = 0; i < b.length(); i++) {
            cb[i] = b.charAt(i) - ‘0‘;
        }

        // 使用ca保存的长度长
        if (ca.length < cb.length) {
            int[] tmp = ca;
            ca = cb;
            cb = tmp;
        }

        int ai = ca.length - 1; // 字符数组ca最后一个索引下标
        int bi = cb.length - 1; // 字符数组cb最后一个索引下标
        int carry = 0; // 下位的进位标识
        int result; // 加载的结果

        // 计算比如:1010101101 + 10100
        while (ai >= 0 && bi >= 0) {
            result = ca[ai] + cb[bi] + carry;
            ca[ai] = result % 2;
            carry = result / 2;

            ai--;
            bi--;
        }

        // 处理余下的数字
        while (ai >= 0) {
            result = ca[ai] + carry;
            ca[ai] = result % 2;
            carry = result / 2;

            if (carry == 0) {
                break;
            }

            ai--;
        }

        // 将字符数组中的值转换了字符的0或者1
        for (int i = 0; i < ca.length; i++) {
            ca[i] += ‘0‘;
        }

        // 不需要扩展一位
        if (carry == 0) {

            char[] ch = new char[ca.length];
            for (int i = 0; i < ca.length; i++) {
                ch[i] = (char) (ca[i]);
            }

            return new String(ch);
        }
        // 需要扩展一位
        else {
            char[] ch = new char[ca.length + 1];
            ch[0] = ‘1‘;
            for (int i = 0; i < ca.length; i++) {
                ch[i + 1] = (char) (ca[i]);
            }
            return new String(ch);
        }
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47203323

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-18 02:55:03

【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】的相关文章

[Leetcode] add binary 二进制加法

Given two binary strings, return their sum (also a binary string). For example,a ="11"b ="1"Return"100". 题意:将两个以字符串形式保存的二进制数进行相加. 思路:其实不管是以数组.字符串形式,加或者乘(multiply strings),一般的思路都是从后往前计算,用一个中间变量保存相加或者相乘的结果(加法是一个变量int 或string就行,

leetCode 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) { int len = Math.max(a.len

[leetcode]67. Add Binary 二进制加法

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

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example

【LeetCode-面试算法经典-Java实现】【056-Merge Intervals(区间合并)】

[056-Merge Intervals(区间合并)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 题目大意 给定一个区间集合,合并有重叠的区间. 解题思路 先对区间进行排序.按開始

【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】

[054-Spiral Matrix(螺旋矩阵)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]

【LeetCode-面试算法经典-Java实现】【130-Surrounded Regions(环绕区域)】

[130-Surrounded Regions(环绕区域)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X

【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】

[225-Implement Stack using Queues(用队列实现栈操作)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. pop() – Removes the element on

【LeetCode-面试算法经典-Java实现】【103-Binary Tree Zigzag Level Order Traversal(二叉树分层Z字形遍历)】

[103-Binary Tree Zigzag Level Order Traversal(二叉树分层Z字形遍历)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alt