日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java面试时候算法题多吗,Java面试必问算法题

發(fā)布時間:2023/12/15 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java面试时候算法题多吗,Java面试必问算法题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

面試的時候,棧和隊列經(jīng)常會成對出現(xiàn)來考察。本文包含棧和隊列的如下考試內(nèi)容:

(1)棧的創(chuàng)建

(2)隊列的創(chuàng)建

(3)兩個棧實現(xiàn)一個隊列

(4)兩個隊列實現(xiàn)一個棧

(5)設計含最小函數(shù)min()的棧,要求min、push、pop、的時間復雜度都是O(1)

(6)判斷棧的push和pop序列是否一致

1、棧的創(chuàng)建:

我們接下來通過鏈表的形式來創(chuàng)建棧,方便擴充。

代碼實現(xiàn):public class Stack {

public Node head;

public Node current;

//方法:入棧操作

public void push(int data) {

if (head == null) {

head = new Node(data);

current = head;

} else {

Node node = new Node(data);

node.pre = current;//current結點將作為當前結點的前驅結點

current = node; //讓current結點永遠指向新添加的那個結點

}

}

public Node pop() {

if (current == null) {

return null;

}

Node node = current; // current結點是我們要出棧的結點

current = current.pre; //每出棧一個結點后,current后退一位

return node;

}

class Node {

int data;

Node pre; //我們需要知道當前結點的前一個結點

public Node(int data) {

this.data = data;

}

}

public static void main(String[] args) {

Stack stack = new Stack();

stack.push(1);

stack.push(2);

stack.push(3);

System.out.println(stack.pop().data);

System.out.println(stack.pop().data);

System.out.println(stack.pop().data);

}

}

入棧操作時,14、15行代碼是關鍵。

2、隊列的創(chuàng)建:

隊列的創(chuàng)建有兩種形式:基于數(shù)組結構實現(xiàn)(順序隊列)、基于鏈表結構實現(xiàn)(鏈式隊列)。

我們接下來通過鏈表的形式來創(chuàng)建隊列,這樣的話,隊列在擴充時會比較方便。隊列在出隊時,從頭結點head開始。

代碼實現(xiàn):

入棧時,和在普通的鏈表中添加結點的操作是一樣的;出隊時,出的永遠都是head結點。public class Queue {

public Node head;

public Node curent;

//方法:鏈表中添加結點

public void add(int data) {

if (head == null) {

head = new Node(data);

curent = head;

} else {

curent.next = new Node(data);

curent = curent.next;

}

}

//方法:出隊操作

public int pop() throws Exception {

if (head == null) {

throw new Exception("隊列為空");

}

Node node = head; //node結點就是我們要出隊的結點

head = head.next; //出隊之后,head指針向下移

return node.data;

}

class Node {

int data;

Node next;

public Node(int data) {

this.data = data;

}

}

public static void main(String[] args) throws Exception {

Queue queue = new Queue();

//入隊操作

for (int i = 0; i < 5; i++) {

queue.add(i);

}

//出隊操作

System.out.println(queue.pop());

System.out.println(queue.pop());

System.out.println(queue.pop());

}

}

3、兩個棧實現(xiàn)一個隊列:

思路:

棧1用于存儲元素,棧2用于彈出元素,負負得正。

說的通俗一點,現(xiàn)在把數(shù)據(jù)1、2、3分別入棧一,然后從棧一中出來(3、2、1),放到棧二中,那么,從棧二中出來的數(shù)據(jù)(1、2、3)就符合隊列的規(guī)律了,即負負得正。

完整版代碼實現(xiàn):import java.util.Stack;

public class Queue {

private Stack stack1 = new Stack<>();//執(zhí)行入隊操作的棧

private Stack stack2 = new Stack<>();//執(zhí)行出隊操作的棧

//方法:給隊列增加一個入隊的操作

public void push(int data) {

stack1.push(data);

}

//方法:給隊列正價一個出隊的操作

public int pop() throws Exception {

if (stack2.empty()) {//stack1中的數(shù)據(jù)放到stack2之前,先要保證stack2里面是空的(要么一開始就是空的,要么是stack2中的數(shù)據(jù)出完了),不然出隊的順序會亂的,這一點很容易忘

while (!stack1.empty()) {

stack2.push(stack1.pop());//把stack1中的數(shù)據(jù)出棧,放到stack2中【核心代碼】

}

}

if (stack2.empty()) { //stack2為空時,有兩種可能:1、一開始,兩個棧的數(shù)據(jù)都是空的;2、stack2中的數(shù)據(jù)出完了

throw new Exception("隊列為空");

}

return stack2.pop();

}

public static void main(String[] args) throws Exception {

Queue queue = new Queue();

queue.push(1);

queue.push(2);

queue.push(3);

System.out.println(queue.pop());

queue.push(4);

System.out.println(queue.pop());

System.out.println(queue.pop());

System.out.println(queue.pop());

}

}

注意第22行和第30行代碼的順序,以及注釋,需要仔細理解其含義。

4、兩個隊列實現(xiàn)一個棧:

思路:

將1、2、3依次入隊列一, 然后最上面的3留在隊列一,將下面的2、3入隊列二,將3出隊列一,此時隊列一空了,然后把隊列二中的所有數(shù)據(jù)入隊列一;將最上面的2留在隊列一,將下面的3入隊列二。。。依次循環(huán)。

代碼實現(xiàn):import java.util.ArrayDeque;

import java.util.Queue;

public class Stack {

Queue queue1 = new ArrayDeque();

Queue queue2 = new ArrayDeque();

//方法:入棧操作

public void push(int data) {

queue1.add(data);

}

//方法:出棧操作

public int pop() throws Exception {

int data;

if (queue1.size() == 0) {

throw new Exception("棧為空");

}

while (queue1.size() != 0) {

if (queue1.size() == 1) {

data = queue1.poll();

while (queue2.size() != 0) { //把queue2中的全部數(shù)據(jù)放到隊列一中

queue1.add(queue2.poll());

return data;

}

}

queue2.add(queue1.poll());

}

throw new Exception("棧為空");//不知道這一行的代碼是什么意思

}

public static void main(String[] args) throws Exception {

Stack stack = new Stack();

stack.push(1);

stack.push(2);

stack.push(3);

System.out.println(stack.pop());

System.out.println(stack.pop());

stack.push(4);

}

}

5、設計含最小函數(shù)min()的棧,要求min、push、pop、的時間復雜度都是O(1)。

min方法的作用是:就能返回是棧中的最小值。【微信面試題】

普通思路:

一般情況下,我們可能會這么想:利用min變量,每次添加元素時,都和min元素作比較,這樣的話,就能保證min存放的是最小值。但是這樣的話,會存在一個問題:如果最小的元素出棧了,那怎么知道剩下的元素中哪個是最小的元素呢?

改進思路:

這里需要加一個輔助棧,用空間換取時間。輔助棧中,棧頂永遠保存著當前棧中最小的數(shù)值。具體是這樣的:原棧中,每次添加一個新元素時,就和輔助棧的棧頂元素相比較,如果新元素小,就把新元素的值放到輔助棧中,如果新元素大,就把輔助棧的棧頂元素再copy一遍放到輔助棧的棧頂;原棧中,出棧時,

完整代碼實現(xiàn):import java.util.Stack;

public class MinStack {

private Stack stack = new Stack();

private Stack minStack = new Stack(); //輔助棧:棧頂永遠保存stack中當前的最小的元素

public void push(int data) {

stack.push(data); //直接往棧中添加數(shù)據(jù)

//在輔助棧中需要做判斷

if (minStack.size() == 0 || data < minStack.peek()) {

minStack.push(data);

} else {

minStack.add(minStack.peek()); //【核心代碼】peek方法返回的是棧頂?shù)脑?/p>

}

}

public int pop() throws Exception {

if (stack.size() == 0) {

throw new Exception("棧中為空");

}

int data = stack.pop();

minStack.pop(); //核心代碼

return data;

}

public int min() throws Exception {

if (minStack.size() == 0) {

throw new Exception("棧中空了");

}

return minStack.peek();

}

public static void main(String[] args) throws Exception {

MinStack stack = new MinStack();

stack.push(4);

stack.push(3);

stack.push(5);

System.out.println(stack.min());

}

}

6、判斷棧的push和pop序列是否一致:

通俗一點講:已知一組數(shù)據(jù)1、2、3、4、5依次進棧,那么它的出棧方式有很多種,請判斷一下給出的出棧方式是否是正確的?

例如:

數(shù)據(jù):

1、2、3、4、5

出棧1:

5、4、3、2、1(正確)

出棧2:

4、5、3、2、1(正確)

出棧3:

4、3、5、1、2(錯誤)

完整版代碼:import java.util.Stack;

public class StackTest {

//方法:data1數(shù)組的順序表示入棧的順序。現(xiàn)在判斷data2的這種出棧順序是否正確

public static boolean sequenseIsPop(int[] data1, int[] data2) {

Stack stack = new Stack(); //這里需要用到輔助棧

for (int i = 0, j = 0; i < data1.length; i++) {

stack.push(data1[i]);

while (stack.size() > 0 && stack.peek() == data2[j]) {

stack.pop();

j++;

}

}

return stack.size() == 0;

}

public static void main(String[] args) {

Stack stack = new Stack();

int[] data1 = {1, 2, 3, 4, 5};

int[] data2 = {4, 5, 3, 2, 1};

int[] data3 = {4, 5, 2, 3, 1};

System.out.println(sequenseIsPop(data1, data2));

System.out.println(sequenseIsPop(data1, data3));

}

}

代碼比較簡潔,但也比較難理解,要仔細體會。

總結

以上是生活随笔為你收集整理的java面试时候算法题多吗,Java面试必问算法题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。