Leetcode- 06-10题 题解
2018-01-03 10:17
495 查看
6. ZigZag
Conversion
The string
this: (you may want to display this pattern in a fixed font for better legibility)
And then read line by line:
Write the code that will take a string and make this conversion given a number of rows:
return
题意:看了很多博客都说是画 __之__ 字,我怎么感觉不像呢?就是单纯的模拟,从第一行写到最后一行,然后再从最后一行写到第一行。
代码:
7. Reverse
Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Example 2:
Example 3:
Note:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
题意:求逆序数,注意溢出,溢出返回0
代码:
to Integer (atoi)
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
Update (2015-02-10):
The signature of the
accepts a
reset your code definition.
spoilers alert... click to show requirements for atoi.
题意:给你一个字符串,把它转换成int的数,>= 1<<31 输出 (1<<31)-1, <= -(1<<31)输出 -(1<<31),注意符号问题,总之,这个题很坑~
代码:
Number
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
题意:问这个数是不是回文数,不能申请多余的空间,比如申请一个字符串存逆序数。注意负数不是回文数,溢出问题等。
代码:
Expression Matching
Implement regular expression matching with support for
题意:给你两个字符串,问这两个字符串是否匹配, ‘.’ 可以匹配任何字符, '*' 和它的前面一个字符X,组成一个0个X,或者XXXXXXX...........
解析:倒着匹配
代码:
Conversion
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 text, int nRows);
convert("PAYPALISHIRING", 3)should
return
"PAHNAPLSIIGYIR".
题意:看了很多博客都说是画 __之__ 字,我怎么感觉不像呢?就是单纯的模拟,从第一行写到最后一行,然后再从最后一行写到第一行。
代码:
//题意是一直没看懂,只好百度题意,原来是摆成 _之_ 字按行输出 class Solution { public: string convert(string s, int numRows) { if(numRows == 1) return s; int len = s.length(), i = 0, f = 0, h = 0; string ans[numRows]; for(int i = 0; i < numRows; i++) ans[i] = ""; while(i < len) { ans[h] += s[i]; i++; if(!f) h++; else h--; if(h == numRows) f = !f, h -= 2; if(h == -1) f = !f, h += 2; } string res = ""; for(int i = 0; i < numRows; i++) res += ans[i]; return res; } };
7. Reverse
Integer
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 hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
题意:求逆序数,注意溢出,溢出返回0
代码:
//求逆序数,注意范围为[-(1<<31), (1<<31)-1] class Solution { public: int reverse(long long x) { long long ans = 0; int f = x > 0 ? 1 : -1; x *= f; while(x) { ans = ans*10+x%10; x/=10; } long long s = 1ll<<31; ans *= f; if(ans >= s || ans < -s) return 0; return ans; } };8. String
to Integer (atoi)
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
Update (2015-02-10):
The signature of the
C++function had been updated. If you still see your function signature
accepts a
const char *argument, please click the reload button to
reset your code definition.
spoilers alert... click to show requirements for atoi.
题意:给你一个字符串,把它转换成int的数,>= 1<<31 输出 (1<<31)-1, <= -(1<<31)输出 -(1<<31),注意符号问题,总之,这个题很坑~
代码:
//这个题虽然简单,但需要注意读题,很坑啊 class Solution { public: int myAtoi(string str) { int len = str.length(), i = 0; vector<int> x; int f = 0; while(i < len) { if(str[i] == ' ') { if(f) break; i++; continue; } if(str[i] == '+') { if(f == 0) f = 1; else break; } else if(str[i] == '-') { if(f == 0) f = -1; else break; } else if(isdigit(str[i])) { if(f == 0) f = 1; x.push_back(str[i] - '0'); } else break; i++; } long long ans = 0, s = 1ll<<31; for(int i = 0; i < x.size(); i++) { ans = ans*10+x[i]; if(ans >= s) break; } ans *= f; if(ans >= s) return (int)(s-1); if(ans < -s) return (int)(-s); return (int)ans; } };9. Palindrome
Number
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
题意:问这个数是不是回文数,不能申请多余的空间,比如申请一个字符串存逆序数。注意负数不是回文数,溢出问题等。
代码:
//负数不是回文数,注意溢出 class Solution { public: bool isPalindrome(int x) { if(x < 0) return false; long long xx = 0, xxx = x; while(x) { xx = xx*10+x%10; x/=10; } if(xx == xxx) return true; return false; } };10. Regular
Expression Matching
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). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true
题意:给你两个字符串,问这两个字符串是否匹配, ‘.’ 可以匹配任何字符, '*' 和它的前面一个字符X,组成一个0个X,或者XXXXXXX...........
解析:倒着匹配
代码:
//借鉴dml学长博客:http://littleblank.net/archives/1045/ class Solution { public: bool check(string s, int i, string p, int j) { if(i == -1 && j == -1) return true; if(j == -1) return false; if(i == -1) { if(!(j&1)) return false; for(int k = 0; k <= j; k++) { if(k&1) { if(p[k] != '*') return false; } } return true; } if(p[j] == '*') { if(p[j-1] == s[i] || p[j-1] == '.') { if(check(s, i-1, p, j)) return true; } return check(s, i, p, j-2); } if(s[i] == p[j] || p[j] == '.') return check(s, i-1, p, j-1); return false; } bool isMatch(string s, string p) { return check(s, s.length()-1, p, p.length()-1); } };
相关文章推荐
- leetcode 10 Regular Expression Matching & 44 Wildcard Matching
- [LeetCode] Valid Palindrome [10]
- leetcode解题报告:10 Regular Expression Matching
- (原创)c#学习笔记10--定义类成员06--示例应用程序03--类库的客户应用程序
- LeetCode(10) Regular Expression Matching
- 刚刚修好了我的鼠标,一个字,爽!(06-8-10)
- [week 10][Leetcode][Dynamic Programming] Climbing Stairs
- leetcode[10]Regular Expression Matching
- 10 06 18 问题
- LeetCode 算法刷题(10)
- leetcode 19:Remove Nth Node From End of List(15-10-7)
- leetcode 22:Generate Parentheses(15-10-9)
- LeetCode(10)Regular Expression Matching
- leetcode(10)21. Merge Two Sorted Lists
- leetcode_10
- [转载] 百科全说——陈允斌:教你生活中的补血妙方(10-12-06)
- leetcode 10 Linked List Cycle II
- leetcode 10 -- Regular Expression Matching
- LeetCode 10
- Leetcode_10