LeetCode 1502 判断能否形成等差数列

题目描述链接:https://leetcode-cn.com/problems/can-make-arithmetic-progression-from-sequence/

解题思路:对于数列进行排序,然后查看排序后的数列是否满足等差数列。

LeetCode代码(冒泡排序)如下:

class Solution {
public:
    bool canMakeArithmeticProgression(vector<int>& arr) {
          int len=arr.size();
          bool flag=false;
          int temp;
          for(int i=0;i<len;++i){
              flag=false;
              for(int j=1;j<len-i;++j){
                     if(arr[j]<arr[j-1]){
                        temp=arr[j-1];
                        arr[j-1]=arr[j];
                        arr[j]=temp;
                        flag=true;
                     }
              }
              if(!flag){
                  break;
              }
          }
          for(int i=1;i<len-1;++i){
              if(2*arr[i]!=arr[i-1]+arr[i+1]){
                  return false;
              }
          }
          return true;
    }
};
原文地址:https://www.cnblogs.com/zzw-/p/13378603.html