[leetcode]Add Two Numbers——JS实现
Javascript的结构体应用,如下:
function station(name, latitude, longitude){
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
}
var s1 = new station('station1', 100, 200);
console.log(s1.name+" 's latitude :" + s1.latitude );
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/ var addTwoNumbers = function(l1, l2) {
var head = new ListNode(0);
var temp1 = 0;
var temp2 = 0;
var val1;
var val2;
while(l1 || l2 || temp1) {
if (l1)val1 = l1.val;
else val1 = 0;
if (l2) val2 = l2.val;
else val2 = 0;
temp2 = Math.floor((val1 + val2 + temp1) % 10);
temp1 = Math.floor((val1 + val2 + temp1) / 10);
head.val += 1;
var newNode = new ListNode(temp2);
if(head.next == null){
head.next = newNode;
}
else {
var tempNode = head.next;
while(tempNode.next != null)
tempNode = tempNode.next;
tempNode.next = newNode;
}
if (l1)l1 = l1.next;
if (l2)l2 = l2.next;
}
return head.next;
};
[leetcode]Add Two Numbers——JS实现的更多相关文章
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
- [LeetCode] Add Two Numbers题解
Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
- [Leetcode] Add two numbers 两数之和
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [LeetCode] Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode——Add Two Numbers
Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...
随机推荐
- 关于ping以及TTL的分析
首先介绍一下ping这个工具 ping [目标] 的意思就是向目标发送几个数据包,之后假设目标接受到一个数据包.那么目标就会向发送ping的主机返回一个数据包 比方上图.我ping了百度的server ...
- log4j 具体解释
通常,我们都提供一个名为 log4j.properties的文件.在第一次调用到Log4J时,Log4J会在类路径(../web-inf/class/当然也能够放到其他不论什么文件夹.仅仅要该文件夹被 ...
- APP漏洞自动化扫描专业评测报告
一.前言 目前在业界有很多自动化检测APP安全性的在线扫描平台.为了了解目前国内移动APP在线漏洞扫描平台的发展情况,我进行了一次移动安全扫描平台的评测分析:主要从漏洞项对比.扫描能力对比以及扫描结果 ...
- 动态生成页面(一)——ASP.NET中Literal使用
在页面中加入内容时,假设是静态内容.无需使用容器,能够直接将标记作为HTML直接加入到页面中:可是,假设是动态内容,则必须借助容器将内容加入到页面中.典型的容器有:Label控件.Literal控件. ...
- UVA1523-Helicopter(暴力+全排列)
题目链接 题意:有八个乘客坐在直升机上,求重心M最小值. 思路:依据题目所给的公式,我们能够知道要使得M最小.也就是要使得Mv和Mh的和最小,我们能够使用全排列,分别将每一个值放在各个位子上,然后更新 ...
- js全局替换空格,制表符,换行符
this.value = this.value.replace(/\s+/g,'') "/ "这个是固定写法, "\s"匹配任何不可见字符,包括空格.制表符.换 ...
- 一条SQL语句面试题:求选修所有课程的学生
前几天求职面试,有一道SQL题:给出三个表:学生.课程.成绩,求选修了所有课程的学生. 一道看似很简单的问题,把我难住了,我改了又改,涂涂画画,抓耳挠腮,因为试卷没有多少空白位置了,最后只好放弃.心情 ...
- redis Database Eviction Policies Redis on Flash
Database Eviction Policies - Redis Enterprise Software | Redis Labs https://redislabs.com/redis-ente ...
- node.js 在函数内获取当前函数
js 如何在函数体内部知道 自己在哪个函数内运行呢? 打比方: function a() { // 我想在这里知道我的函数名a和函数function a () {...} } 使用Error的调用栈可 ...
- document.body.className = document.body.className.replace("siteorigin-panels-before-js","");
document.body.className = document.body.className.replace("siteorigin-panels-before-js",&q ...