#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. 在cmd下用cd怎么进不了其他的盘

    你当前就是在C盘目录下的,可以切换到别的盘比如D:,然后在切换E:!然后可以切换C:,然后可以用cd\回到根目录. cd是打开文件根目录里面文件夹的,比如C:目录下可以cd Windows打开Wind ...

  2. Laravel 学习笔记之数据库操作——Eloquent ORM

    1. 时间戳 默认情况下在使用ORM操作数据库进行添加.修改数据时, created_at 和 updated_at列会自动存在于数据表中,并显示的是 ‘2017’格式,如果想以 Unix时间戳格式存 ...

  3. 行内元素(例如)设置float之后才能用width调整宽度

    因为只有块元素才会有物理属性,在css世界里边,有三种形态的东西, 1. 块元素. 特性:有物理属性,width,height写值起作用,而且要占据一行.2. 内联元素. 特性:没有物理属性.但是ma ...

  4. netperf编译./configure时报错 "error: cannot guess build type;you nust specify one"

    问题: 解决办法-亲测可用: 尝试:./configure --build=mingw提示无法辨别 checking build system type... Invalid configuratio ...

  5. JavaEE高级-MyBatisPlus学习笔记

    第 1 章 简介 1.1 MyBatisPlus 介绍 -MyBatis-Plus(简称 MP),是一个 MyBatis 的增强工具包,只做增强不做改变. 为简化开发工作.提高生产率而生我们的愿景是成 ...

  6. MFC学习笔记2---简单计算器

    前言 学习了鸡啄米网页的前三部分后,我们就可以做一个小软件出来了,我选择先做一个计算器. 这是Win7系统自带的计算器: 为了提升成就感,我将计算器的大部分内容去除,于是就变成这样: 这样就只剩下了1 ...

  7. Keycode含义

    keycode 是键盘上每一个按键对应的码keycode如下 :keycode 0 = keycode 1 = keycode 2 = keycode 3 = keycode 4 = keycode ...

  8. 最小可观(Minimal Observability Problem in Conjunctive Boolean Networks)

    论文链接 1. 什么是 conjunctive Boolean network (CBN) 仅仅包含and运算. 下面这个式子为恒定更新函数 2. 什么是可观 定义在时刻k是CBN的状态为 X(k) ...

  9. C++中vecotr表示二维数组并自己实现一个Grid类

    1 C++中使用vector来表示二维数组 声明一个二维数组: vector<vector<int>> dp(row, vector<int>(col)); 将变量 ...

  10. Django数据库查询优化与AJAX

    目录 数据库设计三大范式 orm相关的数据库查询优化 惰性查询 all.only与defer select_related与prefetch_related MTV与MVC模型 MTV(models ...