题目1:数组-二维数组中的查找
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
输入描述:
array: 待查找的二维数组
target:查找的数字
输出描述:
查找到返回true,查找不到返回false
public boolean Find(int[][] array, int target) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j] == target)
return true;
else
continue;
}
}
return false;
}
题目2:字符串-替换空格
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
public String replaceSpace(StringBuffer str) {
String sti=str.toString();
char[] strChar = sti.toCharArray();
StringBuffer stb = new StringBuffer();
for(int i=0;i<strChar.length;i++){
if(strChar[i]==' '){
stb.append("%20");
}else{
stb.append(strChar[i]);
}
}
return stb.toString();
}
题目3:链表-从尾到头打印链表
题目描述
输入一个链表,从尾到头打印链表每个节点的值。
输入描述:
输入为链表的表头
输出描述:
输出为需要打印的“新链表”的表头
//代码1:链表节点
/**
* @author Home
*
*/
public class ListNode {
int val;
ListNode next = null;
ListNode(int val){
this.val = val;
}
}
//代码2:打印链表
/**
*
*/
package com.printListFromTailToHead;
import java.util.ArrayList;
import java.util.Stack;
/**
* @author Home
*
*/
public class Solution {
/**
* @param args
*/
public ArrayList<Integer> printListFromTailToHead(ListNode listNode){
Stack<Integer> stack = new Stack<Integer>();
ArrayList<Integer> arrayList = new ArrayList<Integer>();
while(listNode!=null){
stack.push(listNode.val);
listNode = listNode.next;
}
while(!stack.isEmpty()){
int v = stack.pop();
arrayList.add(v);
}
return arrayList;
}
}
题目4:树-重建二叉树
题目描述:
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x){
this.val = x;
}
}
/**
* @author Home
*
*/
public class Solution {
//pre:前序遍历序列;in:中序遍历;
//pre = [1,2,4,7,3,5,6,8] in = [4,7,2,1,5,3,8,6]
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,startPre+i-startIn,in,startIn,i-1);
root.right = reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
}
}
return root;
}
}
题目4:栈和队列-用两个栈实现队列
题目描述:
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
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.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
if(stack2.isEmpty())
return 0;
return stack2.pop().intValue();
}
}
题目6:查找和排序-旋转数组的最小数字
题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减序列的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
public int minNumberInRotateArray(int[] array) {
int temp;
if (array.length > 0) {
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[i]) {
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
} else {
return 0;
}
return array[0];
}
题目7: 递归和循环 -斐波那契数列
题目描述:
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。
public int Fibonacci(int n) {
int m = 0;
int N = 1;
int temp = 0;
if (n == 1)
return n;
for (int i = 0; i < n - 1; i++) {
temp = m + N;
// System.out.print(" "+temp);
if (i % 5 == 0 && i != 0)
System.out.println();
m = N;
N = temp;
}
return temp;
}
题目8: 递归和循环 -跳台阶
题目描述:
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
public int JumpFloor(int target) {
int f = 0;
if (target == 0)
f = 0;
else if (target == 1)
f = 1;
else if (target == 2)
f = 2;
else if (target > 2) {
f = JumpFloor(target - 1) + JumpFloor(target - 2);
}
return f;
}
题目9: 递归和循环 -变态跳台阶
题目描述:
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
题目10: 递归和循环-矩形覆盖
题目描述:
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
public int RectCover(int target) {
if (target <= 0) {
return 1;
}
if (target == 1) {
return 1;
}
if (target == 2) {
return 2;
} else {
return RectCover(target - 1) + RectCover(target - 2);
}
}
题目11: 代码的完整性-数值的整数次方
题目描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
public double Power(double base, int exponent) {
double po = base;
if (exponent == 0)
po = 1;
else if (exponent > 0) {
for (int i = 1; i < exponent; i++) {
po = po * base;
}
} else if (exponent < 0) {
for (int i = 1; i < (-exponent); i++) {
po = po * base;
}
po = 1.0 / po;
}
return po;
}
题目12: 代码的完整性-调整数组顺序使奇数位于偶数前面
题目描述
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
import java.util.ArrayList;
import java.util.List;
/**
* @author Home
*输入一个整数数组,实现一个函数来调整该数组中数字的顺序,
*使得所有的奇数位于数组的前半部分,所有的偶数位于位于数
*组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
*/
public class ReOrderArray {
/**
* @param args
*/
public static void main(String[] args) {
//
int[] array = {1,2,3,4,5,6,7,8,9};
reOrderArray(array);
for(Integer i:array)
System.out.print(i+" ");
}
public static void reOrderArray(int[] array){
ArrayList<Integer> oddList = new ArrayList<Integer>();
List<Integer> evenList = new ArrayList<Integer>();
for(int i=0;i<array.length;i++){
if(array[i]%2==0)
evenList.add(array[i]);
else
oddList.add(array[i]);
}
//array = new int[array.length];
int k = 0;
for(Integer i:oddList){
array[k] = i;
k++;
}
for(Integer i:evenList){
array[k] = i;
k++;
}
}
}
题目13:代码的鲁棒性-链表中倒数第k个结点
题目描述
输入一个链表,输出该链表中倒数第k个结点。
解析:这里我们设定2个指针,pre,last;pre在前面走先走k-1步,到达第k个节点。然后pre和last一起走,当pre走到表尾的时候,last所指的地方就是倒数第k个结点。
public ListNode FindKthToTail(ListNode head,int k) {
if(head==null||k<=0){
return null;
}
ListNode pre = head;
ListNode last = head;
for(int i=1;i<k;i++)//重点:这里是走了k-1步,到达第k个节点,千万不要走过
if(pre.next!=null){
pre = pre.next;
}
while(pre.next!=null){
pre = pre.next;
last = last.next;
}
return last;
题目14:代码的鲁棒性-反转链表
题目描述:
输入一个链表,反转链表后,输出链表的所有元素。
public ListNode ReverseList(ListNode head) {
ListNode pre = null;
ListNode next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
题目15:代码的鲁棒性-合并两个排序的链表
题目16:代码的鲁棒性-树的子结构
题目17:面试思路-二叉树的镜像
题目18:画图让抽象形象化-顺时针打印矩阵
题目19:举例让抽象具体化-包含min函数的栈
题目20:举例让抽象具体化-栈的压入、弹出序列
题目21:举例让抽象具体化-从上往下打印二叉树
题目:
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
//代码1:二叉树节点
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
//代码2:从上往下层次遍历打印二叉树
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author Home
*
*/
public class Solution {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
if (root == null)
return arrayList;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode treeNode = queue.poll();
if (treeNode.left != null) {
queue.offer(treeNode.left);
}
if (treeNode.right != null)
queue.offer(treeNode.right);
arrayList.add(treeNode.val);
}
return arrayList;
}
}