You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Given a string, find the length of the longest substring without repeating characters.
Example 1:
1 2 3
Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3.
Example 2:
1 2 3
Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1.
Example 3:
1 2 3 4
Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
// 二分查找、分治算法 classSolution{ publicdoublefindMedianSortedArrays(int[] A, int[] B){ //m:A数组的长度 int m = A.length; //n:B数组的长度
int n = B.length; //如果A的长度大于B if (m > n) { // to ensure m<=n //交换AB数组,确保m<=n int[] temp = A; A = B; B = temp; int tmp = m; m = n; n = tmp; } //设置两个指针,iMin为头指针,IMAX为尾指针,halfLen为中位数指针 int iMin = 0, iMax = m, halfLen = (m + n + 1) / 2; //如果头指针走向不大于尾指针,进行循环 while (iMin <= iMax) { //i为中位数 int i = (iMin + iMax) / 2; //j为 int j = halfLen - i; if (i < iMax && B[j - 1] > A[i]){ iMin = i + 1; // i is too small } elseif (i > iMin && A[i - 1] > B[j]) { iMax = i - 1; // i is too big } else { // i is perfect int maxLeft = 0; if (i == 0) { maxLeft = B[j-1]; } elseif (j == 0) { maxLeft = A[i - 1]; } else { maxLeft = Math.max(A[i - 1], B[j - 1]); } if ( (m + n) % 2 == 1 ) { return maxLeft; }
int minRight = 0; if (i == m) { minRight = B[j]; } elseif (j == n) { minRight = A[i]; } else { minRight = Math.min(B[j], A[i]); }