Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4b87258

Browse files
committed
revist roman numbers
1 parentc387a9c commit4b87258

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

‎Easy/13-romanToInteger.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
*@param {string} s
3+
*@return {number}
4+
*/
5+
varromanToInt=function(s){
6+
varnumber=0;
7+
varmap={
8+
'I':1,
9+
'V':5,
10+
'X':10,
11+
'L':50,
12+
'C':100,
13+
'D':500,
14+
'M':1000
15+
};
16+
varlength=s.length;
17+
18+
number=map[s[length-1]];
19+
for(vari=length-2;i>=0;i--){
20+
if(map[s[i]]<map[s[i+1]]){
21+
number-=map[s[i]];
22+
}else{
23+
number+=map[s[i]];
24+
}
25+
}
26+
27+
returnnumber;
28+
29+
};

‎Medium/12-integerToRoman.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,51 @@ var intToRoman = function(num) {
99
varI=["","I","II","III","IV","V","VI","VII","VIII","IX"];
1010
returnM[Math.floor(num/1000)]+C[Math.floor((num%1000)/100)]+X[Math.floor((num%100)/10)]+I[num%10];
1111
};
12+
13+
14+
// this is awkward because the order of object keys is not reserved
15+
varintToRoman=function(num){
16+
varmap={
17+
1000:'M',
18+
900:'CM',
19+
500:'D',
20+
400:'CD',
21+
100:'C',
22+
90:'XC',
23+
50:'L',
24+
40:'XL',
25+
10:'X',
26+
9:'IX',
27+
5:'V',
28+
4:'IV',
29+
1:'I'
30+
};
31+
varromanStr='';
32+
33+
Object.keys(map).sort(function(a,b){
34+
returnb-a;
35+
}).forEach(function(val){
36+
while(num>=val){
37+
num-=val;
38+
romanStr+=map[val];
39+
}
40+
});
41+
42+
returnromanStr;
43+
};
44+
45+
// although you can use two arrays to do it.
46+
varintToRoman=function(num){
47+
varvals=[1000,900,500,400,100,90,50,40,10,9,5,4,1];
48+
varstrs=['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'];
49+
varromanStr='';
50+
51+
vals.forEach(function(val,index){
52+
while(num>=val){
53+
num-=val;
54+
romanStr+=strs[index];
55+
}
56+
});
57+
58+
returnromanStr;
59+
};

‎Medium/245-shortestWordDistIII.js‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* question: http://www.programcreek.com/2014/08/leetcode-shortest-word-distance-iii/
3+
*
4+
*/
5+
6+
functionshortestWordDist(words,word1,word2){
7+
varindex1=-1;
8+
varindex2=-1;
9+
vardist=Math.pow(2,32)-1;// for some reason in JavaScript, manually set max.
10+
11+
words.forEach(function(word,index){
12+
if(word1===word2){
13+
if(word1===word){
14+
if(index1>index2){
15+
index2=index;
16+
}else{
17+
index1=index;
18+
}
19+
}
20+
}else{
21+
if(word===word1){
22+
index1=index;
23+
}
24+
25+
if(word===word2){
26+
index2=index;
27+
}
28+
}
29+
30+
if(index1>=0&&index2>=0){
31+
dist=Math.min(Math.abs(index1-index2),dist);
32+
}
33+
})
34+
35+
returndist;
36+
}
37+
38+
// test cases
39+
varwords=["practice","makes","perfect","coding","makes"];
40+
varword1='practice';
41+
varword2='coding';
42+
varword3='makes';
43+
// console.log(shortestWordDist(words, word1, word2));
44+
console.log(shortestWordDist(words,word3,word3));

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*[7. Reverse Integer](https://oj.leetcode.com/problems/reverse-integer/) -[Solution](./Easy/7-reverseInteger.js)
99
*[8. String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) -[Solution](./Easy/8-stringToInteger.js)
1010
*[9. Palindrome Number](https://leetcode.com/problems/palindrome-number/) -[Solution](./Easy/9-palindromeNumber.js)
11+
*[13. Roman to Integer](https://leetcode.com/problems/roman-to-integer/) -[Solution](./Easy/13-romanToInteger.js)
1112
*[14. Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) -[Solution](./Easy/14-longestCommonPrefix.js)
1213
*[19. Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) -[Solution](./Easy/19-removeNthNodeFromEndofList.js)
1314
*[20. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) -[Solution](./Easy/20-validParentheses.js)
@@ -60,6 +61,7 @@
6061
*[235. Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) -[Solution](./Easy/235-lcaBST.js)
6162
*[237. Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) -[Solution](./Easy/237-deleteLinkedListNode.js)
6263
*[242. Valid Anagram](https://leetcode.com/problems/valid-anagram/) -[Solution](./Easy/242-anagram.js)
64+
*[243. Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance) -[Solution](./Medium/245-shortestWordDist.js)
6365
*[258. Add Digits](https://leetcode.com/problems/add-digits/) -[Solution](./Easy/258-addDigits.js)
6466
*[263. Ugly Number](https://leetcode.com/problems/ugly-number/) -[Solution](./Easy/263-uglyNumber.js)
6567
*[278. First Bad Version](https://leetcode.com/problems/first-bad-version/) -[Solution](./Easy/278-firstBadVersion.js)
@@ -169,6 +171,8 @@
169171
*[238. Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) -[Solution](./Medium/238-productExceptSelf.js)
170172
*[240. Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) -[Solution](./Medium/240-Search2DMatrixII.js)
171173
*[241. Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/) -[Solution](./Medium/241-differentWaysAddParentheses.js)
174+
*[244. Shortest Word Distance II](https://leetcode.com/problems/shortest-word-distance-ii) -[Solution](./Medium/245-shortestWordDistII.js)
175+
*[245. Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii) -[Solution](./Medium/245-shortestWordDistIII.js)
172176
*[260. Single Number III](https://leetcode.com/problems/single-number-iii/) -[Solution](./Medium/260-singleNumberIII.js)
173177
*[268. Missing Number](https://leetcode.com/problems/missing-number/) -[Solution](./Medium/268-missingNumber.js)
174178
*[274. H-Index](https://leetcode.com/problems/h-index/) -[Solution](./Medium/274-hIndex.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp