總有一些題,超越了歲月,即便是經過了新框架的層層迭代,它依然散發着令人回味無窮的味道。下面的幾個筆試題目,是JAVA面試中經常遇見的,大家一定要牢記於心,可別複習到了到時候又說不出來。我就喫過這種虧,不說啦,下面來看題目。
二維數組中的查找
- 面試題
在一個二維數組中(每個一維數組的長度相同),每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。
- @代碼
public class Test7 {
public static void main(String[] args) {
int[][] array = new int[][] {{1,2},{2,3},{3,4}};
boolean find1 = find(3, array);
boolean find2 = find(8, array);
System.out.println(find1); // 輸出true
System.out.println(find2); // 輸出 false
}
/**
* @param target
* @param array
* @return
*/
public static boolean find(int target, int [][] array) {
int row = 0;
int col = array[0].length-1;
while(row<array.length && col>=0){
if(array[row][col] == target)
return true;
else if(array[row][col] > target)
col-=1;
else
row+=1;
}
return false;
}
}
鏈表題
- 面試題
輸入一個鏈表,按鏈表從尾到頭的順序返回一個ArrayList
- 代碼
class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
public class Test8 {
public static void main(String[] args) {
ArrayList<Integer> printListFromTailToHead = printListFromTailToHead(new ListNode(10));
System.out.println(printListFromTailToHead.size());
for (Integer integer : printListFromTailToHead) {
System.out.println(integer);
}
}
/**
*
* @param listNode
* @return
*/
public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> arr = new ArrayList<Integer>();
ListNode p = listNode;
ArrayList<Integer> stack = new ArrayList<Integer>();
while(p!=null){
stack.add(p.val);
p = p.next;
}
int n = stack.size();
for(int i=n-1;i>=0;i--){
arr.add(stack.get(i));
}
return arr;
}
}
隊列題
- 面試題
用兩個棧來實現一個隊列,完成隊列的Push和Pop操作。 隊列中的元素爲int類型
- 代碼
public class Test9 {
static Stack<Integer> stack1 = new Stack<Integer>();
static Stack<Integer> stack2 = new Stack<Integer>();
public static void main(String[] args) {
push(1);
push(2);
push(3);
System.out.println(stack1.size());
System.out.println(stack2.size());
pop();
System.out.println(stack1.size());
System.out.println(stack2.size());
}
public static void push(int node) {
stack1.push(node);
}
/**
* pop操作 複雜
* @return
*/
public static int pop() {
int temp;
while(!stack1.empty()){
temp = stack1.pop();
stack2.push(temp);
}
int res = stack2.pop();
while(!stack2.empty()){
temp = stack2.pop();
stack1.push(temp);
}
return res;
}}
數組題
- 面試題
把一個數組最開始的若干個元素搬到數組的末尾,我們稱之爲數組的旋轉。 輸入一個非遞減排序的數組的一個旋轉,輸出旋轉數組的最小元素。 例如數組 {3,4,5,1,2} 爲 {1,2,3,4,5}的一個旋轉,該數組的最小值爲1。
- 代碼
public class Test10 {
public static void main(String[] args) {
int[] array = new int[] {1,2,4,3,5,6,0,-1,-100};
int minNumberInRotateArray = minNumberInRotateArray(array );
System.out.println(minNumberInRotateArray);
}
public static int minNumberInRotateArray(int [] array) {
if(array.length==0){
return 0;
}
for(int i=0;i<array.length-1;i++){
if(array[i] > array[i+1]){
return array[i+1];
}
}
return array[0];
}
}
斐波那契數列問題
- 面試題
大家都知道斐波那契數列,現在要求輸入一個整數n,請你輸出斐波那契數列的第n項 [科普] 斐波那契數列指的是這樣一個數列 0, 1,1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368…
- 代碼
public class Test11 {
public static void main(String[] args) {
int fibonacci = fibonacci(10);
System.out.println(fibonacci);
}
public static int fibonacci(int n) {
if (n<=0)
return 0;
int a=1,b = 1;int temp;
for(int i=2;i<n;i++){
temp = a;
a = b;
b = temp + b;
}
return b;
}
}
青蛙上臺階問題
- 面試題
一隻青蛙一次可以跳上1級臺階,也可以跳上2級。求該青蛙跳上一個n級的臺階總共有多少種跳法(先後次序不同算不同的結果)
- 代碼
public class Test12 {
public static void main(String[] args) {
int jumpFloor = jumpFloor(18);
System.out.println(jumpFloor);
}
public static int jumpFloor(int target) {
if(target <= 0)
return 0;
if(target <= 2)
return target;
int a=1,b=2;
int temp;
for(int i=3;i<=target;i++){
temp = a;
a = b;
b += temp;
}
return b;
}
}
變態青蛙跳臺階問題
- 面試
一隻青蛙一次可以跳上1級臺階,也可以跳上2級……它也可以跳上n級。 求該青蛙跳上一個n級的臺階總共有多少種跳法。
- 代碼
public class Test13 {
public static void main(String[] args) {
int jumpFloorII = jumpFloorII(18);
System.out.println(jumpFloorII);
}
public static int jumpFloorII(int target) {
if(target<=0)
return 0;
int sumPath = 0;
int path = 0;
for(int i=0;i<target;i++){
path = sumPath + 1;
sumPath = sumPath * 2 + 1;
}
return path;
}
}
矩形覆蓋問題
- 面試題
我們可以用 2×1 的小矩形橫着或者豎着去覆蓋更大的矩形。請問用n個2*1的小矩形無重疊地覆蓋一個2×n的大矩形,總共有多少種方法?
- 代碼
public class Test14 {
public static void main(String[] args) {
int rectCover = rectCover(10);
System.out.println(rectCover);
}
public static int rectCover(int target) {
if(target <= 0)
return 0;
if(target <= 2)
return target;
int a=1,b=2;
int temp;
for(int i=3;i<=target;i++){
temp = a;
a = b;
b += temp;
}
return b;
}
}
這些題目雖然看上去比較簡單,但是蘊含了豐富的編程思想,大家可以經常練習,提高思維能力。這是面試題中經常出現的,還有一些類似的題目,大家有的話也可以提出來,我們一起進步!
作者: 愛喫早餐的程序員
原文鏈接:https://blog.csdn.net/weixin_39076203/article/details/110404021