LeetCode 11~15
2021/10/5 6:14:03
本文主要是介绍LeetCode 11~15,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
前言
本文隶属于专栏《LeetCode 刷题汇总》,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢!
本专栏目录结构请见LeetCode 刷题汇总
正文
幕布
幕布链接
11. 盛最多水的容器
题解
官方题解
双指针 left,right
class Solution { public int maxArea(int[] height) { int maxArea = 0; int low = 0, high = height.length - 1; while (low < high) { maxArea = Math.max(maxArea, Math.min(height[low], height[high]) * (high - low)); if (height[low] > height[high]) { high--; } else { low++; } } return maxArea; } }
双指针 left,right,leftMax,rightMax
class Solution { public int maxArea(int[] height) { int n = height.length, left = 0, right = n - 1, leftMax = 0, rightMax = 0, max = 0; while(left < right){ leftMax = Math.max(leftMax, height[left]); rightMax = Math.max(rightMax, height[right]); max = Math.max(max, Math.min(height[left], height[right]) * (right - left)); if(leftMax >= rightMax){ right--; }else{ left++; } } return max; } }
12. 整数转罗马数字
题解
My java solution easy to understand
2个倒序数组,2个 while,sb
class Solution { public String intToRoman(int num) { int[] nums = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; String[] romans = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; StringBuilder res = new StringBuilder(); int index = 0; while(index < 13){ while(num >= nums[index]){ res.append(romans[index]); num -= nums[index]; } index++; } return res.toString(); } }
13. 罗马数字转整数
题解
Clean O(n) c++ solution
map,4/9 特殊处理
class Solution { Map<Character, Integer> symbolValues = new HashMap<>() {{ put('I', 1); put('V', 5); put('X', 10); put('L', 50); put('C', 100); put('D', 500); put('M', 1000); }}; public int romanToInt(String s) { int ans = 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))) { ans -= value; } else { ans += value; } } return ans; } }
14. 最长公共前缀
题解
Java code with 13 lines
先遍历第一个字符串,再嵌套整个数组,遇到不相等直接返回substring
class Solution { public String longestCommonPrefix(String[] strs) { if (strs == null || strs.length == 0) { return ""; } int length = strs[0].length(); int count = strs.length; for (int i = 0; i < length; i++) { char c = strs[0].charAt(i); for (int j = 1; j < count; j++) { if (i == strs[j].length() || strs[j].charAt(i) != c) { return strs[0].substring(0, i); } } } return strs[0]; } }
15. 三数之和
题解
Concise O(N^2) Java solution
排序+双指针
class Solution { public List<List<Integer>> threeSum(int[] nums) { int n = nums.length; if(n < 3){ return new ArrayList<>(0); } Arrays.sort(nums); List<List<Integer>> res = new ArrayList<>(); for(int i = 0; i < n - 2; i++){ if(i > 0 && nums[i] == nums[i - 1]){ continue; } int a = nums[i], j = i + 1, k = n - 1; while(j < k){ if(nums[j] + nums[k] + a == 0){ res.add(Arrays.asList(a, nums[j], nums[k])); while(j < k && nums[j] == nums[j + 1]){ j++; } j++; while(j < k && nums[k] == nums[k - 1]){ k--; } k--; }else if(nums[j] + nums[k] + a < 0){ j++; }else{ k--; } } } return res; } }
这篇关于LeetCode 11~15的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-01lip-sync公司指南:一文读懂主要玩家和技术
- 2024-11-01Anthropic的新RAG方法——提升大型语言模型在特定领域的表现
- 2024-11-01UniApp 中组件的生命周期是多少-icode9专业技术文章分享
- 2024-11-01如何使用Svg Sprite Icon简化网页图标管理
- 2024-10-31Excel数据导出课程:新手从入门到精通的实用教程
- 2024-10-31Excel数据导入课程:新手入门指南
- 2024-10-31RBAC的权限课程:新手入门教程
- 2024-10-31Svg Sprite Icon课程:新手入门必备指南
- 2024-10-31怎么配置 L2TP 允许多用户连接-icode9专业技术文章分享
- 2024-10-31怎么在FreeBSD上 安装 OpenResty-icode9专业技术文章分享