- 博客(164)
- 收藏
- 关注
原创 5. 最长回文子串
5. 最长回文子串第一种解法,动态规划class Solution { public String longestPalindrome(String s) { int len = s.length(); if(len <= 1) return s; boolean[][] dp = new boolean[len][len]; for(int j = 0;j < len;j++){ f.
2021-09-14 04:14:26 212
原创 46. 全排列
46. 全排列class Solution { List<List<Integer>> res = new LinkedList<>(); List<Integer> list = new LinkedList<>(); Set<Integer> set = new HashSet<>(); public List<List<Integer>> per
2021-09-13 08:43:53 142
原创 142. 环形链表 II
142. 环形链表 IIpublic class Solution { public ListNode detectCycle(ListNode head) { ListNode fast = head, slow = head; while(fast != null){ if(fast.next == null || fast.next.next == null) return null; fast = fas.
2021-09-13 08:20:54 142
原创 33. 搜索旋转排序数组
class Solution { public int search(int[] nums, int target) { int n = nums.length; if (n == 0) { return -1; } if (n == 1) { return nums[0] == target ? 0 : -1; } int l = 0, r = n - .
2021-09-13 07:34:02 102
原创 415. 字符串相加
415. 字符串相加class Solution { public String addStrings(String num1, String num2) { int i = num1.length() - 1, j = num2.length() - 1, add = 0; StringBuffer ans = new StringBuffer(); while (i >= 0 || j >= 0 || add != 0) {
2021-09-13 03:52:59 50
原创 20. 有效的括号
20. 有效的括号class Solution { public boolean isValid(String s) { Set<Character> left = new HashSet<>(); Set<Character> right = new HashSet<>(); left.add('('); left.add('{'); left.add(
2021-09-13 03:31:53 50
原创 236. 二叉树的最近公共祖先
236. 二叉树的最近公共祖先/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { private TreeNode ans; publi
2021-09-13 02:12:17 49
原创 88. 合并两个有序数组
88. 合并两个有序数组class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int[] res = new int[m + n]; int i = 0,j = 0; while(i < m && j < n){ if(nums1[i] < nums2[j]){
2021-09-12 21:59:15 42
原创 103. 二叉树的锯齿形层序遍历
103. 二叉树的锯齿形层序遍历class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> res = new LinkedList<>(); if(root == null) return res; Deque<TreeNode> st .
2021-09-12 21:02:31 50
原创 102. 二叉树的层序遍历
102. 二叉树的层序遍历class Solution { public List<List<Integer>> levelOrder(TreeNode root) { Deque<TreeNode> st = new LinkedList<>(); List<List<Integer>> res = new LinkedList<>(); if(root ==
2021-09-12 17:16:25 48
原创 121. 买卖股票的最佳时机
121. 买卖股票的最佳时机class Solution { public int maxProfit(int[] prices) { int profit = 0; int min = prices[0]; for(int i = 1;i < prices.length;i++){ if(prices[i] > min) profit = Math.max(profit,prices[i] - min);.
2021-09-10 22:40:30 45
原创 160. 相交链表
160. 相交链表public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { Set<ListNode> visited = new HashSet<ListNode>(); ListNode temp = headA; while (temp != null) { vis
2021-09-10 22:29:09 82
原创 21. 合并两个有序链表
21. 合并两个有序链表class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode head1 = l1; ListNode head2 = l2; ListNode dummy = new ListNode(0); ListNode res = dummy; while(head1 != nul
2021-09-10 21:53:17 45
原创 53. 最大子序和
53. 最大子序和class Solution { public int maxSubArray(int[] nums) { int sum = 0; int max = nums[0]; for(int num:nums){ if(sum > 0){ sum += num; }else{ sum = num; .
2021-09-10 21:43:57 42
原创 141. 环形链表
141. 环形链表public class Solution { public boolean hasCycle(ListNode head) { Set<ListNode> seen = new HashSet<>(); while(head != null){ if(!seen.add(head)){ return true; }
2021-09-10 21:31:22 79
原创 ArrayList和LinkedList的区别
区别:1.ArrayList是用数组实现的,LinkedList是链表实现的2.因此ArrayList适合随机查找,而LinkedList适合删除和添加元素。查询,删除,添加的时间复杂度自然就不同了3.他们都实现了List接口,而LinkedList额外实现了Deque接口,可以当作队列来使用。ArrayList.add(1) 需要时间扩容ArrayList.add(index:1, element:1) 找到index很快,因为是数组,但是添加后可能需要将元素后移Lin.
2021-09-08 21:58:19 68
原创 15. 三数之和
15. 三数之和又做一遍啊,双指针class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new LinkedList<>(); Arrays.sort(nums); if(nums.length < 3) return res; f
2021-09-07 21:28:55 52
原创 912. 排序数组
912. 排序数组class Solution { public int[] sortArray(int[] nums) { quickSort(nums,0,nums.length - 1); return nums; } public void quickSort(int[] a, int l,int r){ int q = randomizedPartition(a,l,r); if(q > l) qu
2021-09-07 21:12:56 52
原创 25. K 个一组翻转链表
25. K 个一组翻转链表/** * 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
2021-09-07 07:32:29 42
原创 215. 数组中的第K个最大元素
215. 数组中的第K个最大元素class Solution { Random random = new Random(); public int findKthLargest(int[] nums, int k) { return quicksort(nums,0,nums.length - 1,nums.length - k); } public int quicksort(int[] nums,int l, int r,int k){
2021-09-06 19:23:46 47
原创 146. LRU 缓存机制
146. LRU 缓存机制class LRUCache { public class DoubleListNode{ private int key; private int value; DoubleListNode prev; DoubleListNode next; public DoubleListNode(){}; public DoubleListNode(int key,int val
2021-09-06 04:55:11 124
原创 3. 无重复字符的最长子串
3. 无重复字符的最长子串class Solution { public int lengthOfLongestSubstring(String s) { if(s.length() == 0) return 0; Map<Character,Integer> map = new HashMap<>(); int l = 0; int r = 0; int max = 0;
2021-09-05 22:24:22 59
原创 84. 柱状图中最大的矩形
84. 柱状图中最大的矩形class Solution { public int largestRectangleArea(int[] heights) { //数组首尾增加0,确保组成有金字塔结构,满足函数运行模式。 int[] arr = new int[heights.length + 2]; arr[0] = 0; for(int i = 1; i < heights.length + 1;i++) arr[i] =
2021-09-02 01:03:27 60
原创 42. 接雨水
42. 接雨水class Solution { public int trap(int[] height) { Deque<Integer> st = new LinkedList<>(); st.addLast(0); int res = 0; for(int i = 1;i < height.length;i++){ while(st.size() > 0 &
2021-09-01 21:19:30 47
原创 503. 下一个更大元素 II
503. 下一个更大元素 IIclass Solution { public int[] nextGreaterElements(int[] nums) { Deque<Integer> st = new LinkedList<>(); st.addLast(0); int[] res = new int[nums.length]; for(int i = 1;i < nums.length;i++)
2021-09-01 16:56:12 56
原创 496. 下一个更大元素 I
496. 下一个更大元素 Iclass Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { Deque<Integer> st = new LinkedList<>(); int[] res1 = new int[10010]; st.addLast(nums2[0]); for(int i = 1; i < n
2021-09-01 03:05:36 118
原创 739. 每日温度
739. 每日温度class Solution { public int[] dailyTemperatures(int[] temperatures) { Deque<Integer> st = new LinkedList<>(); st.addLast(0); int[] res = new int[temperatures.length]; for(int i = 1; i < tempera
2021-09-01 02:53:24 102
原创 516. 最长回文子序列
516. 最长回文子序列class Solution { public int longestPalindromeSubseq(String s) { int[][] dp = new int[s.length()][s.length()]; for(int i = s.length() - 1; i >= 0;i--){ for(int j = i; j < s.length();j++){
2021-09-01 02:31:58 71
原创 647. 回文子串
647. 回文子串class Solution { public int countSubstrings(String s) { boolean[][] dp = new boolean[s.length()][s.length()]; for(int i = s.length() - 1;i >= 0;i--){ for(int j = i;j < s.length();j++){ if(s
2021-09-01 02:07:14 82
原创 72. 编辑距离
72. 编辑距离class Solution { public int minDistance(String word1, String word2) { int[][] dp = new int[word1.length() + 1][word2.length() + 1]; for(int i = 0; i < word1.length() + 1;i++) dp[i][0] = i; for(int i = 0; i < w.
2021-09-01 01:25:22 55
原创 583. 两个字符串的删除操作
583. 两个字符串的删除操作class Solution { public int minDistance(String word1, String word2) { int[][] dp = new int[word1.length() + 1][word2.length() + 1]; for(int i = 0; i < word1.length() + 1;i++) dp[i][0] = i; for(int i = 0; i &
2021-08-31 14:53:18 54
原创 115. 不同的子序列
115. 不同的子序列class Solution { public int numDistinct(String s, String t) { int[][] dp = new int[s.length() + 1][t.length() + 1]; for(int i = 0; i < s.length() + 1;i++) dp[i][0] = 1; for(int i = 1; i < s.length() + 1;i++){
2021-08-31 00:13:02 61
原创 392. 判断子序列
392. 判断子序列class Solution { public boolean isSubsequence(String s, String t) { boolean[][] dp = new boolean[s.length() + 1][t.length() + 1]; for(int i = 0;i < t.length() + 1;i++) dp[0][i] = true; for(int i = 1; i < s.len
2021-08-30 23:23:54 43
原创 53. 最大子序和
53. 最大子序和class Solution { public int maxSubArray(int[] nums) { int[] dp = new int[nums.length]; dp[0] = nums[0]; int max = dp[0]; for(int i = 1;i < nums.length;i++){ dp[i] = Math.max(nums[i],dp[i - 1] +
2021-08-30 22:20:19 43
原创 1035. 不相交的线
1035. 不相交的线class Solution { public int maxUncrossedLines(int[] nums1, int[] nums2) { int[][] dp = new int[nums1.length + 1][nums2.length + 1]; int max = 0; for(int i = 1; i < nums1.length + 1;i++){ for(int j =
2021-08-30 21:47:28 41
原创 1143. 最长公共子序列
1143. 最长公共子序列class Solution { public int longestCommonSubsequence(String text1, String text2) { int[][] dp = new int[text1.length() + 1][text2.length() + 1]; int max = 0; for(int i = 1; i < text1.length() + 1;i++){
2021-08-30 20:49:31 44
原创 718. 最长重复子数组
718. 最长重复子数组class Solution { public int findLength(int[] nums1, int[] nums2) { int[][] dp = new int[nums1.length + 1][nums2.length + 1]; int max = 0; for(int i = 1; i <= nums1.length;i++){ for(int j = 1;j <=
2021-08-30 19:30:48 43
原创 674. 最长连续递增序列
674. 最长连续递增序列class Solution { public int findLengthOfLCIS(int[] nums) { int length = 1; int max = 1; for(int i = 1; i < nums.length;i++){ if(nums[i] > nums[i - 1]){ length++; }els
2021-08-30 19:14:04 43
原创 300. 最长递增子序列
300. 最长递增子序列class Solution { public int lengthOfLIS(int[] nums) { int[] dp = new int[nums.length]; Arrays.fill(dp, 1); int res = 1; for(int i = 1; i < nums.length;i++){ for(int j = 0; j < i;j++){
2021-08-30 18:34:29 40
原创 714. 买卖股票的最佳时机含手续费
714. 买卖股票的最佳时机含手续费class Solution { public int maxProfit(int[] prices, int fee) { int[][] dp = new int[prices.length + 1][2]; dp[0][0]= -prices[0] - fee; for(int i = 1; i < prices.length + 1;i++){ int price = pr
2021-08-30 00:03:56 43
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人