811. Subdomain Visit Count (5月23日)
阿新 • • 發佈:2018-05-25
sys 有符號 using pan bstr leet double類型 sim clas
解答
class Solution {
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
vector<string> result;
map<string,int> pair;
for(string str:cpdomains){
auto space=str.find(‘ ‘);
int temp=stoi(str.substr(0,space));
str=str.substr(space+1 );
while(!str.empty()){
pair[str] += temp;
if(str.find(‘.‘)!=string::npos){
str=str.substr(str.find(‘.‘)+1);
}
else{
str.clear();
}
}
}
for (auto temp:pair){
result.push_back(to_string(temp.second)+‘ ‘+temp.first);
}
return result;
}
};
此題個人有思路,但無從下手,是參考他人解法後從而解出來的。
參考鏈接:https://leetcode.com/problems/subdomain-visit-count/discuss/129879/C++-O(sum-of-lengths-of-strings)-simple-Solution
筆記
- string類型中的find,substr成員函數的使用
- 字符串轉整數函數stoi,整數轉字符串函數to_string
函數學習
1. string::find 查找
參考C++ Primer 5th 9.5.3節
1. 函數原型,有四個重載版本
// 1
size_type find( CharT ch, size_type pos = 0 ) const;
// 2
size_type find( const basic_string& str, size_type pos = 0 ) const
// 3
size_type find( const CharT* s, size_type pos = 0 ) const;
// 4
size_type find( const CharT* s, size_type pos, size_type count ) const;
2. 解釋
返回類型:是一個size_type類型,是一個整數,表示匹配位置的下標;如果沒有找到,則會返回string::npos,這是一個確定值,類型是整數,初始值為-1
查找開始位置:即pos,在1,2,3中默認從開始查找
前三個版本解釋:1。查找字符;2. 查找string類型字符串;3.查找C風格字符串
第四個版本解釋:查找一個C風格字符串的前n個字符字符,同時開始查找位置pos需指定
3. 示例程序
/********************************************
*
* 程序作用:測試string類型中的find成員函數
*
* a11測試第一個重載版本,能夠找到;
* a12測試第一個重載版本,但不能找到
* a21測試第二個重載版本,能夠找到;
* a22測試第二個重載版本,但不能找到
* a3 測試第三個重載版本,但不能找到
* a4 測試第四個重載版本,能夠找到
*
* 編 制 人:niaocaics
* 編制時間: 2018.5.23
* 聯系郵箱:[email protected]
*
*/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
string s{ "Hello World" };
size_t a11 = s.find(‘ ‘),a12=s.find(‘ ‘,7);
size_t a21 = s.find("lo"),a22=s.find("lol");
const char * str = "lol";
size_t a3 = s.find(str);
size_t a4 = s.find(str,1,2);
cout << a11 << " " << a12 << endl << a21 << " " << a22
<< endl << a3 << endl << a4 << endl;
system("pause");
return 0;
}
輸出結果:
環境:Visual Studio 2017
輸出:
5 4294967295
3 4294967295
4294967295
3
由此可見,npos會返回-1,因為size_t是一個無符號整數,所以整數-1會被解析成最大數
2. string::substr 求子串
參考C++ Primer 5th 9.5.1節
1. 函數原型:
basic_string substr( size_type pos = 0,
size_type count = npos ) const;
2. 解釋
pos代表開始位置,默認會從字符串開頭開始;
count代表復制的字符數,默認為npos,但由於size_type是無符號整數,因此有符號的-1會被解析成最大數,即count=最大數即到字符串結尾
3. 示例程序
/****************************************************************
* 程序作用:測試string類型中的substr成員函數
*
* s1使用默認參數
* s2只對開始位置進行處理
* s3指定開始字符,和字符長度(在字符串範圍內)
* s3指定開始字符,和字符長度(但大於在字符串原本長度)
*
* 編 制 人:niaocaics
* 編制時間: 2018.5.24
* 聯系郵箱:[email protected]
*
*/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
string s = { "Hello World!" };
string s1 = s.substr();
string s2 = s.substr(6);
string s3 = s.substr(6, 3);
string s4 = s.substr(6, 100);
cout << s1 << endl << s2 << endl <<
s3 << endl << s4 << endl;
system("pause");
return 0;
}
輸出結果:
環境:Visual Studio 2017
輸出:
Hello World!
World!
Wor
World!
3 stoi 將字符串轉化為整數
1 函數原型
int stoi( const std::string& str,
std::size_t* pos = 0, int base = 10 );
字符串有倆種選擇,寬字符串wstring和一般string,
返回類型有三種,int;long;long long
2 解釋
str是要轉換的字符串,pos代表開始位置默認從字符串,base代表進制,默認十進制
3 示例程序
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
string s = { "1234567" };
int a = stoi(s);
cout << a << endl;
system("pause");
return 0;
}
輸出:1234567
4. to_string 把數轉換成字符串(C++11)
std::string to_string( int value );
value可以是int,long,long long,unsigned int,unsigned long,unsigned long long,float,double,long double類型
2 解釋
value是數字,返回一個字符串
3 示例程序
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
int a1 = 23456;
float a2 = 3.1415;
string s1 = to_string(a1);
string s2 = to_string(a2);
cout << s1 << endl << s2 << endl;
system("pause");
return 0;
}
輸出:
23456
3.141500
總結
此題暴露了我對庫函數的不熟悉,以後要多了解庫函數
811. Subdomain Visit Count (5月23日)