LN : leetcode 413 Arithmetic Slices
lc 413 Arithmetic Slices
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.
规律公式 Accepted
这个问题就是找有几个长度大于等于3的等差数列,我们知道如果有一个等差数列长为n,可将其裂为(n-1)*(n-2)/2个长度大于等于3的子数列。
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int len = 2, sum = 0;
for (int i = 2; i < A.size(); i++) {
if (A[i]+A[i-2] == 2*A[i-1]) {
len++;
} else {
if (len >= 3) sum += (len-1)*(len-2)/2;
len = 2;
}
}
if (len >= 3) sum += (len-1)*(len-2)/2;
return sum;
}
};
DP Accepted
如果用dp[i]表示到i位置为止的算数切片的个数,若能与之前两个数构成等差数列,则dp[i] = dp[i-1]+1,累加所有的dp[i]即可得到答案。
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
vector<int> dp(A.size(), 0);
int ans = 0;
for (int i = 2; i < A.size(); i++) {
if (A[i]+A[i-2] == 2*A[i-1]) dp[i] = dp[i-1]+1;
ans += dp[i];
}
return ans;
}
};
LN : leetcode 413 Arithmetic Slices的更多相关文章
- 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 Slice 算术序列切片(动态规划,暴力)
Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力) 题目描述 如果一个数组1.至少三个元素2.两两之间差值相同,那么这个数组就是算术序列 比如下面的数组都是算 ...
- 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 等差数列划分
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...
- 【Leetcode】413. Arithmetic Slices
Description A sequence of number is called arithmetic if it consists of at least three elements and ...
- 413. Arithmetic Slices
/**************************Sorry. We do not have enough accepted submissions.*********************** ...
- LeetCode 446. Arithmetic Slices II - Subsequence
原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...
随机推荐
- 找不到方法 Void Newtonsoft.Json.JsonConvert.set_DefaultSettings
找不到方法 Void Newtonsoft.Json.JsonConvert.set_DefaultSettings 因为 Newtonsoft.Json.dll 的版本号问题: C:\Program ...
- Python标准库:内置函数complex([real[, imag]])
本函数能够使用參数real + imag*j方式创建一个复数.也能够转换一个字符串的数字为复数:或者转换一个数字为复数.假设第一个參数是字符串,第二个參数不用填写.会解释这个字符串且返回复数.只是,第 ...
- Posting array of JSON objects to MVC3 action method via jQuery ajax
Does the model binder not suport arrays of JSON objects? The code below works when sending a single ...
- select标签multiple属性的用法
前些日子公司让做一个功能模块.对于里面一个小功能费了些周折,现将其总结一下: 一.实现效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2FvaHVh ...
- 常用的Sublime Text插件及安装方法
Package Control 功能:安装包管理 简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能 使用:https://sublime.wbond.net/installatio ...
- JavaScript 实现的 SHA1 散列
1.代码:/**** Secure Hash Algorithm (SHA1)* http://www.webtoolkit.info/***/ function SHA1 (msg) { ...
- c#截图工具
厚积薄发,丰富的公用类库积累,助你高效进行系统开发(6)----全屏截图.图标获取.图片打印.页面预览截屏.图片复杂操作等 俗话说,一个好汉十个帮,众人拾柴火焰高等都说明一个道理,有更多的资源,更丰富 ...
- appium的get_attribute方法
转http://blog.csdn.net/bear_w/article/details/50330753 问题描述 当使用类似下面的代码获取元素的 content-desc 属性时,会报 NoSuc ...
- Linux后门入侵检测工具,附bash漏洞解决方法
一.rootkit简介 rootkit是Linux平台下最常见的一种木马后门工具,它主要通过替换系统文件来达到入侵和和隐蔽的目的,这种木马比普通木马后门更加危险和隐蔽,普通的检测工具和检查手段很难发现 ...
- silverlight 4中datagrid列标题和列内容居中问题,增加自增长列
转载:http://www.cnblogs.com/guoyuanwei/archive/2011/01/02/1924163.html 命名空间:xmlns:Primitives="clr ...