剑指Offer相关练习

  • 数组连续子数组和最大
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);
		}
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值