# 牛客网 - 剑指Offer（上）

### 1. 二维数组中的查找

##### 题目描述

``````public class Solution {
public boolean Find(int target, int [][] array) {
int rowSize = array.length;
int colSize = array[0].length;
if (rowSize == 0 || colSize == 0) {
return false;
}
for (int i = 0; i < rowSize; i++) {
int first = array[i][0];
int last = array[i][colSize - 1];
if (target == first || target == last) {
return true;
} else if (target > first && target < last) { //二分查找
int head = 1;
int tail = colSize - 2;
while (head <= tail) {
if (array[i][head] == target || array[i][tail] == target) {
return true;
}
int mid = (head + tail) / 2;
if (array[i][mid] < target) {
head = mid + 1;
tail --;
} else if (array[i][mid] > target) {
tail = mid - 1;
} else {
return true;
}
}
} else {
continue;
}
}
return false;
}
}
``````

### 2. 替换空格

##### 题目描述

``````public class Solution {
public String replaceSpace(StringBuffer str) {
StringBuilder builder = new StringBuilder();
char mChar;
for (int i = 0; i < str.length(); i++) {
mChar = str.charAt(i);
if (str.charAt(i) == ' ') {
builder.append("%20");
} else {
builder.append(mChar);
}
}
return builder.toString();
}
}
``````

### 3. 从尾到头打印表单

##### 题目描述

``````/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> listRes = new ArrayList<Integer>();
if (listNode == null) {
return list;
}
while (listNode != null) {
listNode = listNode.next;
}
int size = list.size();
for (int i = 0; i < size; i++) {
listRes.add(list.get(size - i - 1));
}
return listRes;
}
}
``````

### 4. 重建二叉树

##### 题目描述

``````import java.util.Arrays;
/**
* Definition for binary tree
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
TreeNode root = null;
if (pre.length == 0) {
return root;
}
root = new TreeNode(pre[0]);
return createTree(pre, in, root);

}

public TreeNode createTree(int [] pre, int [] in, TreeNode root) {
int len = pre.length;
if (len == 0 ||  root == null) {
return root;
}
int index = -1;
for (int i = 0; i < len; i ++) {
if (in[i] == root.val) {
index = i;
break;
}
}
int[] inLeft = new int[index];
int[] preLeft = new int[index];
int[] inRight = new int[len - index - 1];
int[] preRight = new int[len - index - 1];
for (int i = 0; i < len; i++) {
if (i < index) {
inLeft[i] = in[i];
} else if(i > index){
inRight[i - index - 1] = in[i];
}
if (i == 0) {
continue;
}
if (i <= index) {
preLeft[i - 1] = pre[i];
} else {
preRight[i - index - 1] = pre[i];
}
}
TreeNode leftChild = null;
TreeNode rightChild = null;
if (index > 0) {
leftChild = new TreeNode(pre[1]);
}
if (index < len - 1) {
rightChild = new TreeNode(pre[index + 1]);
}
root.left = leftChild;
root.right = rightChild;
createTree(preLeft, inLeft, leftChild);
createTree(preRight, inRight, rightChild);
return root;

}
}
``````

### 5. 用两个栈实现队列

##### 题目描述

``````public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();

public void push(int node) {
stack1.push(node);
}

public int pop() {
if (!stack2.empty()) {
return stack2.pop();
} else {
while (!stack1.empty()) {
stack2.push(stack1.pop());
}
return stack2.pop();
}
}
}
``````

### 6. 旋转数组的最小数字

##### 题目描述

``````public class Solution {
public int minNumberInRotateArray(int [] array) {
int size = array.length;
if (size == 0) {
return 0;
}
int last = 0;
for (int i = 0; i < size; i++) {
if (array[i] < last) {
return array[i];
}
last = array[i];
}
return array[0];
}
}
``````

### 7. 斐波那契数列

##### 问题描述

n<=39

``````public class Solution {
public int Fibonacci(int n) {
if (n == 0) {
return 0;
} else if ( n == 1 || n == 2) {
return 1;
} else {
return getFib(n, 1, 1);
}
}

public int getFib(int n, int p1, int p2) {
if (n == 3) {
return p1 + p2;
} else {
return getFib(n - 1, p2, p1 + p2);
}
}
}
``````

### 8. 跳台阶

##### 问题描述

``````public class Solution {
public int JumpFloor(int target) {
if (target == 0) {
return 0;
} else if (target == 1) {
return 1;
} else if (target == 2) {
return 2;
} else {
return getFib(target, 1, 2);
}
}

public int getFib(int n, int p1, int p2) {
if (n == 3) {
return p1 + p2;
} else {
return getFib(n - 1, p2, p1 + p2);
}
}
}
``````

### 9. 变态跳台阶

##### 问题描述

``````public class Solution {
public int JumpFloorII(int target) {
int res = 0;
if (target == 0 || target == 1) {
return 1;
}
for (int i = 0; i < target; i++) {
res += JumpFloorII(i);
}
return res;
}
}
``````

### 10. 矩阵覆盖

##### 问题描述

``````public class Solution {
public int RectCover(int target) {
if (target > 2) {
return RectCover(target - 1) + RectCover(target - 2);
} else if (target == 2) {
return 2;
} else if (target == 1) {
return 1;
}
return 0;
}
}
``````

### 11. 二进制中1的个数

``````public class Solution {
public int NumberOf1(int n) {
String binaryStr = Integer.toBinaryString(n);
int count = 0;
int len = binaryStr.length();
for (int i = 0; i < len; i++) {
if (binaryStr.charAt(i) == '1') {
count++;
}
}
return count;
}
}
``````

### 12. 数值的整数次方

``````public class Solution {
public double Power(double base, int exponent) {
return Math.pow(base, Double.parseDouble(String.valueOf(exponent)));
}
}
``````

### 13. 调整数组顺序使奇数位于偶数前面

``````
/**
* System.arraycopy(arr1, n, arr2, m, p)
* 参数说明:
* arr1: 原数组
* n: 从原数组的第n位开始复制
* arr2: 目标数组
* m: 从目标数组第n位开始接受复制
* p: arr1 需要被复制的长度
*/
public class Solution {
public void reOrderArray(int [] array) {
int oddSize = 0;
int evenSize = 0;
int len = array.length;
for (int i = 0; i < len; i++) {
if (array[i] % 2 == 1) {
oddSize++;
}
}
evenSize = len - oddSize;
int[] oddArr = new int[oddSize];
int[] evenArr = new int[evenSize];
int oddIndex = 0;
int evenIndex = 0;
for (int i = 0; i < len; i++) {
if (array[i] % 2 == 1) {
oddArr[oddIndex] = array[i];
oddIndex++;
} else {
evenArr[evenIndex] = array[i];
evenIndex++;
}
}
System.arraycopy(oddArr, 0, array, 0, oddSize);
System.arraycopy(evenArr, 0, array, oddSize, evenSize);
}
}
``````

### 14. 链表中倒数第k个节点

``````import java.util.Stack;
/*
public class ListNode {
int val;
ListNode next = null;

ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
Stack<ListNode> stack = new Stack<ListNode>();
int size = 0;
while (head != null) {
size++;
}
if (size < k) {
return null;
}
for (int i = 0; i < k; i++) {
}
}
}
``````

### 15. 反转链表

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

ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode cur = head;
ListNode pre = null;
ListNode temp = null;
while (cur != null) {
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
``````

### 16. 合并两个排序的链表

``````import java.util.LinkedList;
import java.util.Queue;
/*
public class ListNode {
int val;
ListNode next = null;

ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode resNode = null;
ListNode head = null;
if (list1 == null && list2 == null) {
return null;
} else if (list1 == null && list2 != null) {
return list2;
} else if (list1 != null && list2 == null) {
return list1;
} else {
Queue<Integer> valQueue = new LinkedList<Integer>();
while(list1 != null && list2 != null) {
int val1 = list1.val;
int val2 = list2.val;
if (val1 < val2) {
valQueue.offer(val1);
list1 = list1.next;
} else {
valQueue.offer(val2);
list2 = list2.next;
}
}
int pollVal = valQueue.poll();
resNode = new ListNode(pollVal);
while(valQueue.size() > 0 && (pollVal = valQueue.poll()) != -1) {
ListNode nodeTemp = new ListNode(pollVal);
resNode.next = nodeTemp;
resNode = resNode.next;
}
if (list1 != null) {
resNode.next = list1;
} else {
resNode.next = list2;
}
}
}
}
``````

### 17. 树的子结构

``````/**
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) {
if (root2 == null || root1 == null) {
return false;
}
StringBuilder builder1 = new StringBuilder();
StringBuilder builder2 = new StringBuilder();
builder1 = saveInfo(builder1, root1);
builder2 = saveInfo(builder2, root2);
if (builder1.toString().contains(builder2.toString())) {
return true;
}
return false;
}

public StringBuilder saveInfo(StringBuilder builder, TreeNode node) {
builder.append(node.val);
if (node.left == null && node.right == null) {
return builder;
}
if (node.left != null) {
builder = saveInfo(builder, node.left);
}
if (node.right != null) {
builder = saveInfo(builder, node.right);
}
return builder;
}
}
``````

### 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;
} else {
TreeNode temp = null;
temp = root.left;
root.left = root.right;
root.right = temp;
}
if (root.left != null) {
Mirror(root.left);
}
if (root.right != null) {
Mirror(root.right);
}
}

}
``````

### 19. 顺时针打印矩阵

``````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;
}
roundTraverse(0, 0, matrix.length - 1, matrix[0].length - 1, result, matrix);
return result;
}

public void roundTraverse(int rowStart, int colStart, int rowEnd,
int colEnd, ArrayList<Integer> list, int [][] matrix) {
if (rowStart > rowEnd || colStart > colEnd) {
return;
} else {
for (int i = colStart; i <= colEnd; i++) {
}
for (int i = rowStart + 1; i <= rowEnd; i++) {
}
if (rowStart != rowEnd) {
for (int i = colEnd - 1; i >= colStart; i--) {
}
}
if (colStart != colEnd) {
for (int i = rowEnd - 1; i > rowStart; i--) {
}
}
roundTraverse(rowStart + 1, colStart + 1, rowEnd - 1, colEnd - 1, list, matrix);
}
}
}
``````

？？？ 这道题抽风了？

### 21. 栈的压入、弹出序列

``````import java.util.ArrayList;
import java.util.Stack;

public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
Stack<Integer> stack = new Stack<Integer>();
int index = 0;
int popIndex = 0;
int size = pushA.length;
for(int i = 0; i < size; i++) {
if (index > size - 1) {
break;
}
for (int j = index; j < size; j++) {
index++;
if (pushA[j] != popA[i]) {
if (j == size - 1) {
return false;
}
stack.push(pushA[j]);
} else {
break;
}
}
popIndex = i + 1;
}
if (popIndex == size) {
return true;
} else {
while (!stack.isEmpty()) {
if (stack.pop() != popA[popIndex]) {
return false;
} else {
popIndex++;
}
}
}
return true;
}
}
``````

### 22. 从上往下打印二叉树

``````import java.util.ArrayList;
import java.util.Queue;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;

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

}

}
*/
public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
if (root == null) {
return list;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
TreeNode leftChild = node.left;
TreeNode rightChild = node.right;
if (leftChild != null) {
queue.offer(leftChild);
}
if (rightChild != null) {
queue.offer(rightChild);
}
}
return list;
}
}
``````
原文作者：JYGod丶
原文地址: https://www.jianshu.com/p/7136b1bd22c0
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。