[日常刷题]leetcode D31

401. Binary Watch

A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).

Each LED represents a zero or one, with the least significant bit on the right.
在这里插入图片描述
For example, the above binary watch reads “3:25”.

Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.
Example:

Input: n = 1
Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]

Note:

  • The order of output does not matter.
  • The hour must not contain a leading zero, for example “01:00” is not valid, it should be “1:00”.
  • The minute must be consist of two digits and may contain a leading zero, for example “10:2” is not valid, it should be “10:02”.

Solution in C++:

关键点:

  • 位数与数字的对应

思路:

  • 我的思路有点复杂,其实总的来说是不知道STL的使用的锅。先说下我的大概思路,首先构造分钟和时钟分别不同位数对应的不同字符串。然后根据时钟最多4位,分钟最多6位的限制,进行整数划分。其中包含了几个操作,首先是初始化了tables用于存储每个数字含有几个bit的1;bits函数用于计算num中含有bit的数目,然后根据整数划分直接读取map中的值就OK了。
  • 看到discuss里面有直接关于bit的STL bitset我真的是学到了,这题花了我一个多小时。。。今天注定刷不了几题了。

方法一:手动求num->bits

vector<int> tables;
    
    int bits(int num){
        int result = 0;
        while(num){
            ++result;
            num &= (num - 1);
        }
        return result;
    }
    
    void init(vector<int> &t){
        for(int i = 0; i <= 59; ++i)
            tables.push_back(bits(i));
    }
    
    vector<string> readBinaryWatch(int num) {
                
        init(tables);
        vector<string> result, str;
        map<int,vector<string>> hours, minutes;
        
        // 计算时钟字符串
        for(int i = 0; i <= 4; ++i){
            str.clear();
            vector<int> tmp;
            for(int j = 0; j <= 11; ++j)
                if(tables[j] == i)
                    tmp.push_back(j);
            for(auto t : tmp){
                str.push_back(to_string(t) + ":");
            }
            hours[i] = str;
        }
        
        // 计算分钟字符串
        for(int j = 0; j <= 6; ++j){
            str.clear();
            vector<int> tmp;
            for(int i = 0; i <= 59; ++i)
                if(tables[i] == j)
                    tmp.push_back(i);
            for(auto t : tmp){
                if (t < 10)
                    str.push_back("0"+to_string(t));
                else    
                    str.push_back(to_string(t));
            }
            minutes[j] = str;
        }
         
        // 整数划分 时钟部分:0-4  分钟:0-6
        for(int i = 0; i <= 4; ++i){
            int j = num - i;
            if ((j <= 6) && (j >= 0)){
                for(auto h : hours[i]){
                    for(auto m : minutes[j])
                        result.push_back(h + m);
                }      
            }
        }
        
        return result;
    }

方法二:bitset

vector<string> readBinaryWatch(int num) {
        vector<string> result;
        for(int h = 0; h < 12; ++h){
            for (int m = 0; m < 60; ++m){
                if (bitset<10>(h<<6 | m).count() == num)
                    result.emplace_back(to_string(h) + (m < 10?":0":":") + to_string(m));
            }
        }
        
        return result;
    }

405. Convert a Number to Hexadecimal

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complementmethod is used.

Note:

  1. All letters in hexadecimal (a-f) must be in lowercase.
  2. The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character '0'; otherwise, the first character in the hexadecimal string will not be the zero character.The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character ‘0’; otherwise, the first character in the hexadecimal string will not be the zero character.
  3. The given number is guaranteed to fit within the range of a 32-bit signed integer.The given number is guaranteed to fit within the range of a 32-bit signed integer.
  4. You must not use any method provided by the library which converts/formats the number to hex directly.You must not use any method provided by the library which converts/formats the number to hex directly.

Example 1:

Input:
26

Output:
"1a"

Example 2:

Input:
-1

Output:
"ffffffff"

Solution in C++:

关键点:

  • 计算机中数的表示

思路:

  • 看了2的补码wiki,得到计算负数 n u m num num的补码的数值大小为 2 n + n u m 2^n+num 2n+num,但是int的范围是[- 2 31 2^{31} 231, 2 31 − 1 2^{31}-1 2311],所以这里表示的时候需要通过long long类型的变量来转存。这里的思路就是小于0时令变量为 2 n + n u m 2^n+num 2n+num否则为 n u m num num即可。其他按进制转换操作来。
  • 看到discuss里面很多人都是通过强制类型转换为unsigned int来实现的。

方法一: 2 n + n u m 2^n+num 2n+num

string toHex(int num) {
        
        if (num == 0)
            return "0";
        
        string result; 
        long long tmp = num;
        
        // 负数
        if (num < 0){
             tmp = pow(2,32) + num;
        }
           
        while(tmp){
            int n = tmp % 16;
            if (n >= 10){
                char ch = 'a' + n - 10;
                result = ch + result;
            } else{
                result = to_string(n) + result;
            }
            tmp /= 16;
        }    
            
        return result;
    }

方法二:强制类型转换为unsigned

string helper(unsigned int num){
        string result; 
   
        while(num){
            int n = num % 16;
            if (n >= 10){
                char ch = 'a' + n - 10;
                result = ch + result;
            } else{
                result = to_string(n) + result;
            }
            num /= 16;
        }
    
        return result;
    }
    
    string toHex(int num) {
        return helper(num); 
    }

小结

哎,今天成就感蛮低的,首先题目数量刷的不多意味着每题的耗时比较长,然后就是基本没完全做出来,不过好在写博客的时候自己写的代码都调试出来了。不然真的是满满的挫败感啊。

知识点

  • bit-num映射 及 bitset
  • 补码及强制类型转换unsigned
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 是一种流行的高级编程语言,因其简洁易读的语法和广泛的应用领域而受到开发者喜爱。LeetCode 是一个在线编程平台,专门用于算法和技术面试的准备,提供了大量的编程题目,包括数据结构、算法、系统设计等,常用于提升程序员的编程能力和解决实际问题的能力。 在 Python 中刷 LeetCode 题目通常涉及以下步骤: 1. **注册账户**:首先在 LeetCode 官网 (https://leetcode.com/) 注册一个账号,这样你可以跟踪你的进度和提交的代码。 2. **选择语言**:登录后,在个人主页设置中选择 Python 作为主要编程语言。 3. **学习和理解题目**:阅读题目描述,确保你理解问题的要求和预期输出。题目通常附有输入示例和预期输出,可以帮助你初始化思考。 4. **编写代码**:使用 Python 编写解决方案,LeetCode 提供了一个在线编辑器,支持实时预览和运行结果。 5. **测试和调试**:使用给出的测试用例来测试你的代码,确保它能够正确地处理各种边界条件和特殊情况。 6. **提交答案**:当代码完成后,点击 "Submit" 提交你的解法。LeetCode 会自动运行所有测试用例并显示结果。 7. **学习他人的代码**:如果遇到困难,可以查看社区中的其他优秀解法,学习他人的思路和技术。 8. **反复练习**:刷题不是一次性的事情,通过反复练习和优化,逐渐提高解题速度和代码质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值