413. Arithmetic Slices
/**************************Sorry. We do not have enough accepted submissions.************************/
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.
http://www.cnblogs.com/rgvb178/p/5967894.html
刚开始看这道题的时候我还挺纠结的,然后发现其实是我对题意的理解有问题,或者说题目本身sample给的也不好。
(1)首先,题目输入的数列是0索引的数组,注意,这个输入的数组的各个数字不一定是等差数列,比如说可能输入的是{1,2,5,6}。
(2)所求的P+1<Q,也就是说所求出的等比数列中至少包含3个元素,比如{1,2,3,4}中{1,2}就不算是arithmetic。
(3)所求的arithmetic在原数组中要求是位置连续的,比如原数组若给定的是{1,2,4,3},这里的{1,2,3}在原数组中并不连续,因此会返回0。而{1,3,5,6}中{1,3,5}位置则是连续的。
再举几个例子:
输入:{1,2} 返回0 。因为没有3个以上的元素。
输入:{1,3,5,6,7} 返回2。因为有{1,3,5}和{5,6,7}。而{1,3,5,7}则不算,因为它们在原数组里不连续。
输入:{1,3,5} 返回1。
http://blog.csdn.net/camellhf/article/details/52824234
//https://discuss.leetcode.com/topic/62992/3ms-c-standard-dp-solution-with-very-detailed-explanation
class Solution {
// 2nd round date: 2016-10-15 location: Vista Del Lago III Apartement
public:
int numberOfArithmeticSlices(vector<int>& A) {
) ;
vector<);
;
; i < A.size(); i ++) {
] == A[i - ] - A[i - ])
dp[i] = dp[i - ] + ;
res += dp[i];
}
return res;
}
};
//https://discuss.leetcode.com/topic/62162/3ms-question-maker-solution-in-cpp-o-n-time-and-in-space
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
) ;
int size = (int) A.size();
; i < size - ; i++) {
A[i] = A[i + ] - A[i];
}
A.resize(size - );
size--;
;
;
; i < size; i++) {
]) {
res += len * (len - ) / ;
len = ;
} else {
len++;
}
}
) res += len * (len - ) / ;
return res;
}
};
int numberOfArithmeticSlices(int* A, int ASize) {
;
;
) ;
; i < ASize-; ++i) {
] == A[i+]-A[i]){
cnt += ending;
++ending;
}else {
ending = ;
}
}
return cnt;
}
413. Arithmetic Slices的更多相关文章
- LN : leetcode 413 Arithmetic Slices
lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...
- Week 8 - 338.Counting Bits & 413. Arithmetic Slices
338.Counting Bits - Medium Given a non negative integer number num. For every numbers i in the range ...
- LeetCode 413 Arithmetic Slices详解
这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...
- [LeetCode]413 Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- 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 ...
- 【Leetcode】413. Arithmetic Slices
Description A sequence of number is called arithmetic if it consists of at least three elements and ...
- LeetCoce 413. Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- LC 413. Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- 【LeetCode】413. Arithmetic Slices 等差数列划分
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...
随机推荐
- Android学习笔记(六)——活动的启动模式
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 活动的启动模式共有四种: standard.singleTop.singleTask 和 singleInst ...
- mac 系统通用快捷键(mac 下的应用多数会往这些标准看齐)(转:http://yang3wei.github.io/blog/2013/02/08/chen-ni-yu-mac-chen-ni-yu-xcode/)
command + w: 关闭当前窗口 command + q: 退出程序 (Google Chrome 有点奇葩,按下之后还需要hold 那么一小会儿才能退出) command + m: 最小化当前 ...
- MATLAB命令大全
一.常用对象操作:除了一般windows窗口的常用功能键外.1.!dir 可以查看当前工作目录的文件. !dir& 可以在dos状态下查看.2.who 可以查看当前工作空间变量名, whos ...
- 有关lucene的问题
1.异常:Lock obtain timed out: NativeFSLock 原因:没有及时关闭indexWriter或者indexReader,lucene进行读写的时候会在文件夹里面创建loc ...
- 关于在android 4.2.2 上运行runlmbench
在剑锋的基础上加一些自己笔记,让自己更懂一些这个流程. 参考:http://www.cnblogs.com/zengjfgit/p/5731655.html runlmbench 是一款在linux ...
- 编写Delphi控件属性Stored和Default的理解及应用
property ButtonSize: Integer read FButtonSize write SetButtonSize default 0; property Color: TCol ...
- HDU——PKU题目分类
HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...
- MFC----任务管理器的制作
首先建立一个MFC项目,因为进程有多个并且是动态的,所以可以看做链表,获得头结点&&依次向下遍历: 首先 我们使用CreateToolhelp32Snapshot提取出进程表,之后
- good luck
ACM大法好,明天求轻虐,水一波~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Tomcat部署Web应用的两种方式
WEB工程目录结构 部署方式一:此种方式部署,jsp页面修改后,不能动态更新,需要重新部署才能看到效果.不过可以配置动态更新实现. 部署方式二:此种方式部署,jsp修改后,直接在页面可以看到效果.(因 ...