Arithmetic Slices LT413
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.
Idea 1. How to extend the solution from A[i...j] to A[i...j+1]? dynamic programming的应用解法,试着想如果A[i..j]是arithmetic sequence,dp[j]是以A[j]结尾的arithmetic sequence的个数,那么
- class Solution {
- public int numberOfArithmeticSlices(int[] A) {
- int count = 0;
- int curr = 0;
- for(int i = 2; i < A.length; ++i) {
- if(A[i-1] - A[i-2] == A[i] - A[i-1]) {
- ++curr;
- count += curr;
- }
- else {
- curr = 0;
- }
- }
- return count;
- }
- }
python:
- class Solution:
- def numberOfArithmeticSlices(self, A: List[int]) -> int:
- curr: int = 0
- count: int = 0
- for i in range(2, len(A)):
- if A[i-1] - A[i-2] == A[i] - A[i-1]:
- curr += 1
- count += curr
- else:
- curr = 0
- return count
Idea 2. Instead of updating the accumulated sum for each consecutive sequence, only updating the sum once the longest sequence so far is found. 根据公式 curr * (curr+1)/2来计算现在这一段的arithmetic sequence的个数.
总个数 1 + 2 + 3 = (1+3)*3/2 = 6
- class Solution {
- public int numberOfArithmeticSlices(int[] A) {
- int count = 0;
- int curr = 0;
- for(int i = 2; i < A.length; ++i) {
- if(A[i-1] - A[i-2] == A[i] - A[i-1]) {
- ++curr;
- }
- else {
- count += curr * (curr + 1)/2;
- curr = 0;
- }
- }
- count += curr * (curr + 1)/2;
- return count;
- }
- }
python:
- class Solution:
- def numberOfArithmeticSlices(self, A: List[int]) -> int:
- curr: int = 0
- count: int = 0
- for i in range(2, len(A)):
- if A[i-1] - A[i-2] == A[i] - A[i-1]:
- curr += 1
- else:
- count += curr * (curr + 1)/2
- curr = 0
- count += curr * (curr + 1)/2
- return int(count)
Arithmetic Slices LT413的更多相关文章
- Arithmetic Slices II - Subsequence LT446
446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consis ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] 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
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- 413. Arithmetic Slices
/**************************Sorry. We do not have enough accepted submissions.*********************** ...
- 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: Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- [Swift]LeetCode413. 等差数列划分 | Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- LeetCode——Arithmetic Slices
Question A sequence of number is called arithmetic if it consists of at least three elements and if ...
随机推荐
- Gamma Correction
[Gamma Correction] 磁盘上存储的纹理可分为Linear Texture.Gamma Texture. sRGB sampling allows the Unity Editor to ...
- 分享一个 Java String split 快速分割的方法
java中string.split() 方法比较强大,但是split()方法采用正则表达式,速度相对会慢一点, 其实大多数场景下并不需要使用正则表达式,下面分享一个不使用正则表达式分隔字符串的方法. ...
- DRDS 概述
DRDS 概述 更新时间:2017-08-04 13:53:50 分布式关系型数据库服务(Distributed Relational Database Service , 简称 DRDS ) ...
- 修改mysql 数据库编码
查看编码 SHOW VARIABLES LIKE 'character_set_%'; 依次修改like出来的字段 例如 set character_set_results=utf8; 完了修改/e ...
- Java输入输出流详解2
InputStream/Reader:所有输入流的基类,只能从中读取数据: OutputStream/Writer:所有输出流的基类,只能向其写入数据.
- 树的子结构(python)
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) # -*- coding:utf-8 -*- # class TreeNode: # def __ ...
- centos openvpn 安装
安装环境: 系统:centos 6.5 openvpn:openvpn-2.2.1 lzo:lzo-2.09 下载地址:http://www.oberhumer.com/opensource/l ...
- 5. Longest Palindromic Substring (DP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Python+Selenium学习--键盘事件
场景 我们在实际的测试工作中,有时候需要使用tab键将焦点移动到下一个元素,用于验证元素的排序是否正确.webdriver的Keys()类提供键盘上所有的操作,甚至可以模拟一些组合键的操作,如Ctrl ...
- 【linux C】C语言中常用的几个函数的总结【一】
1.memset函数 定义变量时一定要进行初始化,尤其是数组和结构体这种占用内存大的数据结构.在使用数组的时候经常因为没有初始化而产生“烫烫烫烫烫烫”这样的野值,俗称“乱码”.每种类型的变量都有各自的 ...