刷题笔记 Binary watch

来源:互联网 发布:古代贵族生活知乎 编辑:程序博客网 时间:2024/05/04 20:24

#include <iostream>#include <vector>#include <map>using namespace std;class Solution{public:    vector<string> readBinaryWatch(int num){        vector<string> ret;        map<int,vector<int>> m;        for(int i=0;i<60;i++){            int count=0,tmp=i;            while(tmp){                tmp = tmp & (tmp-1);                count ++;            }            m[count].push_back(i);        }                for(int i=0;i<=num;i++){            vector<int> hour=m[i];                  for(int j=0;j<hour.size();j++){                                            int h=hour[j];            if(h<12){                std::vector<int> min=m[num-i];                      for(int k=0;k<min.size();k++){                      if(min[k]<10)                                ret.push_back(to_string(h)+":0"+std::to_string(min[k]));                      else                                ret.push_back(to_string(h)+":"+std::to_string(min[k]) );                      }            }                  }        }              return ret;            }          };

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”. 

二、向量的声明及初始化
    vector 型变量的声明以及初始化的形式也有许多, 常用的有以下几种形式:


        vector<int> a ;                                //声明一个int型向量a        vector<int> a(10) ;                            //声明一个初始大小为10的向量        vector<int> a(10, 1) ;                         //声明一个初始大小为10且初始值都为1的向量        vector<int> b(a) ;                             //声明并用向量a初始化向量b        vector<int> b(a.begin(), a.begin()+3) ;        //将a向量中从第0个到第2个(共3个)作为向量b的初始值


除此之外, 还可以直接使用数组来初始化向量:

        int n[] = {1, 2, 3, 4, 5} ;        vector<int> a(n, n+5) ;              //将数组n的前5个元素作为向量a的初值        vector<int> a(&n[1], &n[4]) ;        //将n[1] - n[4]范围内的元素作为向量a的初值

三、元素的输入及访问
    元素的输入和访问可以像操作普通的数组那样, 用cin>>进行输入, cout<<a[n]这样进行输出:
    示例:


 1     #include<iostream> 2     #include<vector> 3  4     using namespace std ; 5  6     int main() 7     { 8         vector<int> a(10, 0) ;      //大小为10初值为0的向量a 9 10         //对其中部分元素进行输入11         cin >>a[2] ;12         cin >>a[5] ;13         cin >>a[6] ;14 15         //全部输出16         int i ;17         for(i=0; i<a.size(); i++)18             cout<<a[i]<<" " ;19 20         return 0 ;21     }

    
    在元素的输出上, 还可以使用遍历器(又称迭代器)进行输出控制。在 vector<int> b(a.begin(), a.begin()+3) ; 这种声明形式中, (a.begin()、a.begin()+3) 表示向量起始元素位置到起始元素+3之间的元素位置。(a.begin(), a.end())则表示起始元素和最后一个元素之外的元素位置。
    向量元素的位置便成为遍历器, 同时, 向量元素的位置也是一种数据类型, 在向量中遍历器的类型为: vector<int>::iterator。 遍历器不但表示元素位置, 还可以再容器中前后移动。
    
    在上例中讲元素全部输出部分的代码就可以改写为:

    //全部输出    vector<int>::iterator t ;    for(t=a.begin(); t!=a.end(); t++)        cout<<*t<<" " ;

        
    *t 为指针的间接访问形式, 意思是访问t所指向的元素值。
    
    

 


四、向量的基本操作


      1>. a.size()                 //获取向量中的元素个数    2>. a.empty()                //判断向量是否为空    3>. a.clear()                //清空向量中的元素    4>. 复制        a = b ;            //将b向量复制到a向量中    5>. 比较        保持 ==!=>>=<<= 的惯有含义 ;        如: a == b ;    //a向量与b向量比较, 相等则返回1    6>. 插入 - insert        ①、 a.insert(a.begin(), 1000);            //将1000插入到向量a的起始位置前                ②、 a.insert(a.begin(), 3, 1000) ;        //将1000分别插入到向量元素位置的0-2处(共3个元素)                ③、 vector<int> a(5, 1) ;            vector<int> b(10) ;            b.insert(b.begin(), a.begin(), a.end()) ;        //将a.begin(), a.end()之间的全部元素插入到b.begin()前    7>. 删除 - erase        ①、 b.erase(b.begin()) ;                     //将起始位置的元素删除        ②、 b.erase(b.begin(), b.begin()+3) ;        //将(b.begin(), b.begin()+3)之间的元素删除    8>. 交换 - swap        b.swap(a) ;            //a向量与b向量进行交换

 


        
        
五、二维向量
    与数组相同, 向量也可以增加维数, 例如声明一个m*n大小的二维向量方式可以像如下形式:

        vector< vector<int> > b(10, vector<int>(5));        //创建一个10*5的int型二维向量

 
    在这里, 实际上创建的是一个向量中元素为向量的向量。同样可以根据一维向量的相关特性对二维向量进行操作。
    
    :


 1     #include<iostream> 2     #include<vector> 3  4     using namespace std ; 5  6     int main() 7     { 8         vector< vector<int> > b(10, vector<int>(5, 0)) ; 9 10         //对部分数据进行输入11         cin>>b[1][1] ;12         cin>>b[2][2] ;13         cin>>b[3][3];14 15         //全部输出16         int m, n ;17         for(m=0; m<b.size(); m++)           //b.size()获取行向量的大小18         {19             for(n=0; n<b[m].size(); n++)    //获取向量中具体每个向量的大小20                 cout<<b[m][n]<<" " ;21             cout<<"\n" ;22         }23 24         return 0;25     }



0 0
原创粉丝点击