麻豆精品无码av,欧美1区2区,久久中文字幕乱码人妻,亚洲欧美另类少妇精品,在线看黄射,69pao高清,九九九久久久国产精品,子操大逼1234区,九九爱99热精品

14
點(diǎn)贊
3
評論
5
轉(zhuǎn)載
收藏

代碼面試最常用的10大算法

摘要:面試也是一門學(xué)問,在面試之前做好充分的準(zhǔn)備則是成功的必須條件,而程序員在代碼面試時,常會遇到編寫算法的相關(guān)問題,比如排序、二叉樹遍歷等等。

在程序員的職業(yè)生涯中,算法亦算是一門基礎(chǔ)課程,尤其是在面試的時候,很多公司都會讓程序員編寫一些算法實(shí)例,例如快速排序、二叉樹查找等等。

本文總結(jié)了程序員在代碼面試中最常遇到的10大算法類型,想要真正了解這些算法的原理,還需程序員們花些功夫。

1.String/Array/Matrix

在Java中,String是一個包含char數(shù)組和其它字段、方法的類。如果沒有IDE自動完成代碼,下面這個方法大家應(yīng)該記?。?nbsp;

toCharArray() //get char array of a String Arrays.sort() //sort an array Arrays.toString(char[] a) //convert to string charAt(int x) //get a char at the specific index length() //string length length //array size substring(int beginIndex) substring(int beginIndex, int endIndex) Integer.valueOf()//string to integer String.valueOf()/integer to stringString/arrays很容易理解,但與它們有關(guān)的問題常常需要高級的算法去解決,例如動態(tài)編程、遞歸等。

下面列出一些需要高級算法才能解決的經(jīng)典問題:

2.鏈表

在Java中實(shí)現(xiàn)鏈表是非常簡單的,每個節(jié)點(diǎn)都有一個值,然后把它鏈接到下一個節(jié)點(diǎn)。 

class Node { int val; Node next; Node(int x) { val = x; next = null; } }

比較流行的兩個鏈表例子就是棧和隊(duì)列。

棧(Stack) 

class Stack{ Node top; public Node peek(){ if(top != null){ return top; } return null; } public Node pop(){ if(top == null){ return null; }else{ Node temp = new Node(top.val); top = top.next; return temp; } } public void push(Node n){ if(n != null){ n.next = top; top = n; } } }隊(duì)列(Queue)

class Queue{ Node first, last;   public void enqueue(Node n){ if(first == null){ first = n; last = first; }else{ last.next = n; last = n; } }   public Node dequeue(){ if(first == null){ return null; }else{ Node temp = new Node(first.val); first = first.next; return temp; } } }

值得一提的是,Java標(biāo)準(zhǔn)庫中已經(jīng)包含一個叫做Stack的類,鏈表也可以作為一個隊(duì)列使用(add()和remove())。(鏈表實(shí)現(xiàn)隊(duì)列接口)如果你在面試過程中,需要用到?;蜿?duì)列解決問題時,你可以直接使用它們。

在實(shí)際中,需要用到鏈表的算法有:

3.樹&堆

這里的樹通常是指二叉樹。

class TreeNode{ int value; TreeNode left; TreeNode right; } 下面是一些與二叉樹有關(guān)的概念:

  • 二叉樹搜索:對于所有節(jié)點(diǎn),順序是:left children <= current node <= right children;
  • 平衡vs.非平衡:它是一 棵空樹或它的左右兩個子樹的高度差的絕對值不超過1,并且左右兩個子樹都是一棵平衡二叉樹;
  • 滿二叉樹:除最后一層無任何子節(jié)點(diǎn)外,每一層上的所有結(jié)點(diǎn)都有兩個子結(jié)點(diǎn);
  • 完美二叉樹(Perfect Binary Tree):一個滿二叉樹,所有葉子都在同一個深度或同一級,并且每個父節(jié)點(diǎn)都有兩個子節(jié)點(diǎn);
  • 完全二叉樹:若設(shè)二叉樹的深度為h,除第 h 層外,其它各層 (1~h-1) 的結(jié)點(diǎn)數(shù)都達(dá)到最大個數(shù),第 h 層所有的結(jié)點(diǎn)都連續(xù)集中在最左邊,這就是完全二叉樹。

堆(Heap)是一個基于樹的數(shù)據(jù)結(jié)構(gòu),也可以稱為優(yōu)先隊(duì)列( PriorityQueue),在隊(duì)列中,調(diào)度程序反復(fù)提取隊(duì)列中第一個作業(yè)并運(yùn)行,因而實(shí)際情況中某些時間較短的任務(wù)將等待很長時間才能結(jié)束,或者某些不短小,但具有重要性的作業(yè),同樣應(yīng)當(dāng)具有優(yōu)先權(quán)。堆即為解決此類問題設(shè)計(jì)的一種數(shù)據(jù)結(jié)構(gòu)。

下面列出一些基于二叉樹和堆的算法:

4.Graph 

與Graph相關(guān)的問題主要集中在深度優(yōu)先搜索和寬度優(yōu)先搜索。深度優(yōu)先搜索非常簡單,你可以從根節(jié)點(diǎn)開始循環(huán)整個鄰居節(jié)點(diǎn)。下面是一個非常簡單的寬度優(yōu)先搜索例子,核心是用隊(duì)列去存儲節(jié)點(diǎn)。

 

第一步,定義一個GraphNode

class GraphNode{ int val; GraphNode next; GraphNode[] neighbors; boolean visited; GraphNode(int x) { val = x; } GraphNode(int x, GraphNode[] n){ val = x; neighbors = n; } public String toString(){ return "value: "+ this.val; } }

第二步,定義一個隊(duì)列

class Queue{ GraphNode first, last; public void enqueue(GraphNode n){ if(first == null){ first = n; last = first; }else{ last.next = n; last = n; } } public GraphNode dequeue(){ if(first == null){ return null; }else{ GraphNode temp = new GraphNode(first.val, first.neighbors); first = first.next; return temp; } } }第三步,使用隊(duì)列進(jìn)行寬度優(yōu)先搜索

public class GraphTest { public static void main(String[] args) { GraphNode n1 = new GraphNode(1); GraphNode n2 = new GraphNode(2); GraphNode n3 = new GraphNode(3); GraphNode n4 = new GraphNode(4); GraphNode n5 = new GraphNode(5); n1.neighbors = new GraphNode[]{n2,n3,n5}; n2.neighbors = new GraphNode[]{n1,n4}; n3.neighbors = new GraphNode[]{n1,n4,n5}; n4.neighbors = new GraphNode[]{n2,n3,n5}; n5.neighbors = new GraphNode[]{n1,n3,n4}; breathFirstSearch(n1, 5); } public static void breathFirstSearch(GraphNode root, int x){ if(root.val == x) System.out.println("find in root"); Queue queue = new Queue(); root.visited = true; queue.enqueue(root); while(queue.first != null){ GraphNode c = (GraphNode) queue.dequeue(); for(GraphNode n: c.neighbors){ if(!n.visited){ System.out.print(n + " "); n.visited = true; if(n.val == x) System.out.println("Find "+n); queue.enqueue(n); } } } } }輸出結(jié)果:

value: 2 value: 3 value: 5 Find value: 5 
value: 4

實(shí)際中,基于Graph需要經(jīng)常用到的算法:

5.排序

不同排序算法的時間復(fù)雜度,大家可以到wiki上查看它們的基本思想。

 

BinSort、Radix Sort和CountSort使用了不同的假設(shè),所有,它們不是一般的排序方法。 

下面是這些算法的具體實(shí)例,另外,你還可以閱讀: Java開發(fā)者在實(shí)際操作中是如何排序的。

6.遞歸和迭代

下面通過一個例子來說明什么是遞歸。

問題:

這里有n個臺階,每次能爬1或2節(jié),請問有多少種爬法?

步驟1:查找n和n-1之間的關(guān)系

為了獲得n,這里有兩種方法:一個是從第一節(jié)臺階到n-1或者從2到n-2。如果f(n)種爬法剛好是爬到n節(jié),那么f(n)=f(n-1)+f(n-2)。 

步驟2:確保開始條件是正確的

f(0) = 0; 
f(1) = 1; 

public static int f(int n){ if(n <= 2) return n; int x = f(n-1) + f(n-2); return x; }

遞歸方法的時間復(fù)雜度指數(shù)為n,這里會有很多冗余計(jì)算。

f(5) f(4) + f(3) f(3) + f(2) + f(2) + f(1) f(2) + f(1) + f(2) + f(2) + f(1)該遞歸可以很簡單地轉(zhuǎn)換為迭代。 

public static int f(int n) { if (n <= 2){ return n; } int first = 1, second = 2; int third = 0; for (int i = 3; i <= n; i++) { third = first + second; first = second; second = third; } return third; }

在這個例子中,迭代花費(fèi)的時間要少些。關(guān)于迭代和遞歸,你可以去 這里看看。

7.動態(tài)編程

動態(tài)編程主要用來解決如下技術(shù)問題:

  • An instance is solved using the solutions for smaller instances;
  • 對于一個較小的實(shí)例,可能需要許多個解決方案;
  • 把較小實(shí)例的解決方案存儲在一個表中,一旦遇上,就很容易解決;
  • 附加空間用來節(jié)省時間。

上面所列的爬臺階問題完全符合這四個屬性,因此,可以使用動態(tài)編程來解決: 

public static int[] A = new int[100]; public static int f3(int n) { if (n <= 2) A[n]= n; if(A[n] > 0) return A[n]; else A[n] = f3(n-1) + f3(n-2);//store results so only calculate once! return A[n]; }

一些基于動態(tài)編程的算法:

8.位操作

位操作符:


從一個給定的數(shù)n中找位i(i從0開始,然后向右開始)

public static boolean getBit(int num, int i){ int result = num & (1<<i); if(result == 0){ return false; }else{ return true; } }

例如,獲取10的第二位:

i=1, n=10 1<<1= 10 1010&10=10 10 is not 0, so return true;典型的位算法:

9.概率

通常要解決概率相關(guān)問題,都需要很好地格式化問題,下面提供一個簡單的例子: 

有50個人在一個房間,那么有兩個人是同一天生日的可能性有多大?(忽略閏年,即一年有365天)

算法:

public static double caculateProbability(int n){ double x = 1; for(int i=0; i<n; i++){ x *= (365.0-i)/365.0; } double pro = Math.round((1-x) * 100); return pro/100; }結(jié)果:

calculateProbability(50) = 0.97

10.組合和排列

組合和排列的主要差別在于順序是否重要。

例1:

1、2、3、4、5這5個數(shù)字,輸出不同的順序,其中4不可以排在第三位,3和5不能相鄰,請問有多少種組合?

例2:

有5個香蕉、4個梨、3個蘋果,假設(shè)每種水果都是一樣的,請問有多少種不同的組合?

基于它們的一些常見算法

來自:ProgramCreek

聲明:本內(nèi)容系學(xué)者網(wǎng)用戶個人學(xué)術(shù)動態(tài)分享,不代表平臺立場。

評論 3

ChaoChang 2018-04-03 19:25
Nice article. Understood the topic very well. Thanks for sharing. Cheers, http://www.flowerbrackets.com/how-to-implement-quick-sort-in-java-program/
李亞文 2014-04-11 21:25
CSDN....
ChaoChang 回復(fù) 李亞文 2018-04-03 19:27
CSDN - Chinese Software Developer Network
廣東藥科大學(xué) 醫(yī)藥信息工程學(xué)院
SCHOLAT.com 學(xué)者網(wǎng)
免責(zé)聲明 | 關(guān)于我們 | 聯(lián)系我們
聯(lián)系我們:
返回頂部
股票| 滨州市| 河北区| 漯河市| 抚松县| 凉城县| 阜城县| 雷波县| 芦溪县| 民乐县| 双流县| 辽宁省| 富川| 临颍县| 桃园县| 营山县| 南和县| 平舆县| 盘山县| 琼海市| 乌兰浩特市| 吐鲁番市| 信丰县| 开江县| 土默特右旗| 新宾| 苏州市| 宁国市| 灌阳县| 沾益县| 临猗县| 资兴市| 六枝特区| 玛曲县| 泰兴市| 临猗县| 右玉县| 景德镇市| 金乡县| 汉川市| 云龙县|