OK, Let’s continue….
ZIGZAG
Problem Statement:
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: P I N A L S I G Y A H R P I
Solution:
This problem is more like find the right rules of zigzag strings.
If we take a close look at the picture above, we will notice that the graph is periodical. We can see the elements from the first row has the rule like below:
The index of the first element starts at 0; the index of the second element in the first row (row 0) starts at 2*NumofRow – 2, which is also the cycle. In this case it is 2*5 – 2 = 8. While the index of the third element should add another cycle, which is 8 + 8 = 16. The same rule also applies to the last line. Even if we draw another graph (with different numRows), the cycle should follow the same rule: 2 * NumofRow – 2.
OK, Let’s go to the second line. We will immediate recognize that from the 2 to n – 1 line, we will have two types of elements. The first type is similar to the elements we discuss above, we simply need to add number of lines – 1 to generate the new indices for these elements from the first line elements. For example, from the above graph, we can see, 1 is 0 + 1, 9 is 8 + 1, 18 is 16 + 2. A little bit tricky part is the rest of elements such as 5, 6, 7 or 13, 14, 15. How can we generate these indices? The answer is simple, we simply deduct the number of lines – 1 to the first line elements’ indices. For example, 7 is 8 – 1, 14 is 16 – 2.
OK, now we have known basic rules. We only need to apply these rules to our code. We write loops to scan each row, and calculate the indices for each element, and store them in a vector. Each time when we scan a new elements, we need to check it is not out of boundary.
Time complexity: O(n). We only need to touch every element once.
Space complexity: O(n)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
string convert(string s, int num) { | |
int len = s.size(); | |
string finalStr; | |
if(num == 1||len<=1) return s; | |
int k = 2 * num – 2; | |
for(int i = 0; i< num; i++) | |
{ | |
for(int j = 0; i + j<len; j = j + k) | |
{ | |
finalStr.push_back(s[i+j]); | |
// get rid of the first and last line | |
if(i!=0 && (i!= num – 1) && (j + k – i < len); | |
finalStr.push_back(s[j+k – i]); | |
} | |
} | |
return finalStr; | |
} | |
}; |
ATOI
Problem Statement:
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.
Solution:
Although the statement is very long. Actually, it is not a hard problem. Basically, we need to take care of several situations:
1. Discard as many white spaces as possible;
2. If the first element is not digit, then return 0;
3. If the final number is greater than 2^31 – 1 or less than – 2^31. We return the boundary;
4. When we first encounter ‘+/-‘ sigh, we need to determine whether the final number is positive or negative;
5. Except all the situations above, we need to extract as many digits as possible.
The solution is pretty straightforward.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int myAtoi(string str) { | |
long temp = 0; | |
int i = 0, sign = 1; | |
while(str[i] == ' ') | |
{ | |
i++; | |
} | |
if(str[i] == '–'){ | |
sign = –1; | |
i++; | |
} | |
else if(str[i] == '+') | |
i++; | |
while(str[i] – '0' >=0 && str[i] – '0' <=9) | |
{ | |
temp = temp*10 + (str[i] – '0'); | |
if(sign>0 && temp>=INT_MAX) | |
return INT_MAX; | |
if(sign<0 && -temp <=INT_MIN) | |
return INT_MIN; | |
i++; | |
} | |
return temp*sign; | |
} | |
}; |
The two problems are not hard… That’s all for this week.