目录
01 二维数组中的查找
题目:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
知识点: “查找” “数组”
分析:二维数组有序排列,从右上角来看,向左数字递减,向下数字递增。因此从右上角开始查找,当要查找数字比右上角数字大时,下移;当要查找数字比右上角数字小时,左移;如果相等,返回true。
/*Java runtime = 143ms*/
public class Solution {
public boolean Find(int target, int [][] array) {
if(array == null || array.length == 0 || array[0].length == 0)
return false;
int row = 0;
int col = array[0].length - 1;
while(row < array.length && col >= 0) {
if(array[row][col] < target) {
row++;
} else if(array[row][col] > target) {
col--;
} else {
return true;
}
}
return false;
}
}
02 替换空格
题目:请实现一个函数,将一个字符串中的每个空格替换成"%20"。例如,当字符串为"We Are Happy"。则经过替换之后的字符串为"We%20Are%20Happy"。
知识点: “字符串”
方法1
分析:使用StringBuilder、StringBuffer类,遍历复制,遇到空格替换成"%20"。
/*Java runtime = 17ms*/
public class Solution {
public String replaceSpace(StringBuffer str) {
if(str == null) //"if(str = null)" 应该是"=="
return null;
StringBuilder sb = new StringBuilder();
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) == ' ') {
sb.append("%20");
} else {
sb.append(str.charAt(i));
}
}
return String.valueOf(sb);
}
}
方法2
分析:不使用StringBuilder、StringBuffer类,每有一个空格,长度增加2,先计算增加后的总长度length,后new一个length长度的字符数组,从后往前开始复制,遇到空格替换成"%20"。
/*Java runtime = 26ms*/
public class Solution {
public String replaceSpace(StringBuffer str) {
String str1 = str.toString();
if(str1.equals(""))
return str1;
char [] strArray = str1.toCharArray();
int i =0;
int lengthSpace = 0;
while(i < strArray.length)
{
if(strArray[i] == ' ')
lengthSpace++;
i++;
}
int newStrLength = strArray.length + lengthSpace*2;
char [] newStr = new char[newStrLength];
int j = newStrLength-1;
i = strArray.length - 1;
while(i >= 0)
{
if(strArray[i] != ' ')
{
newStr[j--] = strArray[i--];
}else{
newStr[j--] = '0';
newStr[j--] = '2';
newStr[j--] = '%';
i--;
}
}
return new String(newStr);
}
}
03 从尾到头打印链表
题目:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
知识点: “链表”
方法1
分析:使用Stack。
/*Java runtime = 17ms*/
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> list = new ArrayList<>();
if (listNode == null)
return list;
Stack<ListNode> stack = new Stack<>();
while (listNode != null) {
stack.push(listNode);
listNode = listNode.next;
}
while (!stack.isEmpty()) {
list.add(stack.pop().val);
}
return list;
}
}
方法2
分析:使用递归。
/*Java runtime = 18ms*/
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> list = new ArrayList<>();
if (listNode == null)
return list;
dfs(listNode, list);
return list;
}
public void dfs(ListNode listNode, ArrayList<Integer> list) {
if(listNode.next != null) {
dfs(listNode.next, list);
}
list.add(listNode.val); //"list.add(ListNode.val)" "ListNode"是类不是变量
}
}
方法3
分析:利用三个指针把链表反转,再用ArrayList记录反转后的链表值。
/*Java runtime = 20ms*/
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if(listNode == null)
return new ArrayList<Integer>();
ListNode head = listNode;
ListNode cur = listNode.next;
head.next = null; //断开头结点
while( cur!= null){
ListNode temp = cur.next;
cur.next = head;
head = cur;
cur = temp;
}
ArrayList<Integer> res = new ArrayList<Integer>();
while(head !=null){
res.add(head.val);
head = head.next;
}
return res;
}
}
04 重建二叉树
题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
知识点: “二叉树”
分析:前序遍历的第一个节点就是树的根节点,所以我们先根据前序遍历序列的第一个数字创建根结点,接下来在中序遍历序列中找到根结点的位置,根节点的左边就是左子树,右边就是右子树,这样就能确定左、右子树结点的数量。在前序遍历和中序遍历的序列中划分了左、右子树结点的值之后,就可以递归地去分别构建它的左右子树。
/*Java runtime = 293ms*/
import java.util.Arrays;
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if (pre == null || in == null) {
return null;
}
if (pre.length == 0 || in.length == 0) {
return null;
}
if (pre.length != in.length) {
return null;
}
TreeNode root = new TreeNode(pre[0]);
for (int i = 0; i < pre.length; i++) {
if (pre[0] == in[i]) {
root.left = reConstructBinaryTree(
Arrays.copyOfRange(pre,1,i+1),Arrays.copyOfRange(in,0,i));
//Arrays.copyOfRange()方法返回复制的子列表,前闭后开.
root.right = reConstructBinaryTree( Arrays.copyOfRange(pre,i+1,pre.length),Arrays.copyOfRange(in,i+1,in.length));
}
}
return root;
}
}
05 用两个栈实现队列
题目:用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
知识点: “队列” “栈”
分析:使用两个栈 stack1 和 stack2。push 动作都在 stack1 中进行,pop 动作在 stack2 中进行。当 stack2 不为空时,直接 pop,当 stack2 为空时,先把 stack1 中的元素 全部pop 出来,push 到 stack2 中,再从 stack2 中 pop 元素。
/*Java runtime = 22ms*/
import java.util.Stack;
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(stack1.isEmpty() && stack2.isEmpty())
throw new RuntimeException("Queue is empty!");
int node;
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
node = stack1.pop();
stack2.push(node);
}
}
return stack2.pop();
}
}