[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 ...
随机推荐
- AutoCAD如何倒角 倒圆角 倒直角
倒圆角:输f 再输r 再输入你想倒的半径,然后选相邻的两边倒直角:输chamfer 再输d 再输你想倒的距离,然后先相邻的两边 祝你成功
- Android 4.2 project导入 5.0 SDK Eclipse 开发环境出现的问题总结
Android 4.2 project导入 5.0 SDK Eclipse 开发环境出现的问题总结 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循&qu ...
- i18n国际化的例子
这个可以点击菜单进行中英文切换,每次切换就可以改变sessionStorage.languge,进行改变i18n的参数lang的值,然后重新调用下就可以了. 工程结构: i18n--| |---css ...
- .Net 与 Javascript 混合编程系列
之前的文章有提到 edge 和 nodejs 交互,通过node的模块为C# 提供一些扩展,这个还是比較方便. 这里说下为什么要使用js. 1.SharpKit是一个用于在编译时将C#语言转换为Jav ...
- Spring MVC 数据验证——validate编码方式
1.导入jar包 validation-api-1.0.0.GA.jar这是比較关键的一个jar包,主要用于解析注解@Valid. hibernate-validator-4.3.2.Final.ja ...
- Android学习笔记-listview实现方式之BaseAdapter
listview是Android开发中最为常用的组件,这里我们就学习一下用BaseAdapter的方式实现listview, 主布局activity_main.xml是这样的: <LinearL ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip 树形压位DP
题目链接:http://codeforces.com/contest/766/problem/E Examples input 3 1 2 3 1 2 2 3 out 10 题意: 给你一棵n个点 ...
- HDU5768Lucky7
Lucky7 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- bzoj1778
高斯消元+矩阵的逆 来自popoqqq大神 求矩阵的逆:把I-T放在左边,P/Q*S放在右边,这样就形成了一个n*2n的矩阵,然后把左边高斯消元,右边就是求完逆的矩阵,其实就是ans,矩阵的逆跟乘法逆 ...
- Python split 分割中文
str8="中国 和 韩国 的区别" # a=str8.find("Python") # print a b=str8.find("和") ...