算法練習11-20

  1. 題目描述
    輸入一個整數,輸出該數二進制表示中1的個數。其中負數用補碼錶示。
public class Main11 {
    public int NumberOf1(int n) {
        int count=0;
        if(n<0){
            n= n & 0x7FFFFFFF;
            count++;
        }
        while(n!=0){
            count += n & 1;
            n = n >> 1;
        }
        return count;
    }
}

12.題目描述
給定一個double類型的浮點數base和int類型的整數exponent。求base的exponent次方。

public class Main12 {
    public double Power(double base, int exponent) {
        double db = 1;
        if(exponent>0){
            for (int i = 0; i < exponent; i++) {
                db = db * base;
            }
        }else if(exponent<0){
            if(base==0){
                System.out.println("分母不能爲0");
            }else{
                for (int i = 0; i < -exponent; i++) {
                    db = db * base;
                }
                db=1/db;
            }
        }else{
            return 1;
        }
        return db;
    }
}
  1. 題目描述
    輸入一個整數數組,實現一個函數來調整該數組中數字的順序,
    使得所有的奇數位於數組的前半部分,所有的偶數位於數組的後半部分,
    並保證奇數和奇數,偶數和偶數之間的相對位置不變。
public class Main13 {
    public static void reOrderArray(int [] array) {
        int count = 0;
        for(int i=0; i<array.length; i++){
            if(array[i]%2==1){
                int temp = array[i];
                for(int j=i; j>count; j--){
                    array[j] = array[j-1];
                }
                array[count] = temp;
                count++;
            }
        }
    }

    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7};
        reOrderArray(array);
        for(int i=0; i<array.length; i++){
            System.out.print(array[i]+",");
        }
    }
}

14.題目描述
輸入一個鏈表,輸出該鏈表中倒數第k個結點。

  public class ListNode {
      int val;
      ListNode next = null;
      ListNode(int val) {
          this.val = val;
     }
  }
public class Main14 {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head==null){
            return head;
        }
        ListNode n1=head,n2 = head;
        int i=1;
        while(i++!=k){
            n2=n2.next;
        }
        while(n2.next!=null){
            n1=n1.next;
            n2=n2.next;
        }
        return n1;
    }
}

15.題目描述
輸入一個鏈表,反轉鏈表後,輸出新鏈表的表頭。

/**
     * public class ListNode {
     *     int val;
     *     ListNode next = null;
     *
     *     ListNode(int val) {
     *         this.val = val;
     *     }
     * }
     */
    public class Main15 {
        public static ListNode way(ListNode root){
            if(root==null){
                return null;
            }
            Stack<ListNode> stack = new Stack<>();
            while(root!=null){
                stack.push(root);
                root=root.next;
            }
            ListNode head = null;
            if(!stack.empty()){
                head = root = stack.pop();
            }
            while(!stack.empty()){
                head.next = stack.pop();
                head = head.next;
            }
            head.next=null;
            return root;
        }
    }

16.題目描述
輸入兩個單調遞增的鏈表,輸出兩個鏈表合成後的鏈表,當然我們需要合成後的鏈表滿足單調不減規則。

 /**
 * public class ListNode {
 *     int val;
 *     ListNode next = null;
 *
 *     ListNode(int val) {
 *         this.val = val;
 *     }
 * }
 */
public class Main16 {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1 == null){
            return list2;
        }
        if(list2 == null){
            return list1;
        }
        ListNode head = null;
        ListNode end = null;
        if(list1.val <= list2.val){
            if(head == null){
                head = end = list1;
            }
            end.next = Merge(list1.next, list2);
        }else{
            if(head == null){
                head = end = list2;
            }
            end.next = Merge(list1, list2.next);
        }
        return head;
    }
}

17.題目描述
輸入兩棵二叉樹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 Main17 {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        Boolean flag = false;
        if(root1 != null && root2 != null){
            if(root1.val == root2.val){
                flag = ways(root1, root2);
            }
            if (!flag){
                flag = HasSubtree(root1.left, root2);
            }
            if (!flag){
                flag = HasSubtree(root1.right, root2);
            }
        }
        return flag;
    }
    public static Boolean ways(TreeNode root1,TreeNode root2){
        if(root2 == null){
            return true;
        }
        if(root1 == null && root2 != null){
            return false;
        }
        if(root1.val != root2.val){
            return false;
        }
        return ways(root1.left, root2.left) && ways(root1.right, root2.right);
    }

18.題目描述
操作給定的二叉樹,將其變換爲源二叉樹的鏡像。

 public class Main18 {
    //遞歸
/*    public void Mirror(TreeNode root) {
        if(root != null){
            TreeNode node = root.left;
            root.left = root.right;
            root.right = node;
        }
        Mirror(root.left);
        Mirror(root.right);
    }*/
    //非遞歸
    public void Mirror(TreeNode root) {
        if(root==null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            TreeNode node = queue.poll();
            TreeNode treeNode = node.left;
            node.left = node.right;
            node.right = treeNode;
            if(node.left!=null){
                queue.add(node.left);
            }
            if(node.right!=null){
                queue.add(node.right);
            }
        }
    }
}

19.題目描述
輸入一個矩陣,按照從外向裏以順時針的順序依次打印出每一個數字,例如,如果輸入如下4 X 4矩陣:
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.

 public class Main19 {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
        ArrayList<Integer> list = new ArrayList<>();
        if(matrix==null||matrix.length==0){
            return list;
        }
        ways(matrix, 0, 0, matrix.length-1, matrix[0].length-1, list);
        return list;
    }
    public static void ways(int [][] matrix, int rowStart, int colStart, int rowEnd, int colEnd, ArrayList<Integer> list){
        if(rowStart<rowEnd && colStart<colEnd){
            for(int i=colStart; i<=colEnd; i++){
                list.add(matrix[rowStart][i]);
            }
            for(int i=rowStart+1; i<=rowEnd; i++){
                list.add(matrix[i][colEnd]);
            }
            for(int i=colEnd-1; i>=colStart; i--){
                list.add(matrix[rowEnd][i]);
            }
            for(int i=rowEnd-1; i>rowStart; i--){
                list.add(matrix[i][colStart]);
            }
            ways(matrix, rowStart+1, colStart+1, rowEnd-1, colEnd-1, list);
        }else if(rowStart==rowEnd && colStart<colEnd){
            for(int i=colStart; i<=colEnd; i++){
                list.add(matrix[rowStart][i]);
            }
        }else if(rowStart<rowEnd && colStart==colEnd){
            for(int i=rowStart; i<=rowEnd; i++){
                list.add(matrix[i][colStart]);
            }
        }else if(rowStart==rowEnd && colStart==colEnd){
            list.add(matrix[rowStart][colStart]);
        }
    }
}

20.題目描述
定義棧的數據結構,請在該類型中實現一個能夠得到棧中所含最小元素的min函數(時間複雜度應爲O(1))。

 public class Main20 {
    Stack<Integer> stack = new Stack<>();
    public void push(int node) {
        stack.push(node);
    }

    public void pop() {
        stack.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int min() {
        int min = stack.peek();
        Iterator<Integer> iterator = stack.iterator();
        while(iterator.hasNext()){
            int temp = iterator.next();
            if(temp<min){
                min = temp;
            }
        }
        return min;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章