
LeetCode
STU756
这个作者很懒,什么都没留下…
展开
-
leetcode31-40
31. 下一个排列class Solution { public void nextPermutation(int[] nums) { int len = nums.length, i = len - 1;; while(i > 0 && nums[i-1] >= nums[i]) --i; if(i <= 0) { reverse(nums, 0, len - 1); }原创 2022-03-03 02:09:25 · 184 阅读 · 0 评论 -
LeetCode 24-30
24. 两两交换链表中的节点class Solution { public ListNode swapPairs(ListNode head) { ListNode dummy = new ListNode(-1); ListNode cur = dummy, pre = head, tail = null; cur.next = head; while(pre!= null && pre.next!= null)原创 2022-02-25 23:58:28 · 177 阅读 · 0 评论 -
23. 合并K个排序链表
public class Solution { //优先队列:时间复杂度:O(kn*logk) 空间:O(k) public ListNode mergeKLists(ListNode[] lists) { PriorityQueue<ListNode> minHeap = new PriorityQueue<>((n1, n2)->n1.val - n2.val); //小根堆 for(ListNode list : li.原创 2022-02-25 01:06:47 · 284 阅读 · 0 评论 -
22. 括号生成
class Solution { List<String> ans = new ArrayList<>(); //Time:O(C(n, 2n)) 卡特兰数 public List<String> generateParenthesis(int n) { if(n <= 0) return ans; dfs(n, 0, 0,""); return ans; } //l表示"(.原创 2022-02-25 00:42:49 · 106 阅读 · 0 评论 -
21. 合并两个有序链表
class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { //辅助节点 ListNode dummy = new ListNode(-1); for(ListNode cur = dummy; l1!= null || l2!=null;) { if(l1 == null){ cur.next = l2;.原创 2022-02-25 00:42:08 · 330 阅读 · 0 评论 -
LeetCode 20. 有效的括号
class Solution { public boolean isValid(String s) { int len = s.length(); if(len % 2== 1) return false;//单数不配对 Stack<Character> stack = new Stack<>(); for(char c : s.toCharArray()){ if(c == '(') .原创 2022-02-25 00:43:03 · 115 阅读 · 0 评论 -
19. 删除链表的倒数第 N 个结点
class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { //辅助节点 ListNode dummy = new ListNode(-1); dummy.next = head; //链表长度 int len = 0; ListNode cur = head; while(cur != null){.原创 2022-02-24 18:17:02 · 123 阅读 · 0 评论 -
17. 电话号码的字母组合
class Solution { static String str[] = { "", "","abc","def", "ghi","jkl", "mno","pqrs","tuv","wxyz" }; /* 执行用时: 5 ms , 在所有 Java 提交中击败了 19.26% 的用户 内存消耗: 41.4 MB , 在所有 Java 提交中击败了 5.02% 的用户 */.原创 2022-02-24 18:07:06 · 344 阅读 · 0 评论 -
18. 四数之和
class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { //双指针 List<List<Integer>> res = new ArrayList<>(); if(nums == null || nums.length < 3) return res; //排序 .原创 2022-02-24 17:41:23 · 103 阅读 · 0 评论 -
16. 最接近的三数之和
class Solution { public int threeSumClosest(int[] nums, int target) { int res = Integer.MAX_VALUE, min= Integer.MAX_VALUE; //排序 Arrays.sort(nums); for(int i = 0; i < nums.length; i++){ if(i > 0 &&a.原创 2022-02-24 17:34:57 · 295 阅读 · 0 评论 -
15. 三数之和
class Solution { //双指针 public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new ArrayList<>(); if(nums == null || nums.length < 3) return res; //排序 Arrays.sort.原创 2022-02-24 17:24:43 · 198 阅读 · 0 评论 -
14. 最长公共前缀
class Solution { public String longestCommonPrefix(String[] strs) { if(strs == null || strs.length == 0) return ""; Arrays.sort(strs); //取头尾 int len = Math.min(strs[0].length(), strs[strs.length-1].length()); in.原创 2022-02-24 16:47:42 · 187 阅读 · 0 评论 -
13. 罗马数字转整数
class Solution { static Map<Character, Integer> map = new HashMap<>(); static { map.put('I', 1); map.put('V', 5); map.put('X', 10); map.put('L', 50); map.put('C', 100); map.put('D', 500);.原创 2022-02-24 16:26:49 · 189 阅读 · 0 评论 -
12. 整数转罗马数字
class Solution { static String[] reps = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; static int[] values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, .原创 2022-02-24 16:18:22 · 195 阅读 · 0 评论 -
10. 正则表达式匹配
public class Solution { public boolean isMatch(String s, String p) { if(s.length() == 0 && p.length() == 0) return true; if(p.length() == 0) return false; boolean dp[][] = new boolean[s.length() + 1][p.length() + 1]; .原创 2022-02-24 16:08:31 · 187 阅读 · 0 评论 -
11. 盛最多水的容器
class Solution { //双指针 贪心 //假设l < r, l 和 r之间是容纳的水是最优解。 //当i到达左边边界l时候,只需要j在r右边位置向左移,height[l] >= height[j],j不停的移动到r就是找到最优解, //所以height[j]<= height[l]严格小于左边界的。 /*反正法:条件是l到r之间最优解max,r<= j <len, height[j]严格小于height[l]边界。 .原创 2022-02-24 16:07:21 · 726 阅读 · 0 评论 -
2.两数相加
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummyNode = new ListNode(-1); ListNode p = l1, q = l2, curr = dummyNode; int carry = 0; while(p!=null || q!=null) { int ...原创 2018-08-15 01:22:40 · 290 阅读 · 0 评论 -
3.无重复字符的最长子串
import java.util.Map; import java.util.HashMap; //维持最大字符不重复的滑动窗口 public int lengthOfLongestSubstring(String s) { if(null==s || s.length() == 0) return 0; Map<Charac...原创 2018-08-15 01:23:12 · 200 阅读 · 0 评论 -
4.寻找两个正序数组的中位数
public double findMedianSortedArrays(int[] nums1, int[] nums2) { int m = nums1.length, n = nums2.length; //l 和 r 解决(m + n)奇偶性问题 int l = (m + n + 1) / 2; int r = (m...原创 2018-08-15 01:23:50 · 248 阅读 · 0 评论 -
5. 最长回文子串
//动态规划 //假设字符串下标为i到j之间判断是否是回文串,dp[i][j](其中i <= j), //如果s.charAt(i)==s.chatAt(j)那么需要判断从下标i+1到下标j-1是否为回文串,即dp[i+1][j-1](其中i+1<=j-1)是否是回文串 //dp[i][j] = 1 (i==j) // =s.c...原创 2022-02-23 13:10:35 · 232 阅读 · 0 评论 -
6.ZigZag Conversion
//看不懂题,在网上查找才知道,打印之字形,垂直打印完之后,从倒数第二个倒过来打印到第二个。 // * * * // * * * * * // * * * * * // * * * * // * * ...原创 2018-08-15 01:25:19 · 346 阅读 · 0 评论 -
7. 整数反转
//不使用long类型的话。可以通过Integer.MAX_VALUE/10和Integer.MIN_VALUE/10同x进行比较进行比较判断 //Integer.MAX_VALUE%10和(x % 10)对比判断,如果为负数反之同理。 public int reverse(int x) { long res = 0; while(0 !=...原创 2018-08-15 01:25:49 · 216 阅读 · 0 评论 -
8.String to Integer (atoi)
private static final int maxDiv10 = Integer.MAX_VALUE / 10; public int myAtoi(String str) { int len = str.length(); int sign = 1;//符号位 int num = 0; int i = 0;...原创 2018-08-15 01:26:36 · 337 阅读 · 0 评论 -
9.Palindrome Number
public boolean isPalindrome(int x) { if(x < 0) return false; int temp = x; int val = 0; while(x != 0) { val = val * 10 + x % 10; x /= 10; } return (val == temp)? true : false; ...原创 2018-08-15 01:27:04 · 327 阅读 · 0 评论 -
1.两数之和
import java.util.HashMap; import java.util.Map; public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> map = new HashMap<Integer,Integer>(); for(int i =0; i<...原创 2018-08-15 01:21:54 · 211 阅读 · 0 评论