public static int[] maxMin(int a[]) {
int[] res = new int[2];
int len = a.length;
if (len <= 0) {
return res;
}
res[0] = res[1] = a[0];
if (len % 2 == 0) {
for (int i = 0; i < len - 1; i += 2) {
if (a[i] > a[i + 1]) {
int tem = a[i];
a[i] = a[i + 1];
a[i + 1] = tem;
}
}
for (int i = 0; i < len; i += 2) {
if (res[0] > a[i]) {
res[0] = a[i];
}
}
for (int i = 1; i < len; i += 2) {
if (res[1] < a[i]) {
res[1] = a[i];
}
}
} else {
for (int i = 0; i < len - 1 - 1; i += 2) {
if (a[i] > a[i + 1]) {
int tem = a[i];
a[i] = a[i + 1];
a[i + 1] = tem;
}
}
for (int i = 0; i < len - 1; i += 2) {
if (res[0] > a[i]) {
res[0] = a[i];
}
}
for (int i = 1; i < len - 1; i += 2) {
if (res[1] < a[i]) {
res[1] = a[i];
}
}
if (res[0] > a[len - 1]) {
res[0] = a[len - 1];
} else if (res[1] < a[len - 1]) {
res[1] = a[len - 1];
}
}
return res;
}

两两比较的方法,大的在右边,小的在左边,然后在分别找最大最小值,n是偶数奇数要注意。

求n的元素的最大最小值的更多相关文章

  1. leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  2. hdu 1856 求集合里元素的个数 输出最大的个数是多少

    求集合里元素的个数 输出最大的个数是多少 Sample Input41 23 45 61 641 23 45 67 8 Sample Output42 # include <iostream&g ...

  3. 求矩阵主对角线元素的和 Exercise08_02

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年12月 * 题目:求矩阵主对角线元素的和 * */ public class Exercis ...

  4. java编程基础篇---------> 编写一个程序,从键盘输入三个整数,求三个整数中的最小值。

    编写一个程序,从键盘输入三个整数,求三个整数中的最小值. 关键:声明变量temp   与各数值比较. package Exam01; import java.util.Scanner; public ...

  5. 删除列表中重复元素以及求list中元素个数

    Python 去除列表中重复的元素 来自比较容易记忆的是用内置的set l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) print l2 还 ...

  6. 常用的函数式接口_Supplier和常用的函数式接口Supplier接口练习_求数组中元素最大值

    Supplier接口 package com.yang.Test.SupplierStudy; import java.util.function.Supplier; /** * 常用的函数式接口 * ...

  7. 【RMQ问题】求数组区间最大值,NYOJ-1185-最大最小值

    转自:http://blog.csdn.net/lilongherolilong/article/details/6624390 先挖好坑,明天该去郑轻找虐 RMQ(Range Minimum/Max ...

  8. JS基础:求一组数中的最大最小值,以及所在位置

    var arr = [0, 5, -3, 6, 2, -6, 10]; //定义一个最大值和一个最小值,把他们的索引值赋值给固定的两个变量 var maxValue = arr[0]; var min ...

  9. max_element(C++)求数组最大元素

    #include<iostream> #include<vector> #include<algorithm> using namespace std; int m ...

随机推荐

  1. 移动端滑动时页面惯性滑动overflow-scrolling: touch

    -webkit-overflow-scrolling:auto | touch; auto: 普通滚动,当手指从触摸屏上移开,滚动立即停止 touch:滚动回弹效果,当手指从触摸屏上移开,内容会保持一 ...

  2. sort sorted() reverse() reversed() 的区别

    sort()是可变对象(字典.列表)的方法,无参数,无返回值,sort()会改变可变对象,因此无需返回值.sort()方法是可变对象独有的方法或者属性,而作为不可变对象如元组.字符串是不具有这些方法的 ...

  3. window server 搭建git服务器

    Git服务器Gogs简易安装-Windows环境   1.下载git for windows 1 https://github.com/git-for-windows/git/releases/dow ...

  4. IE浏览器报Promise未定义

    用vue-cli做的项目,用了promise,结果IE下报promise未定义,甚至在比较老的andriod手机浏览器上会显示空白页面,解决方案如下: 首先安装:babel-polyfill npm ...

  5. java java-web linux C C++ 安装系统 数据结构 算法 教程分享

    qq1206630874 v18455730857 免费分享

  6. java--线程状态【转】

    1.新建状态 Thread t1 = new Thread(); 创建之后,就已经有了相应的内存和其他资源,但是还是处于不可运行状态. 2.就绪状态 当一个线程使用.start()启动之后就处于就绪状 ...

  7. 此请求的URL的长度超过配置的maxUrlLength值

    aps.net 网站的url长度默认限制为260个字符. 修改web.config即可   <system.web> <httpRuntime maxUrlLength=" ...

  8. git提交空文件夹和删除远程文件

    git提交空文件夹 在文件夹中创建 .gitkeep 文件,文件内容如下 # Ignore everything in this directory * # Except this file !.gi ...

  9. redis(3)发布订阅

    一.发布/订阅模式 在软件工程里面,发布/订阅是一种消息模式,这种模式旨在将消息发送者和消息接收者解耦.发送者不需要关心将消息发送给谁,接收者也不需要知道消息的发送者是谁.发送者将消息发布以后就结束动 ...

  10. gradle -v不是外部命令, 内部命令,或批处理文件

    安装完gradle并且配置了环境变量之后,使用windos+R,cmd 进入Dos命令gradle -v检测版本号出现了: 1 --首先找到gradle文件所在目录 一般是在C:\Users\su\. ...