leetcode实践:通过链表存储两数之和
- 题目:
两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
- 示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
- 代码实现:
public class AddTwoNumbers {
/**
* 思路:实现链表与long数值的互转
* 失败:链表结构太长超过long值无法转换
* @param l1 ListNode
* @param l2 ListNode
* @return ListNode
*/
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
long m = 0, mIndex = 0;
long n = 0, nIndex = 0;
while(true) {
Double d = Math.pow(10, mIndex);
m += l1.val * d.longValue();
if(l1.next != null) {
l1 = l1.next;
mIndex++;
} else {
break;
}
}
while(true) {
Double d = Math.pow(10, nIndex);
n += l2.val * d.longValue();
if(l2.next != null) {
l2 = l2.next;
nIndex++;
} else {
break;
}
}
return ListNode.buildFromNumber(m + n);
}
/**
* 思路:每一个节点进行相加,如果有进位,则给下一节点额外加1
* @param l1 ListNode
* @param l2 ListNode
*/
public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
ListNode p1 = l1;
ListNode p2 = l2;
ListNode head = new ListNode(0);
ListNode curr = head;
int bit = 0;
while(p1 != null || p2 != null) {
int x = p1 == null ? 0 : p1.val;
int y = p2 == null ? 0 : p2.val;
int sum = x + y + bit;
if (sum >= 10) {
sum = sum % 10;
bit = 1;
} else {
bit = 0;
}
curr.next = new ListNode(sum);
curr = curr.next;
p1 = p1 == null ? null : p1.next;
p2 = p2 == null ? null : p2.next;
}
if(bit > 0) {
curr.next = new ListNode(bit);
}
return head.next;
}
public static void main(String[] args) {
int[] n1 = {9};
int[] n2 = {1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9};
ListNode l1 = ListNode.buildFromArray(n1);
ListNode l2 = ListNode.buildFromArray(n2);
System.out.println(l1.dump());
System.out.println(l2.dump());
AddTwoNumbers addTwoNumbers = new AddTwoNumbers();
ListNode l3 = addTwoNumbers.addTwoNumbers2(l1, l2);
System.out.println(l3.dump());
}
public static void main2(String[] args) {
long n1 = 9L;
long n2 = 999999999999999991L;
ListNode l1 = ListNode.buildFromNumber(n1);
ListNode l2 = ListNode.buildFromNumber(n2);
System.out.println(l1.dump());
System.out.println(l2.dump());
AddTwoNumbers addTwoNumbers = new AddTwoNumbers();
ListNode l3 = addTwoNumbers.addTwoNumbers(l1, l2);
System.out.println(l3.dump());
}
}
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
public String dump() {
StringBuilder out = new StringBuilder();
out.append(val);
ListNode nextNode = this.next;
while(nextNode != null) {
out.append(" -> ").append(nextNode.val);
nextNode = nextNode.next;
}
return out.toString();
}
public static ListNode buildFromArray(int[] arr) {
ListNode head = new ListNode(0);
ListNode curr = head;
for (int a : arr) {
curr.next = new ListNode(a);
curr = curr.next;
}
return head.next;
}
public static ListNode buildFromNumber(long num) {
ListNode head = new ListNode(0);
ListNode curr = head;
int sumIndex = 1;
while(true) {
Double d = Math.pow(10, sumIndex);
long bitVal = num % d.longValue();
long val = bitVal / (d.longValue() / 10);
curr.next = new ListNode((int)val);
curr = curr.next;
sumIndex++;
num -= bitVal;
if (num <= 0) {
break;
}
}
return head.next;
}
}
leetcode实践:通过链表存储两数之和的更多相关文章
- LeetCode(1): 两数之和
本内容为LeetCode第一道题目:两数之和 # -*- coding: utf-8 -*- """ Created on Sun Mar 10 19:57:18 201 ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- Leetcode#1.Two Sum(两数之和)
题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], ta ...
- 【LeetCode】1. Two Sum 两数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...
- leetcode题库练习_两数之和
题目:两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能 ...
- [LeetCode] Sum of Two Integers 两数之和
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- LeetCode每天一题之两数之和
这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...
- leetcode刷题笔记-1. 两数之和(java实现)
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...
- LeetCode 1. Two Sum (两数之和)
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
随机推荐
- ASP.NET MVC5 高级编程-学习日记-第二章 控制器
2.1 控制器的角色 MVC模式中的控制器(Controller)主要负责响应用户的输入,冰球在响应时修改模型(Model).通过这种方式,MVC模式中的控制器主要关注的是应用程序流.输入数据的处理, ...
- C#基础之访问修饰符
C#访问修饰符,无时无刻都在使用,这里记录一下,如果写错的地方,欢迎指正. public :公有的,任何代码均可以访问,应用于所有类或成员: internal:内部的,只能在当前程序集中使用,应用于所 ...
- WPF MeasureOverride和 ArrangeOverride做个 页面导航
public class NavigationPanel:Panel { protected override Size MeasureOverride(Size availableSize) { S ...
- Android开发教程 - 使用Data Binding(二)集成与配置
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- 【文文殿下】NOIp2018游记
Day-1 本段更新于 2018年11月8日23:26:44 今天还在机房里面,无所事事吧.上午睡了一上午,出去理了一下发,花了20块钱 QAQ. 下午来到机房,复习了一下exgcd的东西. 发现自己 ...
- [HEOI2016/TJOI2016]字符串(后缀数组+二分+主席树/后缀自动机+倍增+线段树合并)
后缀数组解法: 先二分最长前缀长度 \(len\),然后从 \(rnk[c]\) 向左右二分 \(l\) 和 \(r\) 使 \([l,r]\) 的 \(height\geq len\),然后在主席树 ...
- [NOI2018]你的名字(后缀自动机+线段树合并)
看到题目名字去补番是种怎么样的体验 我只会 \(68\) 分,打了个暴力.正解看了一会儿,发现跟 \([HEOI2016/TJOI2016]\) 字符串很像,用线段树合并维护 \(endpos\) 集 ...
- spring cloud学习(六) 配置中心-自动更新
上一篇学习了spring cloud config的基本使用,但发现有个问题,就是每次更改配置后,都需要重启服务才能更新配置,这样肯定是不行的.在上网查资料了解后,spring cloud支持通过AM ...
- MySQL查询50例
创建表和关系 /* 创建表 */ /*年级表*/ DROP TABLE IF EXISTS `class_grade`; CREATE TABLE `class_grade` ( `gid` int( ...
- JIRA服务器搭建
JJIRA服务器搭建 http://wiki.csdn.net/display/CSDN/Atlassian CSDN国内代理: http://atlassian.csdn.net/m/btc/atl ...