Giter VIP home page Giter VIP logo

Comments (2)

jack-pearson avatar jack-pearson commented on July 18, 2024

链表

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.

jack-pearson avatar jack-pearson commented on July 18, 2024

数组

// 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.