Skip to main content

10 posts tagged with "math"

View All Tags

· One min read

https://leetcode.com/problems/fizz-buzz/

Solution 1

/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function (n) {
const result = [];

for (let i = 1; i <= n; i++) {
if (i % 15 === 0) {
result.push('FizzBuzz');
} else if (i % 5 === 0) {
result.push('Buzz');
} else if (i % 3 === 0) {
result.push('Fizz');
} else {
result.push(i.toString());
}
}

return result;
};

Runtime: 102 ms, faster than 64.62% of JavaScript online submissions for Fizz Buzz.

Memory Usage: 44.3 MB, less than 69.20% of JavaScript online submissions for Fizz Buzz.

· One min read

https://leetcode.com/problems/roman-to-integer/

Solution 1

/**
* @param {string} s
* @return {number}
*/
var romanToInt = function (s) {
let total = 0;

for (let i = 0; i < s.length; i++) {
if (values[s[i]] < values[s[i + 1]]) {
total += values[s[i + 1]] - values[s[i]];
i++;
} else {
total += values[s[i]];
}
}
return total;
};

const values = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};

Runtime: 235 ms, faster than 40.69% of JavaScript online submissions for Roman to Integer.

Memory Usage: 47.1 MB, less than 67.53% of JavaScript online submissions for Roman to Integer.

· One min read

https://leetcode.com/problems/excel-sheet-column-title/

Solution 1

/**
* @param {number} columnNumber
* @return {string}
*/
var convertToTitle = function (columnNumber) {
// String.fromCharCode(65) === "A"
// 한 자릿수일 경우
if (columnNumber - 1 < 26) {
return String.fromCharCode(65 + ((columnNumber - 1) % 26));
}

// 두 자릿수 이상일 경우
let result = '';
while (columnNumber > 0) {
const remainder = (columnNumber - 1) % 26;
const remainderCharCode = String.fromCharCode(65 + remainder);
result = remainderCharCode + result;
columnNumber = parseInt((columnNumber - 1) / 26);
}
return result;
};

다시 봐야함

· One min read

https://leetcode.com/problems/excel-sheet-column-number/

Solution 1

/**
* @param {string} columnTitle
* @return {number}
*/
var titleToNumber = function (columnTitle) {
if (columnTitle.length === 1) {
return (columnTitle.charCodeAt(0) % 65) + 1;
}

let result = 0;

for (let i = columnTitle.length - 1; i >= 0; i--) {
const strCode = (columnTitle[i].charCodeAt(0) % 65) + 1;
if (i === columnTitle.length - 1) {
result += strCode;
} else {
result += strCode * 26 ** (columnTitle.length - i - 1);
}
}
return result;
};

· One min read

https://leetcode.com/problems/happy-number/

Solution 1

/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function (n) {
let set = new Set();

while (!set.has(n)) {
set.add(n);
let s = n.toString();
n = 0;
for (let i = 0; i < s.length; ++i) {
n += squares[s[i]];
}
if (n === 1) return true;
}

return false;
};

const squares = {
0: 0,
1: 1,
2: 4,
3: 9,
4: 16,
5: 25,
6: 36,
7: 49,
8: 64,
9: 81,
};

· One min read

https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/

Solution 1

/**
* @param {number} num
* @return {number}
*/
var minimumSum = function (num) {
const numToArr = num.toString().split('');

for (let i = 1; i < numToArr.length; i++) {
for (let j = 0; j < i; j++) {
if (numToArr[i] < numToArr[j]) {
const smallNum = numToArr[i];
numToArr[i] = numToArr[j];
numToArr[j] = smallNum;
}
}
}

const new1 = numToArr[0] + numToArr[2];
const new2 = numToArr[1] + numToArr[3];

return Number(new1) + Number(new2);
};