Leetcode的个人记录

博主分享了在LeetCode上练习C++和C#语言的心得,与朋友一起做题逐渐进步。文章中提到了一些题目解题策略,如字符串操作、递归、动态规划等,并特别提到了一些算法的应用,如KMP算法、滑动窗口、二叉树遍历等。
摘要由CSDN通过智能技术生成

记录一些自己觉得需要注意的小点。
目前主要练习c++ c#。

和闺女约了一起做leetcode,慢慢地开始长进。
我没有闺女,这么可爱的女孩纸当然是别人家的宝贝女儿啦,闺女是我朋友的外号。

  • 1103 分糖果 II
//c++
class Solution {
   
public:
    vector<int> distributeCandies(int candies, int num_people) {
   
        vector <int> ans(num_people,0);
        int candiesToPeople = 0;
        int peopleNo = 0;
        while(candies - candiesToPeople>0){
   
            candies -= ++candiesToPeople;
            ans[peopleNo++%num_people] += candiesToPeople;
        }
        ans[peopleNo%num_people] += candies;
        return ans;
    }
};

主要是复习一下 i++,++i 的区别与用法。

  • 168 Excel表列名称
//c++
class Solution {
   
public:
    string convertToTitle(int n) {
   
        string ans="";
        while(n>0){
   
            if(n%26>0)
            {
   
                ans = char(n%26 -1 + 'A') + ans;
                n = n / 26;
            }
            else
            {
   
                ans = 'Z' + ans;
                n=(n-26)/26;
            }
        }
        return ans;
    }
};

一般情况下用的是 string += char把char加到string的末尾,但是这样本体就需要做一个字符串翻转,而string类里面已经重载了+,可以直接用char + string构成新的string字符串。

  • 171 Excel表列序号
//c++
class Solution {
   
public:
    int titleToNumber(string s) {
   
        int res = 0;
        for(char c : s){
   
            res = res * 26 + (c - 'A' + 1);
        }
        return res;
    }
};

比起168,同样作为简单题的171似乎没有什么值得注意的。

  • 172 阶乘后的零
//c++
class Solution {
   
public:
    int trailingZeroes(int n) {
   
        /*int res = 0;
        while(n){
            res += n/5;
            n /= 5;
        }
        return res;*/

        return n>0?n/5+trailingZeroes(n/5):0;

    }
};

在评论中看到了一个用递归调用写的,看起俩非常的精简,记录一下。

  • 189 旋转数组
//c++
class Solution {
   
public:
    void rotate(vector<int>& nums, int k) {
   
        k = k % nums.size();
        if(k<=0) return;
        int mSize = nums.size();
        vector<int> newNums(mSize,0);
        for(int i=0; i<mSize; i++){
   
            newNums[(i+k)%mSize] = nums[i];
        }
        nums = newNums;
    }
};

这个题自己写的就很暴力,浪费空间,并不太好,记录一下非常经典的三次翻转的做法hhhhhh

n=7 k=3
原始数组 : 1 2 3 4 5 6 7
反转所有数字后 : 7 6 5 4 3 2 1
反转前 k 个数字后 : 5 6 7 4 3 2 1
反转后 n-k 个数字后 : 5 6 7 1 2 3 4

  • 121 买卖股票的最佳时机
//c++
class Solution {
   
public:
    int maxProfit(vector<int>& prices) {
   
        if(prices.size()<=0) return 0;
        int minPrice = prices[0];
        int res = 0;
        for(int curPrice : prices)
        {
   
            if(curPrice < minPrice)
            {
   
                minPrice = curPrice;
                continue;
            }
            if(curPrice - minPrice > res){
   
                res = curPrice - minPrice;
            }
        }

        /*for(int i = 0; i<prices.size(); ++i)
        {
            for(int j = i; j<prices.size(); ++j)
            {
                if(prices[j]-prices[i]>res)
                res = prices[j]-prices[i];
            }
        }*/

        return res;
    }
};
  • 543 二叉树的直径
//c++
class Solution {
   
public:
    int depthOfTree(TreeNode * root){
   
        if(!root) return 0;
        return 1+max(depthOfTree(root->left),depthOfTree(root->right));
    }

    int diameterOfBinaryTree(TreeNode* root) {
   
        if(!root) return 0;
        int res = depthOfTree(root->left)+depthOfTree(root->right);
        res = max(diameterOfBinaryTree(root->left),res);
        res = max(diameterOfBinaryTree(root->right),res);
        return res;
    }
};

一个暴力的遍历。

  • 204 计数质数
//c++
class Solution {
   
public:
    /*bool isPrimes(int & n) {
        if( n == 2 ) return true;
        if(n>2 && n%2!=0){
            int i = 2;
            int cap = sqrt(n)+1;
            for(i = 2; i< cap; i++)
            {
                if( n % i == 0)
                break;
            }
            if(i == cap ) return true;
        }
        return false;
    }
    int countPrimes(int n) {
        int cnt =0 ;
        for(int i=1;i<n;i++){
            if(isPrimes(i)){
                cnt ++ ;
            }
        }
        return cnt;
    }*/

    int countPrimes(int n)
    {
   
        vector <bool> isPrimes(n+1,true); 
        int cnt = 0;
    
        for(int i = 2; i < n; i++)
            if(isPrimes[i])
            {
   
                cnt++;
                for(int j = 2 * i; j < n; j += i)
                    isPrimes[j] = false;
            }
    
        return cnt;
    }
};

码一下厄拉多塞筛法。还可以用bitmap来做。

  • 1013 将数组分成和相等的三个部分
//c++
class Solution {
   
public:
    bool canThreePartsEqualSum(vector<int>& A) {
   
        int i=0,j=0;
        int sum=0;
        int sumDividedBy3=0;
        int yushu=32766;
        int sum1=0;
        int sum2=0;
        int sum3=0;

        for(i=0;i<A.size();++i) {
   
            sum=sum+A[i];
        }
        yushu=sum%3;
        //sum=sum/3;
        sumDividedBy3 = sum/3;
        if(yushu!=0) return false;
        for(i=0;i<A.size()-2;++i) {
   
            sum1=sum1+A[i];
            if(sumDividedBy3 == sum1)
            break;
        }
        for(j=i+1;j<A.size()-1;++j) {
   
            sum2=sum2+A[j];
            if(sumDividedBy3 == sum2)
            break;
        }
        sum3=sum-sum1-sum2;
        if(sum1==sum2 && sum2==sum3)
        {
      return true;}
        return false;

    }
};

总觉得这个版本好像不是我写那个,嘛,不管了,就先马克一下hhhh

  • 202 快乐数
//c++
class Solution {
   
public:
    int squareSum(int n){
   
        int res = 0;
        while(n>0){
   
            res += (n%10)*(n%10);
            n /= 10;
        }
        return res;
    }
    bool isHappy(int n) {
   
        int fast = n;
        int slow = n;
        do{
   
            slow = squareSum(slow);
            if(slow == 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值