-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6c6ba9a
Showing
3 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} | ||
|