原题链接:
第一题:二维数组中的查找
第二题:替换空格
第三题:从尾到头打印链表
第四题:重建二叉树
第五题:用两个栈实现队列
第六题:旋转数组的最小数字
第一题:二维数组中的查找
题目描述
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 |
---|
public boolean Find(int target, int [][] array) {
boolean isfind=false;
int rows=array.length;
int cols=array[0].length;
int row=0;
int col=cols-1;//从右上角开始扫描
if(array!=null && rows>0 && cols>0){
while(col>=0 && row<=rows-1)
if(array[row][col]==target){
isfind=true;break;
}else if(array[row][col]>target){
col--;
}else row++;
}
return isfind;
}
第二题:替换空格
题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy |
---|
解析
1.得到替换后字符串的长度 2.从后向前替换,每次原字符串的指针向前走一个 遇到字符时,新字符串也向前走一个; 遇到空格时,新字符向前走三个位置,替换为'0','2','%'; |
public String replaceSpace(StringBuffer str) {
if (str == null)
return null;
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ')
count++;
}
int newlen=str.length()+count*2-1;
int oldlen=str.length()-1;
char[] chs=new char[newlen+1];
while(oldlen>=0&&newlen>=oldlen) {
if(str.charAt(oldlen)==' ') {
chs[newlen--]='0';
chs[newlen--]='2';
chs[newlen--]='%';
}else {
chs[newlen--]=str.charAt(oldlen);
}
oldlen--;
}
return String.valueOf(chs);
}
第三题:从尾到头打印链表
题目描述
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList |
---|
解析
idea1 保存到栈里,在把栈中元素添加到list |
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack <Integer>stack=new Stack<Integer>();
ArrayList<Integer> list=new ArrayList<Integer>();
while(listNode!=null){
stack.push(listNode.val);
listNode=listNode.next;
}
while(!stack.isEmpty()){
list.add(stack.pop());
}
return list;
}
idea2 递归栈 |
public class Solution {
ArrayList<Integer> list = new ArrayList<Integer>();
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if(listNode!=null) {
printListFromTailToHead(listNode.next);
list.add(listNode.val);
}
return list;
}
}
第四题:重建二叉树
题目描述
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。 |
---|
解析
递归解法(base-case): 前序:1,2,4,7,3,5,6,8 中序:4,7,2,1,5,3,8,6 case: 1.先在中序中找到root(前序中的第一个)计为i,然后以i划分:i左边的为左子树(inStart,i-1),右边的为右子树(i+1,inEnd); 2.在前序中找到对应的左子树(preStart+1,preStart+ (i-inStart ))和右子树((preStart+i-inStart)+1,preEnd); 3.对左右子树进行递归; base:前序和中序的数据下标终止位置大于起始位置,递归停止 |
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
TreeNode root=reConstructBinaryTree( pre,0,pre.length-1,in,0,in.length-1);
return root;
}
public TreeNode reConstructBinaryTree( int []pre,int preStart,int preEnd,int []in,int inStart,int inEnd){
if(preStart>preEnd||inStart>inEnd)return null;
TreeNode root=new TreeNode(pre[preStart]);
for(int i=inStart;i<=inEnd;i++){
if(root.val==in[i]){
root.left=reConstructBinaryTree( pre,preStart+1,preStart+i-inStart,in,inStart,i-1);
root.right=reConstructBinaryTree( pre,preStart+i-inStart+1,preEnd,in,i+1,inEnd);
break;
}
}
return root;
}
}
第五题:用两个栈实现队列
题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。 |
---|
解析
两个栈来回倒: 注意两点: 1.第二个栈为空时才能倒 2.第一个栈的数据要一次性倒完 |
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(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
第六题:旋转数组的最小数字
题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。 |
---|
解析
找规律(二分查找): 旋转后的数组是两个递增的部分,计为左部分和右部分; 下标为mid的数如果>下标为high,则最小值在右边递增数组; 下标为mid的数如果<下标为high,则最小值在右边递增数组中high的前面; 如果high-low==1,二分结束; 特殊情况:如 0,1,1,1,1 旋转结果有:1,0,1,1,1 1,1,1,0,1(下标为low的数据=下标为mid的数据=下标为high的数据) 那么就必须遍历整个数组求最小值 |
public class Solution {
public int minNumberInRotateArray (int [] array) throws Exception{
if(array==null ||array.length==0)
throw new Exception("Invalid parameters");
int low = 0 ; int high = array.length - 1;
int mid=low;
while(array[low]>=array[high]){
if(high-low==1){
mid=high;
break;
}
mid = low + (high - low) / 2;
if(array[low]==array[mid]&& array[mid]==array[high])
minInOrder(array,low,high);
if(array[mid] >= array[high]){
low = mid ;
}else if(array[mid]< array[high]){
high = mid;
}
}
return array[mid];
}
public int minInOrder(int [] array,int low,int high){
int min=array[low];
for(int i=low+1;i<=high;i++){
if(array[i]<min)
min=array[i];
}
return min;
}
}