Reprint please specify the source:
Http://blog.csdn.net/miaoyunzexiaobao
4. Median Oftwo Numbers
https://leetcode.com/problems/median-of-two-sorted-arrays/
given two ordered arrays A (length is m ) and B (length is N ), locate the median of two arrays. If the number of two numbers is the same as the even, the average of the median two is returned. Example:a={1,5,14},b={3,7,30}, then return (5+7)/2=6.a={1,5,14} ,b={3,7}, returns 5. Requires a time complexity of log (m+n)
Ideas:
Reference Links: http://blog.csdn.net/yutianzuijin/article/details/11499917/
the simple method is to merge two ordered arrays into one, and then return the middle value of the array. However , the time-out problem occurs when testing in Leetcode.
This turns the problem into a more general case: Two sequential arrays are combined, ordered byka value. Then useAand theBis the characteristic of an ordered array, first assuming that the arrayAand theBthe number of elements is greater thanK/2, we compareA[k/2-1]and theB[k/2-1]two elements, each of which represents the two elements of aAthe firstK/2the small elements andBthe firstK/2the small element. There are three different cases for these two elements:>,<and the=.
1. a[k/2-1]<b[k/2-1], this means that the elements of a[0] to a[k/2-1] are in A and the B the former after merging k the small element. In other words,a[k/2-1] cannot be larger than the K-decimal value after the two array is merged , so we can discard it.
2.&NBSP;&NBSP;&NBSP; a[k/2-1]>b[k/2-1]
3.&NBSP;&NBSP;&NBSP; a[k/2-1]=b[k/2-1] We have found the section k The small number, which is the equal element, is recorded as m a B k/2-1 m m k small number.
here if k is odd, the m is not the median number. In the actual code , the K/2is first obtained, and then the k-k/2 is used to obtain another number
through the above analysis, we can use recursive approach to find the first k a small number. In addition we need to consider several boundary conditions:
- If A or B is empty, return directly to B[k-1] or a[k-1];
- If k is 1, we only need to return the smaller values in a[0] and b[0];
- If a[k/2-1]=b[k/2-1], return one of them;
C++:
int findkth (int a[],int b[],int k,int m,int N) {if (m>n) return findkth (b,a,k,n,m); if (M = = 0) return b[k-1];if (k = = 1) retur N (a[k-1]<b[k-1]? A[K-1]:B[K-1]); int pa = (K/2 < m K/2: m); int PB = K-pa;if (A[pa-1] < b[pb-1]) return findkth (A+PA,B,K-PA,M-PA , n), else if (A[pa-1] > B[pb-1]) return findkth (A,B+PB,K-PB,M,N-PB); else return a[pa-1];} Double findmediansortedarrays (int a[], int m, int b[], int n) {int total = m + n;if (total% 2!=0) return (double) (Findkth (A , b,total/2+1,m,n)); Elsereturn (findkth (a,b,total/2,m,n) +findkth (a,b,total/2+1,m,n))/2.0;}
Java:
public static int findkth (int a[], int b[], int k) {int aLen = A.length;int Blen = b.length;if (ALen > Blen) return find Kth (B, A, k); if (ALen = = 0) return b[k-1];if (k = = 1) return math.min (A[0], b[0]); int pa = math.min (K/2, aLen); int PB = K-pa;if (A[pa-1] < b[pb-1]) return findkth (Arrays.copyofrange (A, PA, aLen), B, K-PA); Elsereturn findkth (A, Array S.copyofrange (B, Pb, Blen), K-PB);} public static double findmediansortedarrays (int a[], int b[]) {int m = a.length;int n = b.length;int mid = (m + N)/2;if ((m + N)% 2 = = 0) return (double) (Findkth (A, B, mid) + findkth (A, B, mid + 1))/2.0;elsereturn (double) findkth (A, B, MI D + 1);}
5. Longestpalindromic Substring
https://leetcode.com/problems/longest-palindromic-substring/
Given a string, the longest palindrome substring is obtained.
Ideas:
Reference Links:
http://blog.csdn.net/zhouworld16/article/details/16842467
Solution 1 : Reverses the string, the problem is converted to the longest common substring that asks two strings
Solution 2 : Dynamic planning. Use booldp[i][j] to indicate whether a substring is a palindrome from the i character to the J -character string. Easy to know:
initial conditions can be set to a length of 1 and for 2 of the DP . You can determine dp[i][i]= true,dp[i][i+1] = (s[i] = = s[i+1])
The judging condition is: Dp[i] [j] = dp[i + 1][j-1] && s[i] = = s[J] .
Solution 3 : Center extension method. The use of palindrome is the central symmetry of the characteristics, starting from subscript i , with two pointers to the left and right side of I move to determine whether equal. It is important to note that Palindrome has odd even points. such as ABC and ABBA two kinds.
C + +:
static string Expandaroundcenter (string s, int c1, int c2) {int L = c1, r = c2;int n = s.length (); while (l >= 0 &&A mp R <= n-1 && s[l] = = S[r]) {l--;r++;} Return S.substr (l+1, r-l-1);} static string Longestpalindrome (string s) {int n = s.length (), if (n = = 0) return "", String longest = S.substr (0, 1); A single char itself is a palindromefor (int i = 0; i < n-1; i++) {string P1 = Expandaroundcenter (s, I, I); if (p1.le Ngth () > Longest.length ()) longest = p1;string P2 = expandaroundcenter (s, I, i+1); if (P2.length () > Longest.length ()) Longest = P2;} return longest;}
Java:
public static String Longestpalindrome (string s) {if (S.isempty ()) {return null;} if (s.length () = = 1) {return s;} string longest = s.substring (0, 1); for (int i = 0; i < s.length (); i++) {String tmp = Helper (s, I, I); if (Tmp.length () > Longest.length ()) {longest = tmp;} TMP = Helper (s, I, i + 1), if (Tmp.length () > Longest.length ()) {longest = tmp;}} return longest;} public static string helper (string s, int begin, int end) {while (Begin >= 0 && end <= s.length ()-1&&A mp S.charat (BEGIN) = = S.charat (end)) {begin--;end++;} return s.substring (begin + 1, end);}
6. Zigzagconversion
https://leetcode.com/problems/zigzag-conversion/
to use a string ZigZag form and print it out. For example, "paypalishiring" is in the form of ZIgZag :
P A H N
A P L S i i G
Y I R
to print out is: "Pahnaplsiigyir"
Ideas:
See Reference Links:
http://blog.csdn.net/zhouworld16/article/details/14121477
C++:
string convert (string s, int nRows) {if (nRows <= 1 | | s.length () = = 0) return s; string res = ""; int len = S.length (); for (int i = 0; i < len && i < nRows; ++i) {int indx = i; Res + = S[indx]; for (int k = 1; indx < Len; ++k) {//First or last line, using formula 1:if (i = = 0 | | i = = NROWS-1) {indx + = 2 * nRows-2; }//Middle row, judging parity, using equation 2 or 3 else {if (K & 0x1)//Odd bit indx + = 2 * (nRows-1-i); else indx + = 2 * i; }//Judge indx legality if (Indx < len) {res + = S[indx]; }}} return res; }
Java:
public static String convert (string s, int nRows) {if (s = = NULL | | s.length () <= nRows | | nRows <= 1) return s; StringBuffer sb = new StringBuffer (); for (int i = 0; i < s.length (); i + = 2 * (nRows-1)) Sb.append (S.charat (i)); for (i NT i = 1; i < nRows-1; ++i) for (int j = i; J < S.length (); j + = 2 * (nRows-1)) {Sb.append (S.charat (j)); if (j + 2 * (Nrows-i-1) < S.le Ngth ()) Sb.append (S.charat (j + 2 * (Nrows-i-1)));} for (int i = nRows-1; I < s.length (); i + = 2 * (nRows-1)) Sb.append (S.charat (i)); return sb.tostring ();}
One day three questions Leetcode (C++&java) -4~6