String

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 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.
大数乘法,思路先将两个字符串逆序,逐位相乘存入一个新的字符串,最后再逆序,去除串首多余的0。在逐位相乘时要注意用一个变量暂存进位值。

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;
    }
};




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值