当前位置:网站首页>力扣每日一题-第31天-1502.判断能否形成等差数列
力扣每日一题-第31天-1502.判断能否形成等差数列
2022-07-01 07:46:00 【重邮研究森】
2022.6.30今天你刷题了吗?
题目:
给你一个数字数组 arr 。
如果一个数列中,任意相邻两项的差总等于同一个常数,那么这个数列就称为 等差数列 。
如果可以重新排列数组形成等差数列,请返回 true ;否则,返回 false 。
分析:
给你一个数组,判断数组元素是否构成了等差数列,注意,这里要求顺序的!
思路:这里不能用求和公式是因为一些元素满足求和但不是等差,所以我们利用等差的另外一个性质
2*a(n2)=a(n1)+a(n3)
也就是中间项的两倍=前后项的和
解析:
1.暴力求解
class Solution {
public:
bool canMakeArithmeticProgression(vector<int>& arr) {
sort(arr.begin(), arr.end());
for (auto i = 1; i < arr.size()-1 ; i++)
{
if (2 * arr[i] != arr[i - 1] + arr[i + 1])
{
return false;
}
}
return true;
}
};边栏推荐
- 2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation
- C language implementation [minesweeping game] full version (implementation source code)
- matlab保存DB4i深度相机图片
- 漏刻有时API接口实战开发系列(14):身份证实名鉴权验证
- MySQL and redis consistency solution
- PWN攻防世界int_overflow
- 热烈祝贺五行和合酒成功挂牌
- 软件测试方法和技术 - 基础知识概括
- [MySQL learning notes 25] SQL statement optimization
- iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
猜你喜欢
![[programming training] delete public characters (hash mapping) + team competition (greedy)](/img/cd/63eb9da1e8956df0763797f079b67f.png)
[programming training] delete public characters (hash mapping) + team competition (greedy)

Will Internet talents be scarce in the future? Which technology directions are popular?

2022 tea master (intermediate) recurrent training question bank and answers

Apple账号密码自动填充

redisson使用全解——redisson官方文档+注释(中篇)

【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案

2022年茶艺师(中级)复训题库及答案

2022危险化学品经营单位主要负责人试题及模拟考试

LeetCode+ 71 - 75
![[Shenzhen IO] precise Food Scale (some understanding of assembly language)](/img/8c/1e64401e812538d8606df557054355.png)
[Shenzhen IO] precise Food Scale (some understanding of assembly language)
随机推荐
Gui Gui programming (XV) - use scale to control font size changes
[MySQL learning notes 25] SQL statement optimization
长路漫漫、技术作伴
Challenges faced by operation and maintenance? Intelligent operation and maintenance management system to help you
[R language] two /n data merge functions
Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?
Conscience Amway universal wheel SolidWorks model material website
1286_ Implementation analysis of task priority setting in FreeRTOS
Vscode automatically formats code according to eslint specification
TCP/UDP 通信问题整理
Oracle create auto increment ID
Vhost kick & call principle
Kdtree notes
【mysql学习笔记26】视图
MATLAB之基础知识
The computer has a network, but all browser pages can't be opened. What's the matter?
I bet on performance and won the CTO of the company. I want to build Devops platform!
如何让两融交易更极速
She is the "HR of others" | ones character
Alibaba OSS postman invalid according to policy: policy condition failed: ["starts with", "key", "test/"]