1 条题解
-
0
练70.2 判断字符串是否为回文
解题思路
复制字符串并反转,与原串比较,完全相同为回文。
正确性说明
依题目给出的对应关系逐项处理,位置与原输入保持明确对应。每一步完成当前项的全部贡献,结束后得到所有位置或字符的目标结果。
易错点
大小写不同仍视为不同字符;本题不忽略空格或大小写。
复杂度
时间O(L),空间O(L)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; string t = s; reverse(t.begin(), t.end()); cout << (s == t ? "yes" : "no") << "\n"; return 0; }
- 1