国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【C++刷題】經(jīng)典簡單題第二輯

這篇具有很好參考價值的文章主要介紹了【C++刷題】經(jīng)典簡單題第二輯。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

  1. 回文排列
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
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;
    }
};
  1. URL化
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
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;
    }
};
  1. 配對交換
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
class Solution {
public:
    int exchangeBits(int num) 
    {
        int num1 = 0b01010101010101010101010101010101;
        int num2 = 0b10101010101010101010101010101010;
        // 獲取奇數(shù)位
        num1 &= num;
        // 獲取偶數(shù)位
        num2 &= num;

        return (num1 << 1) + (num2 >> 1);
    }
};
  1. 遞歸乘法
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
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. 階乘尾數(shù)
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
/*
* 尾數(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;
    }
};
  1. 二進制鏈表轉(zhuǎn)整數(shù)
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
class Solution
{
public:
    int getDecimalValue(ListNode* head) 
    {
        int num = 0;
        while(head != NULL)
        {
            num = (num << 1) + head->val;
            head = head->next;
        }

        return num;
    }
};
  1. 從鏈表中刪去總和值為零的連續(xù)節(jié)點
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
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. 括號的最大嵌套深度
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
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;
    }
};
  1. 整理字符串
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
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. 奇偶樹
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
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;
    }
};
  1. 將句子排序
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
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];
    }
};
  1. 最長和諧子序列
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
    【C++刷題】經(jīng)典簡單題第二輯,C/C++刷題,c++,LeetCode
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

到了這里,關(guān)于【C++刷題】經(jīng)典簡單題第二輯的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 代碼隨想錄刷題第4天|LeetCode24、LeetCode19、LeetCode160、LeetCode142

    代碼隨想錄刷題第4天|LeetCode24、LeetCode19、LeetCode160、LeetCode142

    1、LeetCode24 兩兩交換鏈表中的節(jié)點 題目鏈接:24、兩兩交換鏈表中的節(jié)點 要想清楚終止條件,cur每次指向要交換的兩個節(jié)點的前一個節(jié)點,cur = cur-next-next; 若鏈表元素個數(shù)為偶數(shù) , 則最后時刻 cur-next = NULL; 若鏈表元素個數(shù)為奇數(shù),則最后時刻 cur-next-next = NULL; 最后要返回

    2024年02月05日
    瀏覽(27)
  • 代碼隨想錄刷題第6天|哈希表 LeetCode242、LeetCode349、LeetCode202、LeetCode1

    1、LeetCode242 有效的字母異位詞 題目鏈接:242、有效的字母異位詞 用哈希表,record[s[i]-\\\'a\\\']++,record[t[i]-\\\'a\\\']--,最后判斷record里是否有元素不為0。 2、LeetCode349、兩個數(shù)組的交集 題目鏈接:349、兩個數(shù)組的交集 題目如果沒有限制數(shù)值的大小,就無法使用數(shù)組來做哈希表。如果哈

    2024年02月06日
    瀏覽(24)
  • 代碼隨想錄刷題第55天|Leetcode392判斷子序列、Leetcode115不同的子序列

    1、Leetcode392判斷子序列 題目鏈接:392判斷子序列 本題與1143最長公共子序列 有一點不一樣,最長公共子序列求 兩個字符串 的最長公共子序列的長度,本題判斷s是否為t的子序列。即t的長度是大于等于s的。 1、確定dp數(shù)組及下標(biāo)含義 dp[i][j] 表示以下標(biāo)i-1為結(jié)尾的字符串s,和以

    2024年02月16日
    瀏覽(19)
  • 代碼隨想錄刷題第48天|LeetCode198打家劫舍、LeetCode213打家劫舍II、LeetCode337打家劫舍III

    代碼隨想錄刷題第48天|LeetCode198打家劫舍、LeetCode213打家劫舍II、LeetCode337打家劫舍III

    1、LeetCode198打家劫舍 題目鏈接:198、打家劫舍 1、dp[i]:考慮下標(biāo)i(包括i)以內(nèi)的房屋,最多可以偷竊的金額為dp[i] 。 2、遞推公式: 如果偷第i房間,那么dp[i] = dp[i - 2] + nums[i] ; 如果不偷第i房間,那么dp[i] = dp[i - 1]; 然后dp[i]取最大值,即dp[i] = max(dp[i - 2] + nums[i], dp[i - 1

    2024年02月08日
    瀏覽(158)
  • 如何用matlab做高精度計算?【第二輯】

    如何用matlab做高精度計算?【第二輯】

    高精度計算是一種程序設(shè)計的算法。由于中央處理器的字長限制,如32位CPU中一個整數(shù)最大只能取值4,294,967,295(=2^32-1),因此在超范圍數(shù)值計算中,往往要采用模擬手段。通常使用分離字符的方法來處理數(shù)字?jǐn)?shù)組。 維基百科【高精度計算】 在上一輯中,給大家介紹了如何使

    2024年02月07日
    瀏覽(18)
  • C++刷題第六天 454.四數(shù)相加II 383. 贖金信 15. 三數(shù)之和 18. 四數(shù)之和

    C++刷題第六天 454.四數(shù)相加II 383. 贖金信 15. 三數(shù)之和 18. 四數(shù)之和

    給你四個整數(shù)數(shù)組 nums1、nums2、nums3 和 nums4 ,數(shù)組長度都是 n ,請你計算有多少個元組 (i, j, k, l) 能滿足: 0 = i, j, k, l n nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 這個題目是哈希表應(yīng)用的經(jīng)典題目。 如果用暴力解法,四個數(shù)組,那肯定要四層for循環(huán)嵌套,時間復(fù)雜度就是n的四次方

    2024年02月13日
    瀏覽(24)
  • Leetcode面試經(jīng)典150題刷題記錄 —— 矩陣篇

    Leetcode面試經(jīng)典150題刷題記錄 —— 矩陣篇

    Leetcod面試經(jīng)典150題刷題記錄-系列 Leetcod面試經(jīng)典150題刷題記錄——數(shù)組 / 字符串篇 Leetcod面試經(jīng)典150題刷題記錄 —— 雙指針篇 本篇 Leetcod面試經(jīng)典150題刷題記錄 —— 矩陣篇 Leetcod面試經(jīng)典150題刷題記錄 —— 滑動窗口篇 Leetcod面試經(jīng)典150題刷題記錄 —— 哈希表篇 Leetcod面試

    2024年01月16日
    瀏覽(30)
  • Leetcode面試經(jīng)典150題刷題記錄 —— 數(shù)學(xué)篇

    Leetcode面試經(jīng)典150題刷題記錄-系列 Leetcod面試經(jīng)典150題刷題記錄——數(shù)組 / 字符串篇 Leetcod面試經(jīng)典150題刷題記錄 —— 雙指針篇 Leetcod面試經(jīng)典150題刷題記錄 —— 矩陣篇 Leetcod面試經(jīng)典150題刷題記錄 —— 滑動窗口篇 Leetcod面試經(jīng)典150題刷題記錄 —— 哈希表篇 Leetcod面試經(jīng)典

    2024年01月21日
    瀏覽(28)
  • 【leetcode 力扣刷題】字符串匹配之經(jīng)典的KMP?。?!

    【leetcode 力扣刷題】字符串匹配之經(jīng)典的KMP?。?!

    以下是能用KMP求解的算法題,KMP是用于字符串匹配的經(jīng)典算法【至今沒學(xué)懂………啊啊啊】 題目鏈接:28. 找出字符串中第一個匹配項的下標(biāo) 題目內(nèi)容: 題意還是很好理解的,要在字符串haystack中查找一個完整的needle,即字符串匹配。 暴力求解就是用 兩層循環(huán) :haystack從第

    2024年02月09日
    瀏覽(33)
  • Leetcode面試經(jīng)典150題刷題記錄 —— 二叉搜索樹篇

    Leetcod面試經(jīng)典150題刷題記錄-系列 Leetcod面試經(jīng)典150題刷題記錄——數(shù)組 / 字符串篇 Leetcod面試經(jīng)典150題刷題記錄 —— 雙指針篇 Leetcod面試經(jīng)典150題刷題記錄 —— 矩陣篇 Leetcod面試經(jīng)典150題刷題記錄 —— 滑動窗口篇 Leetcod面試經(jīng)典150題刷題記錄 —— 哈希表篇 Leetcod面試經(jīng)典

    2024年01月23日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包