面試準備系列02----面試中的棧和隊列題目彙總

                                          面試準備系列02----面試中的棧和隊列題目彙總

      本文是面試準備系列的第二篇文章,第一篇爲面試準備系列01----面試中的鏈表題目彙總》,下一篇爲《面試準備系列03----面試中的二叉樹題目彙總》

1.棧的經典應用:括號匹配+表達式計算

http://blog.csdn.net/sheepmu/article/details/21563557

2.兩個棧實現隊列+兩個隊列實現棧

http://blog.csdn.net/sheepmu/article/details/38428205

3.實現包含最大最小值的棧O(1)

http://blog.csdn.net/sheepmu/article/details/38459165

4.Linux簡化文件路徑

題目:https://oj.leetcode.com/problems/simplify-path/

思路:以/分隔字符串,遇到 . 和 空格什麼都不做,遇到..退棧,其他都進棧,最後把棧中的都用/連接起來就是簡化後的路徑

package com.sheepmu;

import java.util.Stack;

/**
 * Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
click to show corner cases.

Corner Cases:
Did you consider the case where path = "/../"?
In this case, you should return "/".
Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
In this case, you should ignore redundant slashes and return "/home/foo".
 * @author Sheep Mu
 *
 */
public class SimplilyPath 
{
	public static void main(String[] args)
	{
		String path="/a/./b/../../c/";
		String result=sp(path);
		System.out.println(result);
	}
	public static String sp(String path)
	{
		if(path==null||path.length()==0)
			return "";
		String[] splits=path.split("/");
		Stack<String> stack=new Stack<String>();
		StringBuffer sb=new StringBuffer();
		for(String ss:splits)
		{
			if(ss.equals(".")||ss.length()==0)//防止//的情況
				continue;
			else if(ss.equals(".."))
			{
				if(!stack.isEmpty())
					stack.pop();
			}
			else
				stack.push(ss);				
		}
		if(!stack.isEmpty())
		{//這裏其實我們又需要把棧裏面的內容先進先出了,所以還是用LinkedList方便,它可以同時實現棧和隊列的方法
		 String[] as=stack.toArray(new String[stack.size()]);//不能toArray()後再強制轉換,會報錯
		 for(String sss:as)
			 sb.append("/"+sss);
		}
		else//stack爲空,
		{
			sb.append("/");
		}
		return sb.toString();
	}
}

5.二叉樹的非遞歸前中後序遍歷用Stack,層序遍歷用Queue

http://blog.csdn.net/sheepmu/article/details/28941285

6.棧的壓人、彈出序列(劍橋Offer22)

輸入兩個整數序列,第一個序列表示棧的壓入順序,請判斷第二個序列是否爲該棧的彈出順序。假設壓入棧的所有數字均不相等。例如序列1,2,3,4,5是某棧的壓入順序,序列4,5,3,2,1是該壓棧序列對應的一個彈出序列,但4,3,5,1,2就不可能是該壓棧序列的彈出序列。

package com.sheepmu;

import java.util.Stack;

public class Offer22 {
	public static void main(String[] args)
	{
		int[] input={1,2,3,4,5};
		int[] output={2,1,4,3,5};
		if( isPopSequence(input,output))
			System.out.println("true");
		else
			System.out.println("false");
	}
	public static boolean isPopSequence(int[] input,int[]output)
	{
		if(input==null||output==null)
			return false;
		if(input.length==0||output.length==0)
			return false;
		if(input.length!=output.length)
			return false;
		int len=input.length;
		Stack<Integer> stack=new Stack<Integer>();
		int i=0,j=0;
		while(j<len)
		{
			 if(i<len)
			 {
				 stack.push(input[i++]);
				 while(!stack.isEmpty()&&stack.peek()==output[j])
				 { 
					 stack.pop();
					 j++;
				 }
			 }
			 else
				 return false;
		}
		
		return true;
	}
}



發佈了82 篇原創文章 · 獲贊 76 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章