由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 罗马转数字,数字转罗马问题
相关主题
也贴个转罗马数字的codeleetcode上最搞笑的是这题
问个amazon面试题做题做得很郁闷,求指点
大家看看我写的这个itoa有没有buglargest rectangle in histogram
做了一下merge BSTms面试题目
攒人品,Twitter 电面题目问一道微软面试题
贡献一道G家的onsite题和简单面经(已悲剧)求教:这两道题有什么陷阱吗?
很可能被这题搞挂了,sort linked list请教c++的string vector问题,谢谢!
请问如何sort一个linked list?【一个BB公司问的字母排序的问题】
相关话题的讨论汇总
话题: mapnode话题: int话题: string话题: new话题: input
进入JobHunting版参与讨论
1 (共1页)
l***m
发帖数: 339
1
JAVA 不太熟,尝试着快速写了下这两道题,请各位大牛帮忙看看啊,有没有什么
问题。当然我这里没有考虑大数问题,这个呆会我再想想,或者谁教我也好。
public class RomansToInt {
public static HashMap map;
public static int RomanToInt(String input) {
if (input == null || input.length() == 0) {
System.out.println("The input is not valid");
return -1;
}
int len = input.length();
if (len == 1) {
return map.get(input.charAt(0));
}
int result = 0;
int i = 0;
while (i < len) {
if ((i + 1) < len
&& map.get(input.charAt(i + 1)) > map.get(input.charAt(i
))) {
result += (map.get(input.charAt(i + 1)) - map.get(input
.charAt(i)));
i = i + 2;
} else {
result += map.get(input.charAt(i));
i = i + 1;
}
}
return result;
}
public static void main(String[] args) {
map = new HashMap();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
String s = "";
System.out.println(s + " : " + RomanToInt(s));
}
}
~~~~~~~~~~~~
Int 转罗马
class MapNode{
public int key;
public String value;
MapNode(int k, String str){
key = k;
value = str;
}
}
public class IntToRoman {
public static ArrayList map;
private static String IntToRomans(int input) {
if (input == 0) {
return null;
}
String result = "";
for(MapNode node:map){
int current = node.key;
while (input >= current) {
result += node.value;
input = input - current;
}
}
return result;
}
private static void Init() {
map = new ArrayList();
map.add(new MapNode(1000,"M"));
map.add(new MapNode(900,"CM"));
map.add(new MapNode(500,"D"));
map.add(new MapNode(400,"CD"));
map.add(new MapNode(100,"C"));
map.add(new MapNode(90,"XC"));
map.add(new MapNode(50,"L"));
map.add(new MapNode(40,"XL"));
map.add(new MapNode(10,"X"));
map.add(new MapNode(9,"IX"));
map.add(new MapNode(5,"V"));
map.add(new MapNode(4,"IV"));
map.add(new MapNode(1,"I"));
}
public static void main(String[] args) {
Init();
int testInput = 10;
System.out.println(testInput + " : " + IntToRomans(testInput));
}
}
h********6
发帖数: 285
2
一直觉得考这题很无聊。。
l***m
发帖数: 339
3
昨天同学google onsite考了这题,所以我就写写。。

【在 h********6 的大作中提到】
: 一直觉得考这题很无聊。。
C***U
发帖数: 2406
4
!!!!!!!!!!

【在 l***m 的大作中提到】
: 昨天同学google onsite考了这题,所以我就写写。。
h********6
发帖数: 285
5
好吧,瞬间不无聊了。。

【在 l***m 的大作中提到】
: 昨天同学google onsite考了这题,所以我就写写。。
l***m
发帖数: 339
6
你感叹我代码太弱了。。。。赶紧给我改改呀。。。

【在 C***U 的大作中提到】
: !!!!!!!!!!
d**e
发帖数: 6098
7

// move the .length() after null check.
(i

【在 l***m 的大作中提到】
: 你感叹我代码太弱了。。。。赶紧给我改改呀。。。
l***m
发帖数: 339
8
嗯,对。是。没检查代码就有各种问题。谢谢啊!还有别得问题没?

【在 d**e 的大作中提到】
:
: // move the .length() after null check.
: (i

d**e
发帖数: 6098
9
it should be correct.
but i am not so familiar with the rules.
18 -> XVIII
19 -> XIX
can i say 18 == XIIX as well?

【在 l***m 的大作中提到】
: 嗯,对。是。没检查代码就有各种问题。谢谢啊!还有别得问题没?
l***m
发帖数: 339
10

No.... 你可以去wiki上看看规则。

【在 d**e 的大作中提到】
: it should be correct.
: but i am not so familiar with the rules.
: 18 -> XVIII
: 19 -> XIX
: can i say 18 == XIIX as well?

相关主题
贡献一道G家的onsite题和简单面经(已悲剧)leetcode上最搞笑的是这题
很可能被这题搞挂了,sort linked list做题做得很郁闷,求指点
请问如何sort一个linked list?largest rectangle in histogram
进入JobHunting版参与讨论
e***s
发帖数: 799
11
不可以

【在 d**e 的大作中提到】
: it should be correct.
: but i am not so familiar with the rules.
: 18 -> XVIII
: 19 -> XIX
: can i say 18 == XIIX as well?

e***s
发帖数: 799
12
我觉得Integer to Roman 要是能用look up table。那不是切菜一样切
x*****n
发帖数: 195
13
你去看wiki,规则很复杂,要面面俱到

【在 e***s 的大作中提到】
: 我觉得Integer to Roman 要是能用look up table。那不是切菜一样切
w***o
发帖数: 109
14
有点罗嗦,这样应该简洁一点:
public static int RomanToInt(String input) {
// null check ...

int pre = map.get(input.charAt(0));
int ret = pre;
for(int i = 1; i < input.length; i++ {
int cur = map.get(input.charAt(i));
ret += cur;
if(cur > pre)
{ret -= pre * 2;pre = 1001;}
else
pre = cur;
}
return ret;
}
C***U
发帖数: 2406
15
没 是感叹这个题目我写的很长

【在 l***m 的大作中提到】
: 你感叹我代码太弱了。。。。赶紧给我改改呀。。。
w****x
发帖数: 2483
16

//Turn Roman number to decimal
//I(1), V(5), X(10), L(50), C(100), D(500), M(1000)
int GetNum(char c)
{
if (c == 'I' || c == 'i')
return 1;
else if (c == 'V' || c == 'v')
return 5;
else if (c == 'X' || c == 'x')
return 10;
else if (c == 'L' || c == 'l')
return 50;
else if (c == 'C' || c == 'c')
return 100;
else if (c == 'D' || c == 'd')
return 500;
else if (c == 'M' || c == 'm')
return 1000;

return 0;
}
//compare current digit to previous one,
//if bigger or equal, plus current digit
//if less, minus current digit
int RomanToDec(const char* szNum)
{
assert(szNum);
int nLen = strlen(szNum);
if (nLen == 0)
return 0;
const char* pIter = szNum + nLen - 1;
int nRet = 0;
int nPrev = 0;
while (pIter >= szNum)
{
int nCur = GetNum(*pIter);
if (nCur >= nPrev)
nRet += nCur;
else nRet -= nCur;
nPrev = nCur;
pIter--;
}
return nRet;
}
string intToRoman(int num) {
string table[] = {"M", "CM","D","CD","C","XC","L","XL","X","IX","V","IV"
,"I"};
int values[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
string result;
for(int iter = 0; iter < 13;)
{
if(num >= values[iter])
{
result += table[iter];
num -= values[iter];
}
else
++iter;
}
return result;
}

【在 l***m 的大作中提到】
:
: No.... 你可以去wiki上看看规则。

w***o
发帖数: 109
17
倒着来想法很新颖,但好像有问题,比如 IVX = (V - I) + X, not (X - V - I)

const char* pIter = szNum + nLen - 1;
int nRet = 0;
int nPrev = 0;
while (pIter >= szNum)
{
int nCur = GetNum(*pIter);
if (nCur >= nPrev)
nRet += nCur;
else nRet -= nCur;
nPrev = nCur;
pIter--;
}

【在 w****x 的大作中提到】
:
: //Turn Roman number to decimal
: //I(1), V(5), X(10), L(50), C(100), D(500), M(1000)
: int GetNum(char c)
: {
: if (c == 'I' || c == 'i')
: return 1;
: else if (c == 'V' || c == 'v')
: return 5;
: else if (c == 'X' || c == 'x')

1 (共1页)
进入JobHunting版参与讨论
相关主题
【一个BB公司问的字母排序的问题】攒人品,Twitter 电面题目
input a string "hello word", print l:3 o:2 e:1 d:1 h:1 r:1 w:1.不知道哪错了贡献一道G家的onsite题和简单面经(已悲剧)
给大家推荐个网站,interviewstreet.com很可能被这题搞挂了,sort linked list
请教一个新鲜算法面试题请问如何sort一个linked list?
也贴个转罗马数字的codeleetcode上最搞笑的是这题
问个amazon面试题做题做得很郁闷,求指点
大家看看我写的这个itoa有没有buglargest rectangle in histogram
做了一下merge BSTms面试题目
相关话题的讨论汇总
话题: mapnode话题: int话题: string话题: new话题: input