回溯法和bitset Binary Watch
2017-03-28 10:38
99 查看
这个是在回溯法的标签里的。回溯法大致是这样,按照深度优先搜索,在解空间树里往前走,如果不行就回退,然后换一条路。在换路的同时剪去错枝,就是一种蛮力的有剪枝的遍历......于是我先用的回溯:
然而可以不用回溯,直接遍历所有的情况,这种情况是1的位数等于num的就塞进去......很简单.....用到了bitset。bitset<>里的是位数,后面的和别的容器意思差不多,()里是初始化。
class Solution { public: vector<string> readBinaryWatch(int num) { vector<string> re; vector<int> bit(10,1); if(num==0) { re.push_back("0:00"); return re; } int deep=0; int hour=0; int minu=0; int flag=0; while(deep>=0) { if(flag==1) //flag=1是回溯的过程,在下面往下搜索的时候,改变deep和flag,flag决定回不回溯,deep是定位的,bit[deep]是标志位。 { if(bit[deep]==1) { if(deep<4) hour-=pow(2,deep); else minu-=pow(2,deep-4); bit[deep]=0; num++; if(deep==9) //最后一个是1还得回 { flag=1; continue; } else //如果不是最后一个且后面1的位置足够,那么还可以往下走,否则还要回 { if(9-deep>=num) { deep++; bit[deep]=1; } else { continue; } } } else if(bit[deep]==0) //如果回溯的时候这个位置标志为0,那么这个位以下的情况都找遍了,还要往上回 { bit[deep]=1; --deep; continue; } flag=0; } num--; if(deep<4) { hour+=pow(2,deep); if(hour>11) { flag=1; continue; } else { deep++; } 4000 } else if(deep<10) { minu+=pow(2,deep-4); if(minu>59) { flag=1; continue; } else { deep++; } } else { deep--; flag=1; continue; } if(num==0) { deep--; string temp; stringstream ss; ss<<hour; ss>>temp; temp+=":"; string mi; ss.clear(); ss<<minu; ss>>mi; if(minu<10) temp+="0"; temp+=mi; re.push_back(temp); flag=1; } } return re; } };
然而可以不用回溯,直接遍历所有的情况,这种情况是1的位数等于num的就塞进去......很简单.....用到了bitset。bitset<>里的是位数,后面的和别的容器意思差不多,()里是初始化。
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.push_back(to_string(h)+(m<10?":0":":")+to_string(m)); } } return rs; } };
相关文章推荐
- NYOJ 题目412 Same binary weight(bitset)
- NYOJ412 Same binary weight 【bitset位操作】
- nyoj412 Same binary weight(bitset类运用)
- 401. Binary Watch*
- [leetcode][401]. Binary Watch
- LeetCode Binary Watch
- Same binary weight 位操作 bitset用法 STL
- Hihocoder1350-Binary Watch
- Boost 实用工具:noncopyable、BOOST_BINARY宏、BOOST_CURRENT_FUNCTION宏、bitset类型
- #1350 : Binary Watch(dfs)
- NYOJ 412 Same binary weight(bitset位运算)
- [LeetCode]401. Binary Watch &bitset的用法&to_string
- Same binary weight bitset(STL)
- binary-watch
- Binary Watch
- NYOJ 412 Same binary weight(bitset位运算)
- leetcode oj java Binary Watch
- watch embedded binary's bundle identifier is not prefixed with the parent app's bundle identifier.
- Binary Tree Zigzag Level Order Traversal
- Maximum Depth of Binary Tree 【leetCode】