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 ...
随机推荐
- C# 嵌入dll
在很多时候我们在生成C#exe文件时,如果在工程里调用了dll文件时,那么如果不加以处理的话在生成的exe文件运行时需要连同这个dll一起转移,相比于一个单独干净的exe,这种形式总归让人不爽,那么有 ...
- Asp.Net 学习笔记(IIS不同版本和Asp.Net)
主要目的是在网上记录一下学习笔记,如有不对,请指出 谢谢!! iis5.x: 存在问题,inet info收到动态请求后,aspnt_isapi.dll会被加载到inetinfo.exe(挂载w3sv ...
- Spring IOC 容器源码分析 - 填充属性到 bean 原始对象
1. 简介 本篇文章,我们来一起了解一下 Spring 是如何将配置文件中的属性值填充到 bean 对象中的.我在前面几篇文章中介绍过 Spring 创建 bean 的流程,即 Spring 先通过反 ...
- Linux下安装MySQL以及一些小坑
第一次写博客,各位凑合着看吧(假装有人看). 我这里使用的是centos7. 1.首先打开终端,查看有没有安装过MySQL: [root@localhost lyp]# rpm -qa | grep ...
- Swift5 语言参考(一) 关于语言参考
本系列文章的这一部分描述了Swift编程语言的形式语法.此处描述的语法旨在帮助您更详细地理解语言,而不是允许您直接实现解析器或编译器. Swift语言相对较小,因为Swift代码中几乎无处不在的许多常 ...
- 常用的SQL调优
1. 不要使用 select * ,使用select *的话会增加解析的时间,另外会把不需要的数据也给查询出来,数据传输也是耗费时间的. 2. 避免在 where 子句中使用 or 来连接条件,可以 ...
- postgresql和redis
redis 和postgresql区别以及其优缺点 一刹那者为一念,二十念为一瞬,二十瞬为一弹指,二十弹指为一罗预,二十罗预为一须臾,一日一夜有三十须臾. 那么,经过周密的计算,一瞬间为0.36 秒, ...
- spring boot 中使用swagger 来自动生成接口文档
1.依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...
- 前端基础——css
前端基础——css css的内容主要包括:盒子模型.定位.单位与取值.属性.选择器.
- Centos Android开发环境配置-Android Tools -android list sdk --extended --all
Centos Android开发环境配置-Android Tools -android list sdk --extended --all 安装完Android Tools后执行 android ...