problem 8,9,10
8. String to Integer (atoi)
Medium
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:
1 | |
Example 2:
1 | |
Example 3:
1 | |
Example 4:
1 | |
Example 5:
1 | |
这个题目无法吐槽
1 | |
9. Palindrome Number
Easy
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
1 | |
Example 2:
1 | |
Example 3:
1 | |
和字符回文一模一样
1 | |
10. Regular Expression Matching
Hard
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.'and '*'.
1 | |
The matching should cover the entire input string (not partial).
Note:
scould be empty and contains only lowercase lettersa-z.pcould be empty and contains only lowercase lettersa-z, and characters like.or*.
Example 1:
1 | |
Example 2:
1 | |
Example 3:
1 | |
Example 4:
1 | |
Example 5:
1 | |
这个题要求做简单的正则匹配
正常的思路是一个递归问题,不过这个递归会有四种情况,有一种情况会出现两个分支
于是就写出了我自己的弟弟代码
1 | |
虽然看起来还行,但是在递归里面都是垫底的性能
1 | |
优化之后的递归,第一个是长度减少了,索引起来快很多,第二个充分考虑到了*不会单独出现,所以倒向匹配。
1 | |
不使用递归,使用动态规划,这要求更多的内存,来存储所有的状态