leetcode第一题~~~Counting Bits
来源:互联网 发布:pro怎么拆卸软件 编辑:程序博客网 时间:2024/06/07 22:14
听说找工作最好刷一下这个,于是刷了一下。
主要是前几天面试的时候面试小哥说我数据结构学得不咋地。。。。
其实还是更喜欢POJ。
直接上代码了,觉得这题就是一个简单的动态规划嘛~~~~
#include<iostream>
#include<cmath>
#include<vector>
#define max 32
using namespace std;
class Solution {
public:
int arr[max];
void initarr(int num)
{
for(int i=0;i<=max-1;i++ )
{
arr[i]=pow(2,i);
if(arr[i]>=num)
return ;
}
}
vector<int> countBits(int num) {
vector<int> v;
int i,j;
// initarr(num);
v.push_back(0);
cout<<"[0,";
int *result=new int[num+1];
for(i=1,j=0;i<=num;i++)
{
if(i==pow(2,j))
{
result[i]=1;
// j++;
}
else if (i == pow(2, j + 1))
{
result[i] = 1;
j++;
}
else
{
int t=i-pow(2,j);
result[i]=1+result[t];
}
}
for(i=1;i<=num;i++)
{
v.push_back(result[i]);
if(i!=num)
cout<<result[i]<<",";
else
{
cout<<result[num]<<"]";
}
}
// v.push_back(result[num]);
// v.push(result[num]);
delete result;
return v;
}
};
- leetcode第一题~~~Counting Bits
- leetcode:bits:Counting Bits(338)
- LeetCode 第 338 题 (Counting Bits)
- leetcode算法题总结之Counting Bits
- 【leetcode】经典算法题-Counting Bits
- [leetcode] 338. Counting Bits
- 【LeetCode-338】Counting Bits
- leetcode-338-Counting Bits
- leetcode:Counting Bits
- [C]LeetCode:Counting Bits
- LeetCode Counting Bits
- leetcode 338. Counting Bits
- leetcode Counting Bits
- [LeetCode]338. Counting Bits
- LeetCode#338. Counting Bits
- Leetcode 338 Counting Bits
- [LeetCode] 338. Counting Bits
- LeetCode之Counting Bits
- 英特尔® 硬件加速执行管理器安装指南 — Microsoft Windows*
- POJ 1284 Primitive Roots 原根个数
- Qt5以上QMYSQL driver not loaded问题
- TopCoder SRM606 DIV1 250
- 中文分词技术(中文分词原理)
- leetcode第一题~~~Counting Bits
- iOS开发-通过btn传递两个实例对象 firstObject和secondObject
- 请求编码
- C++ 个人所得税
- 图示经典算法--选择排序
- 状态模式
- ClassLoader源码
- Reverse Nodes in k-Group
- C#读取CSV文件到DataTable