LintCode题目解答之字符串操作(2)

来源:互联网 发布:淘宝互刷平台源码 编辑:程序博客网 时间:2024/05/20 12:21


接上一篇博客,本次包括3题。

  • 题目2:比较字符串(LintCode题号:55)

比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是大写字母(这好像没什么关系···)。

i 注意事项
在A中出现的B字符串里的字符不需要连续或者有序。

样例
给出 A = “ABCD” B = “ACD”,返回 true
给出 A = “ABCD” B = “AABC”, 返回 false

解答如下:

class Solution {public:    /**     * @param A: A string     * @param B: A string     * @return: if string A contains all of the characters in B return true else return false     */    bool compareStrings(string A, string B) {        // write your code here        vector<char> vecSearched;        vector<int> vecCharNum;        // deduplicate string B        for (int i = 0; i < B.size(); i++)        {            bool bFound = false;            for (int j = 0; j < vecSearched.size(); j++)            {                if (B[i] == vecSearched[j])                {                    bFound = true;                    break;                }            }            if (false == bFound)               {                vecSearched.push_back(B[i]);                int iAmount = 1;                for (int j = i + 1; j < B.size(); j++)                {                    if (B[i] == B[j])                    {                        iAmount++;                    }                }                vecCharNum.push_back(iAmount);            }        }        // for each character in searched, find same ones in string A        for (int i = 0; i < vecSearched.size(); i++)        {            int iAmount = 0;            bool bFound = false;            for (int k = 0; k < A.size(); k++)            {                if (vecSearched[i] == A[k])                {                    iAmount++;                    if (iAmount >= vecCharNum[i])                    {                        break;                    }                }            }            // once no enough amount in string A            if (iAmount < vecCharNum[i])            {                return false;            }            else            {                continue;            }        }        return true;    }};
  • 题目3:报数(LintCode题号:420)

报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数。如下所示:

1, 11, 21, 1211, 111221, …

1 读作 “one 1” -> 11.

11 读作 “two 1s” -> 21.

21 读作 “one 2, then one 1” -> 1211.

给定一个整数 n, 返回第 n 个顺序。

i 注意事项
整数的顺序将表示为一个字符串。

解答如下:

class Solution {public:    /*     * @param n: the nth     * @return: the nth sequence     */    string countAndSay(int n) {        // write your code here        // analysis: a string is written by such principle that        // from the first number in previous string.        // if there are continuous same number,        // then current string is written as 'amount of continuous number'+'that same number';        // if there is a number different from its adjacent number        // then current string is written as '1'+'that number'        string strPred = "1";        string strSucc = strPred;        for (int j = 1; j < n; j++)        {            strPred = strSucc;            strSucc.clear();            int iSameAmount = 1;            for (int i = 0; i < strPred.size(); i++)            {                if ((strPred[i] == strPred[i + 1]) && (strPred.size() > (i + 1)))                {                    iSameAmount++;                }                else                {                    strSucc = strSucc + (char)(iSameAmount + 48) + strPred[i];                    iSameAmount = 1;                }            }        }        return strSucc;    }};
  • 题目4:最后一个单词的长度(LintCode题号:422)

给定一个字符串, 包含大小写字母、空格’ ‘,请返回其最后一个单词的长度。

如果不存在最后一个单词,请返回 0 。

i注意事项
一个单词的界定是,由字母组成,但不包含任何的空格。

解答如下:

class Solution {public:    /*     * @param s: A string     * @return: the length of last word     */    int lengthOfLastWord(string s) {        // write your code here        int iLength = s.size();        int iWordLen = 0;        for (int i = 0; i < iLength; i++)        {            if (' ' != s[i])            {                iWordLen++;            }            else            {                if (' ' == s[i])                {                    int j = i + 1;                    for (; j < iLength; j++)                    {                        if (' ' != s[j])                        {                            break;                        }                    }                    // if letters from the jth to the last are all spaces, then quit                    if (iLength == j)                    {                        break;                    }                    // if the last two letters are space and a letter                    else if ((iLength - 1) == j)                    {                        iWordLen = 1;                    }                    else                    {                        iWordLen = 0;                    }                }            }        }        return iWordLen;    }};