求n的元素的最大最小值
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的元素的最大最小值的更多相关文章
- 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 ...
- hdu 1856 求集合里元素的个数 输出最大的个数是多少
求集合里元素的个数 输出最大的个数是多少 Sample Input41 23 45 61 641 23 45 67 8 Sample Output42 # include <iostream&g ...
- 求矩阵主对角线元素的和 Exercise08_02
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年12月 * 题目:求矩阵主对角线元素的和 * */ public class Exercis ...
- java编程基础篇---------> 编写一个程序,从键盘输入三个整数,求三个整数中的最小值。
编写一个程序,从键盘输入三个整数,求三个整数中的最小值. 关键:声明变量temp 与各数值比较. package Exam01; import java.util.Scanner; public ...
- 删除列表中重复元素以及求list中元素个数
Python 去除列表中重复的元素 来自比较容易记忆的是用内置的set l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) print l2 还 ...
- 常用的函数式接口_Supplier和常用的函数式接口Supplier接口练习_求数组中元素最大值
Supplier接口 package com.yang.Test.SupplierStudy; import java.util.function.Supplier; /** * 常用的函数式接口 * ...
- 【RMQ问题】求数组区间最大值,NYOJ-1185-最大最小值
转自:http://blog.csdn.net/lilongherolilong/article/details/6624390 先挖好坑,明天该去郑轻找虐 RMQ(Range Minimum/Max ...
- JS基础:求一组数中的最大最小值,以及所在位置
var arr = [0, 5, -3, 6, 2, -6, 10]; //定义一个最大值和一个最小值,把他们的索引值赋值给固定的两个变量 var maxValue = arr[0]; var min ...
- max_element(C++)求数组最大元素
#include<iostream> #include<vector> #include<algorithm> using namespace std; int m ...
随机推荐
- java的值传递
java中都是值传递 如果参数是引用类型的,实际上是把引用地址复制了一份传递 例如 User user = new User(); user存放在栈中,里面存放着内存地址,new User()会在堆中 ...
- 在Vue项目中使用html2canvas生成页面截图并上传
使用方法 项目中引入 npm install html2canvas html代码 //html代码 <!-- 把需要生成截图的元素放在一个元素容器里,设置一个ref --> <di ...
- oracle--块信息深入解析
一,创建 Data Block是数据库中最小的I/O单元 01,建立一个新的表空间 查看默认表空间位置 select TABLESPACE_NAME,FILE_NAME from dba_data_f ...
- hibernate 自动创建表中文乱码问题
<property name="connection.url" > <![CDATA[jdbc:mysql:///test?useUnicode=true& ...
- Android Studio的串口通讯开发
基于android-serialport-api实现 前言RS232标准接口UARTRS232与UART转接下载 NDK 和构建工具创建支持 C/C++ 的新项目编译C/C++代码串口通讯原理关于校验 ...
- StringUtils 中 isEmpty 和 isBlank 的区别
在项目的工作学习中经常用到了 apache commons 中的 StringUtils 的 isBlank 和 isEmpty 来判断字符串是否为空,这个方法都是判断字符串是否为空做判断的,以至于 ...
- CCF 201412-4 最优灌溉
问题描述 试题编号: 201412-4 试题名称: 最优灌溉 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖了一口很 ...
- C#操作Redis List 列表
/// <summary> /// Redis 列表 /// </summary> public static void Redis_List() { RedisClient ...
- Windows开启telnet命令
1.点击开始 → 运行 → 输入telnet,回车. 2.点击启用或关闭Windows功能 3.找到Telnet客户端,勾选,点击确认 4.搞定,测试一下 打开CMD,在出来的DOS界面里,输入tel ...
- [转]ORA-00979: not a GROUP BY expression报错处理
本文转自:http://blog.itpub.net/29154652/viewspace-772504/ 环境:Oracle Database 11gR2(11.2.0.2) on Linux 故 ...