![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode算法合集
leetcode
huuuuufei
这个作者很懒,什么都没留下…
展开
-
16. 最接近的三数之和
class Solution { public int threeSumClosest(int[] nums, int target) { int best = nums[0] + nums[1] + nums[2]; Arrays.sort(nums); for(int i = 0; i < nums.length - 2; i++){ if(i > 0 && nums[i - 1] == num.原创 2021-03-18 20:05:52 · 84 阅读 · 0 评论 -
15. 三数之和
class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); for(int i = 0; i < nums.length - 2; i++){ //如果第一个值.原创 2021-03-18 15:55:20 · 67 阅读 · 0 评论 -
14. 最长公共前缀
纵向扫描class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length == 0 || strs[0].length() == 0) return ""; if(strs.length == 1) return strs[0]; StringBuffer sb = new StringBuffer(); for(int i = 0;.原创 2021-03-18 11:00:14 · 64 阅读 · 0 评论 -
13. 罗马数字转整数
class Solution { public int romanToInt(String s) { int ans = 0; for(int i = 0; i < s.length(); i++){ char ch = s.charAt(i); if(i < s.length() - 1){ if(ch == 'C' && (s.charAt(i + 1) .原创 2021-03-18 01:22:29 · 87 阅读 · 0 评论 -
12. 整数转罗马数字
class Solution { public String intToRoman(int num) { StringBuffer ans = new StringBuffer(); int[] nodes = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; String[] signs = new String[]{"M", "CM", "D", "CD", "C",.原创 2021-03-17 21:17:13 · 70 阅读 · 0 评论 -
11. 盛最多水的容器
双指针法class Solution { public int maxArea(int[] height) { int l = height.length; int start = 0, end = l - 1, max = 0; while(start <= end){ int h = Math.min(height[start], height[end]); max = Math.max(m.原创 2021-03-17 20:53:53 · 61 阅读 · 0 评论 -
9. 回文数
class Solution { public boolean isPalindrome(int x) { if(x < 0 || (x % 10 == 0 && x != 0)) return false; int revertedNumber = 0; while (x > revertedNumber) { revertedNumber = revertedNumber * 10 + x % .原创 2021-03-17 15:18:28 · 95 阅读 · 0 评论 -
8. 字符串转换整数 (atoi)
初始方法,写出所有边界条件,扩展性不强,容易漏class Solution { public int myAtoi(String s) { int index = 0, prefix = 1, result = 0; boolean isZero = false; while(index < s.length() && s.charAt(index) == ' '){ index++; .原创 2021-03-16 23:05:48 · 61 阅读 · 0 评论 -
7. 整数反转
class Solution { public int reverse(int x) { int max = (int)(Math.pow(2, 31) - 1); int min = (int)Math.pow(2, 31) + 1; if(x == max || x == min) return 0; int result = 0, i = 0, n = 0; while((n = x / (int)Math.po.原创 2021-03-16 19:50:52 · 59 阅读 · 0 评论 -
6. Z 字形变换
class Solution { public String convert(String s, int numRows) { int d1 = 0; int d2 = 0; String ans = ""; if(numRows == 1) return s; for(int i = 0; i < s.length() && i < numRows; i++){ a.原创 2021-03-12 22:40:19 · 78 阅读 · 0 评论 -
5. 最长回文子串
动态规划边界条件:dp[i][i] = true (L == 0)dp[i][i + 1] = s[i] == s[i + 1] (L == 1)状态转移方程(L >= 2):dp[i][i + L] = dp[i + 1][i + L - 1] && (s[i] == s[i + L])class Solution { public String longestPalindrome(String s) { int length ..原创 2021-03-12 21:04:07 · 82 阅读 · 0 评论 -
3. 无重复字符的最长子串
//滑动窗口法class Solution { public int lengthOfLongestSubstring(String s) { int l = s.length(); if(l == 1) return 1; int max = 0; Map<Character, Integer> map = new HashMap<>(); int first = 0, second = 0.原创 2021-03-12 10:41:51 · 78 阅读 · 0 评论 -
2. 两数相加
解:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } .原创 2021-01-28 22:12:43 · 71 阅读 · 0 评论 -
1. 两数之和
解:class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < nums.length; i++){ if(map.containsKey(nums[i])){ return new in.原创 2021-01-28 21:42:46 · 54 阅读 · 0 评论 -
0. 算法题索引
算法索引:滑动窗口:https://blog.csdn.net/qq_40622253/article/details/114687050动态规划:https://blog.csdn.net/qq_40622253/article/details/114706356原创 2021-03-16 21:39:28 · 67 阅读 · 0 评论