Skip to content

Commit

Permalink
2019-03-15
Browse files Browse the repository at this point in the history
  • Loading branch information
mancelee committed Mar 16, 2019
0 parents commit 6c6ba9a
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
99 changes: 99 additions & 0 deletions 13.罗马数字转整数.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import java.util.HashMap;
import java.util.Map;

/*
* @lc app=leetcode.cn id=13 lang=java
*
* [13] 罗马数字转整数
*
* https://leetcode-cn.com/problems/roman-to-integer/description/
*
* algorithms
* Easy (57.10%)
* Total Accepted: 45.8K
* Total Submissions: 80.2K
* Testcase Example: '"III"'
*
* 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
*
* 字符 数值
* I 1
* V 5
* X 10
* L 50
* C 100
* D 500
* M 1000
*
* 例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做  XXVII, 即为 XX + V +
* II 。
*
* 通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数
* 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
*
*
* I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
* X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。 
* C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
*
*
* 给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
*
* 示例 1:
*
* 输入: "III"
* 输出: 3
*
* 示例 2:
*
* 输入: "IV"
* 输出: 4
*
* 示例 3:
*
* 输入: "IX"
* 输出: 9
*
* 示例 4:
*
* 输入: "LVIII"
* 输出: 58
* 解释: L = 50, V= 5, III = 3.
*
*
* 示例 5:
*
* 输入: "MCMXCIV"
* 输出: 1994
* 解释: M = 1000, CM = 900, XC = 90, IV = 4.
*
*/
class Solution {
public int romanToInt(String s) {
Map<String,Integer> map = new HashMap<String,Integer>();
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);
map.put("IV", 4);
map.put("IX", 9);
map.put("XL", 40);
map.put("XC", 90);
map.put("CD", 400);
map.put("CM", 900);
int length = s.length();
int result = 0;
for (int i = 0; i < length; i++) {
if(i +1 < length && map.get(s.charAt(i) + "" + s.charAt(i+1)) != null){
result += map.get(s.charAt(i) + "" + s.charAt(++i));
}else{
result += map.get(s.charAt(i)+"");
}
}
return result;
}
}

54 changes: 54 additions & 0 deletions 7.整数反转.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* @lc app=leetcode.cn id=7 lang=java
*
* [7] 整数反转
*
* https://leetcode-cn.com/problems/reverse-integer/description/
*
* algorithms
* Easy (31.90%)
* Total Accepted: 93.5K
* Total Submissions: 293K
* Testcase Example: '123'
*
* 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
*
* 示例 1:
*
* 输入: 123
* 输出: 321
*
*
* 示例 2:
*
* 输入: -123
* 输出: -321
*
*
* 示例 3:
*
* 输入: 120
* 输出: 21
*
*
* 注意:
*
* 假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−2^31,  2^31 − 1]。请根据这个假设,如果反转后整数溢出那么就返回
* 0。
*
*/
class Solution {
public int reverse(int x) {
int result = 0;
while (x != 0) {
result = result * 10 + x % 10;
x = x / 10;
if (x != 0 && (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && x > Integer.MAX_VALUE % 10)
|| result < Integer.MIN_VALUE / 10 || (result == Integer.MIN_VALUE / 10 && x < Integer.MIN_VALUE % 10))) {
result = 0;
break;
}
}
return result;
}
}
58 changes: 58 additions & 0 deletions 9.回文数.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* @lc app=leetcode.cn id=9 lang=java
*
* [9] 回文数
*
* https://leetcode-cn.com/problems/palindrome-number/description/
*
* algorithms
* Easy (56.04%)
* Total Accepted: 81.7K
* Total Submissions: 145.8K
* Testcase Example: '121'
*
* 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
*
* 示例 1:
*
* 输入: 121
* 输出: true
*
*
* 示例 2:
*
* 输入: -121
* 输出: false
* 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
*
*
* 示例 3:
*
* 输入: 10
* 输出: false
* 解释: 从右向左读, 为 01 。因此它不是一个回文数。
*
*
* 进阶:
*
* 你能不将整数转为字符串来解决这个问题吗?
*
*/
class Solution {
public boolean isPalindrome(int x) {
if(x < 0){
return false;
}
String s = x +"";
StringBuilder reverse = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--) {
reverse.append(s.charAt(i));
}
if(s.equals(reverse.toString())){
return true;
}
return false;

}
}

0 comments on commit 6c6ba9a

Please sign in to comment.