Write a program that outputs the string representation of numbers from 1 to n.
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
/** * @param {number} n * @return {string[]} */ var fizzBuzz = function(n) { let ans = []; for (let i = 1; i <= n; i++) { let str = ''; if (i % 3 === 0) str += 'Fizz'; if (i % 5 === 0) str += 'Buzz'; if (str === '') str += i;
ans.push(str); }
return ans; };
Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.
/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * function NestedInteger() { * * Return true if this NestedInteger holds a single integer, rather than a nested list. * @return {boolean} * this.isInteger = function() { * ... * }; * * Return the single integer that this NestedInteger holds, if it holds a single integer * Return null if this NestedInteger holds a nested list * @return {integer} * this.getInteger = function() { * ... * }; * * Return the nested list that this NestedInteger holds, if it holds a nested list * Return null if this NestedInteger holds a single integer * @return {NestedInteger[]} * this.getList = function() { * ... * }; * }; */
while (this.a.length) { if (this.a[0] instanceof NestedInteger) { var obj = this.a.shift(); // if a[0] is List if (!obj.isInteger()) { var arr = obj.getList(); if (arr.length) Array.prototype.unshift.apply(this.a, arr); } else { var item = obj.getInteger(); Array.prototype.unshift.apply(this.a, [item]); returntrue; } } else { returntrue; } }