I
, V
, X
, L
, C
, D
and M
.Symbol | Value |
---|---|
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
For example, 2
is written as II
in Roman numeral, just two ones added together. 12
is written as XII
, which is simply X + II. The number 27
is written as XXVII
, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I
can be placed before V
(5) and X
(10) to make 4 and 9. X
can be placed before L
(50) and C
(100) to make 40 and 90. C
can be placed before D
(500) and M
(1000) to make 400 and 900.Given a roman numeral, convert it to an integer.
Example 1:
Example 2:
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
let answer = 0;
const symbolArr = ['I', 'V', 'X', 'L', 'C', 'D', 'M'];
const valueArr = [1, 5, 10, 50, 100, 500, 1000];
const symbolMap = new Map();
symbolArr.forEach((symbol, index) => {
symbolMap.set(symbol, valueArr[index]);
});
let prevStrIndex = symbolArr.length - 1;
let str, prevStr, strIndex;
for (let i = 0; i < s.length; i++) {
str = s[i];
strIndex = symbolArr.indexOf(str);
if (strIndex <= prevStrIndex) {
answer += symbolMap.get(str);
} else {
const value = symbolMap.get(str).toString();
const digit = value[0] == '1' ? value.length - 2 : value.length - 1;
answer -= symbolMap.get(prevStr);
answer += symbolMap.get(str) - (10 ** digit);
}
prevStr = str;
prevStrIndex = strIndex;
}
return answer;
};
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Constraints
strs.length
<= 200strs[i].length
<= 200strs[i]
consists of only lowercase English letters.Example 1:
Example 2 :
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function(strs) {
let prefix = '';
const firstWord = strs[0];
let hasPrefix = true;
for (let i = 0; i < firstWord.length; i++) {
prefix += firstWord[i];
hasPrefix = strs.every((word) => word.startsWith(prefix));
if (!hasPrefix) {
prefix = prefix.slice(0, prefix.length - 1);
break;
}
}
return prefix.length ? prefix : "";
};
Given an integer array nums
sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums
. More formally, if there are k
elements after removing the duplicates, then the first k
elements of nums
should hold the final result. It does not matter what you leave beyond the first k elements.
Return k
after placing the final result in the first k
slots of nums
.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Example 2:
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
let currIndex = 0;
let prevNum;
nums.forEach((num) => {
if (num !== prevNum) {
nums[currIndex] = num;
prevNum = num;
currIndex += 1;
return;
}
});
nums.splice(currIndex);
};
root
of a binary tree, return the inorder traversal of its nodes' values./**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function(root) {
const data = [];
const traverse = (node) => {
if (!node) return;
if (node.left) traverse(node.left);
data.push(node.val);
if (node.right) traverse(node.right);
}
traverse(root);
return data;
};
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
const queue = [root];
let depth = 0;
let node, length;
while(queue.length) {
length = queue.length;
for (let i = 0; i < length; i++) {
node = queue.shift();
if (!node) return 0;
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
depth++;
}
return depth;
};