From bf6fc8571ce9960d1f8c24c0d8174695092c9083 Mon Sep 17 00:00:00 2001 From: ruanwenjun Date: Fri, 3 May 2019 12:37:59 +0800 Subject: [PATCH] update --- .../Java/0004._Median_of_Two_Sorted_Arrays.md | 99 ++++++++++--------- .../Java/0005._Longest_Palindromic_Substring | 0 .../0005._Longest_Palindromic_Substring.md | 78 +++++++++++++++ 3 files changed, 132 insertions(+), 45 deletions(-) delete mode 100644 docs/Leetcode_Solutions/Java/0005._Longest_Palindromic_Substring create mode 100644 docs/Leetcode_Solutions/Java/0005._Longest_Palindromic_Substring.md diff --git a/docs/Leetcode_Solutions/Java/0004._Median_of_Two_Sorted_Arrays.md b/docs/Leetcode_Solutions/Java/0004._Median_of_Two_Sorted_Arrays.md index f540daf..ee43d88 100644 --- a/docs/Leetcode_Solutions/Java/0004._Median_of_Two_Sorted_Arrays.md +++ b/docs/Leetcode_Solutions/Java/0004._Median_of_Two_Sorted_Arrays.md @@ -1,78 +1,87 @@ -# 4. Longest Palindromic Substring +# 4. Median of Two Sorted Arrays -**难度: Medium** +**难度: Hard** ## 刷题内容 > 原题连接 -* https://leetcode-cn.com/problems/longest-palindromic-substring/description +* https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description > 内容描述 ``` -Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. +There are two sorted arrays nums1 and nums2 of size m and n respectively. + +Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). + +You may assume nums1 and nums2 cannot be both empty. Example 1: -Input: "babad" -Output: "bab" -Note: "aba" is also a valid answer. +nums1 = [1, 3] +nums2 = [2] +The median is 2.0 Example 2: -Input: "cbbd" -Output: "bb" +nums1 = [1, 2] +nums2 = [3, 4] + +The median is (2 + 3)/2 = 2.5 ``` ## 解题方案 > 思路 1 -******- 时间复杂度: O(n^2)******- 空间复杂度: O(n^2)****** +******- 时间复杂度: O(log(m + n))******- 空间复杂度: O(1)****** -使用动态规划的思路,用一个二维数组cache[i][j]记录i到j是否为回文串, beats 54.36% +将问题转化为两个有序数组,找出其中的第K大的数, beats 99.80% ```java class Solution { - // 采用动态规划 - // 如果 i == j ,则只有一个字母 肯定是回文串 - // 如果 char[i] == char[j] && j == i + 1 , 两个字母相等 肯定是回文串 - // 如果 char[i] == char[j] && j > i + 1 && cache[i+1][j-1]为true,则肯定是回文串 - public String longestPalindrome(String s) { - if(s == null || s.length() <2){ - return s; +// 寻找两个有序数组的中位数 + // 问题转换为求第K大的数 + public double findMedianSortedArrays(int[] nums1, int[] nums2) { + if(nums1 == null || nums1.length == 0){ + // 求nums2的中位数 + return nums2.length % 2 == 0 ? (nums2[nums2.length / 2] + nums2[nums2.length / 2 - 1]) / 2.0 : nums2[nums2.length / 2]; + } + if(nums2 == null || nums2.length == 0){ + return nums1.length % 2 == 0 ? (nums1[nums1.length / 2] + nums1[nums1.length / 2 - 1]) / 2.0 : nums1[nums1.length / 2]; + } + int len = nums1.length + nums2.length; + return len % 2 == 0 ? (topK(nums1,nums2,0,0,len/2)+topK(nums1,nums2,0,0,len/2+1))/2.0 : topK(nums1,nums2,0,0,len/2 + 1); + } + // 找两个有序数组的topk小的数 + public int topK(int[] nums1,int[] nums2,int start1,int start2,int k){ + if(start1 >= nums1.length){ + return nums2[start2 + k - 1]; } - boolean[][] cache = new boolean[s.length()][s.length()]; // 记录 i ~ j 是否是回文串 - char[] chars = s.toCharArray(); - int len = s.length(); - int start = 0; - int end = 0; - // 采用至底向上的动态规划,也可以采用递归方式 - for(int i = len - 1; i >= 0; i --){ - for(int j = i; j < len; j ++){ - if(i == j){ - cache[i][j] = true; - }else if(j == i + 1 && chars[i] == chars[j]){ - cache[i][j] = true; - if(end - start + 1 < 2){ - end = j; - start = i; - } - }else if(chars[i] == chars[j] && cache[i + 1][j-1]){ - cache[i][j] = true; - if(end - start < j-i){ - start = i; - end = j; - } - }else{ - cache[i][j] = false; - } - } + if(start2 >= nums2.length){ + return nums1[start1 + k - 1]; + } + + if(k == 1){ + return Math.min(nums1[start1] , nums2[start2]); + } + + if(start1 + k / 2 > nums1.length){ // 肯定不会在nums2的前 k / 2 + return topK(nums1,nums2,start1,start2 + k / 2,k - k / 2); + }else if(start2 + k / 2 > nums2.length){ + return topK(nums1,nums2,start1 + k / 2,start2,k - k / 2); + } + + int mid1 = nums1[start1 + k / 2 - 1]; + int mid2 = nums2[start2 + k / 2 - 1]; + if(mid1 > mid2){ // 移除nums2的前k/2 + return topK(nums1,nums2,start1,start2 + k / 2,k - k / 2); + }else { + return topK(nums1,nums2,start1 + k / 2,start2,k - k/2); } - return s.substring(start,end+1); } } ``` diff --git a/docs/Leetcode_Solutions/Java/0005._Longest_Palindromic_Substring b/docs/Leetcode_Solutions/Java/0005._Longest_Palindromic_Substring deleted file mode 100644 index e69de29..0000000 diff --git a/docs/Leetcode_Solutions/Java/0005._Longest_Palindromic_Substring.md b/docs/Leetcode_Solutions/Java/0005._Longest_Palindromic_Substring.md new file mode 100644 index 0000000..6ff2a68 --- /dev/null +++ b/docs/Leetcode_Solutions/Java/0005._Longest_Palindromic_Substring.md @@ -0,0 +1,78 @@ +# 5. Longest Palindromic Substring + +**难度: Medium** + +## 刷题内容 + +> 原题连接 + +* https://leetcode-cn.com/problems/longest-palindromic-substring/description + +> 内容描述 + +``` +Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. + + +Example 1: + +Input: "babad" +Output: "bab" +Note: "aba" is also a valid answer. + + +Example 2: + +Input: "cbbd" +Output: "bb" + +``` + +## 解题方案 + +> 思路 1 +******- 时间复杂度: O(n^2)******- 空间复杂度: O(n^2)****** + +使用动态规划的思路,用一个二维数组cache[i][j]记录i到j是否为回文串, beats 54.36% + +```java +class Solution { + // 采用动态规划 + // 如果 i == j ,则只有一个字母 肯定是回文串 + // 如果 char[i] == char[j] && j == i + 1 , 两个字母相等 肯定是回文串 + // 如果 char[i] == char[j] && j > i + 1 && cache[i+1][j-1]为true,则肯定是回文串 + public String longestPalindrome(String s) { + if(s == null || s.length() <2){ + return s; + } + boolean[][] cache = new boolean[s.length()][s.length()]; // 记录 i ~ j 是否是回文串 + char[] chars = s.toCharArray(); + int len = s.length(); + int start = 0; + int end = 0; + // 采用至底向上的动态规划,也可以采用递归方式 + for(int i = len - 1; i >= 0; i --){ + for(int j = i; j < len; j ++){ + if(i == j){ + cache[i][j] = true; + }else if(j == i + 1 && chars[i] == chars[j]){ + cache[i][j] = true; + if(end - start + 1 < 2){ + end = j; + start = i; + } + }else if(chars[i] == chars[j] && cache[i + 1][j-1]){ + cache[i][j] = true; + if(end - start < j-i){ + start = i; + end = j; + } + }else{ + cache[i][j] = false; + } + } + } + return s.substring(start,end+1); + } +} +``` -- GitLab