• [LeetCode]习题记录


    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <cctype>
    #include <array>
    #include <iomanip>
    
    using namespace std;
    string count(int n);
    int main() {
        int n;
        string countout;
        cin >> n;
        countout=count(n);
        cout << """ << countout << """;
        return 0;
    }
    string count(int n) {
        string out;
        if (n == 1) {
            return "1";
        }
        else if (n == 2) {
            return "11";
        }
        else {
            string a = count(n - 1);
            int i = 1, j = 1;
            int num = a.size();
            while (i<num)
            {
                if (a[i] == a[i - 1]) {
                    j++;
                    if(i==(num-1))
                        out =out + to_string(j) + a[i];
                }
                else
                {
                    out =out + to_string(j) + a[i-1];
                    j = 1;
                    if (i == (num - 1))
                        out =out + to_string(j) + a[i];
                }
                i++;
            }
        }
        return out;
    }

     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.

     解决方法

    //法1:找到规律,用技巧解
    class Solution {
    public:
        vector<string> readBinaryWatch(int num) {
            vector<string> rs;
            for (int h = 0; h < 12; ++h) {
                for (int m = 0; m < 60; ++m) {
                    if (bitset<10>(h << 6 | m).count() == num)
                        rs.emplace_back(to_string(h) + ((m < 10) ? ":0" : ":") + to_string(m));
                }
            }
            return rs;
        }
    };
    //法2:回溯法,逐个深入解决
    class Solution 
    {
        // date: 2016-09-18     location: Vista Del Lago III
        vector<int> hour = {1, 2, 4, 8}, minute = {1, 2, 4, 8, 16, 32};
    public:
        vector<string> readBinaryWatch(int num) {
            vector<string> res;
            helper(res, make_pair(0, 0), num, 0);
            return res;
        }
        
        void helper(vector<string>& res, pair<int, int> time, int num, int start_point) {
            if (num == 0) {
                res.push_back(to_string(time.first) +  (time.second < 10 ?  ":0" : ":") + to_string(time.second));
                return;
            }
            for (int i = start_point; i < hour.size() + minute.size(); i ++)
                if (i < hour.size()) {    
                    time.first += hour[i];
                    if (time.first < 12)     helper(res, time, num - 1, i + 1);     // "hour" should be less than 12.
                    time.first -= hour[i];
                } else {     
                    time.second += minute[i - hour.size()];
                    if (time.second < 60)    helper(res, time, num - 1, i + 1);     // "minute" should be less than 60.
                    time.second -= minute[i - hour.size()];
                }
        }
    };

     法1将每一时刻穷举出来,使用bitset进行位运算,将h左移六位与m拼接起来,并计算其中1的位数与给定数字比较,若相等则存入。

    法2使用回溯法,按照DFS(深度优先搜索)规则进行带剪枝的穷举。

  • 相关阅读:
    接触C# 反射
    未能找出类型或命名空间名称“T” 问题的解决方案
    Win7 文件加密存储操作后,如何在事后备份证书、秘钥
    [转] SQL SERVER 2008 R2 安装中的账户设置问题
    centos6.5+Django+mysql+nginx+uwsgi
    JAVA解决大数
    STL algorithm算法min,min_element(35)
    Java中间MD5加密算法完整版
    IP地址和子网掩码
    加州理工大学公开课:机器学习与数据挖掘_线性模型 II(第IX类)
  • 原文地址:https://www.cnblogs.com/lightmonster/p/10315172.html
Copyright © 2020-2023  润新知