#338.Counting Bits - Medium

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.

Example:
For num = 5 you should return [0,1,1,2,1,2].

Follow up:

  • It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
  • Space complexity should be O(n).
  • Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.

Credits:

Special thanks to @ syedee for adding this problem and creating all test cases.

my solution:

#include<vector>
#include<iostream>
using namespace std;
class Solution {
public:
vector<int> countBits(int num) {
vector<int> result;
result.push_back(0);
for (int i = 1; i <= num; i++) {
result.push_back(result[i&(i-1)] + 1);
}
return result;
}
};

这道题的主要想法是,每个数和前一个数做按位与再加一就能得到1的个数。这道题要求不高,暴力地数1的个数也能AC。。。

#413. Arithmetic Slices - Medium
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

For example, these are arithmetic sequence:

1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

The following sequence is not arithmetic.

1, 1, 2, 5, 7

A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 ⇐ P < Q < N.

A slice (P, Q) of array A is called arithmetic if the sequence:
A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.

The function should return the number of arithmetic slices in the array A.

Example:

A = [1, 2, 3, 4]

return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.

my solution:

#include<vector>
#include<iostream>
using namespace std;
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int count = 0;
vector<int> dp(A.size(), 0);
for (int i = 2; i < A.size(); i++) {
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
dp[i] = dp[i - 1] + 1;
count += dp[i];
}
}
return count;
}
};

这道题的大意是返回给定数组中不同的等差数列的个数。这道题我的解法是:一个项数为n的等差数列(n >= 3),每多一项,它的子等差数列的数量就多(n-2)项。这个子等差数列数量构成的数列也是一个等差数列。

Week 8 - 338.Counting Bits & 413. Arithmetic Slices的更多相关文章

  1. LN : leetcode 413 Arithmetic Slices

    lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...

  2. LN : leetcode 338 Counting Bits

    lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...

  3. LeetCode 413 Arithmetic Slices详解

    这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...

  4. [LeetCode]413 Arithmetic Slices

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

  5. 413. Arithmetic Slices

    /**************************Sorry. We do not have enough accepted submissions.*********************** ...

  6. 【LeetCode】338. Counting Bits (2 solutions)

    Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num  ...

  7. LeetCode - 413. Arithmetic Slices - 含中文题意解释 - O(n) - ( C++ ) - 解题报告

    1.题目大意 A sequence of number is called arithmetic if it consists of at least three elements and if th ...

  8. 【Leetcode】413. Arithmetic Slices

    Description A sequence of number is called arithmetic if it consists of at least three elements and ...

  9. LeetCoce 413. Arithmetic Slices

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

随机推荐

  1. 115-基于TI TMS320DM6467无操作系统Camera Link智能图像分析平台

    基于TI TMS320DM6467无操作系统Camera Link智能图像分析平台 1.板卡概述 该板卡是我公司推出的一款具有高可靠性.效率大化.无操作系统的智能视频处理卡,是机器视觉开发上的选.  ...

  2. 基于FPGA的以太网开发

    基于FPGA的以太网开发,在调试过的FPGA玩家开来,其实算不上很难的技术!但是如果只是菜鸟级别的选手,没有调试过的话,就有些头疼了!早在自己在实习的时候,就接触到XAUI(万兆以太网口)接口,但是由 ...

  3. 4、LayIM 开发者文档

    一.配置文档目录 1.好友列表状态 2.当前会话状态 3.查看群成员 4.业务暂无此必要 5.发送消息 6.接受消息 7.监听我主面板的在线状态 8.弹出申请好友面板(业务场景用于好友添加需申请) 9 ...

  4. 四、ARM 异常处理

    4.1 模式与异常 当正常程序流程被暂时停止发生异常,例如响应一个来自外设的中断.在处理异常前,必须保护当前的处理器状态,以便在完成处理程序后能恢复到原来的程序 . 异常的类型: Reset unde ...

  5. Bloom Filter的算法

     Bloom Filter的算法: 为了降低冲突的概念,Bloom Filter使用了多个哈希函数,而不是一个.创建一个m位BitSet,先将所有位初始化为0,然后选择k个不同的哈希函数.第i个哈希函 ...

  6. java web中乱码的种类和一些解决方式

    在java web课堂测试中遇到了一些乱码问题 ,从百度上找到了许多种解决方法和乱码的种类,在这里总结一下. 一.文件出现乱码 [右击文件]->[Properties]->[Resourc ...

  7. SpringCloud学习系列-Eureka服务注册与发现(4)

    actuator与注册微服务信息完善 1.主机名称:服务名称修改 当前问题 含有主机名称 修改修改microservicecloud-provider-dept-8001 的yml文件 修改内容 eu ...

  8. LeetCode--008--字符串转换整数 (atoi)(python)

    示例 1: 输入: "42"输出: 42示例 2: 输入: " -42"输出: -42解释: 第一个非空白字符为 '-', 它是一个负号.  我们尽可能将负号与 ...

  9. 数组Array方法: indexOf、filter、forEach、map、reduce使用实例

  10. 【bozj2287】【[POJ Challenge]消失之物】维护多值递推

    (上不了p站我要死了) Description ftiasch 有 N 个物品, 体积分别是 W1, W2, -, WN. 由于她的疏忽, 第 i 个物品丢失了. "要使用剩下的 N - 1 ...