剑指offer 练习二(Java版)

题十一   输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

public class Solution {
    public int NumberOf1(int n) {
           int count = 0;
        
        for(; n !=0; count ++){
            n &= (n-1);
        }
        return count;
    }
}
题十二     给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
public class Solution {
    public double Power(double base, int exponent) {
        int n = Math.abs(exponent);
        double result = 0.0;
        
        if(n == 0) return 1.0;
        if(n == 1) return base;
        
        result = Power(base, n>>1);
        result *= result;
        
        if((n&1)==1) result *=base;
        if(exponent <0) result = 1/result;
        
        return result;
        
  }
}
题十三     输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

public class Solution {
    public void reOrderArray(int [] array) {
        
        if(array == null) return;
        
       for (int i =0;i<array.length; i++){
           if(array[i]%2 == 1)  {
               int temp = array[i];
               int j =i-1;
               
               while(j >=0 && array[j]%2==0){
                   array[j+1] = array[j];
                   j--;
               }
               array[j+1] = temp;
           }
       }
                  
    }
}

题十四     输入一个链表,输出该链表中倒数第k个结点。
/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head == null || k<=0) return null;
        
        ListNode node = head;  
        ListNode result = head;
        for(int i=1;i<k;i++){
            if(node.next != null){
             node = node.next;   
            }
            else{
                return null; 
            }
               
          }
        while(node.next != null){
            node = node.next;
            result = result.next;
        }
        return result;
    }
}
题十五     输入一个链表,反转链表后,输出链表的所有元素。
/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
     //if (head.next == null || head == null){
     //    return head; 
     //}
        
        ListNode pre = null;
        ListNode after = null;
        while(head != null){
            after = head.next;
            head.next = pre;
            pre = head;
            head = after;
        }
        return pre;
    }
}
题十六     输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1 == null) return list2;
        if(list2 == null) return list1;
        
        ListNode result = null;
        
        if(list1.val > list2.val){
            result = list2;
            result.next = Merge(list1, list2.next);
        }else {
            result = list1;
            result.next = Merge(list1.next, list2);
        }
    
        return result;
    }
}
题十七     输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        boolean result = false;
        
        if( root1 != null && root2 != null) {
            if(root1.val == root2.val){
                result = doesTree1HaveTree2(root1,root2);
            }
            if(!result){
                result = HasSubtree(root1.left, root2);
            }
             if(!result){
                result = HasSubtree(root1.right, root2);
            }
        }
        return result;
    }
     public static boolean doesTree1HaveTree2(TreeNode node1,TreeNode node2){
         if(node2 == null)  return true;
         if(node1 == null)  return false;
         if(node1.val != node2.val) return false;
         
         return doesTree1HaveTree2(node1.left,node2.left)&& doesTree1HaveTree2(node1.right,node2.right);
     } 
        
    }
题十八     操作给定的二叉树,将其变换为源二叉树的镜像。
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public void Mirror(TreeNode root) {
        if(root == null)  return;
        
        if(root.left == null && root.right == null) return;
            
            TreeNode tmp = root.left;
            root.left = root.right;
            root.right = tmp;
        
        
        if(root.left != null)  Mirror(root.left);
        if(root.right != null)  Mirror(root.right);
    }
}
题十九     输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
       ArrayList<Integer> result = new ArrayList<Integer> ();
       if(matrix.length==0) return result;
        int n = matrix.length,  m = matrix[0].length;
        if(m==0) return result;
        int layers = (Math.min(n,m)-1)/2+1;//这个是层数
        for(int i=0;i<layers;i++){
            for(int k = i;k<m-i;k++)  result.add(matrix[i][k]);//左至右
            for(int j=i+1;j<n-i;j++)   result.add(matrix[j][m-i-1]);//右上至右下
            for(int k=m-i-2;(k>=i)&&(n-i-1!=i);k--)    result.add(matrix[n-i-1][k]);//右至左
            for(int j=n-i-2;(j>i)&&(m-i-1!=i);j--)   result.add(matrix[j][i]);//左下至左上
        }
        return result; 

    }
}
题二十     定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
import java.util.Stack;

public class Solution {

    Stack<Integer> stack=new Stack<>();
    Stack<Integer> minNum=new Stack<>();
    public void push(int node) {
         stack.push(node);
        if(minNum.isEmpty()) minNum.push(stack.peek());
        else if(stack.peek()<minNum.peek()) minNum.push(stack.peek());
        else minNum.push(minNum.peek());
    }
    
    public void pop() {
        stack.pop();
        minNum.pop();
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int min() {
         return  minNum.peek();
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值