gpt4 book ai didi

java - Int 无法容纳结果,因此程序失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:14:14 24 4
gpt4 key购买 nike

这是一个 leetcode 问题 2。我想我真的很笨无法解决这个问题。我遇到了一个特殊的问题,我无法为测试用例解决它

[9]
[9,9,9,9,9,9,9,9,9,1]

问题是当我尝试从链接列表中提取数字时,我的程序返回的是 1410065399 而不是 9999999991。我已经提到了实际的问题陈述和我的解决方案。请提供一些信息,以便我了解出了什么问题。

问题给定两个非空链表,代表两个非负整数。这些数字以相反的顺序存储,并且它们的每个节点都包含一个数字。将这两个数字相加并将其作为链表返回。

您可以假设这两个数字不包含任何前导零,除了数字 0 本身。

示例

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

我写的实际代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}

class Solution {
ListNode first;
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int finalResult = returnNumber(l1)+returnNumber(l2);
System.out.println(returnNumber(l1));
System.out.println(returnNumber(l2));
int length=0;
if(finalResult!=0){
length = (int)(Math.log10(finalResult)+1);
}else{
length =1;
}
for(int i=length-1;i>=0;i--){
generateList(finalResult,i);
}
return first;
}

public void generateList(int finalResult,int number){
ListNode oldNode = first;
first = new ListNode((finalResult / (int)Math.pow(10,number)) % 10);
System.out.println((finalResult / (int)Math.pow(10,number)) % 10);
first.next = oldNode;
}

public int returnNumber(ListNode list){
int i=0;
int base = 10;
int total = 0;
while(list!=null){
total=total+(list.val*(int)Math.pow(10,i));
i++;
list = list.next;
}
return total;
}
}

public class MainClass {
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}

String[] parts = input.split(",");
int[] output = new int[parts.length];
for(int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}

public static ListNode stringToListNode(String input) {
// Generate array from the input
int[] nodeValues = stringToIntegerArray(input);

// Now convert that list into linked list
ListNode dummyRoot = new ListNode(0);
ListNode ptr = dummyRoot;
for(int item : nodeValues) {
ptr.next = new ListNode(item);
ptr = ptr.next;
}
return dummyRoot.next;
}

public static String listNodeToString(ListNode node) {
if (node == null) {
return "[]";
}

String result = "";
while (node != null) {
result += Integer.toString(node.val) + ", ";
node = node.next;
}
return "[" + result.substring(0, result.length() - 2) + "]";
}

public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
ListNode l1 = stringToListNode(line);
line = in.readLine();
ListNode l2 = stringToListNode(line);

ListNode ret = new Solution().addTwoNumbers(l1, l2);

String out = listNodeToString(ret);

System.out.print(out);
}
}
}

最佳答案

由于输入是列表而不是整数,因此对其长度没有限制。因此,我建议循环遍历输入并在遍历它时收集元素的总和并创建结果列表。 (不要忘记可以是多位数的进位)

您可以在下面找到我的代码以供引用。(任何建议都将受到欢迎)

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null, node = null;
int carry = 0, sum = 0;
while (!(l1 == null && l2 == null)) {
// both not null
if (l1 != null && l2 != null) {
sum = (l1.val + l2.val + carry) % 10;
carry = (l1.val + l2.val + carry) / 10;
l1 = l1.next;
l2 = l2.next;
}
// l1==null
else if (l1 == null && l2 != null) {
sum = (l2.val + carry) % 10;
carry = (l2.val + carry) / 10;
l2 = l2.next;
}
// l2==null
else if (l1 != null && l2 == null) {
sum = (l1.val + carry) % 10;
carry = (l1.val + carry) / 10;
l1 = l1.next;
}
// both null
else {
// break out of the loop
}
if (head == null) {
head = new ListNode(sum);
node = head;
} else {
node.next = new ListNode(sum);
node = node.next;
}
}
if (carry > 0) {
while (carry > 0) {
int curr = carry % 10;
node.next = new ListNode(curr);
node = node.next;
carry = carry / 10;
}
}
return head;
}

关于java - Int 无法容纳结果,因此程序失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51181896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com