当前位置:网站首页>力扣每日一题-第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;
}
};边栏推荐
猜你喜欢

Huawei modelarts training alexnet model
![[target detection] yolov5, the shoulder of target detection (detailed principle + Training Guide)](/img/47/80d2e92ea7347cc5c7410194d5bf2e.png)
[target detection] yolov5, the shoulder of target detection (detailed principle + Training Guide)

Ctfhub port scan (SSRF)

TodoList经典案例①

Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money

【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)

2022年流动式起重机司机考试练习题及在线模拟考试

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

redisson使用全解——redisson官方文檔+注釋(上篇)

C # read and write customized config file
随机推荐
软件测试方法和技术 - 基础知识概括
Illusory and simple screen raindrop post-processing effect
2022 mobile crane driver test exercises and online simulation test
她就是那个「别人家的HR」|ONES 人物
Minecraft 1.16.5 module development (51) tile entity
【R语言】年龄性别频数匹配 挑选样本 病例对照研究,对年龄性别进行频数匹配
Kickback -- find the first palindrome character in a group of characters
Subclasses call methods and properties of the parent class with the same name
【无标题】
How do the top ten securities firms open accounts? In addition, is it safe to open a mobile account?
kubernetes资源对象介绍及常用命令(二)
Which securities company is better or safer for mobile phone account opening
redisson看门狗机制,redisson看门狗性能问题,redisson源码解析
[软件] phantomjs屏幕截图
[MySQL learning notes 26] view
Long way to go with technology
The programmer of Beipiao posted a post for help late at night: I am lonely when my girlfriend is gone
How to make the two financial transactions faster
浅谈CVPR2022的几个研究热点
【剑指offer&牛客101】中那些高频笔试,面试题——链表篇