力扣练习题笔记

news/2025/2/23 1:01:55

788. 旋转数字

788. 旋转数字 - 力扣(LeetCode)

代码

class Solution {
public:
    int rotatedDigits(int n) {
        //数中没有3,4,7
        //数中至少出现一次2或5,6或9
        //对于0,1,8无要求
        int ans = 0;
        for(int i = 1; i <= n; i ++) {
            int j = i;
            int flag = 0;
            while(j) {
                int num = j%10;
                if(num==3||num==4||num==7) {flag = 0; break;}
                if(num==2||num==5||num==6||num==9) 
                    flag = 1;
                j /= 10;
            }
            if(flag) ans ++;
        }
        return ans;
    }
};

 1185. 一周中的第几天

1185. 一周中的第几天 - 力扣(LeetCode)

代码

class Solution {
public:
    string dayOfTheWeek(int day, int month, int year) {
        //1969.12.31 是星期四,只要看距离这天几天,闰年多一天

        vector<string> week = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
        vector<int> monthDays = {31,28,31,30,31,30,31,31,30,31,30}; //最后一个月不用
        // 1073 - 1069 刚好四年,因为1972年闰年多一天
        // 从1971年开始 year-1971  year - 1969因为,1972闰年,1973-1969 = 4
        //年的贡献
        int days = 365*(year - 1971) + (year - 1969)/4;
        //月的贡献
        for(int i = 0; i < month-1; i ++) {
            days += monthDays[i];
        }
        //闰年同时月份>=3
        if((year%400==0 || (year%4==0&&year%100!=0)) && month>=3) {
            days += 1;
        }
        // 天数贡献
        days += day;
        //假设1969,12,13是周一,直接%7 = 0, 右移3到周四
        return week[(days+3)%7];
    }
};

1513. 仅含 1 的子串数

1513. 仅含 1 的子串数 - 力扣(LeetCode)

代码

考虑特殊情况,假设 111 (3个全是1)

那么 子集 1  1  1 11 11 111   (3 + 2 + 1, 等差数列求和 k*(k+1)/2 )

所以每一段都是这个公式。

class Solution {
public:
    const int P = 1e9 + 7;

    int numSub(string s) {
        //字串连续选取
        int p = 0;
        long long ans = 0;

        while(p < s.size()) {
            if(s[p]=='0') {
                ++p; 
                continue;
            }

            int cnt = 0;
            while(p < s.size() && s[p] == '1') {
                ++ cnt;
                ++ p;
            }
            
            ans = ans + (1 + (long long)cnt)*cnt/2;
            ans = ans%P;
        }
        return ans;
    }
};

代码2 超时

会超时

代码3 超时

精妙之处在于l = r的更新。 

遍历寻找每一段 连续子集

然后两个for 求 个数= 执行次数

int numSub(string ss) {
        int n = ss.size();
        int l = 0,ans=0;
        while (l<n){
            if (ss[l]=='0'){
                l++;
                continue;
            }
            int r=l;
            while (r<n && ss[r]=='1'){
                r++;
            }
            //[l,r]是ss全1的子区间
            for (int s=l;s<r;s++){ // 子区间的连续子集的的左端点
                for (int e=s;e<r;e++){// 子区间的连续子集的的右端点
                    ans++;
                    cout<<ss.substr(s,e-s+1)<<endl;
                }
            }
            l = r;
        }
        return ans;
    }

2126. 摧毁小行星

 代码

long long 防爆数据

class Solution {
public:
    bool asteroidsDestroyed(int mass, vector<int>& asteroids) {
        // 每次跟最小的行星碰撞就能让自己不断吃的更大
        sort(asteroids.begin(),asteroids.end());
        long long sum = mass; // 防爆数据
        for(auto x : asteroids) {
            if(sum < x) return false;
            sum += x;
        }
        return true;
    }
};

代码2

只要比所有都大就可以结束了(大于最大)

因为最大也才1e5

class Solution {
public:
    bool asteroidsDestroyed(int mass, vector<int>& asteroids) {
        // 每次跟最小的行星碰撞就能让自己不断吃的更大
        sort(asteroids.begin(),asteroids.end());
        
        int maxx = asteroids.back();
        for(auto x : asteroids) {
            if(mass < x) return false;
            if(mass > maxx) return true;
            mass += x;
        }
        return true;
    }
};


http://www.niftyadmin.cn/n/5862890.html

相关文章

HarmonyOS NEXT 开发实战指南(基于API 12+)

一、HarmonyOS NEXT 核心特性解析 1.1 分布式能力升级 跨设备服务协同&#xff1a;通过ServiceExtensionAbility实现服务原子化拆分&#xff0c;支持设备间按需调用 // 服务提供方声明 "abilities": [{"name": "CameraService","type&qu…

vscode复制到下一行

linux中默认快捷键是ctrl shift alt down/up 但是在vscode中无法使用&#xff0c;应该是被其他的东西绑定了&#xff0c;经测试&#xff0c;可以使用windows下的快捷键shift alt down/up { “key”: “shiftaltdown”, “command”: “editor.action.copyLinesDownAction”…

【HeadFirst系列之HeadFirst设计模式】第6天之单件模式:独一无二的对象,如何优雅实现?

单件模式&#xff1a;独一无二的对象&#xff0c;如何优雅实现&#xff1f; 大家好&#xff01;今天我们来聊聊设计模式中的单件模式&#xff08;Singleton Pattern&#xff09;。如果你曾经需要确保一个类只有一个实例&#xff0c;并且这个实例能够被全局访问&#xff0c;那么…

Linux·spin_lock的使用

自旋锁 内核当发生访问资源冲突的时候&#xff0c;可以有两种锁的解决方案选择&#xff1a; 一个是原地等待一个是挂起当前进程&#xff0c;调度其他进程执行&#xff08;睡眠&#xff09; Spinlock 是内核中提供的一种比较常见的锁机制&#xff0c;自旋锁是“原地等待”的方…

Excell 代码处理

文章目录 Excell 代码处理cvc格式xlsl格式小结 Excell 代码处理 有时候要对excell进行分析&#xff0c;或者数据的导入导出&#xff0c;这个时候如果可以用代码读写分析操作那么会方便很多 cvc格式 CSV&#xff08;Comma-Separated Values&#xff0c;逗号分隔值&#xff09;是…

Visual studio 2022 将打开文件的方式由单击改为双击

1. 打开vs2022&#xff0c;选择Tools -> Options打开Options设置页面 2. 在左侧依次展开Environment, 选择Tabs and Windows 3. 在右侧面板往下拖拽滚动条&#xff0c;找到Preview Tab section, unchecked "Preview selected files in Solution Explorer (Altclick t…

pikachu之CSRF防御:给你的请求加上“网络身份证”

CSRF防御&#xff1a;给你的请求加上“网络身份证” 上集回顾 ​在上一章节中&#xff0c;我们化身“遥控黑客”&#xff0c;用GET请求和POST表单把CSRF漏洞玩得风生水起&#xff0c;体验了“隔空改签名”等骚操作。今天&#xff0c;我们将从攻击者变身防御者&#xff0c;揭秘…

MySQL八股学习笔记

文章目录 一、MySQL结构1.宏观结构1.1.Server层1.2.存储引擎层 2.建立链接-连接器3.查询缓存4.解析SQL-解析器&#xff08;1&#xff09;词法分析&#xff08;2&#xff09;语法分析 5.执行SQL5.1.预处理器 prepare5.2.优化器 optimize5.3.执行器 execute&#xff08;1&#xf…