Comments (2)
链表
class ListNode {
val: number;
next: ListNode | null;
constructor(val?: number, next?: ListNode | null) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
}
const addTwoNumbers = (l1: ListNode | null, l2: ListNode | null): ListNode | null => {
let result: ListNode = new ListNode(0);
let temp: ListNode = result;
let carry: number = 0;
while (l1 || l2 || carry) {
let sum = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
carry = Math.floor(sum / 10);
temp.next = new ListNode(sum % 10);
temp = temp.next;
l1 = l1 ? l1.next : null;
l2 = l2 ? l2.next : null;
}
return result.next;
};
const l1 = new ListNode(2);
l1.next = new ListNode(4);
l1.next.next = new ListNode(3);
const l2 = new ListNode(5);
l2.next = new ListNode(6);
l2.next.next = new ListNode(4);
console.log(addTwoNumbers(l1, l2));
// 结果
// ListNode {
// val: 7,
// next: ListNode { val: 0, next: ListNode { val: 8, next: null } }
// }
const l3 = new ListNode(0);
const l4 = new ListNode(0);
console.log(addTwoNumbers(l3, l4));
// ListNode { val: 0, next: null }
from leetcode.
数组
// for 循环
function addTwoNumbers(l1: number[] | null, l2: number[] | null): number[] {
if (l1 === null) return l2;
if (l2 === null) return l1;
let carry = 0;
const result: number[] = [];
for (let i = 0; i < l1.length || i < l2.length; i++) {
const sum = (l1[i] || 0) + (l2[i] || 0) + carry;
carry = Math.floor(sum / 10);
result.push(sum % 10);
}
if (carry) result.push(carry);
return result;
}
// while 循环. 实则是一样
const addTwoNumbers = (l1: number[], l2: number[]): number[] => {
const result: number[] = [];
let i1 = l1.length - 1;
let i2 = l2.length - 1;
let carry = 0;
while (i1 >= 0 || i2 >= 0 || carry) {
const sum = (l1[i1] || 0) + (l2[i2] || 0) + carry;
carry = Math.floor(sum / 10);
result.push(sum % 10);
i1--;
i2--;
}
return result;
};
console.log(addTwoNumbers([2, 4, 3], [5, 6, 4])); // [ 7, 0, 8 ]
console.log(addTwoNumbers([0], [0])); // [ 0 ]
console.log(addTwoNumbers([9, 9, 9, 9, 9, 9, 9], [9, 9, 9, 9])); // [ 8, 9, 9, 9, 0, 0, 0, 1 ]
from leetcode.
Related Issues (11)
- 1 - 两数之和 HOT 1
- 3 - 无重复字符的最长子串 HOT 1
- 4 - 寻找两个正序数组的中位数 HOT 2
- 5 - 最长回文子串 HOT 2
- 10 - 正则表达式匹配 HOT 1
- 11 - 盛最多水的容器 HOT 1
- 15 - 三数之和 HOT 1
- 17 - 电话号码的字母组合 HOT 1
- 19 - 删除链表的倒数第 N 个结点 HOT 1
- 20 - 有效的括号
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leetcode.