寻找旋转数组中最小的数I和II
2017-06-06 19:48
381 查看
I:假设一个旋转排序的数组其起始位置是未知的(比如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
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
;
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
;
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println(findMin(a));
}
}
你需要找到其中最小的元素。
你可以假设数组中不存在重复的元素。
注意事项
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
;
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
;
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println(findMin(a));
}
}
相关文章推荐
- 寻找旋转排序数组中的最小值 II
- LintCode-寻找旋转排序数组中的最小值 II
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
- LintCode-分治-寻找旋转排序数组中的最小值 II
- 寻找旋转排序数组中的最小值 II
- lintcode:寻找旋转排序数组中的最小值 II
- 寻找旋转排序数组中的最小值 II
- lintcode --寻找旋转排序数组中的最小值 II
- lintcode-160-寻找旋转排序数组中的最小值 II
- 寻找旋转排序数组中的最小值 II
- 题目:寻找旋转排序数组中的最小值 II
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
- 寻找旋转排序数组中的最小值 II
- 寻找旋转排序数组中的最小值 II -LintCode
- LeetCode-Find Minimum in Rotated Sorted Array II-旋转排序数组找最小-二分查找
- lintcode- 寻找旋转排序数组中的最小值-159
- 寻找旋转排序数组中的最小值
- 寻找旋转数组的最小数
- 剑指Offer面试题8:寻找旋转数组的最小元素
- 字符串算法——查找有序数组旋转后最小值(有重复元素)(Find Minimum in Rotated Sorted Array II)