1.颠倒数组顺序的代码 如果数组长度为1w,循环1w/2
package com.css.global;
public class Test {
public static void reverse(int[] a, int left, int right) {
if (left >= right)
return;
int temp;
temp = a[left];
a[left] = a[right];
a[right] = temp;
reverse(a, ++left, --right);//采用递归 这样可以避免for循环次数较多的情况
}
public static void main(String args[]) {
int[] a = { 1, 2, 3, 4, 5 };
reverse(a, 0, a.length - 1);
for (int i = 0; i < a.length; i++)
System.out.println(a[i]);
}
}
两个集合找交集!
public class TestRetainAll {
public static void main(String[] args) {
int[] a = { 1, 5, 8, 10, 14, 15, 17, 18, 20, 22, 24, 25, 28 };
int[] b = { 2, 4, 6, 8, 10, 12 };
int i = 0;
int j = 0;
while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
i++;
} else if (a[i] == b[j]) {
System.out.println(a[i]);
i++;
j++;
} else {
j++;
}
}
}
}