- 数组连续子数组和最大
public static void getMaxSum(int[] a){
int curSum =0;
int greateSum=0;
for (int i = 0; i < a.length; i++) {
if (curSum<=0) {
curSum = a[i];
}else {
curSum += a[i];
}
if (curSum > greateSum) {
greateSum = curSum;
}
}
}
- 二位数组中的查找
public static boolean queryByArrays(int[][] a,int number){
boolean bool = false;
if (a.length != 0) {
int rows = a.length;
int clos = a[0].length;
int row = 0;
int column = clos - 1;
while (row<rows && column >=0) {
if (a[row][column] == number) {
bool = true;
break;
}else if(a[row][column] > number) {
--column;
}else {
++row;
}
}
}
return bool;
}
- 两个栈实现队列
public class Queue {
private ArrayStack stack1;
private ArrayStack stack2;
public Queue() {
stack1 = new ArrayStack(10);
stack2 = new ArrayStack(10);
}
public Object pop() throws Exception{
if (stack2.getSize()<=0) {
while (stack1.getSize() >0) {
Object obj = stack1.pop();
stack2.push(obj);
}
}
if (stack2.getSize() <=0) {
throw new Exception("queue is empty");
}
return stack2.pop();
}
public Object push(Object obj){
return stack1.push(obj);
}
}
- 调整数组顺序使奇数位于偶数前面
public static void main(String[] args) {
int[] a = {1,6,7,9,5,4,8};
int i =0;
int j = a.length-1;
while (i<j) {
while (a[i]%2==1 ) {
i++;
break;
}
while (a[j]%2!=1) {
j--;
}
int temp = a[i];
a[i]= a[j];
a[j] = temp;
}
for (int k : a) {
System.out.println(k);
}
}