LeetCode 12.Integer to Roman & 15.3Sum
来源:互联网 发布:编程是干嘛的 编辑:程序博客网 时间:2024/06/05 20:59
Problem 12. Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
解题思路:
1. 首先要搞清楚罗马数字的拼写原则:
罗马数字是阿拉伯数字传入之前使用的一种数码。罗马数字采用七个罗马字母作数字、即Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500)。记数的方法:
(1)相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3;
(2)小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8、Ⅻ=12;
(3)小的数字(限于 Ⅰ、X 和 C)在大的数字的左边,所表示的数等于大数减小数得到的数,如 Ⅳ=4、Ⅸ=9;
(5)在一个数的上面画一条横线,表示这个数增值 1,000 倍。
2. 因为数字的范围是1到3999,所以可以直接用一个二维数组当做表来记录罗马数字的千位、百位、十位和个位。
3. 然后根据每位的数字来转换成对应罗马数字然后拼在一起即可。
4. 因为String是不可变对象,所以这里用StringBuffer的append()方法比几个String相加更加节省内存。
代码如下:
public class Solution { public String intToRoman(int num) { String[][] Roman = {{"","I","II","III","IV","V","VI","VII","VIII","IX"}, {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}, {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}, {"","M","MM","MMM"} }; StringBuffer result = new StringBuffer(); result.append(Roman[3][num /1000 %10]); result.append(Roman[2][num /100 %10]); result.append(Roman[1][num /10 %10]); result.append(Roman[0][num %10]); return result.toString(); }}
Problem 15. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],A solution set is:[ [-1, 0, 1], [-1, -1, 2]]
解题思路:
1. 观察测试用例的输出,可以发现输出的数字都是按照顺序排列的,同时考虑到,如果把数组排好序再处理会更加方便,所以这里用Arrays.sort()方法,先对输入的数据进行排序。
2. 排好序后,负数在前,正数在后,用一个游标从前开始往后遍历数组,记录三个数中最小的数,第二个游标从后往前遍历数组,记录三个数中最大的数,再用一个游标从前到后(或从后到前)记录两个数之间的第三个数。
3. 遍历时计算三个游标指向的数字之和,如果正好等于零则记录数据,同时移动中间和后面两个游标;如果小于零,则中间的游标后移;如果大于零,则后面的游标前移,直到中间的游标与后面的游标相遇,遍历结束。然后第一个游标后移,再次遍历。
4. 当第一个游标所指的数已经大于0时,可以直接结束遍历,因为已经排好序了,之后的数不可能再相加小于零。
代码如下:
public class Solution {public List<List<Integer>> threeSum(int[] nums) {List<List<Integer>> resultList = new ArrayList<>();Arrays.sort(nums);for(int i = 0;i < nums.length - 2;i++){ if(i > 0 && nums[i] == nums[i - 1]){ continue; } if(nums[i] > 0){ break; }int j = i+1;int k = nums.length - 1;while(j < k){int sum = nums[i] + nums[j]+ nums[k];if(sum == 0){List<Integer> list = new ArrayList<>();list.add(nums[i]);list.add(nums[j]);list.add(nums[k]);resultList.add(list);}if(sum >= 0){while(j<k && nums[k] == nums[--k]);}if(sum <= 0) {while(j < k && nums[j] == nums[++j]);}}} return resultList;}}
- LeetCode 12.Integer to Roman & 15.3Sum
- LeetCode 12. Integer to Roman
- [LeetCode]12.Integer to Roman
- LeetCode --- 12. Integer to Roman
- [Leetcode] 12. Integer to Roman
- [leetcode] 12.Integer to Roman
- leetcode 12. Integer to Roman
- leetcode 12. Integer to Roman
- [LeetCode]12. Integer to Roman
- leetcode 12. Integer to Roman
- LeetCode *** 12. Integer to Roman
- LeetCode 12. Integer to Roman
- leetcode 12. Integer to Roman
- leetcode 12. Integer to Roman
- Leetcode 12. Integer to Roman
- [Leetcode] 12. Integer to Roman
- leetcode 12. Integer to Roman
- 【leetcode】12. Integer to Roman
- android7.0 拍照、相册选图、裁剪实现
- Java中创建对象的5种方式
- QUIC中文翻译
- oj Problem D: C语言习题 计算该日在本年中是第几天
- JVM常用配置参数
- LeetCode 12.Integer to Roman & 15.3Sum
- 关于jvm 的一些学习笔记
- maven添加外部依赖包
- Bzoj 3343: 教主的魔法(分块+二分答案)
- form表单提交
- C++ Primer plus 学习笔记之第九章内存模型和名称空间(1)
- ubuntu mysql 安装和外网访问配置
- tensorboard使用1
- oracle case when的使用和将列连接成字符串