标签:mic app substr 技术 str art substring 大于 append
class Solution { public static List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> ans = new ArrayList(); int len = nums.length; if(nums == null || len < 3) return ans; Arrays.sort(nums); // 排序 for (int i = 0; i < len ; i++) { if(nums[i] > 0) break; // 如果当前数字大于0,则三数之和一定大于0,所以结束循环 if(i > 0 && nums[i] == nums[i-1]) continue; // 去重 int L = i+1; int R = len-1; while(L < R){ int sum = nums[i] + nums[L] + nums[R]; if(sum == 0){ ans.add(Arrays.asList(nums[i],nums[L],nums[R])); while (L<R && nums[L] == nums[L+1]) L++; // 去重 while (L<R && nums[R] == nums[R-1]) R--; // 去重 L++; R--; } else if (sum < 0) L++; else if (sum > 0) R--; } } return ans; } }
class Solution { public: string intToRoman(int num) { char* c[4][10] = { {"","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"} }; string roman; roman.append(c[3][num / 1000]); roman.append(c[2][num / 100 % 10]); roman.append(c[1][num / 10 % 10]); roman.append(c[0][num % 10]); return roman; } };
class Solution { public int romanToInt(String s) { Map<String, Integer> map = new HashMap<>(); map.put("I", 1); map.put("IV", 4); map.put("V", 5); map.put("IX", 9); map.put("X", 10); map.put("XL", 40); map.put("L", 50); map.put("XC", 90); map.put("C", 100); map.put("CD", 400); map.put("D", 500); map.put("CM", 900); map.put("M", 1000); int ans = 0; for(int i = 0;i < s.length();) { if(i + 1 < s.length() && map.containsKey(s.substring(i, i+2))) { ans += map.get(s.substring(i, i+2)); i += 2; } else { ans += map.get(s.substring(i, i+1)); i ++; } } return ans; } }
class Solution { public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int ans = nums[0] + nums[1] + nums[2]; for(int i=0;i<nums.length;i++) { int start = i+1, end = nums.length - 1; while(start < end) { int sum = nums[start] + nums[end] + nums[i]; if(Math.abs(target - sum) < Math.abs(target - ans)) ans = sum; if(sum > target) end--; else if(sum < target) start++; else return ans; } } return ans; } }
标签:mic app substr 技术 str art substring 大于 append
原文地址:https://www.cnblogs.com/lemonzhang/p/13114758.html