-
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
liguanghua
committed
Oct 14, 2021
1 parent
d9edc4a
commit dd1565a
Showing
3 changed files
with
140 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,55 @@ | ||
/** | ||
* @author dell | ||
* @version 1.0 | ||
* @description: 整数转罗马数字 | ||
* | ||
* 罗马数字包含以下七种字符: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。 | ||
* 给你一个整数,将其转为罗马数字。 | ||
* | ||
* 来源:力扣(LeetCode) | ||
* 链接:https://leetcode-cn.com/problems/integer-to-roman | ||
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
* @date 2021/10/14 10:10 | ||
*/ | ||
public class LeetCode12 { | ||
/** | ||
* @title | ||
* @description 思路:只要数组列出可能的情况,然后循环比较然后拼接直至目标为0即可 | ||
* @author admin | ||
* @updateTime 2021/10/14 10:22 | ||
* @throws | ||
*/ | ||
public String intToRoman(int num) { | ||
int[] numbers = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; | ||
String[] values = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; | ||
StringBuilder target = new StringBuilder(); | ||
for (int i=0;i<numbers.length;i++){ | ||
while (num >= numbers[i]){ | ||
num -= numbers[i]; | ||
target.append(values[i]); | ||
} | ||
if (num == 0) break; | ||
} | ||
return target.toString(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(new LeetCode12().intToRoman(1994)); | ||
} | ||
} |
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,39 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author dell | ||
* @version 1.0 | ||
* @description: 罗马数字转整数 | ||
* @date 2021/10/14 10:32 | ||
*/ | ||
public class LeetCode13 { | ||
|
||
public int romanToInt(String s) { | ||
Map<Character, Integer> symbolValues = new HashMap<Character, Integer>() {{ | ||
put('I', 1); | ||
put('V', 5); | ||
put('X', 10); | ||
put('L', 50); | ||
put('C', 100); | ||
put('D', 500); | ||
put('M', 1000); | ||
}}; | ||
|
||
int target = 0; | ||
int n = s.length(); | ||
for (int i = 0; i < n; i++) { | ||
int value = symbolValues.get(s.charAt(i)); | ||
if (i < n - 1 && value < symbolValues.get(s.charAt(i + 1))) { | ||
target -= value; | ||
} else { | ||
target += value; | ||
} | ||
} | ||
return target; | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(new LeetCode13().romanToInt("LVIII")); | ||
} | ||
} |
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,46 @@ | ||
/** | ||
* @author dell | ||
* @version 1.0 | ||
* @description: 最长公共前缀 | ||
* 编写一个函数来查找字符串数组中的最长公共前缀。 | ||
* | ||
* 如果不存在公共前缀,返回空字符串""。 | ||
* | ||
* | ||
* 示例 1: | ||
* | ||
* 输入:strs = ["flower","flow","flight"] | ||
* 输出:"fl" | ||
* 示例 2: | ||
* | ||
* 输入:strs = ["dog","racecar","car"] | ||
* 输出:"" | ||
* 解释:输入不存在公共前缀。 | ||
* | ||
* 来源:力扣(LeetCode) | ||
* 链接:https://leetcode-cn.com/problems/longest-common-prefix | ||
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
* @date 2021/10/14 10:55 | ||
*/ | ||
public class LeetCode14 { | ||
|
||
public String longestCommonPrefix(String[] strs) { | ||
if (strs == null || strs.length == 0) return ""; | ||
String first = strs[0];//取出第一个作为初始比较的串 | ||
for (int i = 1; i < strs.length; i++) { | ||
int j = 0; | ||
for (; j < first.length() && j < strs[i].length(); j++) { | ||
if (first.charAt(j) != strs[i].charAt(j)) { | ||
break; | ||
} | ||
} | ||
first = first.substring(0, j); | ||
if (first.equals("")) return ""; | ||
} | ||
return first; | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(new LeetCode14().longestCommonPrefix(new String[]{"flower","flow","flight"})); | ||
} | ||
} |