java代码记录

public class Test7 {

	public static void main(String[] args) {
		int[][] array = new int[][] {{1,2},{2,3},{3,4}};
		boolean find1 = find(3, array);
		boolean find2 = find(8, array);
		System.out.println(find1); // 输出true
		System.out.println(find2); // 输出 false

	}
	
	/**
	 * @param target
	 * @param array
	 * @return
	 */
	public static boolean find(int target, int [][] array) {
		int row = 0;
		int col = array[0].length-1;
		
		while(row<array.length && col>=0){
        if(array[row][col] == target)
            return true;
        else if(array[row][col] > target)
            col-=1;
        else
            row+=1;
		}
		return false;
    }
	

}

class ListNode {
    
	int val;        
	ListNode next = null;
	ListNode(int val) {
	       this.val = val;   
	   }
	}

public class Test8 {

	public static void main(String[] args) {
		ArrayList<Integer> printListFromTailToHead = printListFromTailToHead(new ListNode(10));
		System.out.println(printListFromTailToHead.size());
		for (Integer integer : printListFromTailToHead) {
			System.out.println(integer);
		}

	}
	
	/**
	 * 
	 * @param listNode
	 * @return
	 */
	 public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
		 
	        ArrayList<Integer> arr = new ArrayList<Integer>();
	        ListNode p = listNode;
	        ArrayList<Integer> stack = new ArrayList<Integer>();
	        while(p!=null){
	            stack.add(p.val);
	            p = p.next;
	        }
	        int n = stack.size();
	        for(int i=n-1;i>=0;i--){
	            arr.add(stack.get(i));
	        }
	        return arr;
	    }

}

public class Test9 {
	
    static Stack<Integer> stack1 = new Stack<Integer>();
    static Stack<Integer> stack2 = new Stack<Integer>();
    
    public static void main(String[] args) {
    		push(1);
    		push(2);
    		push(3);
    		System.out.println(stack1.size());
    		System.out.println(stack2.size());
    		pop();
    		System.out.println(stack1.size());
    		System.out.println(stack2.size());
	}
    
    public static void push(int node) {
         stack1.push(node);
    }
    
    /**
     * pop操作 复杂
     * @return
     */
    public static int pop() {
    	int temp;
        
    	while(!stack1.empty()){
            temp = stack1.pop();
            stack2.push(temp);
        }
        
        int res = stack2.pop();
        while(!stack2.empty()){
            temp = stack2.pop();
            stack1.push(temp);
        }
        return res;
    }}

public class Test10 {
	public static void main(String[] args) {
		int[] array = new int[] {1,2,4,3,5,6,0,-1,-100};
		int minNumberInRotateArray = minNumberInRotateArray(array );
		System.out.println(minNumberInRotateArray);
	}
	
	public static int minNumberInRotateArray(int [] array) {
	    if(array.length==0){
	            return 0;
	        }
	        for(int i=0;i<array.length-1;i++){
	            if(array[i] > array[i+1]){
	                return array[i+1];
	            }
	        }
	        return array[0];
	    }
}

public class Test11 {
	public static void main(String[] args) {
		int fibonacci = fibonacci(10);
		System.out.println(fibonacci);
	}
	
	public static int fibonacci(int n) {
			if (n<=0)
            return 0;
			
	        int a=1,b = 1;int temp;
	        for(int i=2;i<n;i++){
	            temp = a;
	            a = b;
	            b = temp + b;
	        }
	        return b;
	    }
}

public class Test12 {
	public static void main(String[] args) {
		int jumpFloor = jumpFloor(18);
		System.out.println(jumpFloor);
	}
	
	public static int jumpFloor(int target) {
		  	if(target <= 0)
		            return 0;
		        if(target <= 2)
		            return target;
		        int a=1,b=2;
		        int temp;
		        for(int i=3;i<=target;i++){
		            temp = a;
		            a = b;
		            b += temp;
		        }
		        return b;
		    }
}

public class Test13 {
	public static void main(String[] args) {
		int jumpFloorII = jumpFloorII(18);
		System.out.println(jumpFloorII);
	}
	
	 public static int jumpFloorII(int target) {
	        if(target<=0)
	            return 0;
	        int sumPath = 0;
	        int path = 0;
	        for(int i=0;i<target;i++){
	            path = sumPath + 1;
	            sumPath = sumPath * 2 + 1;
	        }
	        return path;
	    }
}

public class Test14 {

	public static void main(String[] args) {
		int rectCover = rectCover(10);
		System.out.println(rectCover);
	}
	
	public static int rectCover(int target) {
        if(target <= 0)
           return 0;
       if(target <= 2)
           return target;
       int a=1,b=2;
       int temp;
       for(int i=3;i<=target;i++){
           temp = a;
           a = b;
           b += temp;
       }
       return b;
   }

}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 护眼 设计师:闪电赇 返回首页
实付 19.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值