String13 String12 Integer<->Roman Easy&Medium
来源:互联网 发布:东方红大数据 编辑:程序博客网 时间:2024/06/13 10:44
Pre-defined Methods:
- Int[] to Char[]: Integer.toString(num).toCharArray();
Logic Flaw
num/1 > 0
can benum>0
num %= 1
never been used
*
Thoughts
- 总是觉得懒惰的罗列方法更简单,但写起来发现重复的太多,还是通用方法比较好。intToArray()方法写了好多遍,后来还是觉得直接变成string省事很多。
思路总结
- 先写的easy的RomanToInteger,
- 发现只需要从左到右翻译成int就行,
- 稍加注意减法部分就行了,而减法只有一种情况就是9,并且左边只能有一位数,因此只需要比较当前位置和后面位置的代表数字的大小即可判断
- 对于index和index+1的末位敏感已经很自然,一次写对
- 后写的medium的IntegerToRoman
- 第一反应就是分割,不同的字母对应不同的int算出来,千位数百位数十位数个位数的分割
- 分割的部分变成了helper
- 先写的intToArray,对于它奋斗了好几个版本,后来发现翻译成string再到char[]再到int[]最方便
- 有注意char和int互换时候unicode和ASCII值的不同
- 再写的主程序,千位数。写到百位数的时候写那个intToString helper method
- 本来想直接swith罗列,之前刚用了switch,后来发现很罗嗦,于是建立了两个arguments和rule,这样百位十位个位都可以一次搞定了
- 弄好了rule[]就用switch遍历了1-9,其实”945else”更方便,于是我后来又写了intToStringByDigit()代码量确实少了一些
- FLAW: 其实这时候应该想到如果罗列,可以用string array直接罗列9个,并不多,而三种位数只是9×3的数组,对于decrete answers还是比较方便的。以后会写。
- 最后订正了个位数不需要num%1的小错误
0 0
- String13 String12 Integer<->Roman Easy&Medium
- 12. Integer to Roman Medium
- Leetcode[Easy] -- Roman to Integer
- 【LeetCode】(12)Integer to Roman(Medium)
- LeetCode-12-Integer to Roman(哈希)-Medium
- Leetcode 12. Integer to Roman (Medium) (cpp)
- 【LEET-CODE】12. Integer to Roman【Medium】
- Leetcode OJ 12 Integer to Roman [Medium]
- [Leetcode 13, Easy] Roman To Integer
- 【LeetCode】(13)Roman to Integer(Easy)
- LeetCode 13. Roman to Integer -- Easy
- 13. Roman to Integer [easy] (Python)
- Easy-题目13:13. Roman to Integer
- Leetcode 13. Roman to Integer (Easy) (cpp)
- Easy 13题 Roman to Integer
- LeetCode #13 - Roman to Integer - Easy
- Easy 4 Roman to Integer(13)
- Leetcode OJ 13 Roman to Integer [Easy]
- TOJ 2132.Ambiguous permutations
- 【SDUT】[2146]最小子序列和
- 一位资深程序员大牛给予Java初学者的学习路线建议
- JVM调优总结(7):调优方法
- 【郑轻】[1837]LT说我不服
- String13 String12 Integer<->Roman Easy&Medium
- Android 笔记之 camera的控制---旧API
- 微软面试100题系列---设计包含min函数的栈
- POJ 1704 Georgia and Bob(博弈)
- ISA/ DSI and CISC v.s. RISC
- UVA 1585 Score
- FMOD.一款易用的声音引擎
- 占位
- 占位