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 ...
随机推荐
- 在Windows7系统上能正常使用的程序,Windows10运行后部分状态不能及时变更
这是最近在开发一个通信项目时遇到的问题,一开始以为是窗体样式的原因,把窗体换成系统窗体之后还是在Win10上不能正常使用,后面突然想到会不会是匹配原因,试了一下,结果真的就正常了. 问题:例如一个通信 ...
- 抓取分析网页批量下载评书(3)之批量下载mp3
本系列目录: <1.搜索有声小说> <2.分析详细页地址> <3.批量下载mp3> 本篇是大结局,看过前两篇的放心吧,不会有 ...
- 不同Mesh技术的比较-总结版
引言 在过去的几年里,Mesh 网络逐渐变得流行,随之会有越来越多的无线产品面世.Mesh 网络技术作为一种无线自组网技术是物联网的核心技术.物联网的概念现在也逐渐贴近人们的生活, 据预测 2011 ...
- flaks___git
今天呢 我给大家分享一个超实用的一个把代码分享到云端的一种操作 比如我们在家里,要想做项目的话可以直接从云端上拉取下来代码直接开始工作了 而且还可以随时修改,没有地点的局限性了,只要你想敲,随时随地 ...
- python学习笔记11-文件操作方法
f=open("1.txt","r",encoding='utf-8') # a=f.readline() print(a) #光标会移动 下面两者结果不一样 ...
- Docker三剑客之Docker Swarm
一.什么是Docker Swarm Swarm是Docker公司推出的用来管理docker集群的平台,几乎全部用GO语言来完成的开发的,代码开源在https://github.com/docker/s ...
- 网络Socket编程UDP协议例子
服务端代码 public class UDPChatServer { //通讯端口 private Integer port=8000; //数据报文的通讯通道对象 private DatagramC ...
- C#:时间日期操作(持续更新)
1.给定时间戳返回指定的时间格式 private string StampToDate(string timeStamp,string format) { DateTime dtStart = Tim ...
- Decorator装饰者模式(结构型模式)
1.需求 假设让我们去设计FCL中的Stream类,该类具有流类的基本功能,除了有各种不同类型的流外(如内存流.文件流.网络流等等),但是在不同的业务场景下,如处理银行业务,需要给相关的内存流进行加密 ...
- c++之菱形继承问题
昨天面试问了菱形继承的问题,回答的稀巴烂,回来赶快好好学习一波!!!!! 菱形继承如下图: 上一段代码: #include<bits/stdc++.h> using namespace s ...