- 寻找数组中的最小值与最大值
/**
* 维持两个变量min和max,min标记最小值,max标记最大值,每次比较相邻的两个数,较大者与max比较
* 较小者与min比较,通过比较找出最大值和最小值。
*/
public class GetMaxAndMin {
static int Max;
static int Min;
public static void GetMaxAndMin(int arr[]) {
Max = arr[0];
Min = arr[0];
int len = arr.length;
for (int i = 1; i < len - 1; i = i + 1) {
if (arr[i] > arr[i + 1]) {
if (arr[i] > Max)
Max = arr[i];
if (arr[i + 1] < Min)
Min = arr[i + 1];
}
if (arr[i] < arr[i + 1]) {
if (arr[i + 1] > Max)
Max = arr[i + 1];
if (arr[i] < Min)
Min = arr[i];
}
// if (i + 1 > len) {
// if (arr[i] > Max)
// Max = arr[i];
// if (arr[i] < Min)
// Min = arr[i];
// }
}
}
public static void main(String[] args) {
int[] array = { 7,3, 19, 40, 4, 7, 1,100,120,0};
GetMaxAndMin(array);
System.out.println("max = " + Max);
System.out.println("min = " + Min);
}
}
- 找出数组中第二大的数
思路如下:定义两变量个,一个变量用来存储数组的最大数,初始值为数组首元素,另一个变量用来存储数组元素的第二个最大数,初始值为最小负整数,然后遍历数组元素,最大数变量的值更新该数组元素的值,若数元素的值比最大数的值小,泽判断该数组元素的值是否比第二数大;若数组元素的值比最大数的值大。则更新第二数的值为该数组元素的值。
代码如下:
public class SecondMax {
public static int FindSecondMax(int data[]) {
int count = data.length;
int maxNumber = data[0];
int sec_data = Integer.MIN_VALUE;
for (int i = 1; i < count; i++) {
if (data[i] > maxNumber) {
sec_data = maxNumber;
maxNumber = data[i];
} else {
if (data[i] > sec_data) {
sec_data = data[i];
}
}
}
return sec_data;
}
/**
* @param args
*/
public static void main(String[] args) {
int[] array = { 7, 3, 19, 40, 7, 1, };
System.out.println(FindSecondMax(array));
}
}
- 求最大子数组之和
问题描述:一个有n个元素的数组。这n个元素可以是正数也可以是负数,数组中连续的一个或者多个元素可以组成一个连续的子数组,一个数组可能有多个连续的子数组,求子数组的最大值,例如:对于数组{1,-2,4,8,-4,7,-1,-5}而言,其最大和的子数组为{4,8,-4,7},最大值为15。
代码如下:PS因为博主比较笨只能掌握蛮力破解,即找出所有的子数组,然后求出子数组的和,在所有的子数组的和中取最大值。
public class maxSubArray {
public static int maxSubArrayTest(int arr[]) {
int n = arr.length;
int maxSum = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = i; j < n; j++) {
sum += arr[j];
if (sum > maxSum)
maxSum = sum;
}
}
return maxSum;
}
public static void main(String[] args) {
int array[] = { 1, -2, 4, 8, -4, 7, -1, -5 };
System.out.println(maxSubArrayTest(array));
}
}
- 找出数组中重复元素最多的数
问题描述:对于数组{ 1, 5, 4, 3, 4, 4, 5, 4, 5, 5, 6, 6, 6, 6, 6 };元素1出现的次数为2次,元素3出现的次数为1次,元素4出现的次数为4次,元素5出现的次数为4次,元素6出现的次数为5次,找出数组中出现重复次数最多的数
思路如下:使用Map映射表,通过引入Map映射表(Map提供一对一的数据处理能力,其中第一个为关键字,每个关键字只能在Map中出现依次,第二个称为关键字的值)来记录每一个元素出现的次数,然后判断此数大小。进而找出重复次最多的元素。PS:不懂的话,看看代码,很好理解
public class findMostFrequentInArray {
public static int method(int array[]) {
int result = 0;
int size = array.length;
if (size == 0) {
return Integer.MAX_VALUE;
}
// 记录每个元素的出现的次数
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
for (int i = 0; i < size; i++) {
if (m.containsKey(array[i])) {
m.put(array[i], m.get(array[i]) + 1);
} else {
// 第一次加入该元素
m.put(array[i], 1);
}
}
// 找出出现次数最多的元素
int most = 0;
Iterator<Entry<Integer, Integer>> iterator = m.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Integer, Integer> entry = iterator.next();
int key = entry.getKey();
int val = entry.getValue();
if (val > most) {
result = key;
most = val;
}
}
return result;
}
public static void main(String[] args) {
int array[] = { 1, 5, 4, 3, 4, 4, 5, 4, 5, 5, 6, 6, 6, 6, 6 };
int maxFrecenNum = method(array);
System.out.println(maxFrecenNum);
}
}
- 把一个数组循环右移K位
问题描述:假设把数组序列12345678右移2位变为78123456,比较移位前后数组序列的形式,不难看出,其中有两段序列的顺序是不变的,即78和123456,可以吧这两段看做两个整体,右移k为就是把数组的两部分交换一下。鉴于此,可以设计这样一种算法,步骤如下(以数组序列12345678为例):
1)逆序数组子序列123456,数组序列的形式变为65432178
2)逆序数组子序列78,数组序列的形式变为65432187
3)全部逆序,数组序列的形式变为78123456
代码如下:
public class RightMove {
public static void reverse(int a[], int b, int e) {
for (; b < e; b++, e--) {
int temp = a[e];
a[e] = a[b];
a[b] = temp;
}
}
public static void shift_k(int a[], int k) {
int n = a.length;
k = k % n;
reverse(a, n - k, n - 1);
reverse(a, 0, n - k - 1);
reverse(a, 0, n - 1);
}
/**
* @param args
*/
public static void main(String[] args) {
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
shift_k(a, 3);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}