Skip to content

Commit 30653ad

Browse files
committed
Mar 4, 2019
1 parent adc3f8c commit 30653ad

3 files changed

+184
-0
lines changed

Diff for: 013__easy__roman-to-integer.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
3+
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
4+
5+
Symbol Value
6+
I 1
7+
V 5
8+
X 10
9+
L 50
10+
C 100
11+
D 500
12+
M 1000
13+
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
14+
15+
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:
16+
17+
I can be placed before V (5) and X (10) to make 4 and 9.
18+
X can be placed before L (50) and C (100) to make 40 and 90.
19+
C can be placed before D (500) and M (1000) to make 400 and 900.
20+
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
21+
22+
Example 1:
23+
24+
Input: "III"
25+
Output: 3
26+
Example 2:
27+
28+
Input: "IV"
29+
Output: 4
30+
Example 3:
31+
32+
Input: "IX"
33+
Output: 9
34+
Example 4:
35+
36+
Input: "LVIII"
37+
Output: 58
38+
Explanation: L = 50, V= 5, III = 3.
39+
Example 5:
40+
41+
Input: "MCMXCIV"
42+
Output: 1994
43+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
44+
45+
*/
46+
47+
/**
48+
*
49+
* O(n) time
50+
* O(1) space
51+
*
52+
* 15m
53+
*/
54+
/**
55+
* @param {string} s
56+
* @return {number}
57+
*/
58+
var romanToInt = function(s) {
59+
if (!s) return 0;
60+
const map = {
61+
'I': 1,
62+
'V': 5,
63+
'X': 10,
64+
'L': 50,
65+
'C': 100,
66+
'D': 500,
67+
'M': 1000
68+
}
69+
let sum = map[s[s.length-1]];
70+
for (let i = s.length-2; i >= 0; i--) {
71+
if (map[s[i]] >= map[s[i+1]]) {
72+
sum += map[s[i]];
73+
} else {
74+
sum -= map[s[i]];
75+
}
76+
}
77+
return sum;
78+
};

Diff for: 169__easy__majority-element.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
*
3+
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
4+
5+
You may assume that the array is non-empty and the majority element always exist in the array.
6+
7+
Example 1:
8+
9+
Input: [3,2,3]
10+
Output: 3
11+
Example 2:
12+
13+
Input: [2,2,1,1,1,2,2]
14+
Output: 2
15+
16+
*/
17+
18+
/**
19+
* Because we know majority element is more than 50%, it will always win the count
20+
* O(n) time
21+
* O(1) space
22+
*
23+
* 10m
24+
*
25+
* Runtime: 60 ms, faster than 98.78%
26+
* Memory Usage: 37.1 MB, less than 97.09%
27+
*
28+
*/
29+
var majorityElement = function(nums) {
30+
let count = 1;
31+
let curr_num = nums[0];
32+
33+
for (let i = 1; i < nums.length; i++) {
34+
if (curr_num == nums[i]) count++;
35+
else count--;
36+
37+
if (count === 0) {
38+
count = 1;
39+
curr_num = nums[i];
40+
}
41+
}
42+
return curr_num;
43+
};
44+
45+
/**
46+
* Use hashmap, this is slower than 1st one and using O(n) space
47+
*
48+
* O(n) time
49+
* O(n) space
50+
*
51+
* 10m
52+
*/
53+
var majorityElement = function(nums) {
54+
let map = {};
55+
let n = nums.length;
56+
let half = ~~(n/2);
57+
58+
for (let i = 0; i < n; i++) {
59+
let num = nums[i];
60+
61+
map[num] = (map[num] || 0) + 1;
62+
if (map[num] > half ) {
63+
return num;
64+
}
65+
}
66+
return false;
67+
};

Diff for: 237__easy__delete-node-in-a-linked-list.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
*
3+
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
4+
5+
Given linked list -- head = [4,5,1,9], which looks like following:
6+
7+
Example 1:
8+
9+
Input: head = [4,5,1,9], node = 5
10+
Output: [4,1,9]
11+
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
12+
Example 2:
13+
14+
Input: head = [4,5,1,9], node = 1
15+
Output: [4,5,9]
16+
Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
17+
18+
19+
Note:
20+
21+
The linked list will have at least two elements.
22+
All of the nodes' values will be unique.
23+
The given node will not be the tail and it will always be a valid node of the linked list.
24+
Do not return anything from your function.
25+
26+
*/
27+
28+
/**
29+
*
30+
* O(n) time
31+
* O(1) space
32+
*
33+
* 20m (10sec next time...)
34+
*/
35+
var deleteNode = function(node) {
36+
// very tricky but interesting!
37+
node.val = node.next.val;
38+
node.next = node.next.next;
39+
};

0 commit comments

Comments
 (0)