Given an input string (s) and a pattern (p), implement regular expression matching with support for ‘.’ and ‘‘. ‘.’ 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: Input: s = “aa” p = “a” Output: false Explanation: “a” does not match the entire string “aa”. Example 2: 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: Input: s = “ab” p = “.“ Output: true Explanation: “.*” means “zero or more (*) of any character (.)”. Example 4:
Input: s = “aab” p = “cab” Output: true Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches “aab”. Example 5:
Input: s = “mississippi” p = “misis*p.” Output: false
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121 Output: true Example 2:
Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3:
Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicbooleanisPalindrome(int x){ if(x < 0){ returnfalse; } int a = 0; int b = x; while(x > 0){ int mod = x % 10; a = a * 10 + mod; x = x / 10; } if(b == a){ returntrue; } returnfalse; }
Implement atoi which converts a string to an integer.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned.
Note:
Only the space character ‘ ‘ is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1:
Input: “42” Output: 42 Example 2:
Input: “ -42” Output: -42 Explanation: The first non-whitespace character is ‘-‘, which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3:
Input: “4193 with words” Output: 4193 Explanation: Conversion stops at digit ‘3’ as the next character is not a numerical digit. Example 4:
Input: “words and 987” Output: 0 Explanation: The first non-whitespace character is ‘w’, which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5:
Input: “-91283472332” Output: -2147483648 Explanation: The number “-91283472332” is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned.
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123 Output: 321 Example 2:
Input: -123 Output: -321 Example 3:
Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N A P L S I I G Y I R And then read line by line: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows); Example 1:
Input: s = “PAYPALISHIRING”, numRows = 3 Output: “PAHNAPLSIIGYIR” Example 2:
Input: s = “PAYPALISHIRING”, numRows = 4 Output: “PINALSIGYAHRPI” Explanation:
public String longestPalindromeSolution(String s){ if (s == null || s.length() < 1) return""; int start = 0, end = 0; for (int i = 0; i < s.length(); i++) { //这里要考虑两种情况,回文数可能是总长度为奇数或者偶数,那么如果是奇数的话它的中心就都是i,如果是偶数的话它的中心就是i+1 int len1 = expandAroundCenter(s, i, i); int len2 = expandAroundCenter(s, i, i + 1); int len = Math.max(len1, len2); if (len > end - start) { start = i - (len - 1) / 2; end = i + len / 2; } } return s.substring(start, end + 1); }
privateintexpandAroundCenter(String s, int left, int right){ int L = left, R = right; while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) { L--; R++; } return R - L + 1; }
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: “abcabcbb” Output: 3 Explanation: The answer is “abc”, with the length of 3. Example 2:
Input: “bbbbb” Output: 1 Explanation: The answer is “b”, with the length of 1. Example 3:
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.