剑指offer 11~20
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
public class Solution {
public int NumberOf1(int n) {
String binaryString = Integer.toBinaryString(n);
int count = 0;
for (int i = 0; i < binaryString.length(); i++) {
if (binaryString.charAt(i) == '1') {
count++;
}
}
return count;
}
}
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
public class Solution {
public double Power(double base, int exponent) {
double res =Math.pow(base, exponent);
return res;
}
}
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
public class Solution {
public void reOrderArray(int [] array) {
int temp = 0;
for (int i = 0; i < array.length ; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if ((array[j] % 2 == 0) && (array[j + 1] % 2 == 1)) {
temp = array[j];
array[j] = array[j + 1];
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)
return null;
ListNode a = head, res = head;
int i = 0;
for (; a != null && i < k ; i++) {
a = a.next;
}
if(i != k)return null;
while (a != null) {
a = a.next;
res = res.next;
}
return res;
}
}
输入一个链表,反转链表后,输出新链表的表头。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode prev = null;
ListNode now = head;
while (now != null) {
ListNode next = now.next;
now.next = prev;
prev = now;
now = next;
}
return prev;
}
}
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
/*
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;
if (list1.val <= list2.val) {
list1.next = Merge(list1.next, list2);
return list1;
} else {
list2.next = Merge(list1, list2.next);
return list2;
}
}
}
输入两棵二叉树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;
//只有当root1和root2不为空时才进行比较,否则直接返回false
if (root1 != null && root2 != null) {
//根节点相等,比较左右节点
if (root1.val == root2.val) {
result = AhasB(root1, root2);
}
if (!result) {
result = AhasB(root1.left, root2);
}
if (!result) {
result = AhasB(root1.right, root2);
}
}
return result;
}
private boolean AhasB(TreeNode root1, TreeNode root2) {
if (root2 == null) return true;
if (root1 == null) return false;
if (root1.val != root2.val) return false;
return AhasB(root1.left, root2.left) && AhasB(root1.right, root2.right);
}
}
操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:
二叉树的镜像定义:源二叉树
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 && (root.left != null || root.right != null)) {
TreeNode temp = root.left;
root.left=root.right;
root.right=temp;
Mirror(root.left);
Mirror(root.right);
}
}
}
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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> martList = new ArrayList();
int left = 0, right = matrix[0].length - 1, top = 0, boom = matrix.length - 1;
while (left < right && top < boom) {
//从左至右
for (int i = left; i <= right; i++) {
martList.add(matrix[top][i]);
}
//从上到下
for (int i = top + 1; i <= boom; i++) {
martList.add(matrix[i][right]);
}
//从右到左
for (int i = right - 1; i >= left; i--) {
martList.add(matrix[boom][i]);
}
//从下到上
for (int i = boom - 1; i > top; i--) {
martList.add(matrix[i][left]);
}
top++;
right--;
boom--;
left++;
}
//只有一行
if (boom == top && left < right) {
for (int i = left; i <= right; i++) {
martList.add(matrix[top][i]);
}
}
//只有一列
if (left == right && top < boom) {
for (int i = top; i <= boom; i++) {
martList.add(matrix[i][left]);
}
}
//只有一个
if (left == right && top == boom) {
martList.add(matrix[left][top]);
}
return martList;
}
}
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
import java.util.Stack;
public class Solution {
private Stack<Integer> stack = new Stack<>();
private Stack<Integer> min = new Stack<>();
public static void main(String[] args) {
}
public void push(int node) {
if (min.empty() || min.peek() >= node) {
min.push(node);
} else {
min.push(min.peek());
}
stack.push(node);
}
public void pop() {
if (stack.empty() || min.isEmpty()) {
return;
}
stack.pop();
min.pop();
}
public int top() {
if (!stack.empty()) {
return stack.peek();
}
return 0;
}
public int min() {
if(!min.empty()){
return min.peek();
}
return 0;
}
}