350. 兩個(gè)數(shù)組的交集 II
思路
- 兩個(gè)map分別統(tǒng)計(jì)在兩個(gè)數(shù)組中一個(gè)元素的出現(xiàn)次數(shù)
- 把其中一個(gè)數(shù)組排序去重,然后查詢兩個(gè)map
- 取這個(gè)元素在兩個(gè)數(shù)組里出現(xiàn)次數(shù)的最小值n待笑,往ans里面push該元素n次
AC代碼
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
map<int, int> v,n;
vector<int> ans;
for (int x : nums1) {
v[x]++;
}
for (int x : nums2) {
n[x]++;
}
sort(nums1.begin(), nums1.end());
nums1.erase(unique(nums1.begin(), nums1.end()), nums1.end());
for (int x : nums1) {
if (v[x] && n[x]) {
int l = v[x] > n[x] ? n[x] : v[x];
for (int i = 0; i < l; i++)
ans.push_back(x);
}
}
return ans;
}
};
大佬思路
雙指針法
- 兩個(gè)數(shù)組排序
- 兩個(gè)指針指向第0個(gè)元素
- 循環(huán)比較絮记,如果某一個(gè)指針的元素小摔踱,指針后移,知道值相等時(shí)怨愤,push一次
大佬代碼
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
int n1Size = nums1.size();
int n2Size = nums2.size();
int i = 0;
int j = 0;
vector<int> intersect;
while((i < n1Size) && (j < n2Size))
{
if (nums1[i] < nums2[j])
++i;
else if (nums1[i] > nums2[j])
++j;
else
{
intersect.push_back(nums1[i]);
++i;
++j;
}
}
return intersect;
}
};
367. 有效的完全平方數(shù)
思路
自己沒好好研究這題的算法
AC代碼
class Solution {
public:
bool isPerfectSquare(int num) {
return (int)sqrt(num) == sqrt(num);
}
};
大佬思路
- 自己寫一個(gè)搞笑的mySqrt函數(shù)派敷,用類似二分查找法實(shí)現(xiàn),畢竟這道題的輸入只有整數(shù)
- 暴力搜索
大佬代碼
class Solution {
public:
bool isPerfectSquare(int num) {
int sqrt = mySqrt(num);
return sqrt*sqrt == num;
}
int mySqrt(int x) {
int lo,hi;
long mid;
lo = 0;
hi = x;
while(lo<=hi){
mid = lo + (hi-lo)/2;
if(mid*mid>x){
hi = mid-1;
} else if ((mid+1)*(mid+1)>x) {
return mid;
} else{
lo = mid+1;
}
}
return lo;
}
371. 兩整數(shù)之和
思路
- 用位運(yùn)算
- 不會(huì)
AC代碼
class Solution {
public:
int getSum(int a, int b) {
int temp = 0;
while(a & b){
temp = a;
a ^= b;
b = (temp & b) << 1;
}
return a|b;
}
};
374. 猜數(shù)字大小
思路
- 暴力搜索不可取撰洗,二分查找保平安
- 不要
mid = (high + low) / 2
篮愉,會(huì)溢出
// Forward declaration of guess API.
// @param num, your guess
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num);
class Solution {
public:
int guessNumber(int n) {
int left = 1, right = n, mid = (n + 1)/2;
while (left <= right) {
mid = (left - right)/2 + right;
switch (guess(mid)) {
case -1 :
right = mid - 1;
break;
case 1 :
left = mid + 1;
break;
case 0 :
return mid;
}
}
return -1;
}
};
383. 贖金信
思路
- 兩個(gè)表,分別記錄每個(gè)字母出現(xiàn)次數(shù)
- 遍歷26個(gè)字母差导,magazine中字母出現(xiàn)次數(shù)大于等于ransom就可以
AC代碼
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int m[26] = {0}, n[26] = {0};
for (char x : ransomNote) {
m[x-'a']++;
}
for (char x : magazine) {
n[x-'a']++;
}
for (char x : ransomNote) {
if (m[x-'a'] > n[x-'a'])
return false;
}
return true;
}
};
387. 字符串中的第一個(gè)唯一字符
思路
記錄每個(gè)字母出現(xiàn)次數(shù)试躏,遍歷字符串
,看誰第一個(gè)出現(xiàn)次數(shù)是0
AC代碼
class Solution {
public:
int firstUniqChar(string s) {
int n[26] = {0};
for (char x : s) {
n[x - 'a']++;
}
int len = s.length();
for (int i = 0; i < len; i++) {
if (n[s[i] - 'a'] == 1) return i;
}
return -1;
}
};
389. 找不同
思路
- 記錄次數(shù)设褐,遍歷一遍t颠蕴,看誰出現(xiàn)次數(shù)多一次
- 異或運(yùn)算,抵消相同的
AC代碼
class Solution {
public:
char findTheDifference(string s, string t) {
int m[26] = {0}, n[26] = {0};
for (char x : s) {
m[x - 'a']++;
}
for (char x : t) {
n[x - 'a']++;
}
for (char x : t) {
if (m[x - 'a'] < n[x - 'a']) return x;
}
return -1;
}
};
AC代碼
class Solution {
public:
char findTheDifference(string s, string t) {
int len = s.length();
char c = t[0];
for (int i = 0; i < len; i++) {
c ^= s[i];
c ^= t[i + 1];//t只比s多一個(gè)
}
return c;
}
};
400. 第N個(gè)數(shù)字
思路
- 把
,
,
...之前的數(shù)算出來助析,存到數(shù)組里
- 查詢數(shù)組犀被,得到這個(gè)數(shù)對(duì)應(yīng)的數(shù)量級(jí)之前有多少數(shù),然后算出這個(gè)數(shù)具體是幾
AC代碼
class Solution {
public:
int findNthDigit(int n) {
unsigned long long m[10] = {0, 9, 189, 2889, 38889, 488889, 5888889, 68888889, 788888889, 8888888889};
//10^i之前的數(shù)字個(gè)數(shù)外冀。10之前有9個(gè)數(shù)弱判,100之前有189個(gè)數(shù)
int index = 0;
for (; index < 10; index++) {
if (m[index] >= n) {
break;
}
}//找到n所在的范圍,index是它的位數(shù)len
n -= m[index - 1];//從例如189對(duì)應(yīng)的100后的第幾個(gè)數(shù)字
long long ans = pow(10, index - 1) + (n - 1) / (index);//對(duì)應(yīng)的數(shù)
string t = to_string(ans);
return t[(n - 1) % (index)] - '0';
}
};
405. 數(shù)字轉(zhuǎn)換為十六進(jìn)制數(shù)
思路
- 用一個(gè)
unsigned char
指針指向int
锥惋,循環(huán)4次昌腰,每次取值是兩個(gè)16進(jìn)制數(shù),然后存起來 - 注意局部變量存在棧里膀跌,倒著輸出
- 忽略前導(dǎo)0
AC代碼
class Solution {
public:
string toHex(int num) {
if (!num) return "0";
char m[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
unsigned char *c = (unsigned char *)#
unsigned int n[8] = {0};
for (int i = 0; i < 8; i+=2) {
n[i] = (*c) % 16;
n[i + 1] = (*c/16) % 16;
c++;
}
string ans;
int i = 7;
while (i >= 0 && n[i] == 0)
i--;
for (; i >= 0; i--) {
ans += m[n[i]];
}
return ans;
}
};
412. Fizz Buzz
思路
額遭商,,算就是了
AC代碼
class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> v;
for (int i = 1; i <= n; i++) {
if (i % 15 == 0) {
v.push_back("FizzBuzz");
}
else if (i % 3 == 0) {
v.push_back("Fizz");
}
else if (i % 5 == 0) {
v.push_back("Buzz");
}
else {
v.push_back(to_string(i));
}
}
return v;
}
};
414. 第三大的數(shù)
思路
- 搜索三次
- 第一次最大值
- 第二次不等于第一次的最大值
- 第三次不等于前兩次的最大值
AC代碼
class Solution {
public:
int thirdMax(vector<int>& nums) {
long i = LONG_MIN, j = LONG_MIN, k = LONG_MIN;
for (int x : nums) {
if (x > i) {
i = x;
}
}
for (int x : nums) {
if (x > j && x != i) {
j = x;
}
}
for (int x : nums) {
if (x > k && x != i && x != j) {
k = x;
}
}
if (k == LONG_MIN) return i;
else return k;
}
};
大佬思路
- 搜索一次找最大值
- 如果有最大值且大于最大的最大值捅伤,就把當(dāng)前值先給了第二大值劫流,第二大值給了第三大值
- 如果有最大值且小于最大的最大值大于第二大,往后順延
- 如果有最大值且小于第二大的最大值大于第三大,往后順延
大佬代碼
class Solution {
public:
int thirdMax(vector<int>& nums) {
long first = LONG_MIN, second = LONG_MIN, third = LONG_MIN;
for (int num : nums) {
if (num > first) {
third = second;
second = first;
first = num;
} else if (num > second && num < first) {
third = second;
second = num;
} else if (num > third && num < second) {
third = num;
}
}
return (third == LONG_MIN || third == second) ? first : third;
}
};
415. 字符串相加
思路
- 找到最長字符串的長度祠汇,用這個(gè)長度分別給兩個(gè)字符串添加前導(dǎo)0
- 倒著遍歷字符串仍秤,對(duì)應(yīng)相加模擬就行了
- 別網(wǎng)站最后一位的進(jìn)位
AC代碼
class Solution {
public:
string addStrings(string num1, string num2) {
int carry = 0;
int len1 = num1.length();
int len2 = num2.length();
int len = len1 > len2 ? len1 : len2;
string zero;
for (int i = 0; i < len - len1; i++) {
zero += '0';
}
num1.insert(0, zero);
zero.clear();
for (int i = 0; i < len - len2; i++) {
zero += '0';
}
num2.insert(0, zero);
for (int i = len - 1; i >= 0; i--) {
int n = carry + num1[i] + num2[i] - 2*'0';
num1[i] = n % 10 + '0';
carry = n / 10;
}
if (carry) num1.insert(0, 1, carry + '0');
return num1;
}
};
434. 字符串中的單詞數(shù)
思路
stl大法好
AC代碼
class Solution {
public:
int countSegments(string s) {
stringstream ss(s);
string buf;
int count = 0;
while (ss >> buf) {
count++;
}
return count;
}
};
447. 回旋鏢的數(shù)量
思路
- 把所有點(diǎn)兩兩配對(duì),計(jì)算距離
- 累加
n(n-1)
排列數(shù)
AC代碼
class Solution {
public:
int numberOfBoomerangs(vector<pair<int, int>>& points) {
unordered_map<int ,int> m;
int ans = 0;
int len = points.size();
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
int dx = points[i].first - points[j].first;
int dy = points[i].second - points[j].second;
m[dx*dx + dy*dy]++;
}
for (auto c : m) {
ans += c.second*(c.second-1);
}
m.clear();
}
return ans;
}
};
441. 排列硬幣
思路
- 直接解方程
AC代碼
class Solution {
public:
int arrangeCoins(int n) {
return (sqrt(1 + 8ll * n) - 1) / 2;
}
};
大佬思路
類似二分查找
- 計(jì)算當(dāng)前mid對(duì)應(yīng)的樓梯數(shù)q
- 如果q<=總數(shù)可很,查找右邊
- 否則查找左邊
大佬代碼
class Solution {
public:
int arrangeCoins(int n) {
if (n <= 0) return 0;
unsigned long long i = 1, j = n + 1;
while (j - i>= 1) {
long long mid = i + (j - i) / 2;
long long q = mid*(mid + 1) / 2;
if (q <= n) {
i = mid + 1;
} else {
j = mid;
}
}
return i - 1;
}
};
443. 壓縮字符串
思路
遍歷數(shù)次數(shù)诗力,然后把次數(shù)編程string存起來,最后一個(gè)字符一個(gè)字符的存到vector數(shù)組里我抠,返回
AC代碼
class Solution {
public:
vector<char> ans;
int compress(vector<char>& chars) {
int len = chars.size();
vector<string> s;
int j = 0;
int count = 0;
for (int i = 1; i < len; i++) {
if (chars[j] != chars[i]) {
j++;
chars[j] = chars[i];
s.push_back(to_string(count + 1));
count = 0;
} else {
count++;
}
}
s.push_back(to_string(count + 1));
for (int i = 0; i < j + 1; i++) {
ans.push_back(chars[i]);
if (s[i] == "1") continue;
int num = s[i].length();
for (int k = 0; k < num; k++) {
ans.push_back(s[i][k]);
}
}
chars = ans;
return chars.size();
}
};
大佬思路
遍歷一遍苇本,數(shù)個(gè)數(shù),然后都存到一個(gè)string里面(充分利用string重載的operator+
)菜拓,最后分解成char數(shù)組瓣窄,返回
大佬代碼
class Solution {
public:
int compress(vector<char> &chars) {
int count = 1;
string str = "";
for (int i = 1; i < chars.size(); i++) {
if (chars[i] == chars[i - 1]) {
count++;
}
else {
if (count != 1) {
str += chars[i - 1] + to_string(count);
}
else {
str += chars[i - 1];
}
count = 1;
}
}
if (count != 1) {
str += chars[chars.size() - 1] + to_string(count);
}
else {
str += chars[chars.size() - 1];
}
for (int i = 0; i < str.size(); i++) {
if (i < chars.size()) {
chars[i] = str[i];
}
else {
chars.push_back(str[i]);
}
}
return str.size();
}
};
448. 找到所有數(shù)組中消失的數(shù)字
思路
記錄每個(gè)數(shù)的出現(xiàn)次數(shù),最后返回出現(xiàn)次數(shù)為0的那些數(shù)
AC代碼
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
unordered_map<int, int> m;
vector<int> ans;
int len = nums.size();
for (int x : nums) {
m[x]++;
}
for (int i = 1; i <= len; i++) {
if (m[i] == 0) {
ans.push_back(i);
}
}
return ans;
}
};
大佬思路
- i從0開始遍歷數(shù)組纳鼎,取nums[i]的絕對(duì)值Q(后期正數(shù)可能變負(fù)數(shù))
- 把Q-1作為下標(biāo)俺夕,把nums[Q-1]這個(gè)數(shù)編程負(fù)的(自己的絕對(duì)值的相反數(shù))
- 最后正數(shù)出現(xiàn)的位置就是
1~n
沒出現(xiàn)過的數(shù)
大佬代碼
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> ans;
int len = nums.size();
for (int i = 0; i < len; i++) {
nums[abs(nums[i]) - 1] = -abs(nums[abs(nums[i]) - 1]);
}
for (int i = 0; i < len; i++) {
if (nums[i] > 0) {
ans.push_back(i + 1);
}
}
return ans;
}
};
455. 分發(fā)餅干
思路
貪心算法 + 雙指針法
- 把所有小孩的胃口的出現(xiàn)次數(shù)統(tǒng)計(jì)出來,放到哈希表里面
- 把所有餅干能滿足的最大胃口的出現(xiàn)次數(shù)統(tǒng)計(jì)出來贱鄙,放到另一個(gè)哈希表里面
- 不用
unordered_map
劝贸,要排序的 - 遍歷一遍孩子,如果當(dāng)前餅干能滿足胃口贰逾,就盡量多的喂
- 直到這個(gè)孩子的胃口被滿足后悬荣,i++菠秒,不要j++疙剑,也許當(dāng)前餅干還沒有用完,而且足夠下一個(gè)孩子的胃口
- 如果不能滿足胃口践叠,由于map是排序過的言缤,所以也一定不能滿足后面的孩子的要求,就j++看下一塊餅干的情況
AC代碼
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
map<int, int> bit, chi;
for (int x : g) {
chi[x]++;
}
for (int x : s) {
bit[x]++;
}
int ans = 0;
auto i = chi.begin(), j = bit.begin();
for (; i != chi.end() && j != bit.end(); ) {
if (i->first <= j->first && j->second > 0) {
int a = i->second;
int b = j->second;
int min = a > b ? b : a;
i->second -= min;
j->second -= min;
ans += min;
if (i->second == 0)
i++;
} else {
j++;
}
}
return ans;
}
};
大佬思路
貪心 + 雙指針
- 排序兩個(gè)數(shù)組
- 其他思路和我的基本一樣禁灼,但是人家的代碼又簡潔效率又高
大佬代碼
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int ans = 0;
int i = 0, j = 0;
int len1 = g.size(), len2 = s.size();
for (; i < len1 && j < len2;j++) {
if (g[i] <= s[j]) {
i++;
}
}
return i;
}
};
461. 漢明距離
思路
位運(yùn)算
- 異或管挟,相同為1,不同為0
- 兩個(gè)數(shù)異或弄捕,轉(zhuǎn)二進(jìn)制僻孝,把二進(jìn)制位直接加起來就行
AC代碼
class Solution {
public:
int hammingDistance(int x, int y) {
int ans = x ^ y;
int num = 0;
while (ans) {
num += ans % 2;
ans /= 2;
}
return num;
}
};
463. 島嶼的周長
思路
- 只能暴搜了,如果一個(gè)格子上有顏色守谓,總邊數(shù)+=4
- 如果下方有格子穿铆,總邊數(shù)-=2(不管上面,防止兩條邊重復(fù)計(jì)數(shù))
- 如果右邊有格子斋荞,總邊數(shù)-=2(同理荞雏,不管左邊)
AC代碼
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int ans = 0;
int len = grid.size();
int wide = grid[0].size();
for(int i = 0; i < len; i++) {
for (int j = 0; j < wide; j++) {
if (grid[i][j]) {
ans += 4;
if (i + 1 < len && grid[i + 1][j]) {
ans -= 2;
}
if (j + 1 < wide && grid[i][j + 1]) {
ans -= 2;
}
}
}
}
return ans;
}
};
476. 數(shù)字的補(bǔ)數(shù)
思路
- 轉(zhuǎn)二進(jìn)制數(shù)
- (num%2+1)%2能讓1變0,0變1
AC代碼
class Solution {
public:
int findComplement(int num) {
int n = 0;
long long i = 1;
while (num) {
n += i * ((num % 2 + 1) % 2);
i *= 2;
num /= 2;
}
return n;
}
};
大佬思路
位運(yùn)算,不懂
大佬代碼
class Solution {
public:
int findComplement(int num) {
int temp = num;
int c = 0;
while ( temp > 0 ) {
temp >>= 1;
c = ( c << 1 ) + 1;
}
return num ^ c;
}
};
482. 密鑰格式化
思路
- 把
'-'
全都變成' '
- stringstream把字符串拼起來
- 倒著遍歷每K個(gè)加一個(gè)
'-'
凤优,并且注意前面是不是頭
AC代碼
class Solution {
public:
string licenseKeyFormatting(string S, int K) {
string ans, buf;
for (int i = 0; i < S.length(); i++) {
if (S[i] == '-') {
S[i] = ' ';
} else {
S[i] = toupper(S[i]);
}
}
stringstream ss(S);
while (ss >> buf) {
ans += buf;
}
for (int i = ans.length() - 1, count = 1; i > 0; i--, count++) {
if (count % K == 0 && i != 0) {
ans.insert(i, 1, '-');
}
}
return ans;
}
};
思路
- 遍歷悦陋,判斷是不是字母,是字母筑辨,變大寫俺驶,然后push到新的string里面
- 同時(shí)記錄字符數(shù),每K個(gè)加一個(gè)負(fù)號(hào)
- 清除前后的負(fù)號(hào)
- 反轉(zhuǎn)
AC代碼
class Solution {
public:
string licenseKeyFormatting(string S, int K) {
string ans;
int count = 0;
int len = S.length();
for (int i = len; i >= 0; i--) {
char t = S[i];
t = toupper(t);
if (t != '-') {
ans.push_back(t);
if (count == K /*&& i != 0 && i != len*/) {
ans.push_back('-');
count = 0;
}
count++;
}
}
if (ans.back() == '-') ans.pop_back();
reverse(ans.begin(), ans.end());
if (ans.back() == '-') ans.pop_back();
return ans;
}
};
485. 最大連續(xù)1的個(gè)數(shù)
AC代碼
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int len = nums.size();
int count = 0, max = 0;
for (int x : nums) {
if (x) {
count++;
} else {
//max = max > count ? max : count;
if (max < count) max = count;
count = 0;
}
}
if (max < count) max = count;
return max;
}
};
500. 鍵盤行
思路
- 建立哈希表挖垛,把每個(gè)字母對(duì)應(yīng)的鍵盤行數(shù)標(biāo)號(hào)
- 遍歷所有字符串痒钝,看是不是同一行,統(tǒng)計(jì)痢毒,記錄
- 按照要求輸出
AC代碼
class Solution {
public:
vector<string> findWords(vector<string>& words) {
string keyBoard[3] = {"QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"};
unordered_map<char, char> m;
for (int i = 0; i < 3; i++) {
int len = keyBoard[i].length();
for (int j = 0; j < len; j++) {
m[keyBoard[i][j]] = i;
m[keyBoard[i][j] - 'A' + 'a'] = i;
}
}
int n = words.size();
vector<string> ans;
for (int i = 0; i < n; i++) {
int len = words[i].length();
int cmp = m[words[i][0]];
bool find = true;
for (int j = 0; j < len; j++) {
if (cmp != m[words[i][j]]) {
find = false;
break;
}
}
if (find) {
ans.push_back(words[i]);
}
}
return ans;
}
};
504. 七進(jìn)制數(shù)
思路
就是普通進(jìn)制轉(zhuǎn)換問題
AC代碼
class Solution {
public:
string convertToBase7(int num) {
string ans;
bool nagetive = num < 0;
if (nagetive) {
num *= -1;
}
do {
ans = (char)(num % 7 + '0') + ans;
num /= 7;
} while (num);
if (nagetive){
ans = "-" + ans;
}
return ans;
}
};
506. 相對(duì)名次
思路
- 拷貝一份送矩,排序,map記錄排名
- 遍歷原來的數(shù)組哪替,輸出
AC代碼
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& nums) {
vector<int> copy = nums;
string rank[3] = {"Gold Medal", "Silver Medal", "Bronze Medal"};
map<int, int> m;
sort(copy.begin(), copy.end(), greater<int>());
int len = nums.size();
for (int i = 0; i < len; i++) {
m[copy[i]] = i;
}
vector<string> ans;
for (auto x : nums) {
if (m[x] >= 0 && m[x] < 3) {
ans.push_back(rank[m[x]]);
} else {
ans.push_back(to_string(m[x] + 1));
}
}
return ans;
}
};
507. 完美數(shù)
思路
AC代碼
class Solution {
public:
bool checkPerfectNumber(int num) {
if (num <= 1) return false;
int ans = 1;
for (int i = 2; i < sqrt(num); i++) {
if (num % i == 0) {
ans += i + num/i;
}
}
return ans == num;
}
};
思路
的完美數(shù)只有6,28,496,8128,33550336
AC代碼
class Solution {
public boolean checkPerfectNumber(int num) {
switch(num) {
case 6:
case 28:
case 496:
case 8128:
case 33550336:
return true;
}
return false;
}
}
509. 斐波那契數(shù)
思路
居然真的只是求斐波那契數(shù)列栋荸,還只要前30位
AC代碼
class Solution {
public:
int fib(int N) {
int a = 0, b = 1;
for (int i = 0; i < N; i++) {
int c = a + b;
b = a;
a = c;
}
return a;
}
};
520. 檢測大寫字母
思路
- 先把前導(dǎo)的大寫字母跳過
- 如果當(dāng)前指針正好指在0或1,那么只要后面有大寫字母凭舶,就算錯(cuò)(除非長度只有1)
- 如果指在1后面晌块,那么后面有小寫字母就算錯(cuò)
AC代碼
class Solution {
public:
bool detectCapitalUse(string word) {
int len = word.length();
int i = 0;
while (i < len && word[i] >= 'A' && word[i] <= 'Z') i++;
if (i > 1) {
for ( ; i < len; i++) {
if (word[i] >= 'a' && word[i] <= 'z') {
return false;
}
}
} else {
if (len <= 1) {
return true;
} else {
for ( ; i < len; i++) {
if (word[i] >= 'A' && word[i] <= 'Z') {
return false;
}
}
}
}
return true;
}
};