这是第2个leetcode问题。我想我真的很傻,无法解决此问题。我遇到了一个无法解决测试用例的特殊问题
[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);
}
}
}
答案 0 :(得分:0)
由于输入是列表而不是整数,因此对其长度没有限制。因此,我建议您遍历输入并在您遍历输入并收集结果列表时收集元素总数。 (不要忘了可以是多个数字的进位)
下面您可以找到我的代码以供参考。 (任何建议都将受到欢迎)
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;
}