- 题目描述:
图片说明
- 题目链接:
https://www.nowcoder.com/practice/2470140c86c6480796a487f79cf8df3a?tpId=196&&tqId=37624&rp=1&ru=/activity/oj&qru=/ta/job-code-total/question-ranking

- 设计思想:
图片说明

-视频讲解链接B站视频讲解
- 复杂度分析:
图片说明
- 代码:
c++版本:

class Solution {
public:
    /**
     * 
     * @param arr int整型vector 
     * @return int整型
     */
    int Answerofjudge(vector<int>& arr) {
        // write code here
        int n = arr.size();
        long long sum = 0;//求总和
        for(int i = 0;i < n;i ++){
            sum += arr[i];
        }
        double avg = sum * 1.0 / n;
        int cnt = 0;//用于计算<avg的数字有多少
        int num1 = 0,num2 = 1e9+10;//num1代表中位数左边,num2代表中位数右边
        for(int i = 0;i < n;i ++){
            if(arr[i] < avg){
                num1 = max(num1,arr[i]);
                cnt ++;
            }else{
                num2 = min(num2,arr[i]);
            }
        }

        if(cnt == n /2){
            if(n % 2 == 0){
                ///求偶数的中位数
                if(avg < (double)(num1 + num2) / 2) return 1;
                else if(avg == (double)(num1 + num2) / 2) return 0;
                else return -1;
            }else{
                ///求奇数的中位数
                if(avg <  (double)num2 ) return 1;
                else if(avg == (double)num2 ) return 0;
                else return -1;
            }
        }
        return cnt > n/2 ? -1 : 1;//当<avg的数字大于一半的时候说明中位数要比平均值小 



    }
};

Java版本:

import java.util.*;


public class Solution {
    /**
     * 
     * @param arr int整型一维数组 
     * @return int整型
     */
    public int Answerofjudge (int[] arr) {
        // write code here
        int n = arr.length;
        long  sum = 0;//求总和
        for(int i = 0;i < n;i ++){
            sum += arr[i];
        }
        double avg = sum * 1.0 / n;
        int cnt = 0;//用于计算<avg的数字有多少
        int num1 = 0,num2 = 1000000010;//num1代表中位数左边,num2代表中位数右边
        for(int i = 0;i < n;i ++){
            if(arr[i] < avg){
                cnt ++;
                num1 = Math.max(num1,arr[i]);
            }else{
                num2 = Math.min(num2,arr[i]);
            }
        }
        if(cnt == n /2){
            if(n % 2 == 0){
                //求偶数的中位数
                if(avg < (double)(num1 + num2) / 2) return 1;
                else if(avg == (double)(num1 + num2) / 2) return 0;
                else return -1;
            }else{
                //求奇数的中位数
                if(avg <  (double)num2 ) return 1;
                else if(avg == (double)num2 ) return 0;
                else return -1;
            }
        }
        return cnt > n/2 ? -1 : 1;//当<avg的数字大于一半的时候说明中位数要比平均值小
   }


}

Python版本:

#
# 
# @param arr int整型一维数组 
# @return int整型
#
class Solution:
    def Answerofjudge(self , arr ):
        # write code here
        n = len(arr)
        sum = 0;#求总和
        for i in range(0,n):
            sum += arr[i]
        avg = sum / n
        cnt = 0#用于计算<avg的数字有多少
        num1,num2 = 0,1000000010#num1代表中位数左边,num2代表中位数右边
        for i in range(0,n):
            if arr[i] < avg:
                num1 = max(num1,arr[i])
                cnt += 1
            else :
                num2 = min(num2,arr[i])
        if cnt == n / 2:
            if n % 2 == 0:
                #求偶数的中位数
                if avg < (num1 + num2) / 2 : return 1
                elif avg == (num1 + num2) / 2: return 0
                else :return -1
            else:
                #求奇数的中位数
                if avg <  num2: return 1
                elif avg == num2: return 0
                else :return -1
        return (-1 if cnt > n//2 else 1) #当<avg的数字大于一半的时候说明中位数要比平均值小

JavaScript版本:

/**
 * 
 * @param arr int整型一维数组 
 * @return int整型
 */
function Answerofjudge( arr ) {
    // write code here
        let n = arr.length;
        let  sum = 0;//求总和
        for(let i = 0;i < n; ++ i){
            sum += arr[i];
        }
        let avg = sum * 1.0 / n;
        let cnt = 0;//用于计算<avg的数字有多少
        let num1 = 0,num2 = 1000000010;//num1代表中位数左边,num2代表中位数右边
        for(let i = 0;i < n;++ i){
            if(arr[i] < avg){
                cnt ++;
                num1 = Math.max(num1,arr[i]);
            }else{
                num2 = Math.min(num2,arr[i]);
            }
        }
        if(cnt == n /2){
            if(n % 2 == 0){
                //求偶数的中位数
                if(avg < (num1 + num2) / 2) return 1;
                else if(avg == (num1 + num2) / 2) return 0;
                else return -1;
            }else{
                //求奇数的中位数
                if(avg <  num2 ) return 1;
                else if(avg == num2 ) return 0;
                else return -1;
            }
        }
        return cnt > n/2 ? -1 : 1;//当<avg的数字大于一半的时候说明中位数要比平均值小
}
module.exports = {
    Answerofjudge : Answerofjudge
};