1题目:二维数组的查找
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
思路:
首先选取右上角的数字,如果该数字等于要查找的数字,结束.
如果数组>X,剔除这个数字所在的列,若数组<X,剔除该行,直到找到该数或者为空.
package com.matajie;
/**
* 1.二维数组的查找
* 在一个二维数组中(每个一维数组的长度相同),
* 每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
* 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
*
*
* 我的程序才不会有bug!
* author:年仅18岁的天才少年程序员丶mata杰
**/
public class Find {
public boolean Find1(int target,int[][] array){
boolean find = false;
if(array.length == 0|| array[0].length == 0){
return false;
}
int cols = array.length;
int rows = 0;
int i = 0;
int j = cols-1;
while(i<cols && j>=0){
if(array[i][j] == target){
find = true;
}
else if(array[i][j] > target){
j--;
}
else{
i++;
}
}
return find;
}
}
2.替换空格
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
思路:
若从前往后扫描字符串每碰到空格一次就做替换,由于是把一个字符替换成3个字符,需要把后面的所有字符都后移,否则就会有两个字符被覆盖,如此时间复杂度较大O(n^2),
优化:先遍历一遍字符串,统计出字符串中空格的总长度,每次替换一个空格长度+2,
故所需总长度为oldLength+blankCount*2.这次从后往前移,是字符在第一次就到达自己的位置O(n).
package com.matajie;
/**
* 2.替换空格
* 请实现一个函数,将一个字符串中的每个空格替换成“%20”。
* 例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
*
* 我的程序才不会有bug!
* author:年仅18岁的天才少年程序员丶mata杰
**/
public class ReplaceSpace {
public String replaceSpace(StringBuffer str){
int blankCount = 0;
for(int i = 0;i<str.length();i++){
if(str.charAt(i) == ' '){
blankCount++;//统计空格数量
}
}
int oldLength = str.length();//得到原来字符串的长度
int newLength = oldLength+blankCount*2;//将空格替换为"%20"后的长度
int oldIndex = oldLength-1;//原来字符串的索引
int newIndex = newLength-1;//新字符串的索引
str.setLength(newLength);
//一次遍历,替换所有空格为"%20"
while(oldIndex >= 0 && newIndex>oldIndex){
if(str.charAt(oldIndex) == ' '){
str.setCharAt(newIndex--,'0');
str.setCharAt(newIndex--,'2');
str.setCharAt(newIndex--,'%');
}else{
str.setCharAt(newIndex--,str.charAt(oldIndex));
}
oldIndex--;
}
return str.toString();
}
}
3.题目 从尾到头打印链表
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList.
思路一:
借助于栈,从头到尾遍历链表,把遍历结果放入栈中.
思路二:
栈的本质就是一个递归,先递归输出它后面的节点,再输出该节点自身.
/**
* 3.从尾到头打印链表
* 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
*
* 我的程序才不会有bug!
* author:年仅18岁的天才少年程序员丶mata杰
**/
import java.util.ArrayList;
public class PrintListFromTailToHead {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if(listNode != null){
printListFromTailToHead(listNode.next);
arrayList.add(listNode.val);
}
return arrayList;
}
}
4.题目:重建二叉树
题目描述
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
思路:
递归思想,每次将左右两颗子树当成新的子树进行处理,中序的左右子树索引很好找,前序的开始结束索引通过计算中序左右子树的大小来计算,然后递归求解,直到直到startPre>endPre
|| startIn>endIn 说明子树整理完毕,方法每次返回左子树和右子树的根节点.
如图(字和图有点丑,请见谅qaq)
怎么确定左子树前序序列的最右边界呢?左子树中序序列起点是startIn,终点是i-1,左子树前序序列起点是startPre+1,要求左子树前序序列终点,假设该终点下标为x,由中序序列长度和前序序列长度相等的条件,得
X-(startPre+1) = i-1-startIn, 解得X = i-startIn+startPre.
package com.matajie;
import javax.swing.tree.TreeNode;
/**
* 4.重建二叉树
* 题目描述
* 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
* 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
* 例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
*
* 我的程序才不会有bug!
* author:年仅18岁的天才少年程序员丶mata杰
**/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class ReConstructBinaryTree {
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
TreeNode root = reConstructBinaryTree(pre, 0, pre.length - 1, in, 0, in.length - 1);
return root;
}
private TreeNode reConstructBinaryTree ( int[] pre, int startPre, int endPre, int[] in, int startIn, int endIn){
if (startPre > endPre || startIn > endIn) {
return null;
}
TreeNode root = new TreeNode(pre[startPre]);
for(int i = startIn;i<=endIn;i++){
if(in[i] == pre[startPre]){
root.left = reConstructBinaryTree(pre,startPre+1,i-startIn+startPre,in,startIn,i-1);
root.right = reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
break;
}
}
return root;
}
}
5.用两个栈来实现一个队列
题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
思路:
入队:将元素进栈A.
出队:判断栈B是否为空,如果为空,则将栈A中的所有元素pop,并push进栈B,栈B出栈,
如果不为空,栈B直接出栈.
package com.matajie;
import java.util.Stack;
/**
* 5.用两个栈来实现一个队列
* 题目描述
* 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
*
* 我的程序才不会有bug!
* author:年仅18岁的天才少年程序员丶mata杰
**/
public class TwoStackImplQueue {
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.empty()&&stack2.empty()){
throw new RuntimeException();
}
if(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
那么怎么用两个队列实现一个栈呢?
思路:入栈:将元素进队列A.
出栈:判断队列A中元素的个数是否为1,若等于1,则出队列,否则将队列A中的元素一次出队列并放进队列B,直到队列A中的元素留下一个,然后队列A出队列,再把队列B的元素出队列依次放入队列A中.
package com.matajie;
import java.util.LinkedList;
import java.util.Queue;
**
* 我的程序才不会有bug!
* author:年仅18岁的天才少年程序员丶mata杰
**/
class TwoQueueStack<E> {
private Queue<E> queueA;
private Queue<E> queueB;
public TwoQueueStack() {
queueA = new LinkedList<>();
queueB = new LinkedList<>();
}
public E push(E e) {
if (queueA.size() != 0) {
queueA.add(e);
} else if (queueB.size() != 0) {
queueB.add(e);
} else {
queueA.add(e);
}
return e;
}
public E pop() {
if (queueA.size() == 0 && queueB.size() == 0) {
return null;
}
E result = null;
if (queueA.size() != 0) {
while (queueA.size() > 0) {
result = queueA.poll();
if (queueA.size() != 0) {
queueB.add(result);
}
}
} else {
while (queueB.size() > 0) {
result = queueB.poll();
if (queueB.size() != 0) {
queueA.add(result);
}
}
}
return result;
}
}