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 RAnd 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"
.
字符串规律题,注意到同一行两个完整列之间的间隔为numRows*2-2,如例题第一行P和A之间的间隔为3*2-2=4,A和L之间的间隔同样为4。除第1行和第n行外,相邻三个数之间间隔分别为numRows*2-2-lineNumber*2和lineNumber*2。对第1行,第2~n-1行,第n行分别循环处理即可。
class Solution {
public:
string convert(string s, int numRows) {
if(numRows==1) return s;
int len=s.size(),k=0,interval=numRows*2-2;
string res(len,' ');
for(int j=0;j<len;j+=interval)
res[k++]=s[j];
for(int i=1;i<numRows-1;i++)
{
int inter=i*2;
for(int j=i;j<len;j+=inter)
{
res[k++]=s[j];
inter=interval-inter;
}
}
for(int j=numRows-1;j<len;j+=interval)
res[k++]=s[j];
return res;
}
};
14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
字符串最长公共前缀。先找到最短字符串,记其长度为min,再从各字符串的第一个字符到第min个字符依次比较,看前缀是否相同。
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string word="";
int len=strs.size();
if(len==0) return "";
if(len==1) return strs[0];
int min=strs[0].size();
for(int i=0;i<len;i++){
if(min<strs[i].size()) min=strs[i].size();
}
for(int i=0;i<min;i++){
for(int j=0;j<len-1;j++){
if(strs[j][i]!=strs[j+1][i]) return word;
}
word+=strs[0][i];
}
return word;
}
};
28. Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
寻找子串第一次出现的位置,不存在则返回-1。用两个变量分别记录原串与子串遍历到的位置。遍历终止条件为原串遍历到结尾或子串完全匹配。
class Solution {
public:
int strStr(string haystack, string needle) {
int i=0;
int j=0;
for(i=0,j=0;i<haystack.size() && j<needle.size();){
if(haystack[i]==needle[j]){
i++;
j++;
}else{
i=i-j+1;
j=0;
}
}
return j<needle.size()?-1:i-j;
}
};
38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1
is read off as "one 1"
or 11
.
11
is read off as "two 1s"
or 21
.
21
is read off as "one 2
, then one 1"
or 1211
.
Given an integer n, generate the nth sequence.
字符串递推题,用cur保存当前生成串,res表示每一轮结果。
class Solution {
public:
string countAndSay(int n) {
if(n==0) return "";
string res="1";
while(--n){
string cur="";
for(int i=0;i<res.size();i++){
int count=1;
while(i+1<res.size() && res[i]==res[i+1]){
count++;
i++;
}
cur=cur+to_string(count)+res[i];
}
res=cur;
}
return res;
}
};
43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note:
- The numbers can be arbitrarily large and are non-negative.
- Converting the input string to integer is NOT allowed.
- You should NOT use internal library such as BigInteger.
class Solution {
public:
void reverse(string& num){
int l=0,r=num.size()-1;
while(l<r){
int temp=num[l];
num[l++]=num[r];
num[r--]=temp;
}
}
string multiply(string num1, string num2) {
string temp(num1.size()+num2.size(),'0');
int c=0;
reverse(num1);
reverse(num2);
for(int i=0;i<num1.size();i++){
int j;
for(j=0;j<num2.size();j++){
c+=temp[i+j]-'0'+(num1[i]-'0')*(num2[j]-'0');
temp[i+j]=c%10+'0';
c=c/10;
}
if(c){
temp[i+j]+=c;
c=c/10;
}
}
for(int i=temp.size()-1;i>0;i--){
if(temp[i]!='0')
break;
else
temp.pop_back();
}
reverse(temp);
return temp;
}
};