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. iview 怎样屏蔽掉账户框自动显示账户名和密码(root,***)

    用iview框架做出的登录页面,账户名和密码显示框,会自动有占位信息(root,****) 后来解决问题发现,只要在真正的输入框下面添加这样的一行隐藏的代码,占位信息会自动填充到隐藏的input框内, ...

  2. J15W-J45W铜截止阀厂家,J15W-J45W铜截止阀价格 - 专题栏目 - 无极资讯网

    无极资讯网 首页 最新资讯 最新图集 最新标签   搜索 J15W-J45W铜截止阀 无极资讯网精心为您挑选了(J15W-J45W铜截止阀)信息,其中包含了(J15W-J45W铜截止阀)厂家,(J15 ...

  3. js控制a标签点击事件 触发下载

    问题背景,动态获取data把url赋值到a标签的url中,让a标签自动下载 首先想到的应该是$('xxx').click(), 查资料明白:js中的$(...).click()事件只能触发绑定的onC ...

  4. unittest之断言

    在测试用例中,执行完测试用例后,最后一步是判断测试结果是 pass 还是fail,自动化测试脚本里面一般把这种生成测试结果的方法称为断言(assert).用 unittest 组件测试用例的时候,断言 ...

  5. Android如何避免输入法弹出时遮挡住按钮或输入框

    在AndroidManifest.xml中为对应的activity添加android:windowSoftInputMode="adjustResize" 在AndroidMani ...

  6. Redis的安装(CentOS 7下)

    redis的官网,www.redis.io 1. 先下载 redis: wget http://download.redis.io/releases/redis-3.0.3.tar.gz 2. 解压: ...

  7. C++的中英文字符串表示(string,wstring)

    在C++中字符串类的string的模板原型是basic_string template <class _Elem, class traits = char_traits<_Elem> ...

  8. WEB下渗透测试经验技巧(全)[转载]

    Nuclear’Atk 整理的: 上传漏洞拿shell: 1.直接上传asp.asa.jsp.cer.php.aspx.htr.cdx….之类的马,拿到shell.2.就是在上传时在后缀后面加空格或者 ...

  9. 标准webservice调用

    现代定义的webservice一般都倾向于restfull风格的http请求,但工作中还是会遇到前辈们写的时代代码. 我们更倾向于封装代码来调用,而不是服务引用.请看: Service.asmx服务的 ...

  10. jquery ui dialog弹出窗 清空缓存Cache或强制刷新

    我用jquery ui 弹出一个购物车的对话,通过AJAX加载的数据.发现购物车被缓存,一直看到是旧数据.为了刷新购物车更新,我必须去加一个刷新按钮,点击后更新购物车页面.有没有一种方法来自动刷新加载 ...