剑指Offer-68道面试题(11-20题)-Java实现

11. 二进制中1的个数 – 算法和数据操作

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

public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        while (n != 0) {
            count++;
            n = n & (n - 1);
        }
        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) {
            return 0;
        }
        if (exponent == 0) {
            return 1;
        }
        if (exponent == 1) {
            return base;
        }
        int absExponent = exponent;
        if (exponent < 0) {
            absExponent = -exponent;
        }
        double result = powerAbsexponent(base, absExponent);
        if (exponent < 0) {
            result = 1.0 / result;
        }
        return result;
    }
    
    public double powerAbsexponent(double base, int exponent) {
        if (exponent == 0) {
            return 1;
        }
        if (exponent == 1) {
            return base;
        }
        double result = powerAbsexponent(base, exponent / 2);
        result = result * result;
        if (exponent % 2 == 1) {
            result = result * base;
        }
        return result;
    }
}
13. 调整数组顺序使奇数位于偶数前面 – 代码的完整性

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

public class Solution {
    public void reOrderArray(int[] array) {
        // 不保持相对位置可以用两个指针调整
        // 保持相对位置,这里用的插入排序的思想
        int index = 0;
        int temp;
        int arrayLength = array.length;
        for (int i = 0; i < arrayLength; i++) {
            if (array[i] % 2 == 1) {
                temp = array[i];
                int indextemp = i;
                while (indextemp > index) {
                    array[indextemp] = array[--indextemp];
                }
                array[index++] = temp;
            }
        }
        return;
    }
}
14. 链表中倒数第k个结点 – 代码的鲁棒性

题目描述
输入一个链表,输出该链表中倒数第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) {
        // 输出节点n-k+1=n-(k-1)
        if (head == null || k == 0) {
            return null;
        }
        ListNode phead1 = head;
        ListNode phead2 = head;
        for (int i = 0; i < k - 1; i++) {
            if (phead1.next != null) {
                phead1 = phead1.next;
            } else {
                return null;
            }
        }
        while (phead1.next != null) {
            phead1 = phead1.next;
            phead2 = phead2.next;
        }
        return phead2;
    }
}
15. 反转链表 – 代码的鲁棒性

题目描述
输入一个链表,反转链表后,输出新链表的表头。

/*
public class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        ListNode newHead = null;
        ListNode nodepre = null;
        ListNode node = head;
        while (node != null) {
            ListNode nodenext = node.next;
            if (nodenext == null) {
                newHead = node;
            }
            node.next = nodepre;
            nodepre = node;
            node = nodenext;
        }
        return newHead;
    }
}
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) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }
        ListNode mergeList = null;
        if (list1.val <= list2.val) {
            mergeList = list1;
            mergeList.next = Merge(list1.next, list2);
        } else {
            mergeList = list2;
            mergeList.next = Merge(list1, list2.next);
        }
        return mergeList;
    }
}
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 Solution {
    public boolean HasSubtree(TreeNode root1, TreeNode root2) {
        boolean result = false;
        if (root1 != null && root2 != null) {
            if (root1.val == root2.val) {
                result = isSubtree(root1, root2);
            }
            if (!result) {
                result = HasSubtree(root1.left, root2);
            }
            if (!result) {
                result = HasSubtree(root1.right, root2);
            }
        }
        return result;
    }
    
    public boolean isSubtree(TreeNode root1, TreeNode root2) {
        if (root2 == null) {
            return true;
        }
        if (root1 == null) {
            return false;
        }
        if (root1.val != root2.val) {
            return false;
        }
        return isSubtree(root1.left, root2.left)
                && isSubtree(root1.right, root2.right);
    }
}
18. 二叉树的镜像 – 抽象问题形象化

题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:
二叉树的镜像定义:源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11
镜像二叉树
8
/ \
10 6
/ \ / \
11 9 7 5

/**
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 temp = root.left;
        root.left = root.right;
        root.right = temp;
        if (root.left != null) {
            Mirror(root.left);
        }
        if (root.right != null) {
            Mirror(root.right);
        }
        return;
    }
}
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[][] matrix) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        int rows = matrix.length;
        int cols = matrix[0].length;
        boolean[][] vis = new boolean[rows][cols];
        list.add(matrix[0][0]);
        int count = rows * cols;
        int ans = 1;
        int i = 0, j = 0;
        vis[i][j] = true;
        while (ans < count) {
            while (j + 1 < cols && !vis[i][j + 1]) {
                j++;
                list.add(matrix[i][j]);
                vis[i][j] = true;
                ans++;
            }
            while (i + 1 < rows && !vis[i + 1][j]) {
                i++;
                list.add(matrix[i][j]);
                vis[i][j] = true;
                ans++;
            }
            while (j - 1 >= 0 && !vis[i][j - 1]) {
                j--;
                list.add(matrix[i][j]);
                vis[i][j] = true;
                ans++;
            }
            while (i - 1 >= 0 && !vis[i - 1][j]) {
                i--;
                list.add(matrix[i][j]);
                vis[i][j] = true;
                ans++;
            }
        }
        return list;
    }
}
20. 包含min函数的栈 – 抽象问题具体化

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

import java.util.Stack;

public class Solution 
    Stack<Integer> m_data = new Stack<Integer>();
    Stack<Integer> m_min = new Stack<Integer>();
    
    public void push(int node) {
        m_data.push(node);
        if (m_min.empty()) {
            m_min.push(node);
        } else if (node < m_min.peek()) {
            m_min.push(node);
        } else {
            m_min.push(m_min.peek());
        }
    }
    
    public void pop() {
        m_data.pop();
        m_min.pop();
    }
    
    public int top() {
        return m_data.peek();
    }
    
    public int min() {
        return m_min.peek();
    }
}

其他题目

→→点击跳转

猜你喜欢

转载自blog.csdn.net/weixin_43845524/article/details/105731320
今日推荐