6.ZigZag 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 s, int numRows);
class Solution:
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if numRows == 1:
return s
zigzag = ['' for _ in range(numRows)]
row = 0
step = 1
for c in s:
if row==0:
step=1
if row==numRows-1:
step=-1
zigzag[row] += c
row=row+step
return ''.join(zigzag)
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 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.
class Solution:
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
list = []
ans = 0
y = abs(x)
while y!=0:
list.append(y%10)
y=int(y/10)
else:
pass
length = len(list)-1
i=0
while length>=0:
ans = ans + list[i]*(10**length)
i=i+1
length=length-1
if x==0:
return 0
if ans<-2**31:
return 0
if ans>2**31-1:
return 0
if x>0:
return ans
if x<0:
return -ans
8.String to Integer (atoi)
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.
-
class Solution: def myAtoi(self, str): """ :type str: str :rtype: int """ try: res = int(str) if res>2**31-1: res = 2**31-1 if res<-2**31: res = -2**31 return res except: if len(str)==0: return 0 if str.count(" ")==len(str): return 0 r = [] l = len(str) if str[0]=='+' or str[0]=='-': for i in range(1,l): if str[i] >= '0' and str[i] <= '9': r.append(str[i]) else: break if len(r) > 0: result = ''.join(r) result = str[0] + result result = int(result) if result > 2 ** 31 - 1: result = 2 ** 31 - 1 if result < -2 ** 31: result = -2 ** 31 return result if str[0]==' ': n = 0 for i in range(l): if str[i] == ' ': n = n+1 else: break if str[n] == '+' or str[n] == '-': for i in range(1+n, l): if str[i] >= '0' and str[i] <= '9': r.append(str[i]) else: break if len(r) > 0: result = ''.join(r) result = str[n]+result result = int(result) if result > 2**31-1: result = 2**31-1 if result < -2**31: result = -2**31 return result for i in range(n,l): if str[i] >= '0' and str[i] <= '9': r.append(str[i]) else: break if len(r) > 0: result = ''.join(r) result = int(result) if result > 2 ** 31 - 1: result = 2 ** 31 - 1 if result < -2 ** 31: result = -2 ** 31 return result for i in range(l): if str[i]>='0' and str[i]<='9': r.append(str[i]) else: break if len(r)>0: result = ''.join(r) result = int(result) if result > 2 ** 31 - 1: result = 2 ** 31 - 1 if result < -2 ** 31: result = -2 ** 31 return result return 0
9.Palindrome Number
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.
Follow up:
Coud you solve it without converting the integer to a string?
-
class Solution: def isPalindrome(self, x): """ :type x: int :rtype: bool """ list = [] ans = 0 y = abs(x) while y!=0: list.append(y%10) y=int(y/10) else: pass length = len(list)-1 i=0 while length>=0: ans = ans + list[i]*(10**length) i=i+1 length=length-1 if x==ans: return bool(1) if x!=ans: return bool(0)
10.Regular Expression Matching
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 lettersa-z
.p
could be empty and contains only lowercase lettersa-z
, and characters like.
or*
.-
Input: s = "mississippi" p = "mis*is*p*." Output: false
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 precedeng 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 = "c*a*b" 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 = "mis*is*p*." Output: false
-
class Solution: def isMatch(self, s, p): """ :type s: str :type p: str :rtype: bool """ sLen = len(s) pLen = len(p) if (pLen == 0): return sLen == 0 if (pLen == 1): if (p == s) or ((p == '.') and (len(s) == 1)): return True else: return False #p的最后一个字符不是'*'也不是'.'且不出现在s里,p跟s肯定不匹配 if (p[-1] != '*') and (p[-1] != '.') and (p[-1] not in s): return False if (p[1] != '*'): if (len(s) > 0) and ((p[0]==s[0]) or (p[0]=='.')): return self.isMatch(s[1:],p[1:]) return False else: while (len(s) > 0) and ((p[0]==s[0]) or (p[0]=='.')): if (self.isMatch(s,p[2:])): return True s = s[1:] return self.isMatch(s,p[2:])