剑指offer 11~20

11.输出该数二进制表示中1的个数。其中负数用补码表示。
这是一种简单的方法,直接用API转为二进制,计算

public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        char[] c = Integer.toBinaryString(n).toCharArray();
        for(char cc : c){
            if(cc == '1'){
                count++;
            }
        }
        return count;
    }
}
public class Solution {
    public int NumberOf1(int n) {
        return Integer.toBinaryString(n).replaceAll("0","").length();
    }
}

如果不想偷懒的话

public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        while(n != 0){
            count += n&1;//判断最后一位是否为1;
            n = n>>>1;//无符号右移   1101->110
        }
        return count;
    }
}

12.给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。保证base和exponent不同时为0

public class Solution {
    public double Power(double base, int exponent) {
            if(base == 0 && exponent == 0){
                return -1;
            }else if(base == 0){
                return 0;
            }else if(exponent == 0){
                return 1;
            }else{
                int tmp = exponent;
                if(exponent < 0){//此处要考虑expont<0 的情况
                    exponent = -exponent;
                }
                double target = 1.0;
                for(int i = 0;i < exponent;i++){
                    target *= base;
                }
                return tmp > 0 ? target:(1.0/target) ;
            }
  }
}

13.输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

import java.util.Arrays;
public class Solution {
    public void reOrderArray(int [] array) {
        int len = array.length;
        int[] arr = new int[len];
        int j = 0;
        for(int i = 0;i < len;i ++){
            if(array[i] % 2 != 0){
                arr[j] = array[i];
                j++;
            }
        }
        for(int i = 0;i < len;i ++){
            if(array[i] % 2 == 0){
                arr[j] = array[i];
                j++;
            }
        }
        for(int i = 0;i<len;i++){
            array[i] = arr[i];
        }
    }
}

14.输入一个链表,输出该链表中倒数第k个结点。


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 first = head;
        ListNode end = head;
        for(int i = 0;i < k - 1;i++){
            if(first.next != null){
                first = first.next;
            }else{
                return null;
            }
        }
        while(first.next != null){
            first = first.next;
            end = end.next;
        }
        return end;
    }
}

15.输入一个链表,反转链表后,输出新链表的表头。

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
import java.util.*;
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head == null){
            return null;
        }
        Stack<Integer> stack = new Stack<>();
        ListNode node = head;
        while(node != null){
            stack.push(node.val);
            node = node.next;
        }
        ListNode rehead = new ListNode(stack.pop());
        rehead.next = null;
        node = rehead;
        while(!stack.isEmpty()){
            ListNode nownode = new ListNode((int)stack.pop());
            nownode.next = null;
            rehead.next = nownode;
            rehead = nownode;
        }
        return node;
    }
}

16.输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

/*
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 && list2 != null){
            return list2;
        }else if(list1 != null && list2 == null){
            return list1;
        }
        ListNode head = new ListNode(-1);
        head.next = null;
        ListNode root = head;
        while(list1 != null && list2 != null){
            if(list1.val <= list2.val){
                head.next = list1;
                head = list1;
                list1 = list1.next;
            }else{
                head.next = list2;
                head = list2;
                list2 = list2.next;
            }
        }
        if(list1 != null){
            head.next = list1;
        }
        if(list2 != null){
            head.next = list2;
        }
        return root.next;
    }
}

17.输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if(root1 == null || root2 == null){
            return false;
        }
        boolean flag = false;
        if(root1.val == root2.val){
            flag = isSubtree(root1,root2);
        }
        if(!flag){
            flag = HasSubtree(root1.left,root2);
            if(!flag){
                flag = HasSubtree(root1.right,root2);
            }
        }
        return flag;
    }
    public boolean isSubtree(TreeNode root1,TreeNode root2){
        if(root2 == null){
            return true;
        }
        if(root1 == null){
            return false;
        }
        if(root1.val == root2.val){
            return isSubtree(root1.left,root2.left)&&isSubtree(root1.right,root2.right);
        }else{
            return false;
        }
    }
}

18.操作给定的二叉树,将其变换为源二叉树的镜像。

public class Solution {
    public void Mirror(TreeNode root) {
        if(root == null){
            return;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        Mirror(root.left);
        Mirror(root.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.

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] arr) {
        if(arr == null){
            return null;
        }
        ArrayList<Integer> list = new ArrayList<Integer>();
        int h = arr.length;
        int l = arr[0].length;
        int c = ((h < l ? h : l)-1)/2 +1;
        for(int i = 0; i < c;i++){
            for(int j = i;j < l - i;j++){
                list.add(arr[i][j]);
            }
            for(int j = i + 1;j < h - i;j++){
                list.add(arr[j][l-i-1]);
            }
            for(int j = l - i -2;(j >= i)&&(h-1-i != i);j--){
                list.add(arr[h-1-i][j]);
            }
            for(int j = h - i - 2;(j > i)&&(l-1-i != i);j--){
                list.add(arr[j][i]);
            }
        }
        return list;
    }
}

20.定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。 注意:保证测试中不会当栈为空的时候,对栈调用pop()或者min()或者top()方法。

import java.util.*;
public class Solution {
    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();
        int tmp = 0;
        Iterator<Integer> it = stack.iterator();
        while(it.hasNext()){
            tmp = it.next();
            if(tmp<min){
                min = tmp;
            }
        }
        return min;
    }
}
发布了42 篇原创文章 · 获赞 13 · 访问量 6509

猜你喜欢

转载自blog.csdn.net/weixin_43508555/article/details/104370662