In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other.
Example:
1 2 3 4 5 6 7 8 9
Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
publicclassSolution { public String reverseWords(String s) { if (s == null) returnnull; char[] a = s.toCharArray(); intn= a.length; // step 1. reverse the whole string reverse(a, 0, n - 1); // step 2. reverse each word reverseWords(a, n); // step 3. clean up spaces return cleanSpaces(a, n); } voidreverseWords(char[] a, int n) { inti=0, j = 0; while (i < n) { while (i < j || i < n && a[i] == ' ') i++; // skip spaces while (j < i || j < n && a[j] != ' ') j++; // skip non spaces reverse(a, i, j - 1); // reverse the word } } // trim leading, trailing and multiple spaces String cleanSpaces(char[] a, int n) { inti=0, j = 0; while (j < n) { while (j < n && a[j] == ' ') j++; // skip spaces while (j < n && a[j] != ' ') a[i++] = a[j++]; // keep non spaces while (j < n && a[j] == ' ') j++; // skip spaces if (j < n) a[i++] = ' '; // keep only one space } returnnewString(a).substring(0, i); } // reverse a[] from a[i] to a[j] privatevoidreverse(char[] a, int i, int j) { while (i < j) { chart= a[i]; a[i++] = a[j]; a[j--] = t; } } }
Merge k Sorted Lists -
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
Example:
1 2
Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
Simplify Path
Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path.
In a UNIX-style file system, a period . refers to the current directory. Furthermore, a double period .. moves the directory up a level.
Note that the returned canonical path must always begin with a slash /, and there must be only a single slash / between two directory names. The last directory name (if it exists) must not end with a trailing /. Also, the canonical path must be the shortest string representing the absolute path.
Example 1:
1 2 3
Input: "/home/" Output: "/home" Explanation: Note that there is no trailing slash after the last directory name.
Example 2:
1 2 3
Input: "/../" Output: "/" Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.
Example 3:
1 2 3
Input: "/home//foo/" Output: "/home/foo" Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.
Example 4:
1 2
Input: "/a/./b/../../c/" Output: "/c"
Example 5:
1 2
Input: "/a/../../b/../c//.//" Output: "/c"
Example 6:
1 2
Input: "/a//b////c/d//././/.." Output: "/a/b/c"
二分查找
Search in Rotated Sorted Array √
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
Your algorithm’s runtime complexity must be in the order of O(log n).
public String longestPalindrome(String s) { intlen= s.length(); if (len < 2) return s;
for (inti=0; i < len-1; i++) { extendPalindrome(s, i, i); //assume odd length, try to extend Palindrome as possible extendPalindrome(s, i, i+1); //assume even length. } return s.substring(lo, lo + maxLen); }
privatevoidextendPalindrome(String s, int j, int k) { while (j >= 0 && k < s.length() && s.charAt(j) == s.charAt(k)) { j--; k++; } if (maxLen < k - j - 1) { lo = j + 1; maxLen = k - j - 1; } }
动态规划
Maximum Subarray √
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
1 2 3
Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6.
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.
1 2
'.' Matches any single character. '*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
Note:
s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.
Example 1:
1 2 3 4 5
Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa".
Example 2:
1 2 3 4 5
Input: s = "aa" p = "a*" Output: true Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:
1 2 3 4 5
Input: s = "ab" p = ".*" Output: true Explanation: ".*" means "zero or more (*) of any character (.)".
Example 4:
1 2 3 4 5
Input: s = "aab" p = "c*a*b" Output: true Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".
Example 5:
1 2 3 4
Input: s = "mississippi" p = "mis*is*p*." Output: false
Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a function rand10 which generates a uniform random integer in the range 1 to 10.
Summary: I assume rand7() represents the number from 0 to 6 which is more general in Java, please refer Random class and nextInt(int bound): knock API door
Key is uniform
rand7() * rand7(), rand7() + rand7() are not uniform because some numbers can be generated by different combinations
rand7() + rand7() != rand7() * 2 because rand7() + rand7() is not uniform, whereas rand7() * 2 is uniform
rand7()*c is uniform where c stands for constant integers. (But note: uniform here is meant by discrete numbers, to make the range continuously, we need to fill the gap by adding randc() => which then leads to OP solution)
So pattern we can conclude: k * randk + randk is uniform.(Note for the 3rd rule, don’t mix it up with (k + 1)*randk)