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; }};
阅读全文
0 0
- LintCode题目解答之字符串操作(2)
- LintCode 题目解答之字符串操作(1)
- LintCode 题目之Aplusb2
- C语言学习之文件操作(含重庆大学研究生程序设计大赛的题目和解答)
- 浙大复试上机题目解答(2)
- leetcode题目解答报告(2)
- LintCode之比较字符串
- LintCode之字符串置换
- LintCode之旋转字符串
- SeedCoder2014热身题目2:解答
- csdn题目(含解答)
- LintCode之8 旋转字符串
- LintCode之13 字符串查找
- Lintcode—(2)字符串查找
- Lintcode(2)-比较字符串
- 翻转字符串(LintCode)
- 面试题(一)-------面试中常见的字符串类型题目及思路解答
- 浙江大学计算机系硕士研究生复试题目解答(2)
- hdu 5969 最大的位或(贪心)
- CentOS服务器配置之uwsgi的安装和使用(翻译文档)
- Java反射知识点总结
- CMVS-PMVS 知识库链接
- 负数在计算机中如何表示,计算机中负数为什么用补码表示?
- LintCode题目解答之字符串操作(2)
- Maven私服-Nexus搭建文档
- CentOS服务器配置之python环境配置
- java类型转换方法
- FORM的基础开发
- POJ 1703 带权并查集
- hdu2665 Kth number(主席树模板)
- MySQL主从同步实践
- iOS:json格式字符串转字典,字典转json格式字符串