​LeetCode解法汇总1073. 负二进制数相加

news/2024/7/5 2:25:29

 目录链接:

力扣编程题-解法汇总_分享+记录-CSDN博客

GitHub同步刷题项目:

https://github.com/September26/java-algorithms

原题链接:力扣


描述:

给出基数为 -2 的两个数 arr1 和 arr2,返回两数相加的结果。

数字以 数组形式 给出:数组由若干 0 和 1 组成,按最高有效位到最低有效位的顺序排列。例如,arr = [1,1,0,1] 表示数字 (-2)^3 + (-2)^2 + (-2)^0 = -3数组形式 中的数字 arr 也同样不含前导零:即 arr == [0] 或 arr[0] == 1

返回相同表示形式的 arr1 和 arr2 相加的结果。两数的表示形式为:不含前导零、由若干 0 和 1 组成的数组。

示例 1:

输入:arr1 = [1,1,1,1,1], arr2 = [1,0,1]
输出:[1,0,0,0,0]
解释:arr1 表示 11,arr2 表示 5,输出表示 16 。

示例 2:

输入:arr1 = [0], arr2 = [0]
输出:[0]

示例 3:

输入:arr1 = [0], arr2 = [1]
输出:[1]

提示:

  • 1 <= arr1.length, arr2.length <= 1000
  • arr1[i] 和 arr2[i] 都是 0 或 1
  • arr1 和 arr2 都没有前导0

解题思路:

* 解题思路:
* 这其实就是一个自定义的加法。
* 构建数组local存放两个数组中每个位上累加的值。
* 每次计算的时候,把arr1中index位的值,把arr2中index位的值,把local中index位的值相加。
* 结果有四种可能:
* 0:则不需要处理
* 1:则设置local[index] = 1
* 2:设置local[index] = 0,并且 local[index + 1]++;local[index + 2]++;
* 3:设置local[index] = 1,并且 local[index + 1]++;local[index + 2]++;
* 如果local[index + 1] >= 2 && local[index + 2] >= 1时,可以抵扣一次。
* 则local[index + 1] -= 2;local[index + 2]--;
* 最后,把local翻转数组,取第一个不为0的即可。

代码:

public class Solution1073 {

    public int[] addNegabinary(int[] arr1, int[] arr2) {

        int maxLength = Math.max(arr1.length, arr2.length);
        int[] local = new int[maxLength + 3];

        int index = 0;
        while (index < maxLength) {
            int value1 = 0;
            int value2 = 0;
            if (index < arr1.length) {
                value1 = arr1[arr1.length - index - 1];
            }
            if (index < arr2.length) {
                value2 = arr2[arr2.length - index - 1];
            }
            int value = value1 + value2 + local[index];

            if (value == 1) {
                local[index] = 1;
            } else if (value == 2) {
                local[index] = 0;
                local[index + 1]++;
                local[index + 2]++;
            } else if (value == 3) {
                local[index] = 1;
                local[index + 1]++;
                local[index + 2]++;
            } else {
                System.out.println("");
            }
            if (local[index + 1] >= 2 && local[index + 2] >= 1) {
                local[index + 1] -= 2;
                local[index + 2]--;
            }

            System.out.println(local.length);
            index++;
        }
        List<Integer> integers = new ArrayList<>();
        for (int i = local.length - 1; i >= 0; i--) {
            int value = local[i];
            if (value == 0 && integers.size() == 0) {
                continue;
            }
            integers.add(value);
        }
        if (integers.size() == 0) {
            return new int[]{0};
        }
        return integers.stream().mapToInt(value -> value).toArray();
    }
}


http://lihuaxi.xjx100.cn/news/1151362.html

相关文章

工作流 jbpm(图文并茂)

文章目录 1 工作流概述2 jBPM概述3 jBPM开发环境搭建及其配置3.1 准备工作3.2 搭建jBPM开发环境3.3 加入jar包 4 系统数据库表介绍4.1 建表4.2 数据库逻辑关系4.2.1 资源库与运行时的表4.2.2 历史数据表 4.3 表结构4.4 流程操作与数表交互说明 5 相关概念6 流程图说明6.0 快速上…

代码随想录 哈希表 Java

文章目录 &#xff08;简单&#xff09;242.有效的字母异位词&#xff08;简单&#xff09;383. 赎金信&#xff08;中等&#xff09;49. 字母异位词分组&#xff08;*中等&#xff09;438. 找到字符串中所有字母异位词&#xff08;简单&#xff09;349. 两个数组的交集&#x…

【k8s】【ELK】【三】Sidecar容器运行日志Agent

1、日志收集场景分析与说明 对于那些能够将日志输出到本地文件的Pod&#xff0c;我们可以使用Sidecar模式方式运行一个日志采集Agent&#xff0c;对其进行单独收集日志1、首先需要将Pod中的业务容器日志输出至本地文件&#xff0c;而后运行一个Filebeat边车容器&#xff0c;采…

nginx浅看

nginx start on macos sudo nginx # sudo nginx -s [stop or reload or quit]where the conf file at # at /usr/local/etc/nginx/nginx.conf 这是配置文件 # at /usr/local/Cellar/nginx/<version> 这是nginx安装目录&#xff0c;在这里读取html文件nginx 最出色的能…

chatgpt赋能Python-python3怎么降到2

Python3如何降级到Python2 Python3是Python语言的最新版本&#xff0c;但是一些旧的项目或者库可能只支持Python2&#xff0c;这就需要将Python3降级到Python2。本文将介绍如何在MacOS和Windows系统上进行Python3到Python2的降级&#xff0c;并提供详细的步骤和注意事项。 Ma…

PySide6/PyQT多线程之 线程池的基础概念和最佳实践

前言 在PySide6/PyQT 多线程编程中&#xff0c;线程池也是重要的一项知识点&#xff0c;线程池是一种高效管理和调度多个线程执行任务的方式。 通过结合线程池&#xff08;QThreadPool&#xff09;和任务类&#xff08;QRunnable&#xff09;&#xff0c;可以轻松地实现并发执行…

【已解决】grub引导项修复:Minimal BASH-like line editing is supported.

目录 1 问题背景2 问题探索3 问题解决4 告别Bug 1 问题背景 环境&#xff1a; Win10Ubuntu20.04 现象&#xff1a;双系统电脑向移动硬盘安装Ubuntu系统后&#xff0c;重启黑屏并显示Minimal BASH-like line editing is supported. For the first word, TAB lists possible comm…

一、通过命令行体验长安链

一、通过命令行体验长安链 1 、概述2、环境依赖2.1、硬件依赖2.2、软件依赖2.3、git安装2.4、golang安装2.5、gcc 3、环境搭建3.1、源码下载3.2、 源码编译3.3、编译及安装包制作3.4、启动节点集群3.5、查看节点启动使用正常 4、使用CMC命令行工具部署、调用合约4.1、编译&…