Cover image for leetcode每日一题 P1935 可以输入的最大单词数

leetcode每日一题 P1935 可以输入的最大单词数

字数 185
阅读
访客

时间轴

时间轴

2025-09-15

init

C++分割字符串

题目:

这题简单,主要注意下 C++怎么分割字符串吧

1234567891011121314151617181920212223242526272829303132333435363738
#include <iostream>#include <sstream>#include <stdio.h>#include <string>#include <vector>using std::string;using std::stringstream;using std::vector;class Solution {public:  int canBeTypedWords(string text, string brokenLetters) {    stringstream ss(text);    string token;    vector<string> words;    int broken = 0;    while (std::getline(ss, token, ' ')) {      words.push_back(token);    }    for (string word : words) {      for (char ch : brokenLetters) {        if (word.find(ch) != string::npos) {          broken++;          break;        }      }    }    return words.size() - broken;  }};int main() {  Solution s;  int res = s.canBeTypedWords(string("hello world"), string("ad"));  printf("%d\n", res);}
评论加载中…