每天坚持写几道leetcode,希望几个月后我就不再是小白
今天的题目是521,824,20,520
附stack
string
容器用法总结
stack
用法:empty()
pop()
push()
size()
top()
string
用法:append()
insert()
substr()
append
连接 basic_string &append( const basic_string &str );//在字符串的末尾添加str,
basic_string &append( const char *str );
basic_string &append( const basic_string &str, size_type index, size_type len );//在字符串的末尾添加str的子串,子串以index索引开始,长度为len
basic_string &append( const char *str, size_type num );//在字符串的末尾添加str中的num个字符
basic_string &append( size_type num, char ch );//在字符串的末尾添加num个字符ch
basic_string &append( input_iterator start, input_iterator end );//在字符串的末尾添加以迭代器start和end表示的字符序列
insert
插入 iterator insert( iterator i, const char &ch );
basic_string &insert( size_type index, const basic_string &str );
basic_string &insert( size_type index, const char *str );
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
basic_string &insert( size_type index, const char *str, size_type num );
basic_string &insert( size_type index, size_type num, char ch );
void insert( iterator i, size_type num, const char &ch );
void insert( iterator i, iterator start, iterator end );
substr
basic_string substr( size_type index, size_type num = npos );//substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。
assign
赋值 basic_string &assign( const basic_string &str );
basic_string &assign( const char *str );
basic_string &assign( const char *str, size_type num );
basic_string &assign( const basic_string &str, size_type index, size_type len );
basic_string &assign( size_type num, char ch );
compare
小于零 this < str
int compare( const basic_string &str );//比较自己和str
int compare( const char *str );
int compare( size_type index, size_type length, const basic_string &str );//比较自己的子串和str,子串以index索引开始,长度为length
int compare( size_type index, size_type length, const basic_string &str, size_type index2,
size_type length2 );//比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
int compare( size_type index, size_type length, const char *str, size_type length2 );//比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length
copy
size_type copy( char *str, size_type num, size_type index );//拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数
erase
iterator erase( iterator pos );
iterator erase( iterator start, iterator end );
basic_string &erase( size_type index = 0, size_type num = npos );
find
size_type find( const basic_string &str, size_type index );//返回str在字符串中第一次出现的位置(从index开始查找)
size_type find( const char *str, size_type index );
size_type find( const char *str, size_type index, size_type length );
size_type find( char ch, size_type index );
at
begin
end
empty
用法同vector
容器描述:Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.
A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.
The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn’t exist, return -1.
例子:
Input: "aba", "cdc"
Output: 3
Explanation: The longest uncommon subsequence is "aba" (or "cdc"),
because "aba" is a subsequence of "aba",
but not a subsequence of any other strings in the group of two strings.
代码:
int Solution521::findLUSlength(string a, string b)
{
if (a == b)return -1;
else if (a.size() == b.size())return a.size();
else return max(a.size(), b.size());
}
思路:
1. 如果两个字符串相等不可能有不相同子序列
2. 如果两个字符串不相等,但是长度相同,则最长的不同子序列就是二者任意一个
3. 上述条件都不符合,则最长的字符串就是最长的不同子序列
描述:A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.
We would like to convert the sentence to “Goat Latin” (a made-up language similar to Pig Latin.)
The rules of Goat Latin are as follows:
- If a word begins with a vowel (a, e, i, o, or u), append “ma” to the end of the word.
For example, the word ‘apple’ becomes ‘applema’.
- If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add “ma”.
For example, the word “goat” becomes “oatgma”.
- Add one letter ‘a’ to the end of each word per its word index in the sentence, starting with 1.
For example, the first word gets “a” added to the end, the second word gets “aa” added to the end and so on.
Return the final sentence representing the conversion from S to Goat Latin.
例子:
Input: "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
代码:
string Solution824::toGoatLatin(string S)
{
int j = 0;//当前词开始下标
int k = 1;//记录第k个word
unordered_set<char> vowel = { 'a','e','i','o','u','A','E','I','O','U' };
string t;
for (int i = 0; i < S.size(); ++i)
{
if (S[i + 1] == ' ')
{
if (vowel.find(S[j]) != vowel.end())//是元音
{
t.append(S.substr(j, i - j + 1));
}
else
{
t.append(S.substr(j + 1, i - j));
t.append(S.substr(j, 1));
}
t.append("ma");
for (int a = 0; a < k; a++)t.append("a");
t.append(" ");
j = i + 2;
++k;
}
}
return t;
}
收获:
1. string::append()
2. string::substr
描述:Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
例子:
Input: "()"
Output: true
Input: "([)]"
Output: false
代码:
bool Solution20::isValid(string s)
{
stack<char> t;
for (char c : s)
{
if (c == '(' || c == '[' || c == '{')
t.push(c);
else
{
if (t.empty() || (c == ')' && t.top() != '(') || (c == ']' && t.top() != '[') || (c == '}' && t.top() != '{'))return false;
else t.pop();
}
}
return t.empty();
}
收获:
* stack::push()
stack::pop()
stack::top()
* 多重判断条件时将特殊边界条件放在首位,比如t.empty()
描述:Given a word, you need to judge whether the usage of capitals in it is right or not.
We define the usage of capitals in a word to be right when one of the following cases holds:
Input: "FlaG"
Output: False
代码:
bool Solution520::detectCapitalUse(string word)
{
//way 1
//int k = 0;//0--首字母大写 1--首字母小写
//int flag = 0;//0--首字母之外都是小写,1--首字母之外都是大写
//int len = word.size();
//for (int i = 0; i < len; ++i)
//{
// if (word[i] <= 'z' && word[i] >= 'a' && i == 0)k = 1;
// else if (word[i] <= 'Z' && word[i] >= 'A' && k == 1)return false;//首字母小写,中间出现了大写字母
// if (i == 1 && word[i] <= 'Z' && word[i] >= 'A')flag = 1;
// if (k == 0 && i > 1)//首字母大写
// {
// if ((flag == 1 && word[i] <= 'z' && word[i] >= 'a') || (flag == 0 && word[i] <= 'Z' && word[i] >= 'A'))return false;
// }
//}
//return true;
//way 2
bool firstCapital = false;//首字母小写
int capital = 0;//大写字母个数
for (int i = 0; i < word.size(); ++i)
{
if (i == 0 && word[i] <= 'Z' && word[i] >= 'A')firstCapital = true;
if (word[i] <= 'Z' && word[i] >= 'A')++capital;
if (capital < i + 1 && capital >= 2)return false;//大写字母数目大于等于两个,却小于当前遍历过的字母
}
return (capital == 0 || (firstCapital && capital == 1) || capital == word.size());
}
更多内容请浏览我的博客:博客
源码请见我的GitHub:AlisaBen