- 回文排列
cpp
复制代码
class Solution
{
public:
bool canPermutePalindrome(string s)
{
// 记录字符出现的次数
int count[256] = {0};
for(size_t i = 0; i < s.size(); ++i)
++count[s[i]];
// 记录字符出现次数为奇数的个数
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化
cpp
复制代码
class Solution
{
public:
string replaceSpaces(string S, int length)
{
int front = length - 1;
int back = front;
// 遍历找出空格个数
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;
}
};
- 配对交换
cpp
复制代码
class Solution {
public:
int exchangeBits(int num)
{
int num1 = 0b01010101010101010101010101010101;
int num2 = 0b10101010101010101010101010101010;
// 获取奇数位
num1 &= num;
// 获取偶数位
num2 &= num;
return (num1 << 1) + (num2 >> 1);
}
};
- 递归乘法
cpp
复制代码
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);
}
};
- 阶乘尾数
cpp
复制代码
/*
* 尾数0是乘10来的,10是从2*5来的
* [1,n]中因子5相对2会少些,找出[1,n]中因子5的个数,就是尾数0的个数
*/
class Solution
{
public:
int trailingZeroes(int n)
{
int count = 0;
while(n >= 5)
{
n /= 5;
count += n;
}
return count;
}
};
- 二进制链表转整数
cpp
复制代码
class Solution
{
public:
int getDecimalValue(ListNode* head)
{
int num = 0;
while(head != NULL)
{
num = (num << 1) + head->val;
head = head->next;
}
return num;
}
};
- 从链表中删去总和值为零的连续节点
cpp
复制代码
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;
}
};
- 括号的最大嵌套深度
cpp
复制代码
class Solution {
public:
int maxDepth(string s)
{
int count = 0; // 记录当前深度
int depth = 0; // 记录最深度
for (char c : s)
{
if (c == '(')
{
++count;
if(count > depth)
{
++depth;
}
}
else if (c == ')')
{
--count;
}
}
return depth;
}
};
- 整理字符串
cpp
复制代码
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;
}
};
- 奇偶树
cpp
复制代码
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用于存储一层节点数据用于比较判断
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;
}
};
- 将句子排序
cpp
复制代码
class Solution {
public:
string sortSentence(string s)
{
vector<string> vs;
vs.resize(9);
// 倒序遍历,找数字
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;
}
// 将对应编号的数字对应的字符串,放入数组
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];
}
};
- 最长和谐子序列
cpp
复制代码
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;
}
};