LeetCode--2.Add Two Numbers 445. Add Two Numbers II 989. Add to Array-Form of Integer

2024-04-20 13:18

本文主要是介绍LeetCode--2.Add Two Numbers 445. Add Two Numbers II 989. Add to Array-Form of Integer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 问题链接:

https://leetcode.com/problems/add-two-numbers/

https://leetcode.com/problems/add-two-numbers-ii/

https://leetcode.com/problems/add-to-array-form-of-integer/

这三个问题都是基于特殊数据结构的加法运算。需要注意整数int的范围和加法进位操作的表达。

问题一:基于链表存储的整数加法,数字从低位到高位依次存储在链表中,这是我的代码,有点丑陋,效率较低。

class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode p1=l1;ListNode p2=l2;int sign=0;int temp=0;ListNode r=new ListNode(1);ListNode p3=r;while(p1!=null || p2!=null){if(p1==null)p1=new ListNode(0);if(p2==null)p2=new ListNode(0);temp=p1.val+p2.val+sign;if(temp>=10){sign=1;temp-=10;}elsesign=0;p3.next=new ListNode(temp);if(p2.next==null && sign==1){p3.next.next=new ListNode(1);}p3=p3.next;p1=p1.next;p2=p2.next;}return r.next;}
}

Solutions里面的解法思路基本一致,但是处理的非常优雅:

有些tricks十分值得学习:虚拟头节点的加入,空节点统一处理为加上0。

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode dummyHead = new ListNode(0);ListNode p = l1, q = l2, curr = dummyHead;int carry = 0;while (p != null || q != null) {int x = (p != null) ? p.val : 0;int y = (q != null) ? q.val : 0;int sum = carry + x + y;carry = sum / 10;curr.next = new ListNode(sum % 10);curr = curr.next;if (p != null) p = p.next;if (q != null) q = q.next;}if (carry > 0) {curr.next = new ListNode(carry);}return dummyHead.next;
}

问题二:这个问题与第一问的区别是:数字从高位到低位依次存储在链表中,而链表的遍历只能从头到尾遍历,跟问题一一个解法,先上我捉急的代码:

class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {if(l1==null)return l2;if(l2==null)return l1;int len1=1,len2=1,temp=0;ListNode p1=l1,p2=l2;while(p1.next!=null){p1=p1.next;len1++;}while(p2.next!=null){p2=p2.next;len2++;}if(len1<len2){p1=l2;p2=l1;temp=len1;len1=len2;len2=temp;}else{p1=l1;p2=l2;}ListNode[] arr=new ListNode[len1+1];arr[0]=new ListNode(0);int i=1;while(len1!=len2){arr[i]=new ListNode(p1.val);p1=p1.next;len1--;i++;}while(len1>0){arr[i]=new ListNode(p1.val+p2.val);i++;p1=p1.next;p2=p2.next;len1--;}for(int j=arr.length-1;j>0;j--){if(arr[j].val>=10){arr[j-1].val+=1;arr[j].val-=10;}}if(arr[0].val!=0){for(i=0;i<arr.length-1;i++){arr[i].next=arr[i+1];}return arr[0];}else{for(i=1;i<arr.length-1;i++){arr[i].next=arr[i+1];}return arr[1];}}
}

我的解法十分粗暴,因为是一年前刚开始刷LeetCode时写的,那时候有思路能AC就很不错了。更高效的处理方式是借助栈来从个位开始计算:

public class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {Stack<Integer> s1 = new Stack<Integer>();Stack<Integer> s2 = new Stack<Integer>();while(l1 != null) {s1.push(l1.val);l1 = l1.next;};while(l2 != null) {s2.push(l2.val);l2 = l2.next;}int sum = 0;ListNode list = new ListNode(0);while (!s1.empty() || !s2.empty()) {if (!s1.empty()) sum += s1.pop();if (!s2.empty()) sum += s2.pop();list.val = sum % 10;ListNode head = new ListNode(sum / 10);head.next = list;list = head;sum /= 10;}return list.val == 0 ? list.next : list;}
}

问题三:

思路一:在参加LeetCode contest遇到一道很脏的easy题目,边界条件处理起来比较痛苦(可能我没想到更好的方法,所以处理起来很暴力),代码如下:

class Solution {public List<Integer> addToArrayForm(int[] A, int K) {LinkedList<Integer> ret=new LinkedList<>();boolean flag=false;ArrayList<Integer> arr=new ArrayList<>();while(K!=0){arr.add(0,K%10);K/=10;}int[] B=new int[arr.size()];for(int i=0;i<arr.size();i++)B[i]=arr.get(i);if(A.length<B.length){int[] tmp=B;B=A;A=tmp;}int j=A.length-1;int k=B.length-1;while(j>=0 && k>=0){if(flag){A[j] += B[k]+1;flag=false;}elseA[j] += B[k];if(A[j]>=10){A[j]-=10;flag=true;}k--;j--;}while(j>=0){if(flag){A[j]+=1;flag=false;}elsebreak;if(A[j]>=10){A[j]-=10;flag=true;}j--;}if(flag)ret.add(1);for(int i:A)ret.add(i);return ret;}
}

后来这个问题我看了Solutions觉得十分佩服,观察和思路都十分细腻,借鉴问题一的代码风格,该算法如下:

class Solution {public List<Integer> addToArrayForm(int[] A, int K) {LinkedList<Integer> ret=new LinkedList<>();int i=A.length-1;while(K!=0 || i>=0){int p=i>=0?A[i]:0;ret.add(0,(K+p)%10);K=(K+p)/10;i--;}return ret;}
}

写出这样简洁的代码才让我感觉这是一道easy的问题,思路一的代码提交了5次才过也是没谁了。

这篇关于LeetCode--2.Add Two Numbers 445. Add Two Numbers II 989. Add to Array-Form of Integer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/920364

相关文章

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

AI基础 L9 Local Search II 局部搜索

Local Beam search 对于当前的所有k个状态,生成它们的所有可能后继状态。 检查生成的后继状态中是否有任何状态是解决方案。 如果所有后继状态都不是解决方案,则从所有后继状态中选择k个最佳状态。 当达到预设的迭代次数或满足某个终止条件时,算法停止。 — Choose k successors randomly, biased towards good ones — Close

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

从0到1,AI我来了- (7)AI应用-ComfyUI-II(进阶)

上篇comfyUI 入门 ,了解了TA是个啥,这篇,我们通过ComfyUI 及其相关Lora 模型,生成一些更惊艳的图片。这篇主要了解这些内容:         1、哪里获取模型?         2、实践如何画一个美女?         3、附录:               1)相关SD(稳定扩散模型的组成部分)               2)模型放置目录(重要)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

form表单提交编码的问题

浏览器在form提交后,会生成一个HTTP的头部信息"content-type",标准规定其形式为Content-type: application/x-www-form-urlencoded; charset=UTF-8        那么我们如果需要修改编码,不使用默认的,那么可以如下这样操作修改编码,来满足需求: hmtl代码:   <meta http-equiv="Conte