【C++刷题】经典简单题第二辑

  1. 回文排列
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;
    }
};
  1. 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;
    }
};
  1. 配对交换
cpp 复制代码
class Solution {
public:
    int exchangeBits(int num) 
    {
        int num1 = 0b01010101010101010101010101010101;
        int num2 = 0b10101010101010101010101010101010;
        // 获取奇数位
        num1 &= num;
        // 获取偶数位
        num2 &= num;

        return (num1 << 1) + (num2 >> 1);
    }
};
  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);
    }
};
  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;
    }
};
  1. 二进制链表转整数

cpp 复制代码
class Solution
{
public:
    int getDecimalValue(ListNode* head) 
    {
        int num = 0;
        while(head != NULL)
        {
            num = (num << 1) + head->val;
            head = head->next;
        }

        return num;
    }
};
  1. 从链表中删去总和值为零的连续节点
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;
    }
};
  1. 括号的最大嵌套深度
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;
    }
};
  1. 整理字符串

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;
    }
};
  1. 奇偶树

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;
    }
};
  1. 将句子排序

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];
    }
};
  1. 最长和谐子序列

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;
    }
};
相关推荐
Lenyiin1 小时前
《 C++ 修炼全景指南:十 》自平衡的艺术:深入了解 AVL 树的核心原理与实现
数据结构·c++·stl
程序猿练习生1 小时前
C++速通LeetCode中等第5题-无重复字符的最长字串
开发语言·c++·leetcode
无名之逆2 小时前
云原生(Cloud Native)
开发语言·c++·算法·云原生·面试·职场和发展·大学期末
好蛊2 小时前
第 2 课 春晓——cout 语句
c++·算法
MogulNemenis4 小时前
力扣150题——位运算
数据结构·算法·leetcode
景小雨4 小时前
【数据结构与算法】排序算法之快速排序(简)
c++·算法·排序算法·快速排序
鸽嗷高.5 小时前
C++伟大发明--模版
开发语言·c++
weixin_486681146 小时前
C++系列-STL容器中统计算法count, count_if
开发语言·c++·算法
基德爆肝c语言6 小时前
C++入门
开发语言·c++
怀九日6 小时前
C++(学习)2024.9.18
开发语言·c++·学习·面向对象·引用·