[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(深度优先搜索)规则进行带剪枝的穷举。

posted @ 2019-01-24 16:24  Lightmonster  阅读(122)  评论(0编辑  收藏  举报