problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::number_of_valid_words_in_a_sentence::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static int countValidWords (const string &sentence)
 

详细描述

在文件 leetcode.h754 行定义.

成员函数说明

◆ countValidWords()

int leetcode::number_of_valid_words_in_a_sentence::Solution::countValidWords ( const string &  sentence)
static

< 有效单词数

< 是否是有效单词

< 是否已经存在连接符'-'

当前字符

在文件 leetcode.cpp1718 行定义.

1718 {
1719 auto *str = new char[sentence.length() + 1];
1720 strcpy(str, sentence.c_str());
1721 int count = 0;
1722 for(const char *token = strtok(str, " "); token != nullptr; token = strtok(nullptr, " ")) {
1723 bool is_valid = true;
1724 bool hyphen = false;
1725 for(int i = 0; token[i] != '\0'; i++) {
1726 const char ch = token[i];
1727 if(ch == '-') {
1728 //当前字符是连接符'-'
1729 if(hyphen) {
1730 //已经存在过连接符'-'
1731 is_valid = false;
1732 break;
1733 }
1734 hyphen = true;
1735 if(i == 0 || token[i + 1] == '\0') {
1736 //是否在字符串的开头或结尾
1737 is_valid = false;
1738 break;
1739 }
1740 if(isalpha(token[i - 1]) == 0 || isalpha(token[i + 1]) == 0) {
1741 //前后是否是字母
1742 is_valid = false;
1743 break;
1744 }
1745 } else if(token[i + 1] != '\0' && isalpha(ch) == 0) {
1746 //不是最后一个字符是否不是字母
1747 is_valid = false;
1748 break;
1749 } else if(isdigit(ch) != 0) {
1750 //是否是数字
1751 is_valid = false;
1752 break;
1753 }
1754 }
1755 if(is_valid) {
1756 count++;
1757 }
1758 }
1759 delete[] str;
1760 return count;
1761 }
bool is_valid(int year, int month, int day)
Definition: pat.cpp:915

引用了 pat::b::b1028::is_valid().

被这些函数引用 leetcode::number_of_valid_words_in_a_sentence::TEST().


该类的文档由以下文件生成: