没有合适的资源?快使用搜索试试~ 我知道了~
首页剑指offer(Java版)
剑指offer(Java版)
需积分: 12 253 浏览量
更新于2023-05-23
评论 2
收藏 477KB PDF 举报
剑指offer (Java版),附带详细讲解,以及知识点讲解,搞通搞透,offer随意拿!
资源详情
资源评论
资源推荐

面试题 2:实现单例模式
1.饿汉式单例类
public class SingletonClass{
private static final SingletonClass instance=new SingletonClass();
//私有构造函数
private SingletonClass(){}
public static SingletonClass getInstance(){
return instance;
}
}
2.懒汉式单例模式
public class SingletonClass{
private static SingletonClass instance=null;
//私有构造函数
private SingletonClass(){}
public synchronized static SingletonClass getInstance(){
if(instance==null){
instance=new SingletonClass();
}
return instance;
}
}
面试题 3:二维数组中的查找
题目描述:一个二维数组,每一行从左到右递增,每一列从上到下递增.输
入一个二维数组和一个整数,判断数组中是否含有整数。
public class Find{
public static boolean find(int[][] array,int number){
if(array==null){
return false;
}
int column=array[0].length-1;
int row=0;
while (row<array.length && column>=0){
if(array[row][column]==number){

return true;
}
if(array[row][column]>number){
column--;
}else{
row++;
}
}
return false;
}
public static void main(String args[]){
int[][] testarray=new int[4][4];
testarray[0][0]=1;
testarray[0][1]=2;
testarray[0][2]=8;
testarray[0][3]=9;
testarray[1][0]=2;
testarray[1][1]=4;
testarray[1][2]=9;
testarray[1][3]=12;
testarray[2][0]=4;
testarray[2][1]=7;
testarray[2][2]=10;
testarray[2][3]=13;
testarray[3][0]=6;
testarray[3][1]=8;
testarray[3][2]=11;
testarray[3][3]=15;
System.out.println(find(testarray, 1));
}
}
面试题 4:替换空格
题目:请实现一个函数,把字符串中的每个空格替换成“%20”。
public class ReplaceBlank {
public static void main(String args[]){
String s="We are happy.";
System.out.println(replaceBlank(s));
}
public String replaceBlank(String input){
if(input==null)
return null;
StringBuffer outputBuffer=new StringBuffer();

for(int i=0;i<input.length();i++){
if(input.charAt(i)==' '){
outputBuffer.append("%");
outputBuffer.append("2");
outputBuffer.append("0");
}else {
outputBuffer.append(String.valueOf(input.charAt(i)));
}
}
return new String(outputBuffer);
}
}
面试题 5:从尾到头打印链表
题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值。
方法一:非递归的方式
public class PrintListReverse{
public static void main (String args[])
{
ListNode node1=new ListNode();
ListNode node2=new ListNode();
ListNode node3=new ListNode();
node1.data=1;
node2.data=2;
node3.data=3;
node1.next=node2;
node2.next=node3;
printListReversversingly test=new printListReversversingly();
test.printListReverse(node1);
}
public static void printListReverse(ListNode headNode){
Stack<ListNode> stack=new Stack<ListNode>();
while(headNode!=null){
stack.push(headNode);
headNode=headNode.next;
}
while(!stack.isEmpty()){
System.out.println(stack.pop().data);
}

}
}
方法二:递归方式实现
public class PrintListReverse{
public static void main (String args[])
{
ListNode node1=new ListNode();
ListNode node2=new ListNode();
ListNode node3=new ListNode();
node1.data=1;
node2.data=2;
node3.data=3;
node1.next=node2;
node2.next=node3;
printListReversversingly test=new printListReversversingly();
test.printListReverse(node1);
}
public static void printListReverse(ListNode headNode){
if(headNode!=null){
if(headNode.next!=null){
printListReverse(headNode.next);
}
}
System.out.println(headNode.data);
}
}
面试题 6:重建二叉树
题目描述:输入二叉树的前序遍历和中序遍历的结果,重建出该二叉树。假设前
序遍历和中序遍历结果中都不包含重复的数字,例如输入的前序遍历序列
{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}重建出如图所示的二叉
树。
public class BinaryTreeNode {

public static int value;
public BinaryTreeNode leftNode;
public BinaryTreeNode rightNode;
}
import java.util.Arrays;
public class Problem6{
public static void main(String[] args) throws Exception {
int[] preSort={1,2,4,7,3,5,6,8};
int[] inSort={4,7,2,1,5,3,8,6};
BinaryTreeNode root=constructCore(preSort,inSort);
}
public static BinaryTreeNode constructCore(int[]
preorder,int[] inorder) throws Exception{
if(preorder==null||inorder==null){
return null;
}
if(preorder.length!=inorder.length){
throw new Exception("长度不一样,非法的输入");
}
BinaryTreeNode root=new BinaryTreeNode();
for(int i=0;i<inorder.length;i++){
if(inorder[i]==preorder[0]){
root.value=inorder[i];
System.out.println(root.value);
root.leftNode=constructCore(Arrays.copyOfRange(preorder,1,
i+1), Arrays.copyOfRange(inorder, 0, i));
root.rightNode=constructCore(Arrays.copyOfRange(preorder,i+
1, preorder.length),Arrays.copyOfRange(inorder, i+1,
inorder.length));
}
}
return root;
}
}
面试题 7:用两个栈实现队列
题目描述:用两个栈实现一个队列,实现对了的两个函数 appendTail 和
deleteHead,分别完成在队列尾插入结点和在队列头部删除结点的功能。
public class Problem7<T> {
private Stack<T> stack1=new Stack<T>();
private Stack<T> stack2=new Stack<T>();
剩余46页未读,继续阅读
















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0