求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 ...
随机推荐
- vue项目里登录界面实现回车登录
全局监听enter键,是把监听事件绑定到document上,无需获取焦点之类的 created() { let that = this; document.onkeydown =function(e) ...
- SpringCloud---消息总线---Spring Cloud Bus
1.概述 1.1 在微服务架构的系统中,我们通常会使用 轻量级的消息代理 来 构建一个共同的消息主题 让系统中所有微服务实例都连接上来: 由于 该主题中产生的消息 会被所有实例监听和消 ...
- jacvascript 保留小数点
//四舍五入保留2位小数(若第二位小数为0,则保留一位小数) function keepTwoDecimal(num) { var result = parseFloat(num); if (is ...
- crypto-js计算文件的sha256值
1. 要在浏览器中计算出文件的sha256或md5值,基本思路就是使用HTML5的FileReader接口把文件读取到内存(readAsArrayBuffer),然后获取文件的二进制内容,然后获取文件 ...
- Oracle TM锁和TX锁
CREATE TABLE "TEST6" ( "ID" ), "NAME" ), "AGE" ,), "SEX ...
- jQuery 的动画效果图片----隐藏打开方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- css兼容小问题
1.RGBA在CSS3.0体现,不向下兼容: 2.非float元素和float元素在一起版本时,非float元素会排斥float元素,为避免换行,float元素应优先显示(放非float元素之前)
- Java Native Interface Specification Contents 翻译
https://docs.oracle.com/en/java/javase/12/docs/specs/jni/index.html Google翻译 第1章:简介 本章介绍Java Native ...
- Struts2 学习(一)
一.Struts 介绍 1.Struts2的概述 1.早期开发模型Servlet+JSP+JavaBean(Model2)显得力不从心: 流程凌乱.数据传递无序.缺乏辅助功能. 2.MVC模式的轻量级 ...
- 【转】spring boot使用Druid和监控配置
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012100371/article/details/76602612 Druid是Java语言中最好 ...