-
回文排列
class Solution
{
public:
bool canPermutePalindrome(string s)
{
// 記錄字符出現(xiàn)的次數(shù)
int count[256] = {0};
for(size_t i = 0; i < s.size(); ++i)
++count[s[i]];
// 記錄字符出現(xiàn)次數(shù)為奇數(shù)的個數(shù)
int flag = 0;
for(size_t i = 0; i < 256; ++i)
if(count[i] % 2 == 1)
++flag;
if(flag > 1)
return false;
return true;
}
};
-
URL化
class Solution
{
public:
string replaceSpaces(string S, int length)
{
int front = length - 1;
int back = front;
// 遍歷找出空格個數(shù)
for(int i = 0; i < length; ++i)
{
if(S[i] == ' ')
{
back += 2;
}
}
// 實際URL化后所需的空間大小
S.resize(back + 1);
while(front != back)
{
if(S[front] != ' ')
{
S[back--] = S[front];
}
else
{
S[back--] = '0';
S[back--] = '2';
S[back--] = '%';
}
--front;
}
return S;
}
};
-
配對交換
class Solution {
public:
int exchangeBits(int num)
{
int num1 = 0b01010101010101010101010101010101;
int num2 = 0b10101010101010101010101010101010;
// 獲取奇數(shù)位
num1 &= num;
// 獲取偶數(shù)位
num2 &= num;
return (num1 << 1) + (num2 >> 1);
}
};
-
遞歸乘法
class Solution
{
public:
// A * B = A + A * (B - 1) = A + A + A * (B - 2) = ....
int multiply(int A, int B)
{
if(B == 0)
return 0;
if(B == 1)
return A;
return A + multiply(A, B - 1);
}
};
-
階乘尾數(shù)
/*
* 尾數(shù)0是乘10來的,10是從2*5來的
* [1,n]中因子5相對2會少些,找出[1,n]中因子5的個數(shù),就是尾數(shù)0的個數(shù)
*/
class Solution
{
public:
int trailingZeroes(int n)
{
int count = 0;
while(n >= 5)
{
n /= 5;
count += n;
}
return count;
}
};
-
二進制鏈表轉(zhuǎn)整數(shù)
class Solution
{
public:
int getDecimalValue(ListNode* head)
{
int num = 0;
while(head != NULL)
{
num = (num << 1) + head->val;
head = head->next;
}
return num;
}
};
-
從鏈表中刪去總和值為零的連續(xù)節(jié)點
class Solution {
public:
ListNode* removeZeroSumSublists(ListNode* head)
{
ListNode* newhead = new ListNode;
newhead->next = head;
ListNode* front = newhead;
while (front)
{
int sum = 0;
ListNode* back = front->next;
while (back)
{
sum += back->val;
if (sum == 0)
{
ListNode* del = front->next;
back = back->next;
while (del != back)
{
ListNode* tmp = del;
del = del->next;
// delete tmp;
}
front->next = back;
}
else
{
back = back->next;
}
}
front = front->next;
}
head = newhead->next;
delete newhead;
return head;
}
};
-
括號的最大嵌套深度
class Solution {
public:
int maxDepth(string s)
{
int count = 0; // 記錄當(dāng)前深度
int depth = 0; // 記錄最深度
for (char c : s)
{
if (c == '(')
{
++count;
if(count > depth)
{
++depth;
}
}
else if (c == ')')
{
--count;
}
}
return depth;
}
};
-
整理字符串
class Solution {
public:
string makeGood(string s)
{
size_t i = 0;
while(s.size() > 0 && i < s.size() - 1)
{
if(abs(s[i] - s[i+1]) == 32)
{
s.erase(i, 2);
if(i > 0)
{
--i;
}
continue;
}
++i;
}
return s;
}
};
-
奇偶樹
class Solution {
public:
bool isEvenOddTree(TreeNode* root)
{
queue<TreeNode*> q;
q.push(root);
if((q.front()->val) % 2 == 0)
{
return false;
}
int level = 0;
int num = 1;
while(!q.empty())
{
// v用于存儲一層節(jié)點數(shù)據(jù)用于比較判斷
vector<int> v;
while(num--)
{
if(q.front()->left)
{
q.push(q.front()->left);
v.push_back(q.back()->val);
}
if(q.front()->right)
{
q.push(q.front()->right);
v.push_back(q.back()->val);
}
q.pop();
}
++level;
num = v.size();
if(!v.empty())
{
if(level % 2) // level 是奇
{
int prev = v[0];
if(prev % 2 == 1)
{
return false;
}
for(size_t i = 1; i < v.size(); ++i)
{
if((v[i] % 2 != 0) || prev <= v[i])
{
return false;
}
prev = v[i];
}
}
else // level 是偶
{
int prev = v[0];
if(prev % 2 == 0)
{
return false;
}
for(size_t i = 1; i < v.size(); ++i)
{
if((v[i] % 2 != 1) || prev >= v[i])
{
return false;
}
prev = v[i];
}
}
}
}
return true;
}
};
-
將句子排序
class Solution {
public:
string sortSentence(string s)
{
vector<string> vs;
vs.resize(9);
// 倒序遍歷,找數(shù)字
for(int i = s.size() - 1; i >= 0; --i)
{
if(s[i] >= '0' && s[i] <= '9')
{
int j = i - 1;
while(j >= 0 && s[j] != ' ')
{
--j;
}
// 將對應(yīng)編號的數(shù)字對應(yīng)的字符串,放入數(shù)組
vs[s[i] - '0' - 1] += string(s.begin() + j + 1, s.begin() + i);
i = j;
}
}
// 拼接到vs[0]
for(size_t i = 1; i < 9; ++i)
{
if(vs[i].size() > 0)
{
vs[0] += (' ' + vs[i]);
}
}
return vs[0];
}
};
-
最長和諧子序列
class Solution {
public:
int findLHS(vector<int>& nums)
{
map<int, int> m;
// 去重 + 排序
for (int e : nums)
{
m[e]++;
}
auto it = m.begin();
auto prev = it;
++it;
int max = 0;
while (it != m.end())
{
if ((it->first - prev->first == 1) && (prev->second + it->second > max))
{
max = prev->second + it->second;
}
prev = it;
++it;
}
return max;
}
};
文章來源地址http://www.zghlxwxcb.cn/news/detail-705879.html
文章來源:http://www.zghlxwxcb.cn/news/detail-705879.html
到了這里,關(guān)于【C++刷題】經(jīng)典簡單題第二輯的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!