剑指Offer的一些数据结构面试题目
1.一个二维数组,每一行从左到右递增,每一列从上到下递增.输 入一个二维数组和一个整数,判断数组中是否含有整数。
代码演示
package ArrayTest;
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;
}else if(array[row][column]>number) {
System.out.println("column:"+column);
column--;
}else {
System.out.println("row:"+row);
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, 6));
}
}
2.请实现一个函数,把字符串中的每个空格替换成“%20”。
代码演示
package StringTest;
public class ReplaceBlank {
public static void main(String[] args) {
String s = "We are happy";
System.out.println(replaceBlank(s));
}
public static String replaceBlank(String s) {
if(s==null||"".equals(s)) {
return null;
}
StringBuffer sbuf = new StringBuffer();
for(int i=0;i<s.length();i++) {
if(s.charAt(i)==' ') {
sbuf.append("%");
sbuf.append("2");
sbuf.append("0");
}else {
sbuf.append(String.valueOf(s.charAt(i)));
}
}
return new String(sbuf);
}
}
3.输入二叉树的前序遍历和中序遍历的结果,重建出该二叉树。假设前 序遍历和中序遍历结果中都不包含重复的数字,例如输入的前序遍历序列 {1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}重建出如图所示的二叉树。
首先理一下前序遍历、中序遍历、后序遍历
- 前序遍历:先根节点,再左节点,最后右节点
- 中序遍历:先左节点,再根节点,最后右节点
- 后序遍历:先左节点,再右节点,最后根节点
4.判断二进制数中1的个数
public class Problem01 {
public static void main(String[] args) {
Problem01 test = new Problem01();
System.out.println(test.numberOf1(4));
}
public int numberOf1(int n) {
int count=0;
while(n!=0) {
count++;
n=(n-1) & n;
}
return count;
}
}
5.给定一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分,时间复杂度为o(n),空间复杂度为o(1)
public static int[] function_Application(int[] arr){
int left=0;
int right=arr.length-1
int flag;
while(){
if(arr[left]%2==0){
left++;//说明arr[left]是奇数
continue;
}
if(arr[right]%2!=0{
right--;
continue;
}
if(left<right){
flag=arr[left];
arr[left++]=arr[right];
arr[right--]=flag;
continue;
}
}
}
6.冒泡排序
public int[] bubbleSort(int[] arr){
if(arr.length==0)
return arr;
int flag=0;
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length-i;j++){
if(arr[j+1]<arr[j]){
flag=arr[j+1];
arr[j+1]=arr[j];
arr[j]=flag;
}
}
}
return arr;
}
7.快速排序
public int[] sort(int[] arr, int low, int high){
int start = low;
int end = high;
int key = arr[low];
int temp = 0;
while(end>start){
//从后往前比较
while(end>start&&arr[end]>=key)
end--;
//如果没有比关键值小的,就比较下一个,直到有比关键值小的交换位置,然后又从前往后比较
if(arr[end]<key){
temp = arr[start]
arr[start] = arr[end];
arr[end] = temp;
}
//从前往后比较
while(end>start && arr[start]<=key)
//如果没有比关键值大的就比较下一个,直到有比关键值大的交换位置
start++;
if(arr[start]>key){
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
//此时第一次循环结束,关键值的位置已经确定了,左边序列的值都比关键值小,右边都比关键值大,但是两边的顺序可能还不一样。
}
//递归调用
if(start>low) sort(arr,low,start+1);
if(end<high sort(arr,end+1;high);
}
8.输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
listNode 是链表,只能从头遍历到尾,但是输出却要求从尾到头,这是典型的"先进后出",我们可以想到栈!
LinkedList 中有个方法是 add(index,value),可以指定 index 位置插入 value 值,所以我们在遍历 listNode 的同时将每个遇到的值插入到 list 的 0 位置,最后输出 listNode 即可得到逆序链表
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> list = new ArrayList();
if(listNode!=null){
printListFromTailToHead(listNode.next);
list.add(listNode.val);
}
}
}

浙公网安备 33010602011771号