寻找旋转数组中最小的数I和II
来源:互联网 发布:网站空间数据库上传 编辑:程序博客网 时间:2024/05/06 16:30
I:假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。
你需要找到其中最小的元素。
你可以假设数组中不存在重复的元素。
注意事项
You may assume no duplicate exists in the array.
样例
你需要找到其中最小的元素。
你可以假设数组中不存在重复的元素。
注意事项
You may assume no duplicate exists in the array.
样例
给出[4,5,6,7,0,1,2] 返回 0
II:假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。
你需要找到其中最小的元素。
数组中可能存在重复的元素。
注意事项
The array may contain duplicates.
样例
给出[4,4,5,6,7,0,1,2] 返回 0
import java.util.Scanner;/** * 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。你需要找到其中最小的元素。你可以假设数组中不存在重复的元素。 注意事项You may assume no duplicate exists in the array.样例给出[4,5,6,7,0,1,2] 返回 0 * * @author Dell * */public class Test159 { public static int findMin(int[] nums) { if(nums==null) return -1; int result=nums[0]; int i; for(i=0;i<nums.length-1;i++) { if(nums[i+1]>nums[i]) continue; else result=nums[i+1]; } return result; }public static void main(String[] args) {Scanner sc=new Scanner(System.in);int n=sc.nextInt();int[] a=new int[n];for(int i=0;i<n;i++){a[i]=sc.nextInt();} System.out.println(findMin(a));}}
import java.util.Scanner;/** * 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。你需要找到其中最小的元素。数组中可能存在重复的元素。 注意事项The array may contain duplicates.样例给出[4,4,5,6,7,0,1,2] 返回 0 * * @author Dell * */public class Test160 {public static int findMin(int[] nums) { if(nums==null) return -1; int result=nums[0]; int i; for(i=0;i<nums.length-1;i++) { if(nums[i+1]>=nums[i]) continue; else result=nums[i+1]; } return result; }public static void main(String[] args) {Scanner sc=new Scanner(System.in);int n=sc.nextInt();int[] a=new int[n];for(int i=0;i<n;i++){a[i]=sc.nextInt();} System.out.println(findMin(a));}}
阅读全文
0 0
- 寻找旋转数组中最小的数I和II
- 寻找旋转数组的最小数
- 旋转数组中最小数
- 旋转数组的最小数
- 旋转数组的最小数
- 寻找一个数组中最小的两个数
- 寻找数组中第i小的数(C++)
- leetcode 找旋转数组中最小的数
- 在一个旋转有序数组中,查找最小的数
- 1、求旋转数组的最小数
- 剑指-旋转数组的最小数
- 旋转数组的最小数(java)
- 8.旋转数组的最小数
- python求解在给定递减数组中寻找两个数和等于定值,乘积最小
- 从数组中寻找和的相加数
- 寻找数组中前K个最小的数(Kth smallest element)---(堆排序的应用)
- 寻找旋转排序数组中的最小值 II
- 寻找旋转排序数组中的最小值 II
- 智能语音是开启新世界大门的钥匙
- netty
- POJ 1008 Maya Calendar
- No1154. Easy sort
- js赋值给angularjs ng-model 数据的千分位格式
- 寻找旋转数组中最小的数I和II
- C++之成员函数指针
- HTTPS 解析
- kibana之index字段类型更新
- 什么是内存泄漏?
- 使用idea+gradle创建springboot项目
- 送给在PHP道路上迷茫的你
- 关于封装部分DOM操作的方法
- 计算机网络知识学习(1)