是否已被访问:布尔型,visited[] 数组
bool visited[100]; // 默认被初始化为 false
1. 冒泡排序:布尔型 sorted 变量
用于标识是否数组已全部排序:
bool sorted = false;
for (int i = 0; i < n-1; ++i){
sorted = true;
for (int j = i + 1; j < n; ++j){
if (arr[i] < arr[j]){
sorted = false;
swap(arr[i], arr[j]);
}
}
}
2. *
通配符匹配:skip,要跳跃几个
- for (int skip = 0; pos + skip < str.size(); ++skip)
bool match(const string& str, const string& pattern){
int pos = 0;
while ((pos < str.size() && pos < pattern.size()) && (pattern[pos] == '?' || pattern[pos] == str[pos]))
++pos;
if (pos == pattern.size())
return pos == str.size();
if (pattern[pos] == '*') {
for (int skip = 0; pos + skip < str.size(); ++skip)
// 使用递归
if (match(str.substr(pos+skip), pattern.substr(pos+1)))
return true;
}
return false;
}