From d8a36707894ed0f98e3c1aa096763b6948a41299 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Thu, 22 Feb 2018 14:47:54 +0800 Subject: [PATCH] auto commit --- notes/2016 校招真题题解.md | 160 ++--- notes/HTTP.md | 432 ++++++------- notes/JVM.md | 642 +++++++++--------- notes/Java IO.md | 282 ++++---- notes/Java 基础语法.md | 118 ++-- notes/Java 容器.md | 140 ++-- notes/Leetcode 题解.md | 1220 +++++++++++++++++------------------ notes/Linux.md | 852 ++++++++++++------------ notes/MySQL.md | 396 ++++++------ notes/SQL 语法.md | 500 +++++++------- notes/剑指 offer 题解.md | 530 +++++++-------- notes/程序员的职业素养.md | 142 ++-- notes/算法.md | 649 +++++++++---------- notes/编写可读代码的艺术.md | 192 +++--- notes/计算机操作系统.md | 632 +++++++++--------- notes/计算机网络.md | 804 +++++++++++------------ notes/设计模式.md | 420 ++++++------ notes/重构.md | 736 ++++++++++----------- notes/面向对象思想.md | 224 +++---- notes/黑客与画家.md | 160 ++--- 20 files changed, 4611 insertions(+), 4620 deletions(-) diff --git a/notes/2016 校招真题题解.md b/notes/2016 校招真题题解.md index b50cce0c..0285debe 100644 --- a/notes/2016 校招真题题解.md +++ b/notes/2016 校招真题题解.md @@ -1,33 +1,33 @@ -* [???](#???) -* [1. ??-??Git](#1-??-??git) -* [2. ??-????????](#2-??-????????) -* [3. ??-?????](#3-??-?????) -* [4. ???-LUCKY STRING](#4-???-lucky-string) -* [5. ???-Numeric Keypad](#5-???-numeric-keypad) -* [6. ???-Spring Outing](#6-???-spring-outing) -* [7. ???-S-expression](#7-???-s-expression) -* [8. ???-?????????](#8-???-?????????) -* [9. ???-???????](#9-???-???????) -* [10. ???-??????](#10-???-??????) -* [11. ????-???????](#11-????-???????) -* [12. ????-?????????](#12-????-?????????) -* [13. ????-???Coder](#13-????-???coder) -* [14. ????-?????](#14-????-?????) -* [15. ????-??????](#15-????-??????) -* [16. ????-???](#16-????-???) -* [17. ????-????????????](#17-????-????????????) -* [18. ????-?????????](#18-????-?????????) -* [19. ????-???????](#19-????-???????) -* [20. ???-?????](#20-???-?????) -* [22. ???-???????](#22-???-???????) -* [23. ???-???????](#23-???-???????) -* [24. ???-?????](#24-???-?????) +* [ǰ](#ǰ) +* [1. С-СGit](#1-С-Сgit) +* [2. С-](#2-С-) +* [3. С-йţ](#3-С-йţ) +* [4. ΢-LUCKY STRING](#4-΢-lucky-string) +* [5. ΢-Numeric Keypad](#5-΢-numeric-keypad) +* [6. ΢-Spring Outing](#6-΢-spring-outing) +* [7. ΢-S-expression](#7-΢-s-expression) +* [8. Ϊ-߷Ƕ](#8-Ϊ-߷Ƕ) +* [9. Ϊ-򵥴¼](#9-Ϊ-򵥴¼) +* [10. Ϊ-˿ƴС](#10-Ϊ-˿ƴС) +* [11. ȥĶ-ֲ](#11-ȥĶ-ֲ) +* [12. ȥĶ-׸ظַ](#12-ȥĶ-׸ظַ) +* [13. ȥĶ-ѰCoder](#13-ȥĶ-Ѱcoder) +* [14. -ֵ](#14--ֵ) +* [15. -ӷת](#15--ӷת) +* [16. -ݷ](#16--ݷ) +* [17. -ֱͼ](#17--ֱͼ) +* [18. -ַ](#18--ַ) +* [19. -ƽ](#19--ƽ) +* [20. ٶ-ﷸת](#20-ٶ-ﷸת) +* [22. ٶ-üֽ](#22-ٶ-üֽ) +* [23. ٶ-](#23-ٶ-) +* [24. ٶ-Ģ](#24-ٶ-Ģ) -# ??? +# ǰ -??????? +ʡԵĴ룺 ```java import java.util.*; @@ -48,10 +48,10 @@ public class Main { } ``` -# 1. ??-??Git +# 1. С-СGit -- ???????? -- ??? LCA +- ؽ +- ʹ LCA ```java private class TreeNode { @@ -65,7 +65,7 @@ private class TreeNode { public int getSplitNode(String[] matrix, int indexA, int indexB) { int n = matrix.length; - boolean[][] linked = new boolean[n][n]; // ????????? + boolean[][] linked = new boolean[n][n]; // ؽڽӾ for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { linked[i][j] = matrix[i].charAt(j) == '1'; @@ -80,7 +80,7 @@ private TreeNode constructTree(boolean[][] linked, int root) { TreeNode tree = new TreeNode(root); for (int i = 0; i < linked[root].length; i++) { if (linked[root][i]) { - linked[i][root] = false; // ??????????????????????????????????????????? + linked[i][root] = false; // ΪĿڽӾ˫ģҪתΪ tree.childs.add(constructTree(links, i)); } } @@ -102,9 +102,9 @@ private TreeNode LCA(TreeNode root, TreeNode p, TreeNode q) { } ``` -# 2. ??-???????? +# 2. С- -??????????????????????????? 1 ?????????????????????? +򣬽ĶƱʾΪ 1 һλͬλ ```java public int countBitDiff(int m, int n) { @@ -112,11 +112,11 @@ public int countBitDiff(int m, int n) { } ``` -# 3. ??-????? +# 3. С-йţ -???????????????????? 2 ??????? +⣬һСΪ 2 ı -????????????? +״̬תƷ£ ```html dp[i, j] = max(dp[i, j-1], prices[j] - prices[jj] + dp[i-1, jj]) { jj in range of [0, j-1] } = max(dp[i, j-1], prices[j] + max(dp[i-1, jj] - prices[jj])) @@ -137,10 +137,10 @@ public int calculateMax(int[] prices) { } ``` -# 4. ???-LUCKY STRING +# 4. ΢-LUCKY STRING -- ????????????????? -- ??????????????????????????????????? Set ??????? i ?? j ?????????????? Set ?????????????????? Set ??????????????????? +- 쳲пԤ㣻 +- ͷβַḶ́ÿһѭʹһ Set i j ֵַ Set ַ֤ͬ Set ĴСDzַͬĸ ```java Set fibSet = new HashSet<>(Arrays.asList(1, 2, 3, 5, 8, 13, 21, 34, 55, 89)); @@ -165,7 +165,7 @@ for (String s : arr) { } ``` -# 5. ???-Numeric Keypad +# 5. ΢-Numeric Keypad ```java private static int[][] canReach = { @@ -209,17 +209,17 @@ public static void main(String[] args) { } ``` -# 6. ???-Spring Outing +# 6. ΢-Spring Outing -?????? N = 3??K = 4 ??????????? + N = 3K = 4 ۡ -????????? 0 ?????????????????????????? +ʼʱ 0 طΪص㣬ҲǴڼ -??? 4 ??????????????????????? 4 ???????? 0 ????????????????????????????????????? 4 ??????????????? 4 ??????????????? +ӵ 4 ص㿪ʼͶƱÿֻҪȽϵ 4 ط͵ 0 طȼѡ˵ 4 طôµ 4 طΪص㡣 -???????????????????k?????????????????????????????? +Ӻǰظϲ裬ϸ´ص㣬ֱеطѾͶƱ -???????????????? 0 ???????????????????????????? 4 ???????????????????????????????????????????????????? 1 ????????????????????? 2 ??????????????????????????? 2 ???????? 1 ????????????????????????????????????? +У 0 صΪص㣬ΪĻ 4 صֻҪصбȽϣÿʼ 1 صΪص㣬ôڶԵ 2 صͶƱʱÿ˲Ҫǵ 2 ص 1 صȼҲҪͶƱصȼ ```java int N = in.nextInt(); @@ -246,9 +246,9 @@ for (int place = K; place > 0; place--) { System.out.println(ret == 0 ? "otaku" : ret); ``` -# 7. ???-S-expression +# 7. ΢-S-expression -# 8. ???-????????? +# 8. Ϊ-߷Ƕ ```java int N = in.nextInt(); @@ -280,7 +280,7 @@ for (int i = 0; i < M; i++) { } ``` -# 9. ???-??????? +# 9. Ϊ-򵥴¼ ```java HashMap map = new LinkedHashMap<>(); @@ -300,7 +300,7 @@ for (int i = 0; i < 8 && i < list.size(); i++) { } ``` -# 10. ???-?????? +# 10. Ϊ-˿ƴС ```java public class Main { @@ -391,12 +391,12 @@ public class Main { } ``` -# 11. ????-??????? +# 11. ȥĶ-ֲ -???????????????????????????????????????? +ظԪص飬ֲҪעҪ㣺 - if (val <= A[m]) h = m; -- ??? h ????? m ?????? m - 1????? while ????????????? l < h????????? m - 1 ???????? l <= h?? +- Ϊ h ĸֵΪ m m - 1 while ѭҲΪ l < h m - 1 ѭΪ l <= h ```java public int getPos(int[] A, int n, int val) { @@ -410,7 +410,7 @@ public int getPos(int[] A, int n, int val) { } ``` -# 12. ????-????????? +# 12. ȥĶ-׸ظַ ```java public char findFirstRepeat(String A, int n) { @@ -424,7 +424,7 @@ public char findFirstRepeat(String A, int n) { } ``` -# 13. ????-???Coder +# 13. ȥĶ-ѰCoder ```java public String[] findCoder(String[] A, int n) { @@ -450,7 +450,7 @@ public String[] findCoder(String[] A, int n) { return ret; } -// ???????????? javafx.util.Pair???????????????? Pair ?? +// ţ޷ javafx.util.PairԼʵһ Pair private class Pair { T t; K k; @@ -470,9 +470,9 @@ private class Pair { } ``` -# 14. ????-????? +# 14. -ֵ -??????? +̰IJԡ ```java public int getDis(int[] A, int n) { @@ -486,7 +486,7 @@ public int getDis(int[] A, int n) { } ``` -# 15. ????-?????? +# 15. -ӷת ```java public int[][] flipChess(int[][] A, int[][] f) { @@ -502,7 +502,7 @@ public int[][] flipChess(int[][] A, int[][] f) { } ``` -# 16. ????-??? +# 16. -ݷ ```java private Set paths; @@ -554,7 +554,7 @@ private void backtracking(int[][] map, int n, int m, int r, int c, int[][] direc } ``` -# 17. ????-???????????? +# 17. -ֱͼ ```java public int countArea(int[] A, int n) { @@ -570,15 +570,15 @@ public int countArea(int[] A, int n) { } ``` -# 18. ????-????????? +# 18. -ַ -?????????????????????????????? 26 ???????????????????????????????????????????????????? "ac" ?? "abc"????????????? "ac" > "abc"???????????????????????? "abc" ??????????????? +ַСдַ԰ַ 26 ơֵıȽϺͨȽϲͬǴҽбȽϣ "ac" "abc"ֵıȽϽΪ "ac" > "abc"ȽϣΪ "abc" λȻ -???????????????????????????? s1 ??????? s2 ?????????????????? s1 ???????????? 'a' ???????????? +ַijȿܲȣ s1 հײֺ s2 ӦֽбȽʱӦð s1 Ŀհײֿ 'a' ַġ -???????????????s1 ?? s2 ????? leni ?????????????????? i ??????????? 'aaa' ?? 'bbb' ??????? 2 ?????? 'aa' ?? 'bb' ???????????????????????????????? +һҪעǣs1 s2 Ϊ leni ַֻȽǰ i ַ 'aaa' 'bbb' Ϊ 2 ĸΪ 'aa' 'bb' ַҪǺ沿ֵַ -????????????? len1 ???????????????????????????????????? i ??????????????? +ͳƸʱ len1 ʼһֱϷȣÿѭͳƳΪ i ַ ```java String s1 = in.next(); @@ -601,7 +601,7 @@ for (int i = len1; i <= len; i++) { System.out.println(ret - 1); ``` -# 19. ????-??????? +# 19. -ƽ ```java int W = in.nextInt(); @@ -609,15 +609,15 @@ double Y = in.nextDouble(); double x = in.nextDouble(); int N = in.nextInt(); while (N-- > 0) { - Y++; // ??????????????? 1 + Y++; // Աÿ䶼Ҫ 1 Y += (21 - Y) * x; } System.out.println((int) Math.ceil(Y)); ``` -# 20. ???-????? +# 20. ٶ-ﷸת -?????????????????????????????? +ֺ⣬ÿIJֺͻ ```java int n = in.nextInt(); @@ -640,7 +640,7 @@ for (int s = 0, e = c - 1; e < n; s++, e++) { System.out.println(cnt); ``` -# 22. ???-??????? +# 22. ٶ-üֽ ```java int n = in.nextInt(); @@ -658,11 +658,11 @@ for (int i = 0; i < n; i++) { System.out.println((int) Math.pow(Math.max(maxX - minX, maxY - minY), 2)); ``` -# 23. ???-??????? +# 23. ٶ- -P ( ?????????? ) = 1 - P ( ?????????? ) +P ( ٵһ ) = 1 - P ( һҲ ) -?????????????????????????????????????????? in.nextDouble()?? +ӣȡʾʱҪһһнжȡֱ in.nextDouble() ```java public static void main(String[] args) { @@ -673,11 +673,11 @@ public static void main(String[] args) { int x = in.nextInt(); int y = in.nextInt(); int t = in.nextInt(); - in.nextLine(); // ?? + in.nextLine(); // double pcc = 0.0; double sum = 0.0; for (int i = 1; i <= n; i++) { - String[] token = in.nextLine().split(" "); // ?? + String[] token = in.nextLine().split(" "); // for (int j = 1; j <= m; j++) { double p = Double.parseDouble(token[j - 1]); // double p = in.nextDouble(); @@ -701,13 +701,13 @@ private static double computePOfIRT(double p, int t) { } ``` -# 24. ???-????? +# 24. ٶ-Ģ -?????????????????? DP?? +ûݻᳬʱҪ DP -dp[i][j] ??????? (i,j) ??????????????????? N\*M ??????? i == N || j == M????? (i,j) ??????????????????????????????????????? +dp[i][j] ʾ (i,j) λòᴥĢĸʡ N\*M i == N || j == Mô (i,j) ֻһƶƶ -??????????????? 3 ??? 3 ????????????????????????????????????????????? +¾е 3 к͵ 3 ֻһƶλÿƶ ```java diff --git a/notes/HTTP.md b/notes/HTTP.md index b29f4a31..f17b79c6 100644 --- a/notes/HTTP.md +++ b/notes/HTTP.md @@ -1,83 +1,83 @@ -* [????????](#????????) - * [Web????](#web????) +* [](#) + * [Web](#web) * [URL](#url) - * [????????????](#????????????) -* [HTTP ????](#http-????) - * [GET????????](#get????????) - * [POST?????????????](#post?????????????) - * [HEAD????????????](#head????????????) - * [PUT????????](#put???????) - * [DELETE????????](#delete????????) - * [OPTIONS????????????](#options????????????) - * [RACE???????](#race???????) - * [CONNECT???????????????????](#connect???????????????????) -* [HTTP ????](#http-????) - * [2XX ???](#2xx-???) - * [3XX ?????](#3xx-?????) - * [4XX ????????](#4xx-????????) - * [5XX ??????????](#5xx-??????????) -* [HTTP???](#http???) - * [?????????](#?????????) - * [??????????](#??????????) - * [?????????](#?????????) - * [?????????](#?????????) -* [???????](#???????) + * [Ӧ](#Ӧ) +* [HTTP ](#http-) + * [GETȡԴ](#getȡԴ) + * [POSTʵ](#postʵ) + * [HEADȡײ](#headȡײ) + * [PUTϴļ](#putϴļ) + * [DELETEɾļ](#deleteɾļ) + * [OPTIONSѯֵ֧ķ](#optionsѯֵ֧ķ) + * [RACE׷·](#race׷·) + * [CONNECTҪЭӴ](#connectҪЭӴ) +* [HTTP ״̬](#http-״̬) + * [2XX ɹ](#2xx-ɹ) + * [3XX ض](#3xx-ض) + * [4XX ͻ˴](#4xx-ͻ˴) + * [5XX ](#5xx-) +* [HTTPײ](#httpײ) + * [ͨײֶ](#ͨײֶ) + * [ײֶ](#ײֶ) + * [Ӧײֶ](#Ӧײֶ) + * [ʵײֶ](#ʵײֶ) +* [Ӧ](#Ӧ) * [Cookie](#cookie) - * [????](#????) - * [???????](#???????) - * [????](#????) - * [????](#????) - * [????????](#????????) - * [??????](#??????) - * [??????](#??????) - * [????????](#????????) - * [??????????](#??????????) + * [](#) + * [־](#־) + * [](#) + * [ֿ鴫](#ֿ鴫) + * [ಿֶ󼯺](#ಿֶ󼯺) + * [Χ](#Χ) + * [Э](#Э) + * [](#) + * [ͨת](#ͨת) * [HTTPs](#https) - * [????](#????) - * [???](#???) - * [??????](#??????) + * [](#) + * [֤](#֤) + * [](#) -# ???????? +# -## Web???? +## Web -HTTP??HyperText Transfer Protocol?????????????? +HTTPHyperText Transfer ProtocolΪЭ飩 -WWW??Word Wide Web?????????????HTML??HTTP??URL?? +WWWWord Wide WebּHTMLHTTPURL -RFC??Request for Comments???????????????????????????????? +RFCRequest for Comments飩ĵ ## URL -URI??Uniform Resource Indentifier????????????????URL??Uniform Resource Locator???????????????URN??Uniform Resource Name?????????????????? urn:isbn:0-486-27557-4 ??URI ???? URL ?? URN???? WEB ??? URL ??????????????????????? URL?? +URIUniform Resource IndentifierͳһԴʶURLUniform Resource LocatorͳһԴλURNUniform Resource NameͳһԴƣ urn:isbn:0-486-27557-4 URI URL URNĿǰ WEB ֻ URL ȽУԼĻ URL -URL????? +URLʽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4102b7d0-39b9-48d8-82ae-ac4addb7ebfb.jpg) -## ???????????? +## Ӧ -**??????** +**** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9dbb5fc2-936b-4c6d-b3a7-9617aae45080.jpg) -**???????** +**Ӧ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c634b5ed-a14b-4302-b40e-3ee387dd3c8a.jpg) -# HTTP ???? +# HTTP -?????????????????????????????????????? +ͻ˷͵ĵһΪУ˷ֶΡ -## GET???????? +## GETȡԴ -## POST????????????? +## POSTʵ -POST ??????????????????????????????????? +POST ҪĿIJǻȡԴǴʵݡ -GET ?? POST ???????????????????????? GET ?????????????????????? URL???? POST ????????????????? +GET POST ʹöIJ GET IJԲѯַ URLУ POST IJ洢ʵ岿֡ ``` GET /test/demo_form.asp?name1=value1&name2=value2 HTTP/1.1 @@ -88,312 +88,312 @@ Host: w3schools.com name1=value1&name2=value2 ``` -GET ??????????? POST ?????????? GET ????????? URL ????????????????????????? GET ???? ASCII ???????????????????????????????? POST ???????????? +GET Ĵηʽ POST ȫԽϲΪ GET IJ URL ǿɼģܻй¶˽Ϣ GET ֻ֧ ASCII ַΪܻ룬 POST ֱ֧׼ַ -## HEAD???????????? +## HEADȡײ -?? GET ?????????????????????????????? + GET һDzرʵ岿֡ -?????????? URL ?????????????????????????? +Ҫȷ URL ЧԼԴµʱȡ -## PUT???????? +## PUTϴļ -???????????????????????????????????????????????????? WEB ????????????? +֤ƣκ˶ϴļ˴ڰȫ⣬һ WEB վʹø÷ -## DELETE???????? +## DELETEɾļ -?? PUT ??????????????????????????? + PUT ෴֤ͬơ -## OPTIONS???????????? +## OPTIONSѯֵ֧ķ -???????? URL ???????????? +ѯָ URL ֵܹ֧ķ -???? Allow: GET, POST, HEAD, OPTIONS ??????????? +᷵ Allow: GET, POST, HEAD, OPTIONS ݡ -## RACE??????? +## RACE׷· -???????????????????????? +Ὣͨ·ظͻˡ -????????????? Max-Forwards ??????????????????????????????????? 1???????? 0 ????????? +ʱ Max-Forwards ײֵֶÿһͻ 1ֵΪ 0 ʱֹͣ䡣 -TRACE ?????????????????????? XST ??????Cross-Site Tracing??????????????????????????? +TRACE һ㲻ʹãܵ XST Cross-Site Tracingվ׷٣˸ȥʹ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ca711108-e937-4d7d-99aa-61b325c61f1a.jpg) -## CONNECT??????????????????? +## CONNECTҪЭӴ -?????????? TCP ???? +Э TCP ͨš -?????? SSL??Secure Sokets Layer????????????? TLS??Transport Layer Security??????????????????????????????????? +Ҫʹ SSLSecure Sokets Layerȫ׽֣ TLSTransport Layer Security㰲ȫЭͨݼܺ䡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d8355d56-aa2b-4452-8001-8475cc095af1.jpg) -# HTTP ???? +# HTTP ״̬ -?????????????????????????????????????????????????????????????????? +صӦеһΪ״̬У״̬Լԭ֪ͻĽ -| ???? | ??? | ?????? | +| ״̬ | | ԭ | | --- | --- | --- | -| 1XX | Informational??????????? | ???????????????? | -| 2XX | Success????????? | ??????????????? | -| 3XX | Redirection??????????? | ????????????????????? | -| 4XX | Client Error?????????????? | ????????????????? | -| 5XX | Server Error???????????????? | ????????????????? | +| 1XX | InformationalϢ״̬룩 | յڴ | +| 2XX | Successɹ״̬룩 | | +| 3XX | Redirectionض״̬룩 | ҪиӲ | +| 4XX | Client Errorͻ˴״̬룩 | ޷ | +| 5XX | Server Error״̬룩 | | -## 2XX ??? +## 2XX ɹ **200 OK** -**204 No Content**????????????????????????????????????????????????????????????????????????????????????????????????? +**204 No Content**ѾɹǷصӦIJʵ岿֡һֻҪӿͻϢҪʱʹá **206 Partial Content** -## 3XX ????? +## 3XX ض -**301 Moved Permanently**????????????? +**301 Moved Permanently**ض -**302 Found**???????????? +**302 Found**ʱض **303 See Other** -?????? HTTP ?? 301??302 ????????????????? POST ??????? GET ???????????????????????? 301??302 ?? 303 ??????????? POST ??????? GET ?????? +עȻ HTTP Э涨 301302 ״̬ضʱ POST ij GET Ǵ 301302 303 ״̬µض POST ij GET -**304 Not Modified**????????????????????????????If-Match??If-ModifiedSince??If-None-Match??If-Range??If-Unmodified-Since???????????????????????????? 304 ???? +**304 Not Modified**ײһЩ磺If-MatchIf-ModifiedSinceIf-None-MatchIf-RangeIf-Unmodified-SinceDz᷵ 304 ״̬롣 -**307 Temporary Redirect**???????????? 302 ?????????????? 307 ??????????????????????? POST ??????? GET ?????? +**307 Temporary Redirect**ʱض 302 ĺƣ 307 Ҫض POST ij GET -## 4XX ???????? +## 4XX ͻ˴ -**400 Bad Request**?????????????????? +**400 Bad Request**д﷨ -**401 Unauthorized**??????????????????????????? HTTP ?????BASIC ?????DIGEST ?????????????????????????????????????????????? +**401 Unauthorized**״̬ʾ͵Ҫͨ HTTP ֤BASIC ֤DIGEST ֤֤Ϣ֮ǰѽйһʾû֤ʧܡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b1b4cf7d-c54a-4ff1-9741-cd2eea331123.jpg) -**403 Forbidden**?????????????????????????????????????? +**403 Forbidden**󱻾ܾûбҪܾϸɡ **404 Not Found** -## 5XX ?????????? +## 5XX -**500 Internal Server Error**???????????????????????????? +**500 Internal Server Error**ִʱ -**503 Service Unavilable**???????????????????????????????????????????????????????????? +**503 Service Unavilable**״̬ʱڳػڽͣά޷ -# HTTP??? +# HTTPײ -HTTP ??????????????????????????? 4 ???????????????????????????????????????????????????????????????????????????????????????? +HTTP İײ֡ 4 ͵ײֶΣͨײֶΡײֶΡӦײֶκʵײֶΡײֶμ京£Ҫȫǣģ -## ????????? +## ͨײֶ -| ???????? | ??? | +| ײֶ | ˵ | | -- | -- | -| Cache-Control | ??????????? | -| Connection | ????????? ???????? | -| Date | ???????????????? | -| Pragma | ??????? | -| Trailer | ?????????????? | -| Transfer-Encoding | ??????????????????? | -| Upgrade | ??????????? | -| Via | ????????????????? | -| Warning | ?????? | +| Cache-Control | ƻΪ | +| Connection | ײ ӵĹ | +| Date | ĵʱ | +| Pragma | ָ | +| Trailer | ĩ˵ײһ | +| Transfer-Encoding | ָĴ뷽ʽ | +| Upgrade | ΪЭ | +| Via | Ϣ | +| Warning | ֪ͨ | -## ?????????? +## ײֶ -| ???????? | ??? | +| ײֶ | ˵ | | -- | -- | -| Accept | ??????????????????? | -| Accept-Charset | ?????????? | -| Accept-Encoding | ???????????? | -| Accept-Language | ?????????????????? | -| Authorization | Web?????? | -| Expect | ????????????????? | -| From | ?????????????? | -| Host | ???????????????? | -| If-Match | ?????????ETag?? | -| If-Modified-Since | ?????????????? | -| If-None-Match | ??????????? If-Match ???? | -| If-Range | ??????????????? Byte ??????? | -| If-Unmodified-Since | ????????????????If-Modified-Since???? | -| Max-Forwards | ??????????? | -| Proxy-Authorization | ??????????????????????? | -| Range | ???????????? | -| Referer | ???????? URI ????????? | -| TE | ????????????? | -| User-Agent | HTTP ???????????? | +| Accept | ûɴý | +| Accept-Charset | ȵַ | +| Accept-Encoding | ȵݱ | +| Accept-Language | ȵԣȻԣ | +| Authorization | Web֤Ϣ | +| Expect | ڴضΪ | +| From | ûĵַ | +| Host | Դڷ | +| If-Match | ȽʵǣETag | +| If-Modified-Since | ȽԴĸʱ | +| If-None-Match | Ƚʵǣ If-Match ෴ | +| If-Range | Դδʱʵ Byte ķΧ | +| If-Unmodified-Since | ȽԴĸʱ䣨If-Modified-Since෴ | +| Max-Forwards | | +| Proxy-Authorization | Ҫͻ˵֤Ϣ | +| Range | ʵֽڷΧ | +| Referer | URI ԭʼȡ | +| TE | ȼ | +| User-Agent | HTTP ͻ˳Ϣ | -## ????????? +## Ӧײֶ -| ???????? | ??? | +| ײֶ | ˵ | | -- | -- | -| Accept-Ranges | ?????????????? | -| Age | ?????????????????? | -| ETag | ??????????? | -| Location | ????????????????URI | -| Proxy-Authenticate | ?????????????????????? | -| Retry-After | ?????????????????? | -| Server | HTTP????????????? | -| Vary | ???????????????????? | -| WWW-Authenticate | ??????????????????? | +| Accept-Ranges | ǷֽڷΧ | +| Age | Դʱ | +| ETag | ԴƥϢ | +| Location | ͻضָURI | +| Proxy-Authenticate | Կͻ˵֤Ϣ | +| Retry-After | ٴηʱҪ | +| Server | HTTPİװϢ | +| Vary | ĹϢ | +| WWW-Authenticate | Կͻ˵֤Ϣ | -## ????????? +## ʵײֶ -| ???????? | ??? | +| ײֶ | ˵ | | -- | -- | -| Allow | ?????????HTTP???? | -| Content-Encoding | ???????????????? | -| Content-Language | ??????????????? | -| Content-Length | ??????????????? ???? | -| Content-Location | ???????????URI | -| Content-MD5 | ????????????? | -| Content-Range | ?????????? | -| Content-Type | ??????????????? | -| Expires | ?????????????????? | -| Last-Modified | ?????????????????? | +| Allow | Դֵ֧HTTP | +| Content-Encoding | ʵõı뷽ʽ | +| Content-Language | ʵȻ | +| Content-Length | ʵĴСλ ֽڣ | +| Content-Location | ӦԴURI | +| Content-MD5 | ʵıժҪ | +| Content-Range | ʵλ÷Χ | +| Content-Type | ʵý | +| Expires | ʵڵʱ | +| Last-Modified | Դ޸ʱ | -# ??????? +# Ӧ ## Cookie -HTTP ????????????????????? HTTP ??????????????????????????HTTP/1.1 ???? Cookie ????????????? +HTTP Э״̬ģҪΪ HTTP Э龡ܼ򵥣ʹܹHTTP/1.1 Cookie ״̬Ϣ -????????????????????? Set-Cookie ?????????????????? Cookie ??????????????????????????????????????? Cookie ??????????????? Cookie ????????????????????????????????Cookie ???????????????????????????????????? +ᷢ͵Ӧİ Set-Cookie ֶΣͻ˵õӦ Cookie ݱ浽С´ٷʱж Cookie ֵа Cookie ֶΣ֪ͻ˵״̬ϢˡCookie ״̬ϢڿͻУǷϡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ff17c103-750a-4bb8-9afa-576327023af9.png) -Set-Cookie ?????????????? +Set-Cookie ֶԣ -| ???? | ??? | +| | ˵ | | -- | -- | -| NAME=VALUE | ???? Cookie ?????????????????? | -| expires=DATE | Cookie ???????????????????????????????????? | -| path=PATH | ??????????????????? Cookie ????????????????????????????????????? | -| domain=???? | ??? Cookie ??????????????????????????????? Cookie ??????????????? | -| Secure | ???? HTTPS ???????????? Cookie | -| HttpOnly | ?????????? Cookie ????? JavaScript ??????? | +| NAME=VALUE | Cookie ƺֵ | +| expires=DATE | Cookie ЧڣȷָĬΪرǰΪֹ | +| path=PATH | ϵļĿ¼Ϊ Cookie öָĬΪĵڵļĿ¼ | +| domain= | Ϊ Cookie öָĬΪ Cookie ķ | +| Secure | HTTPS ȫͨʱŻᷢ Cookie | +| HttpOnly | ƣʹ Cookie ܱ JavaScript ű | -**Session ?? Cookie ????** +**Session Cookie ** -Session ????????????????????????????? Session ?????????????Session ID??????????????????? Session ??????????????????????????? Set-Cookie ??????????????? sid ?????????????????? Session ID?????????????? Cookie ?????????????????????????????????? Session ID??HTTP ???? Session ?? Cookie ???????????????????????????????? Session ????????????Cookie ????????? +Session ǷûһֶΣÿ Session һΨһʶSession IDһ Session ʱͻ˷͵ӦľͰ Set-Cookie ֶΣһΪ sid ļֵԣֵԾ Session IDͻյͰ Cookie У֮͵Ķ Session IDHTTP Session Cookie ַʽһʵָû״̬ģ Session ڷˣCookie ڿͻˡ -**????????? Cookie ?????** +** Cookie ** -????? URL ?????????? URL ??????? sid=xxx ?? +ʹ URL д URL sid=xxx -**??? Cookie ????????????????????** +**ʹ Cookie ʵûԶд** -????????????? Cookie ??????????????????????????? +վűԶ Cookie жȡû룬ӶʵԶд -## ???? +## -????????]????????????????????????????????????? +ֻ淽ôлÿͻл档 -Cache-Control ???????????????? +Cache-Control ڿƻΪ -Cache-Control: no-cache ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +Cache-Control: no-cache ֺ壬ǿͻ򻺴͵киָʾͻ˲ҪԴԴ򻺴͵ӦкиָʾܶԴл档 -Expires ??????????????????????????????????????????? Cache-Control ????? max-age ???????????????? Expires??????????? max-age ?? +Expires ֶοڸ֪Դʲôʱڡײֶ Cache-Control ָ max-age ָʱײֶ Expiresȴ max-age ָ -## ??????? +## ־ -?????????????????????????? HTML ????????????????? HTML ?????????????????????????????????? HTTP ??????????? TCP ?????????????????????????**???????** ??????????? TCP ??????????? HTTP ????HTTP/1.1?????????????????????????? +һͼƬ HTML ҳʱ HTML ҳԴͼƬԴÿһ HTTP ͨžҪϿһ TCP ӣӽͶϿĿܴ**־** ֻҪһ TCP Ӿܽж HTTP ͨšHTTP/1.1ʼеĬ϶dz־ӡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c73a0b78-5f46-4d2d-a009-dab2a999b5d8.jpg) -????????????? Connection ??????????HTTP/1.1 ???HTTP ????????????????????? TCP ??????????????????????????????????? Connection: close ??????HTTP/1.1?????????????????????????????????????? Keep-Alive?? +־Ҫʹ Connection ײֶνйHTTP/1.1 ʼHTTP Ĭdz־ûӵģҪϿ TCP ӣҪɿͻ˻߷Ͽʹ Connection: close HTTP/1.1֮ǰĬǷdz־ûӵģҪάֳӣҪʹ Keep-Alive -??????????????????????????????????????????????????????????????????????? +߻ʽͬʱͶӦҪһȻȴӦ֮ٷһ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6943e2af-5a70-4004-8bee-b33d60f39da3.jpg) -## ???? +## -????Encoding????????????????????????????????gzip??compress??deflate??identity?????? identity ???????????????????? +루EncodingҪΪ˶ʵѹõıУgzipcompressdeflateidentity identity ʾִѹıʽ -## ???? +## ֿ鴫 -????Chunked Transfer Coding????????????????????????????? +ֿ鴫䣨Chunked Transfer Coding԰ݷָɶ飬ʾҳ档 -## ???????? +## ಿֶ󼯺 -??????????????????????????????????????????? boundary ????????????????????????????????????? +һݱڿɺж͵ʵͬʱͣÿ֮ boundary ֶζķָзָÿֶײֶΡ -?????????????????????????? +磬ϴʱʹ·ʽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/decb0936-e83c-4a55-840a-fe8aa101ac61.png) -## ?????? +## Χ -????????????????????????????????????????????????????????????????????????????????????????????????????? +жϣֻһݣΧʹÿͻֻܹδ͵DzݣӶ·ݡ -???????????????? Range ????????????????????? Range : bytes = 5001-10000????????????????????? 206 Partial Content ???? +ײ Range ֶΣȻָķΧ Range : bytes = 5001-10000ɹĻ 206 Partial Content ״̬ -## ?????? +## Э -????????????????????????????????????????????????????????????? +ͨЭ̷ʵݣĬѡ񷵻Ľ滹ӢĽ档 -?p??????????Accept??Accept-Charset??Accept-Encoding??Accept-Language??Content-Language?? +漰ײֶΣAcceptAccept-CharsetAccept-EncodingAccept-LanguageContent-Language -## ???????? +## -????????????????????????????????????????????????????????????????? +ʹʹһ̨ӵж߼ϿԿɶ -## ?????????? +## ͨת -**????** +**** -???????????????????????????????????????????????????????????????????? URL?? +ܿͻ˵󣬲תһ͸ģı URL -???????????????????????????????????????????? +ʹôҪĿǣ桢ʿԼ¼־ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c07035c3-a9ba-4508-8e3c-d8ae4c6ee9ee.jpg) -**????** +**** -????????????????????????????? HTTP ?????????????????????????? HTTP ??????????? +ͬǣطὫ HTTP תΪЭͨţӶ HTTP ķ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/81375888-6be1-476f-9521-42eea3e3154f.jpg) -**???** +**** -??? SSL ??????????????????????????????????????? +ʹ SSL ȼֶΣΪͻ˺ͷ֮佨һȫͨ· ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/64b95403-d976-421a-8b45-bac89c0b5185.jpg) # HTTPs -HTTP ????????????? +HTTP °ȫ⣺ -1. ????????????????????????? -2. ????????????????????????????? -3. ????????????????????????????????? +1. ͨʹģݿܻᱻ +2. ֤ͨŷݣпαװ +3. ޷֤ĵԣп۸ġ -HTTPs ????????????? HTTP ??? SSL??Secure Socket Layer?????????? SSL ?? TCP ?????????? SSL??HTTPs ???????????????????????? +HTTPs Э飬 HTTP Ⱥ SSLSecure Socket Layerͨţ SSL TCP ͨšͨʹ SSLHTTPs ṩ˼ܡ֤Ա -## ???? +## -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ּܷʽԳԿܺ͹ԿܡԳԿܵļܺͽʹͬһԿԿʹһԿڼܺͽܣֱΪԿ˽ԿԿ˶ԻãͨŷͷýշĹԿ֮󣬾ͿʹùԿмܣշյͨݺʹ˽Կܡ -????????????????????????????????????????????????????????? +ԳԿܵȱ㣺޷ȫԿԿܵȱ㣺˵ʱ -HTTPs ???? **??????????**?????????????????????????????????????????????????????????????????????????? +HTTPs **ϵļܻ**ʹùԿڴԳԿ֮ʹöԳԿܽͨšͼУԿԳԿ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/110b1a9b-87cd-45c3-a21d-824623715b33.jpg) -## ??? +## ֤ -?????? **???** ??????????????????????????????????????????????????????????????????????????????????????? +ͨʹ **֤** ͨŷ֤֤йԿݣ֤ԿȷͨŷģôͿȷͨŷǿɿġ -????????????????CA??Certificate -Authority????????????????????? CA ???????????? +֤֤CACertificate +Authority䷢ĹԿ֤飬ͨ CA ֤ -???? HTTPs ????????????????????????????????????????????????????????????? + HTTPs ͨʱ֤鷢͸ͻˣͻȡеĹԿ֮󣬾ͿԿʼ̡ܹ -??? OpenSSL ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʹ OpenSSL ׿Դÿ˶ԹһԼ֤ӶԼԼ䷢֤顣ڷʸ÷ʱʾ޷ȷӰȫԡ򡰸վİȫ֤⡱ȾϢ -????????????????????????????????????????????????????????????????? +ͻ֤ҪûаװֻҵҪdz߰ȫʱʹÿͻ֤飬С -## ?????? +## -SSL ???????????????????? +SSL ṩժҪ֤ԡ diff --git a/notes/JVM.md b/notes/JVM.md index 344d5a65..597eda7c 100644 --- a/notes/JVM.md +++ b/notes/JVM.md @@ -1,166 +1,166 @@ -* [??????](#??????) - * [1. ?????????](#1-?????????) - * [2. Java ??????](#2-java-??????) - * [3. ????????](#3-????????) - * [4. Java ??](#4-java-??) - * [5. ??????](#5-??????) - * [6. ???????????](#6-???????????) - * [7. ??????](#7-??????) -* [???????](#???????) - * [1. ????????????????](#1-????????????????) - * [1.1 ??????](#11-??????) - * [1.2 ?????](#12-?????) - * [1.3 ????????](#13-????????) - * [1.3.1 ?????](#131-?????) - * [1.3.2 ??????](#132-??????) - * [1.3.3 ??????](#133-??????) - * [1.3.4 ??????](#134-??????) - * [1.3 ???????????](#13-???????????) +* [ڴģ](#ڴģ) + * [1. ](#1-) + * [2. Java ջ](#2-java-ջ) + * [3. طջ](#3-طջ) + * [4. Java ](#4-java-) + * [5. ](#5-) + * [6. ʱ](#6-ʱ) + * [7. ֱڴ](#7-ֱڴ) +* [ռ](#ռ) + * [1. жһǷɻ](#1-жһǷɻ) + * [1.1 ü](#11-ü) + * [1.2 ɴ](#12-ɴ) + * [1.3 ](#13-) + * [1.3.1 ǿ](#131-ǿ) + * [1.3.2 ](#132-) + * [1.3.3 ](#133-) + * [1.3.4 ](#134-) + * [1.3 Ļ](#13-Ļ) * [1.4 finalize()](#14-finalize) - * [2. ?????????](#2-?????????) - * [2.1 ??? - ?????](#21-???---?????) - * [2.2 ??????](#22-??????) - * [2.3 ??? - ??????](#23-???---??????) - * [2.4 ????????](#24-????????) - * [3. ?????????](#3-?????????) - * [3.1 Serial ?????](#31-serial-?????) - * [3.2 ParNew ?????](#32-parnew-?????) - * [3.3 Parallel Scavenge ?????](#33-parallel-scavenge-?????) - * [3.4 Serial Old ?????](#34-serial-old-?????) - * [3.5 Parallel Old ?????](#35-parallel-old-?????) - * [3.6 CMS ?????](#36-cms-?????) - * [3.7 G1 ?????](#37-g1-?????) - * [3.8 ?????????????????](#38-?????????????????) - * [4. ??????????????](#4-??????????????) - * [4.1 ?????? Eden ????](#41-??????-eden-????) - * [4.2 ????????????????](#42-????????????????) - * [4.3 ??????????????????](#43-??????????????????) - * [4.4 ?????????????](#44-?????????????) - * [4.5 ????????](#45-????????) - * [4.6 Full GC ?????????](#46-full-gc-?????????) - * [4.6.1 ???? System.gc()](#461-????-systemgc) - * [4.6.2 ??????????](#462-??????????) - * [4.6.3 ???????????](#463-???????????) - * [4.6.4 JDK 1.7 ?????????????????](#464-jdk-17-?????????????????) + * [2. ռ㷨](#2-ռ㷨) + * [2.1 - 㷨](#21----㷨) + * [2.2 㷨](#22-㷨) + * [2.3 - 㷨](#23----㷨) + * [2.4 ִռ㷨](#24-ִռ㷨) + * [3. ռ](#3-ռ) + * [3.1 Serial ռ](#31-serial-ռ) + * [3.2 ParNew ռ](#32-parnew-ռ) + * [3.3 Parallel Scavenge ռ](#33-parallel-scavenge-ռ) + * [3.4 Serial Old ռ](#34-serial-old-ռ) + * [3.5 Parallel Old ռ](#35-parallel-old-ռ) + * [3.6 CMS ռ](#36-cms-ռ) + * [3.7 G1 ռ](#37-g1-ռ) + * [3.8 ռıȽ](#38-ռıȽ) + * [4. ڴղ](#4-ڴղ) + * [4.1 Eden ](#41--eden-) + * [4.2 ֱӽ](#42-ֱӽ) + * [4.3 ڴĶ](#43-ڴĶ) + * [4.4 ̬ж](#44-̬ж) + * [4.5 ռ䵣](#45-ռ䵣) + * [4.6 Full GC Ĵ](#46-full-gc-Ĵ) + * [4.6.1 System.gc()](#461--systemgc) + * [4.6.2 ռ䲻](#462-ռ䲻) + * [4.6.3 ռ䵣ʧ](#463-ռ䵣ʧ) + * [4.6.4 JDK 1.7 ǰôռ䲻](#464-jdk-17-ǰôռ䲻) * [4.6.5 Concurrent Mode Failure](#465-concurrent-mode-failure) -* [????????](#????????) - * [1 ???????????](#1-???????????) - * [2. ?????????](#2-?????????) - * [3. ????????](#3-????????) - * [3.1 ????](#31-????) - * [3.2 ???](#32-???) - * [3.3 ???](#33-???) - * [3.4 ????](#34-????) - * [3.5 ?????](#35-?????) - * [4. ???????](#4-???????) - * [4.1 ???????????](#41-???????????) - * [4.2 ???????????](#42-???????????) - * [4.3 ?????????](#43-?????????) -* [JVM ????](#jvm-????) - * [GC ???????](#gc-???????) - * [GC ????????](#gc-????????) +* [ػ](#ػ) + * [1 ](#1-) + * [2. ʼʱ](#2-ʼʱ) + * [3. ع](#3-ع) + * [3.1 ](#31-) + * [3.2 ֤](#32-֤) + * [3.3 ׼](#33-׼) + * [3.4 ](#34-) + * [3.5 ʼ](#35-ʼ) + * [4. ](#4-) + * [4.1 ](#41-) + * [4.2 ](#42-) + * [4.3 ˫ίģ](#43-˫ίģ) +* [JVM ](#jvm-) + * [GC Ż](#gc-Ż) + * [GC ](#gc-) -# ?????? +# ڴģ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/dc695f48-4189-4fc7-b950-ed25f6c80f82.jpg) -?????????????????????????????????? +עɫΪ߳˽еģɫΪ̹߳ġ -## 1. ????????? +## 1. -???????????????????????????????????????? Native ???????????? +¼ִеָֽĵִַе Native Ϊգ -## 2. Java ?????? +## 2. Java ջ -??? Java ???????????????????????????????????????????????????????????????????????????????????????????????????????? Java ????????????????????? +ÿ Java ִеͬʱᴴһջ֡ڴ洢ֲջ̬ӡڵϢÿһӵֱִɵḶ́ͶӦһջ֡ Java ջջͳջĹ̡ -???????????????????? +׳쳣 -1. ???????????????????????????? StackOverflowError ???? -2. ????????????????????????????? OutOfMemoryError ???? +1. ߳ջȳֵ׳ StackOverflowError 쳣 +2. ջж̬չʱ޷뵼㹻ڴ棬׳ OutOfMemoryError 쳣 -## 3. ???????? +## 3. طջ -?? Java ????????????????????????????????????????????????? + Java ջƣֻ֮DZطջΪط -## 4. Java ?? +## 4. Java -?????????????????????? +жʵڴ档 -??????????????????????????????"GC ?? "????????????????????????????????Java ??????????????????????????????????????? Eden ???From Survivor ???To Survivor ??????? +ռҪ"GC "ռDz÷ִռ㷨Java ѻԷֳɣԷֳ Eden ռ䡢From Survivor ռ䡢To Survivor ռȣ -?????????????????? -Xmx ?? -Xms ????????????????????????????????? OutOfMemoryError ???? +Ҫڴ棬ͨ -Xmx -Xms ƶ̬չڴС̬չʧܻ׳ OutOfMemoryError 쳣 -## 5. ?????? +## 5. -????????????????????????????????????????????????????????????????? +ڴѱصϢ̬ʱĴݡ -?? Java ????????????????????????????????????????????????? OutOfMemoryError ???? + Java һҪڴ棬ҿԶ̬չ̬չʧһ׳ OutOfMemoryError 쳣 -??????????????????????????????????????????????????????????????HotSpot ????????????????????????????????? +յҪĿǶԳصĻպͶжأһȽʵ֣HotSpot ôա -## 6. ??????????? +## 6. ʱ -?????????????????????????? +ʱǷһ֡ -??????Class ??????????????????????????????????????????????????????????? +غClass ļеijأڴűɵĸͷãͻᱻŵ -???????????????? String ??? intern() ????????????????????? +ڼҲù String intern() µij -## 7. ?????? +## 7. ֱڴ -?? JDK 1.4 ????????? NIO ???????????????????Channel??????????Buffer???? I/O ?????????????? Native ????????????????????????????? Java ????? DirectByteBuffer ??????????????????????????????????????????????????????????????? Java ??? Native ???????????????? + JDK 1.4 ¼ NIO ࣬һֻͨChannel뻺Buffer I/O ʽʹ Native ֱӷڴ棬Ȼͨһ洢 Java DirectByteBuffer ΪڴýвһЩܣΪ Java Ѻ Native ظݡ -# ??????? +# ռ -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ջͱطջ߳˽еģ̵ֻ߳ڣ֮߳̽Ҳʧ˲Ҫա -???????????????? Java ???????????? +Ҫ Java ѺͷС -## 1. ???????????????? +## 1. жһǷɻ -### 1.1 ?????? +### 1.1 ü -???????????????????????????????????????????????? 1???????????????? 1?? +һüһʱ 1ʧЧʱ 1 -??????? 0 ???????????? +üΪ 0 Ķɱա -???????????????????????????????????????? 0?????? GC ????????????? +ѭ⣬ʱüԶΪ 0 GC ռ޷ա ```java objA.instance = objB; objB.instance = objA; ``` -### 1.2 ????? +### 1.2 ɴ -??? GC Roots ??????????????????????????????????????????????????????? +ͨ GC Roots ΪʼܹﵽĶǶǿõģɴĶɱա -GC Roots ??????????????? +GC Roots һݣ -1. ???????????????? -2. ??????????????????????? -3. ??????????????????? -4. ?????????????????? +1. ջõĶ +2. ྲ̬õĶ +3. еijõĶ +4. طջõĶ -### 1.3 ???????? +### 1.3 -??????????????????????????????????????????????????????????????????????????????????????? +ͨü㷨ж϶ͨɴԷ㷨ж϶ǷɴжǷ롰áйء -#### 1.3.1 ????? +#### 1.3.1 ǿ -???????????????????????????????????????????? +ֻҪǿôڣԶյõĶ ```java Object obj = new Object(); ``` -#### 1.3.2 ?????? +#### 1.3.2 -????????????????????????? +DZãڴ֮ǰлա ```java Object obj = new Object(); @@ -169,14 +169,14 @@ obj = null; sf.get(); ``` -sf ??? obj ??????????????? sf.get() ??????????????????????????????????????????????????? null?? +sf Ƕ obj һãͨ sf.get() ȡ󣬵Ȼ󱻱ΪҪյĶʱ򷵻 null -???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ҪûʵƻĹܣڴ㹻ֱͨȡֵӷæʵԴѯݣٶȣڴ治ʱԶɾⲿֻݣԴѯЩݡ -#### 1.3.3 ?????? +#### 1.3.3 -????????????????????????????????????????????????????????????????????? +ֻ浽һռ֮ǰռʱ۵ǰڴǷ㹻ᱻա ```java Object obj = new Object(); @@ -186,9 +186,9 @@ wf.get(); wf.isEnQueued(); ``` -#### 1.3.4 ?????? +#### 1.3.4 -?????????????????????????????????????????????????????????????q??????????????????????????????????????????????????????????????????????????????????????????????? +ֳΪû߻ӰãһǷõĴڣȫʱ乹Ӱ죬Ҳ޷ͨȡһʵΪһùΨһĿľռʱյһϵͳ֪ͨ ```java Object obj = new Object(); @@ -198,367 +198,367 @@ pf.get(); pf.isEnQueued(); ``` -### 1.3 ??????????? +### 1.3 Ļ -?????????????????????????????? +ڷҪǶԳصĻպͶжء -???????????????????????? +صĻպͶжơ -?????????????????????????????????????????????????????? +жܶ࣬ҪҲһᱻжأ -1. ???????????????????????????? Java ??????????????????? -2. ???????? ClassLoader ?????????? -3. ???????? java.lang.Class ??????????ʦ??????????????????ʦ??????????????????? +1. еʵѾգҲ Java вڸκʵ +2. ظ ClassLoader Ѿա +3. Ӧ java.lang.Class ûκεطãҲ޷κεطͨʸ෽ -??????? -Xnoclassgc ?????????????????????? +ͨ -Xnoclassgc Ƿжء -?????????????????CGLib ?? ByteCode ??????????? JSP ??? OSGo ???????????? ClassLoader ????????????????????????????????????????????? +ڴʹ÷䡢̬CGLib ByteCode ܡ̬ JSP Լ OSGo ƵԶ ClassLoader ijҪ߱жعܣԱ֤ڴ ### 1.4 finalize() -???????????????????????????????? finalize() ???????????????????????????????????????????????????? +һɱʱöбҪִ finalize() ôпܿͨڸ÷ö±ãӶʵԾȡ -finalize() ???? C++ ???y??????????????????????????????? try-finally ???????????????????????????????????????????????????????????????????????? +finalize() C++ 鹹رⲿԴȹ try-finally ȷʽĸãҸ÷д۸߰ȷԴ޷֤ĵ˳òҪʹá -## 2. ????????? +## 2. ռ㷨 -### 2.1 ??? - ????? +### 2.1 - 㷨 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a4248c4b-6c1d-4fb8-a557-86da92d3a294.jpg) -?????????????????????????? +ҪյĶбǣȻ -???? +㣺 -1. ?????????????????? -2. ???????????? +1. ǺЧʶ +2. Ƭ -??????????????????????? +֮㷨ǻڸ㷨иĽ -### 2.2 ?????? +### 2.2 㷨 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e6b733ad-606d-4028-b3e8-83c3a73a3797.jpg) -????????????????????????????????????????????????????????????????????????????????????????????? +ڴ滮ΪСȵ飬ÿֻʹһ飬һڴ˾ͽĶƵһ棬Ȼٰʹùڴռһ -?????????????????????? +Ҫֻʹڴһ롣 -?????????????????????????????????????????????????????????????????????????????? Eden ??????????? Survior ????????? Eden ??????????? Survivor???????????? Eden ?? Survivor ????????????????????????? Survivor ????????????? Eden ?? Survivor??HotSpot ??????? Eden ?? Survivor ??????????? 8:1?????????????????? 90 %?????????????? 10% ????????????? Survivor ????????????????????????????????????????????????????? +ڵҵռ㷨Dzǽڴ滮ΪСȵ飬ǷΪһϴ Eden ռС Survior ռ䣬ÿʹ Eden ռһ Survivorڻʱ Eden Survivor лŵĶһԸƵһ Survivor ռϣ Eden SurvivorHotSpot Eden Survivor ĴСĬΪ 8:1֤ڴʴﵽ 90 %ÿλж 10% Ķôһ Survivor ռͲˣҪз䵣ҲǽĿռ䡣 -### 2.3 ??? - ?????? +### 2.3 - 㷨 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/902b83ab-8054-4bd2-898f-9a4a0fe52830.jpg) -??????????????????????????????????????????? +дĶһƶȻֱ˱߽ڴ档 -### 2.4 ???????? +### 2.4 ִռ㷨 -??????????????????????????????????????????????????????????????????????????????????????????? +ڵҵ÷ִռ㷨ʹǰܵļռ㷨ݶڽڴ滮Ϊ飬ͬʵռ㷨 -??? Java ??????????????????? +һ㽫 Java ѷΪ -1. ???????????????? -2. ???????????? - ???? ???? ??? - ???? ???? +1. ʹã㷨 +2. ʹã - - 㷨 -## 3. ????????? +## 3. ռ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c625baa0-dde6-449e-93df-c3a67f2f430f.jpg) -?????? HotSpot ??????? 7 ?????????????????????????????????????? + HotSpot е 7 ռ߱ʾռʹá -### 3.1 Serial ????? +### 3.1 Serial ռ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/22fda4ae-4dd5-489d-ab10-9ebfdad22ae0.jpg) -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ǵ̵߳ռζֻʹһ߳̽ռҪڽռʱ̣ͣ߳ɹĵȴʱ䡣 -???????????????????? CPU ????????????????????????????????????????????????? +ŵǼ򵥸Чڵ CPU ˵û߳̽Ŀӵߵĵ߳ռЧʡ -?? Client ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? + Client ӦóУڴһ˵ܴ󣬸ռռʮһ׵ͣʱԿһٶڣֻҪ̫ƵͣǿԽܵġ -### 3.2 ParNew ????? +### 3.2 ParNew ռ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/81538cd5-1bcf-4e31-86e5-e198df1e013b.jpg) -???? Serial ????????????? + Serial ռĶ̰߳汾 -?? Server ??????????????????????????????????????????????????? Serial ???????????????? CMS ????????????? + Server ģʽµѡռԭ⣬ҪΪ Serial ռֻ CMS ռϹ -???????????????? CPU ???????????????? -XX:ParallelGCThreads ????????????????? +ĬϿʼ߳ CPU ͬʹ -XX:ParallelGCThreads ߳ -### 3.3 Parallel Scavenge ????? +### 3.3 Parallel Scavenge ռ -???????????????? +DzеĶ߳ռ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? CPU ?????????????????????????????? +ռעǾռʱû̵߳ͣʱ䣬ĿǴﵽһɿƵΪȡռָ CPU ûʱռʱıֵ -????????????????????????????????????????????????????????????????????????????? CPU ????????????????????????????????????????????????????? +ͣʱԽ̾ԽʺҪûijõӦٶû顣ԸЧʵ CPU ʱ䣬ɳҪʺں̨Ҫཻ̫ -?????????????????????????????????????????????????????? -XX:MaxGCPauseMillis ???????????????????????? -XX:GCTimeRatio ???????????? 0 ???? 100 ??????????????????????????????????????????????????????????????????????????????????????????????? +ṩھȷֱǿռͣʱ -XX:MaxGCPauseMillis ԼֱС -XX:GCTimeRatio ֵΪ 0 С 100 ͣʱռȡģռСձƵ½ -????????????? -XX:+UseAdaptiveSizePolicy???????????????????????????????????????????????-Xmn????Eden ?? Survivor ?????????-XX:SurvivorRatio?????????????????????-XX:PretenureSizeThreshold??????????????????????????????????????????????????????????????????????????????????????????????????????? GC ??????????????GC Ergonomics??????????????????????? ParNew ????????????????? +ṩһ -XX:+UseAdaptiveSizePolicyһز򿪲󣬾ͲҪָֹĴС-XmnEden Survivor ı-XX:SurvivorRatio䣨-XX:PretenureSizeThresholdϸڲˣݵǰϵͳռܼϢ̬ЩṩʵͣʱַʽΪ GC ӦĵڲԣGC ErgonomicsӦڲҲ ParNew ռһҪ -### 3.4 Serial Old ????? +### 3.4 Serial Old ռ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/08f32fd3-f736-4a67-81ca-295b2a7972f2.jpg) -Serial Old ?? Serial ??????????????????? Client ???????????????????? Server ?????????????????? +Serial Old Serial ռ汾ҲǸ Client ģʽµʹá Server ģʽ£; -1. ?? JDK 1.5 ????????Parallel Old ????????????? Parallel Scavenge ???????????? -2. ??? CMS ????????????????????????? Concurrent Mode Failure ???? +1. JDK 1.5 Լ֮ǰ汾Parallel Old ǰ Parallel Scavenge ռʹá +2. Ϊ CMS ռĺԤڲռ Concurrent Mode Failure ʱʹá -### 3.5 Parallel Old ????? +### 3.5 Parallel Old ռ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/278fe431-af88-4a95-a895-9c3b80117de3.jpg) -?? Parallel Scavenge ??????????????? + Parallel Scavenge ռ汾 -?????????????? CPU ???????????????????????? Parallel Scavenge ?? Parallel Old ??????? +עԼ CPU Դеijϣȿ Parallel Scavenge Parallel Old ռ -### 3.6 CMS ????? +### 3.6 CMS ռ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/62e77997-6957-4b68-8d12-bfd609bb2c68.jpg) -CMS??Concurrent Mark Sweep?????? Mark Sweep ?????????????? ??? - ??? ??????? +CMSConcurrent Mark Sweep Mark Sweep ֪ǻ - 㷨ʵֵġ -?????????????????? +ص㣺ռͣ١ -??????????????? +Ϊĸ̣ -1. ??????????????????? GC Roots ??????????????????????????? -2. ???????????? GC Roots Tracing ????????????????????????????????????? -3. ????????????????????????????????????????????????????????????????????????????? -4. ?????????????????? +1. ʼǣֻDZһ GC Roots ֱӹĶٶȺܿ죬Ҫͣ١ +2. ǣ GC Roots Tracing Ḷ́չкʱҪͣ١ +3. ±ǣΪڼû±Dz䶯һֶıǼ¼Ҫͣ١ +4. Ҫͣ١ -????????????????????????????????????????????????????????????????????????????? +кʱIJǺͲУռ̶߳û߳һҪͣ١ -??????????? +ȱ㣺 -1. ?? CPU ???????CMS ??????????????????? (CPU ???? + 3) / 4???? CPU ???? 4 ?????CMS ??????????????????????????? CPU ???????????????????????????????????????????????????????????????????? 50%?????????????????????????????????????????????????????? CPU ????????? +1. CPU ԴСCMS ĬĻ߳ (CPU + 3) / 4 CPU 4 ʱCMS ûӰͿܱúܴ CPU ؾͱȽϴ󣬻Ҫֳһȥִռ̣߳ͿܵûִٶȺȻ 50%ʵҲ޷ܡҵͣʱΪ۵ģ CPU ʱ͡ -2. ??????????????????????????????????????????????????????????????????????????????????????????????????????????CMS ????????????????????????????????? GC ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? -XX:CMSInitiatingOccupancyFraction ???????????????????????????????JDK 1.5 ???????????? 68?????????????????? 68% ?????????????????????????????????????????????????????????????? Concurrent Mode Failure?????????????????????????????? Serial Old ?????????????????????????????? +2. ޷ڲ׶û̻߳ţȻͻµϲһڱǹ֮CMS ޷ڵռдǣֻһ GC ʱһͱΪҲռ׶û̻߳ҪУҲͻҪԤ㹻ڴռû߳ʹãռȵȫٽռҪԤһֿռṩռʱijʹáʹ -XX:CMSInitiatingOccupancyFraction ֵı䴥ռڴռðٷֱȣJDK 1.5 Ĭ¸ֵΪ 68Ҳǵʹ 68% Ŀռ֮ᴥռֵõ̫ߣ¸޷棬ôͻ Concurrent Mode FailureʱԤʱ Serial Old ռ½ռ -3. ??? - ?????????????????????????????????????????????????????????????????????????????????????????????????? Full GC?? +3. - 㷨µĿռƬܴ鷳ռʣ࣬޷ҵ㹻ռ䵱ǰ󣬲òǰһ Full GC -### 3.7 G1 ????? +### 3.7 G1 ռ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f99ee771-c56f-47fb-9148-c0036695b5fe.jpg) -G1??Garbage-First??????????????????????????????????????????????????????????????????HotSpot ????????????????????????????????????I?? JDK 1.5 ?????? CMS ??????? +G1Garbage-Firstռǵռչǰصijɹ֮һһӦõռHotSpot ŶӸʹǣڱȽϳڵģδ滻 JDK 1.5 з CMS ռ -?????????? +߱ص㣺 -- ??????????????????? CPU ???????????????????? CPU ???????????? -- ??????????????????????????????????????????????????????????????? GC ??????????????????????????????????????????????????? GC ??????????????????????? -- ????????????????????????? - ??????????????????????????? Region ??????????????????????????????????????????????????????? -- ??????????????????? CMS ?????????????????????? G1 ?? CMS ??????????? G1 ???????????????????????????????????????????????????????????? M ???????????????????? GC ?????????? N ??????????????? Java??RTSJ?????????????????????? +- 벢ܳö CPU µӲƣʹö CPU ͣʱ䣻 +- ִռִȻԱȻҪռϾܶ GC ѣܹòͬʽȥ´ĶѴһʱ䡢 GC ľɶȡõռЧ +- ռϣǻڡ - 㷨ʵֵռӾֲ Region ֮䣩ǻڡơ㷨ʵֵģζڼ䲻ڴռƬ +- Ԥͣ٣ CMS һƣͣʱ G1 CMS ͬĹע㣬 G1 ˽ͣ⣬ܽԤͣʱģͣʹȷָһΪ M ʱƬڣ GC ϵʱ䲻ó N 룬⼸Ѿʵʱ JavaRTSJռˡ -?? G1 ??????????????????????????????????????????????????? G1 ????????????Java ?????????????????????????????? Java ??????????????????????Region?????????????????????????????????????????????????????????????????????????? Region??????????????????? + G1 ֮ǰռռķΧ G1 Java ѵڴ沼ռкܴ𣬽 Java ѻΪСȵĶRegionȻĸˣһ RegionҪļϡ -??????????????????????????????????????????????????? Java ?????????????????????????????? Region ????????????????????????????????????????????????????????????????????????????????????????????????????? Region????????? Garbage-First ??????????????????? Region ?????????????????????????????????????????????????????????????????????? +֮ܽԤͣʱģͣΪмƻر Java нȫռٸ Region ѻļֵСõĿռСԼʱľֵں̨άһбÿθռʱ䣬Ȼռֵ RegionҲ Garbage-First Ƶɣʹ Region ڴռԼȼշʽ֤޵ʱڿԻȡܸߵռЧʡ -Region ????????????????????????????? Region ???????????? Java ????????????????????????????????????????????????????????? Java ???????????????????? GC ???????????????????????????????? Region ?????????????????? Remembered Set????????????????? Reference ??????????????????????????? Write Barrier ?????????????? Reference ????????????????? Region ?????????????? CardTable ???????????????????????????????? Region ?? Remembered Set ???????????????????? GC ?????????????? Remembered Set ???????????????????????????? +Region ǹģһij Region У Java ĶùϵɴԷȷǷʱҪɨ Java Ѳܱ֤׼ȷԣȻǶ GC Чʵļ˺Ϊ˱ȫɨķÿ Region άһ֮Ӧ Remembered Setֳڶ Reference ͵ݽдʱһ Write Barrier ʱжд Reference õĶǷڲͬ Region ֮Уǣͨ CardTable Ϣ¼ö Region Remembered Set ֮Сڴʱ GC ڵöٷΧм Remembered Set ɱ֤ȫɨҲ© -???????????? Remembered Set ???????G1 ??????????????????????????????s +ά Remembered Set IJG1 ռ¿ɻΪ¼裺 -1. ?????? -2. ??????? -3. ????????????????????????????????????????????????????????????????????????????????????????????? Remembered Set Logs ???????????????? Remembered Set Logs ?????????? Remembered Set ??????????????????????????? -4. ??????????????? Region ????????????????????????????????? GC ?????????????????????????????????????????????????????????????????????? Region????????????????????????????????????????????? +1. ʼ +2. +3. ձǣΪڲڼû±Dz䶯һֱǼ¼ʱ仯¼̵߳ Remembered Set Logs 棬ձǽ׶Ҫ Remembered Set Logs ݺϲ Remembered Set С׶Ҫ̣ͣ߳ǿɲִС +4. ɸѡգȶԸ Region еĻռֵͳɱ򣬸û GC ͣʱƶռƻ˽׶ʵҲûһ𲢷ִУΪֻһ RegionʱûɿƵģͣû߳̽ռЧʡ -### 3.8 ????????????????? +### 3.8 ռıȽ -| ????? | ???????? or ???? | ?????? / ????? | ?? | ??? | ?????? | +| ռ | С or | / | 㷨 | Ŀ | ó | | --- | --- | --- | --- | --- | --- | -| **Serial** | ???? | ?????? | ?????? | ?????????? | ?? CPU ??????? Client ?? | -| **Serial Old** | ???? | ????? | ??? - ???? | ?????????? | ?? CPU ??????? Client ????CMS ?????? | -| **ParNew** | ???? | ?????? | ?????? | ?????????? | ?? CPU ??????? Server ?????? CMS ??? | -| **Parallel Scavenge** | ???? | ?????? | ?????? | ?????????? | ????????????????????????? | -| **Parallel Old** | ???? | ????? | ??? - ???? | ?????????? | ????????????????????????? | -| **CMS** | ???? | ????? | ??? - ??? | ?????????? | ?????????????? B/S ?????????? Java ??? | -| **G1** | ???? | both | ??? - ???? + ?????? | ?????????? | ?????????????????I CMS | +| **Serial** | | | 㷨 | Ӧٶ | CPU µ Client ģʽ | +| **Serial Old** | | | - | Ӧٶ | CPU µ Client ģʽCMS ĺԤ | +| **ParNew** | | | 㷨 | Ӧٶ | CPU ʱ Server ģʽ CMS | +| **Parallel Scavenge** | | | 㷨 | | ں̨Ҫཻ̫ | +| **Parallel Old** | | | - | | ں̨Ҫཻ̫ | +| **CMS** | | | - | Ӧٶ | ڻվ B/S ϵͳϵ Java Ӧ | +| **G1** | | both | - + 㷨 | Ӧٶ | Ӧã滻 CMS | -## 4. ?????????????? +## 4. ڴղ -### 4.1 ?????? Eden ???? +### 4.1 Eden -??????????????????????? Eden ???????? Eden ?????????????? Minor GC?? +£ Eden 䣬 Eden ռ䲻ʱ Minor GC -### 4.2 ???????????????? +### 4.2 ֱӽ -?? -XX:PretenureSizeThreshold ????????????????????????????????????? Eden ???? Survivor ?????????????? -### 4.3 ?????????????????? +ṩ -XX:PretenureSizeThreshold ڴֵĶֱ䣬 Eden Survivor ֮Ĵڴ渴ƣ +### 4.3 ڴĶ -JVM ?????????????????????? Minor GC ????????? Survivor ????????????? Survivor ????????? 1?????????? Minor GC ??????????????? 1???????????????????????????????? 15 ????? -XX:MaxTenuringThreshold ??????? +JVM Ϊ Minor GC Ȼұ Survivor ɵģƶ Survivor 1ÿһ Minor GC 1ӵһƶĬ 15 ꣬ͨ -XX:MaxTenuringThreshold ã -### 4.4 ????????????? +### 4.4 ̬ж -?? Survivor ?????????????????????? Survivor ??????????????????????????????????????????? + Survivor ͬжСܺʹ Survivor ռһ룬ڵڸĶֱӽ -### 4.5 ???????? +### 4.5 ռ䵣 -????? Minor GC ????JVM ??????????????????????????????????????????????????? Minor GC ??????????????????????????????????????????????????????????????????????????????? Minor GC?????????? Full GC?? +ڷ Minor GC ֮ǰJVM ȼռǷжܿռ䣬Ļ Minor GC ȷǰȫģռǷνƽСڵĻ Minor GCСڵĻ Full GC -## 4.6 Full GC ????????? +## 4.6 Full GC Ĵ -???? Minor GC?????????????????? Eden ???????????????????? Minor GC???? Full GC ????????????????????? + Minor GC䴥dz򵥣 Eden ռʱͽһ Minor GC Full GC Ըӣ -### 4.6.1 ???? System.gc() +### 4.6.1 System.gc() -??????????????? JVM ???? Full GC??????????????????????????????????? Full GC????????? Full GC ???????????????????????????????????????????????????????????????????????????????? -XX:+ DisableExplicitGC ????? RMI ???? System.gc()?? +˷ĵǽ JVM Full GCȻֻǽһܶᴥ Full GCӶ Full GC ƵʣҲ˼ЪͣٵĴǿҽܲʹô˷ͲҪʹãԼȥڴ棬ͨ -XX:+ DisableExplicitGC ֹ RMI System.gc() -### 4.6.2 ?????????? +### 4.6.2 ռ䲻 -??????????????????????????????????????????????????????????????????????? Full GC ??????????????????????? Java.lang.OutOfMemoryError: Java heap space ????????????????????? Full GC??????????????????????? Minor GC ???????????????????????????????????????????????? +ռ䲻ijΪǰĴֱӽڴĶȣִ Full GC ռȻ㣬׳´ Java.lang.OutOfMemoryError: Java heap space Ϊ״ Full GCʱӦö Minor GC ׶αաöһʱ估ҪĶ顣 -### 4.6.3 ??????????? +### 4.6.3 ռ䵣ʧ -?????????? Minor GC ???????????????????????????????? HandlePromotionFailure ????????????? Full GC?? +ʹø㷨 Minor GC Ҫڴռ HandlePromotionFailure ʧܣᴥ Full GC -### 4.6.4 JDK 1.7 ????????????????? +### 4.6.4 JDK 1.7 ǰôռ䲻 -?? JDK 1.7 ???????HotSpot ????????????????????????????????????? class ??????????????????????????????????????????????????????????????Permanet Generation ????????????????????? CMS GC ????????????? Full GC????????? Full GC ?????????????? JVM ?????????????????java.lang.OutOfMemoryError: PermGen space ????? PermGen ?????? Full GC ????????????????? PermGen ????????? CMS GC?? + JDK 1.7 ǰHotSpot еķôʵֵģôдŵΪһЩ class Ϣ̬ݣϵͳҪصࡢ͵õķ϶ʱPermanet Generation ܻᱻռδΪ CMS GC Ҳִ Full GC Full GC Ȼղˣô JVM ׳´Ϣjava.lang.OutOfMemoryError: PermGen space Ϊ PermGen ռ Full GC 󣬿ɲõķΪ PermGen ռתΪʹ CMS GC -?? JDK 1.8 ?????????I???????????????????????????????????????????? Full GC ???????????? + JDK 1.8 Ԫռ滻ôΪʵ֣ԪռDZڴ棬˼һ Full GC Ŀԡ ### 4.6.5 Concurrent Mode Failure -??? CMS GC ??????????????????????????????????????????????????? CMS GC ??????????????????????????????? Full GC??????? Concurrent Mode Failure ????????? Full GC?? +ִ CMS GC ĹͬʱжҪʱռ䲻㣨ʱ򡰿ռ䲻㡱 CMS GC ʱǰĸർʱԵĿռ䲻㴥 Full GCᱨ Concurrent Mode Failure 󣬲 Full GC -# ???????? +# ػ -???????????????????? +ڼ䶯̬صġ -## 1 ??????????? +## 1 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/32b8374a-e822-4720-af0b-c0f485095ea2.jpg) -???????? 7 ????? + 7 ׶Σ -- **?????Loading??** -- **?????Verification??** -- **?????Preparation??** -- **??????Resolution??** -- **???????Initialization??** -- ????Using?? -- ???Unloading?? +- **أLoading** +- **֤Verification** +- **׼Preparation** +- **Resolution** +- **ʼInitialization** +- ʹãUsing +- жأUnloading -????????????????????????????????????????????????? Java ??????? +нijЩ¿ڳʼ׶֮ٿʼΪ֧ Java Ķ̬󶨡 -## 2. ????????? +## 2. ʼʱ -??????Z?????????????????????????Z??????????????????????????????????????( ???????????????????????? ) +淶вûǿԼʱмأǹ淶ϸ涨ֻгʼ( ء֤׼ŷ ) -1. ???? new??getstatic??putstatic??invokestatic ???????????????????????????????????????????????????????????????? 4 ??????????????? new ??????????????????????????????????????? final ?????????????????????????????????????????????????????????????? +1. newgetstaticputstaticinvokestatic ָֽʱûнйʼȴʼ 4 ָijǣʹ new ؼʵʱ򣻶ȡһľֶ̬Σ final Ρڱѽ볣صľֶ̬γ⣩ʱԼһľ̬ʱ -2. ??? java.lang.reflect ???????????????????????????????????????????????????????? +2. ʹ java.lang.reflect ķзõʱûнгʼҪȴʼ -3. ?????????????????????????????????????????????????????????? +3. ʼһʱ丸໹ûнйʼҪȴ丸ijʼ -4. ?????????????????????????????????????? main() ????????????????????????????????? +4. ʱûҪָһҪִеࣨ main() Ǹࣩȳʼࣻ -5. ????? jdk1.7 ???????????????????? java.lang.invoke.MethodHandle ?????????????? REF_getStatic, REF_putStatic, REF_invokeStatic ??????????????????????????????????????????????????????????????? +5. ʹ jdk1.7 Ķ̬֧ʱһ java.lang.invoke.MethodHandle ʵĽΪ REF_getStatic, REF_putStatic, REF_invokeStatic ķӦûнйʼҪȴʼ -???? 5 ?????????????????????????????????????????????????????????????????????????????????????????????????? + 5 ֳеΪΪһá֮⣬ķʽᴥʼΪáõijӰ -1\. ???????????????????????????????????? +1\. ͨøľֶ̬Σᵼʼ ```java -System.out.println(SubClass.value); // value ????? SuperClass ???? +System.out.println(SubClass.value); // value ֶ SuperClass ж ``` -2\. ??????E????????????????????????????????????????????????????????????????????????????????? Object ????????????????????????????? +2\. ͨ鶨࣬ᴥijʼù̻гʼһԶɵġֱӼ̳ Object ࣬аԺͷ ```java SuperClass[] sca = new SuperClass[10]; ``` -3\. ????????????????????????????????????????????????i?????????????????i????????????? +3\. ڱ׶λijУϲûֱõ峣࣬˲ᴥ峣ijʼ ```java System.out.println(ConstClass.HELLOWORLD); ``` -## 3. ???????? +## 3. ع -???????????????????????????????? 5 ????? +˼ء֤׼ͳʼ 5 ׶Ρ -### 3.1 ???? +### 3.1 -???????????????????????????? +صһ׶ΣעⲻҪ -????????????????????? +ع£ -1. ???????????????????????????????????????? -2. ???????????????????????????????????????????? -3. ???????????????????????? Class ?????????????????????????????????? +1. ͨһȫ޶ȡĶֽ +2. ֽľ̬洢ṹתΪʱ洢ṹ +3. ڴһ Class Ϊĸݵķڡ -???????????????????????????? +жֽԴ·ʽлȡ -- ?? ZIP ???????????????????????? JAR??EAR??WAR ?????????? -- ????????????????????????????? Applet?? -- ???????????????????????????????????????????? java.lang.reflect.Proxy ?????????? ProxyGenerator.generateProxyClass ???????????????????? -- ??????????????????????? JSP ???????? JSP ??????????? Class ?? -- ????????????????????????????????????????????? SAP Netweaver????????????????????????????????????????????? +- ZIP ȡܳճΪպ JAREARWAR ʽĻ +- лȡֳ͵Ӧ Applet +- ʱɣֳʹõþǶ̬ java.lang.reflect.Proxy У ProxyGenerator.generateProxyClass ĴĶֽ +- ļɣͳ JSP Ӧã JSP ļɶӦ Class ࡣ +- ݿȡֳټЩм SAP NetweaverѡѳװݿɳڼȺķַ ... -### 3.2 ??? +### 3.2 ֤ -??? Class ????????????????????????????????????????????????????????? +ȷ Class ļֽаϢϵǰҪ󣬲ҲΣİȫ -????????? 4 ????? +Ҫ 4 ׶Σ -1. ????????? -2. ??????????????????????????????????????? -3. ????????????????????????????????????????????????????????????????????????????????? -4. ??????????? +1. ļʽ֤ +2. Ԫֽ֤Ϣ +3. ֽ֤ͨͿȷǺϷ߼ģķУ +4. ֤ -### 3.3 ??? +### 3.3 ׼ -???????? static ???????????????????????????g??????????????????????? +DZ static εı׼׶Ϊڴ沢óʼֵʹõǷڴ档 -????????????????????????????????????????????????????? Java ???? +ʵ׶ηڴ棬ڶʵʱŶһ Java С -???????? 0 ????????????????? value ???????? 0 ?????? 123?? +ʼֵһΪ 0 ֵ value ʼΪ 0 123 ```java public static int value = 123; ``` -????????????????????????????????????????????? 0?? +dzôᰴձʽгʼǸֵΪ 0 ```java public static final int value = 123; ``` -### 3.4 ???? +### 3.4 -?????????????????I????????????? +صķ滻ΪֱõĹ̡ -### 3.5 ????? +### 3.5 ʼ -?????????????????????? <clinit>() ?????????? +ʼ׶μִ๹ <clinit>() Ĺ̡ -????????????????????????????????????????????????????????????????????????????????????????????? +׼׶ΣѾһϵͳҪijʼֵڳʼ׶ΣݳԱͨƶۼƻȥʼԴ -<clinit>() ??????????????? +<clinit>() ص㣺 -- ???????????????????????????????????????????static{} ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +- ɱԶռĸֵ;̬飨static{} 飩еϲģռ˳Դļгֵ˳رעǣֻ̬ܷʵ֮ǰֵֻܸܷ֮ʡ´룺 ```java public class Test { static { - i = 0; // ???????????????????????? - System.out.print(i); // ????????????????????????? + i = 0; // ֵͨ + System.out.print(i); // ʾǷǰá } static int i = 1; } ``` -- ?????????????????????????? <init>()??????????????????????????????????????????????????? <clinit>() ????????????????? <clinit>() ?????????????????????????????? <clinit>() ??????????? java.lang.Object?? +- Ĺ캯˵ʵ <init>()ͬҪʽĵøĹԶ֤ <clinit>() ֮ǰ <clinit>() Ѿִнеһִ <clinit>() ϶Ϊ java.lang.Object -- ???????? <clinit>() ??????????????????????????????????????????????????????????????? +- ڸ <clinit>() ִУҲζŸжľ̬Ҫıֵ´룺 ```java static class Parent { @@ -573,53 +573,53 @@ static class Sub extends Parent { } public static void main(String[] args) { - System.out.println(Sub.B); // ???????????????????? A??????? 2 + System.out.println(Sub.B); // Ǹеľֵ̬ AҲ 2 } ``` -- <clinit>() ???????????????????????????????????????????????????????????????????????????????? <clinit>() ?????? +- <clinit>() ӿڲDZģһв̬飬ҲûжĸֵԲΪ <clinit>() -- ???????????????????????????????????????????????????????????????? <clinit>() ?????????????????????????? <clinit>() ?????????????????? <clinit>() ?????????????????????????????????????????????????????????????????????????? <clinit>() ?????? +- ӿвʹþ̬飬Ȼʼĸֵ˽ӿһ <clinit>() ӿ಻ͬǣִнӿڵ <clinit>() Ҫִиӿڵ <clinit>() ֻеӿжıʹʱӿڲŻʼ⣬ӿڵʵڳʼʱҲһִнӿڵ <clinit>() -- ?????????????? <clinit>() ??????????????????????????????????????????????????????????????????????? <clinit>() ???????????????????????????????????? <clinit>() ??????????????????? <clinit>() ?????????????????????????????????????????????????????????? +- ᱣ֤һ <clinit>() ڶ̻߳±ȷļͬ߳ͬʱʼһֻ࣬һִ߳ <clinit>() ̶߳ȴֱִ߳ <clinit>() ϡһ <clinit>() кʱIJͿɶʵʹдΡ -## 4. ??????? +## 4. -????????????????????????????????????????????????????????????? ( ??????? )???????????? Java ?????????????????????????????????????????????????????????????????????????????? +ŶӰؽ׶еġͨһȫ޶ȡĶֽ ( ֽ )ŵ Java ⲿȥʵ֣ԱӦóԼȥȡҪࡣʵĴģΪ -### 4.1 ??????????? +### 4.1 -???????????????????????????????????????????????????? Java ?????????????????????????????????????????????????????????????????????????????????????????????? Class ????? equals() ??????isAssignableFrom() ??????isInstance() ????????????????????? instanceof() ???????????????????????????????????????????????????????????????????????????????????????????????? Class ??????????????????????????????????????????????????????????????? +һ࣬Ҫɼ౾һͬȷ Java еΨһԣÿһӵһƿռ䡣ͨ׶ԣȽǷȡָġȡ Class equals() isAssignableFrom() isInstance() ķؽҲʹ instanceof() ؼֶϵжֻʱͬһصǰ²壬򣬼ʹԴͬһ Class ļͬһأֻҪǵͬͱضȡ -### 4.2 ??????????? +### 4.2 -?? Java ????????????????????????????????????????? + Java ĽǶֲֻͬ -??????????????????Bootstrap ClassLoader???????????????? C++ ?????????????????????????????????????????????????????? Java ???????????????????????????????????? java.lang.ClassLoader?? +һBootstrap ClassLoader C++ ʵ֣һ֣һ־ļЩ Java ʵ֣ⲿȫ̳Գ java.lang.ClassLoader -?? Java ???????????????????????????????????? + Java ԱĽǶȿԻֵøϸһЩ -- ?????????????Bootstrap ClassLoader?? ????????????????? \lib ?????????? -Xbootclasspath ???????????????????????????????????????????????? rt.jar???????????????????? lib ??????????????????????????????? ???????????????? Java ??????????????????????????????????????????????????????????????????????? null ???r?? +- Bootstrap ClassLoader 𽫴 \lib Ŀ¼еģ߱ -Xbootclasspath ָ·еģʶģļʶ rt.jarֲϵ⼴ʹ lib Ŀ¼ҲᱻأصڴС ޷ Java ֱãûڱдԶʱҪѼίɸֱʹ null 漴ɡ -- ????????????Extension ClassLoader?? ?????????????? ExtClassLoader??sun.misc.Launcher$ExtClassLoader????????????? /lib/ext ????? java.ext.dir ?????????????????????????????????????????????????????????? +- չExtension ClassLoader ExtClassLoadersun.misc.Launcher$ExtClassLoaderʵֵġ /lib/ext ߱ java.ext.dir ϵͳָ·еصڴУ߿ֱʹչ -- ??????????????Application ClassLoader?? ?????????????? AppClassLoader??sun.misc.Launcher$AppClassLoader??????????????????????? ClassLoader ?? getSystemClassLoader() ?????????????????????????????????????????????????ClassPath??????????????????????????????????????????????????????????????????????????????????????????????????????????? +- ӦóApplication ClassLoader AppClassLoadersun.misc.Launcher$AppClassLoaderʵֵġ ClassLoader е getSystemClassLoader() ķֵһΪϵͳû·ClassPathָ⣬߿ֱʹӦóûԶԼһdzĬϵ -### 4.3 ????????? +### 4.3 ˫ίģ -???????????????????????????????????????????????????????????????????????????????????????????????????????????????Parents Delegation Model??????????????????????????????????????????????????????????????????????????????????????????????Composition???????????????????????Inheritance?????????? +Ӧó໥ϽмصģбҪԼԼͼչʾ֮IJιϵΪ˫ίģͣParents Delegation ModelģҪ˶⣬ӦԼĸ֮ĸӹϵһͨϣCompositionϵʵ֣̳ͨУInheritanceĹϵʵ֡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2cdc3ce2-fa82-4c22-baaa-000c07d10473.jpg) -**????????** +**** -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +һյصȲԼȥԼأǰίɸÿһεļˣεݹ飬еļնӦô͵УֻеԼ޷ɴ˼ΧûҵࣩʱӼŻ᳢Լء -**???** +**ô** -??????????????????????????????????? Java ???????????????????????????????????????????????? java.lang.Object????????? rt.jar ?????????????????????????????????????????????????????????????????????? Object ????????????????????????????????????????????????????????????????????????????????????????????java.lang.Object ?????????????? ClassPath ???????????????????? Object ????????????????????????????????? rt.jar ????????????????? Java ??????????????????????????????????????? +ʹ˫ίģ֮֯Ĺϵʹ Java һ߱һִȼIJιϵ java.lang.Object rt.jar УĸҪ࣬նίɸģ˵мأ Object ڳĸжͬһࡣ෴û˫ίģͣɸмصĻûдһΪjava.lang.Object ࣬ڳ ClassPath Уϵͳнֶͬ Object ࣬򽫱һƬҡ߳Աдһ rt.jar Java ֿ࣬ᷢ룬Զ޷С -**???** +**ʵ** ```java protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException{ @@ -646,27 +646,27 @@ protected synchronized Class loadClass(String name, boolean resolve) throws C } ``` -# JVM ???? +# JVM -## GC ??????? +## GC Ż -| ???? | ???? | +| | | | --- | --- | -| -Xms | ??????????? | -| -Xmx | ????????? | -| -Xmn | ???????? | -| -XX:PermSize | ???????????? | -| -XX:MaxPermSize | ???????????? | +| -Xms | ʼڴС | +| -Xmx | ڴֵ | +| -Xmn | С | +| -XX:PermSize | ʼôС | +| -XX:MaxPermSize | ô | -## GC ???????? +## GC -| ???? | ???? | +| | | | --- | --- | -| -XX:+UseSerialGC | ?????????????? | -| -XX:+UseParallelGC | ?????????????? | -| -XX:+UseConcMarkSweepGC | ???????????????????? | -| -XX:ParallelCMSThreads= | ???????????????????? = ???????????? | -| -XX:+UseG1GC | G1 ?????????? | +| -XX:+UseSerialGC | | +| -XX:+UseParallelGC | | +| -XX:+UseConcMarkSweepGC | ɨ | +| -XX:ParallelCMSThreads= | ɨ = Ϊʹõ߳ | +| -XX:+UseG1GC | G1 | ```java java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseSerialGC -jar java-application.jar diff --git a/notes/Java IO.md b/notes/Java IO.md index 1f7154c7..c4aa5d39 100644 --- a/notes/Java IO.md +++ b/notes/Java IO.md @@ -1,129 +1,129 @@ -* [????](#????) -* [???????](#???????) -* [??????](#??????) -* [???????](#???????) -* [???????](#???????) -* [???????](#???????) +* [](#) +* [̲](#̲) +* [ֽڲ](#ֽڲ) +* [ַ](#ַ) +* [](#) +* [](#) * [1. InetAddress](#1-inetaddress) * [2. URL](#2-url) * [3. Sockets](#3-sockets) * [4. Datagram](#4-datagram) * [NIO](#nio) - * [1. ?????](#1-?????) - * [2. ?????????](#2-?????????) - * [2.1 ???](#21-???) - * [2.2 ??????](#22-??????) - * [3. ????????????](#3-????????????) - * [4. ????????](#4-????????) - * [5. ???????????](#5-???????????) - * [5.1 ????? I/O](#51-?????-io) - * [5.2 ??????? I/O](#52-???????-io) - * [6. ????????](#6-????????) + * [1. ](#1-) + * [2. ͨ뻺](#2-ͨ뻺) + * [2.1 ͨ](#21-ͨ) + * [2.2 ](#22-) + * [3. ״̬](#3-״̬) + * [4. дļʵ](#4-дļʵ) + * [5. ](#5-) + * [5.1 ʽ I/O](#51-ʽ-io) + * [5.2 ʽ I/O](#52-ʽ-io) + * [6. ׽ʵ](#6-׽ʵ) * [6.1 ServerSocketChannel](#61-serversocketchannel) * [6.2 Selectors](#62-selectors) - * [6.3 ?????](#63-?????) - * [6.4 ??????????](#64-??????????) - * [6.5 ???????????](#65-???????????) - * [6.6 ?????????? SelectionKey](#66-??????????-selectionkey) - * [6.7 ????? I/O](#67-?????-io) -* [??????](#??????) + * [6.3 ѭ](#63-ѭ) + * [6.4 ](#64-) + * [6.5 µ](#65-µ) + * [6.6 ɾ SelectionKey](#66-ɾ-selectionkey) + * [6.7 I/O](#67--io) +* [ο](#ο) -# ???? +# -Java ?? I/O ??????????????? +Java I/O ſԷֳ¼ -1. ?????????File -2. ????????InputStream ?? OutputStream -3. ?????????Reader ?? Writer -4. ?????????Serializable -5. ?????????Socket -6. ??????? IO??NIO +1. ̲File +2. ֽڲInputStream OutputStream +3. ַReader Writer +4. Serializable +5. Socket +6. ʽ IONIO -# ??????? +# ̲ -File ???????????????????????????????????????????????????????????? +File ڱʾļĿ¼ֻڱʾļϢʾļݡ -# ?????? +# ֽڲ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8143787f-12eb-46ea-9bc3-c66d22d35285.jpg) -Java I/O ???????????????????? InputStream ?????InputStream ??????????FileInputStream ?? InputStream ??????????????????????????????????????FilterInputStream ????????????????????????????????????????????????? BufferedInputStream ? FileInputStream ????????????????????????????????????????????? FileInputStream ????????????? BufferedInputStream ????? +Java I/O ʹװģʽʵ֡ InputStream ΪInputStream dzFileInputStream InputStream ࣬ھṩֽFilterInputStream ڳװߣװװΪṩĹܣ BufferedInputStream Ϊ FileInputStream ṩĹܡʵһл湦ֽܵʱֻҪ FileInputStream һ BufferedInputStream 󼴿ɡ ```java BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ``` -DataInputStream ?????????????????????????????????????? int??double ?????????? +DataInputStream װṩ˶ԸͽIJ intdouble Ȼ͡ -??????????????????????????? +ļеݵֽ ```java byte[] buf = new byte[20*1024]; int bytes = 0; -// ????? buf.length ????????????????????????????? -1 ??????????? eof??????? +// ȡ buf.length ֽڣصʵʶȡĸ -1 ʱʾ eofļβ while((bytes = in.read(buf, 0 , buf.length)) != -1) { // ... } ``` -# ??????? +# ַ -??????????????????????????????????????????????? I/O ???????????????????????????????????????????????????????????????????????????????? +Ǵ̻紫䣬СĴ洢Ԫֽڣַ I/O ĶֽڶַڳвַͨʽҪṩַвķ -InputStreamReader ?????????????????????????????OutputStreamWriter ??????????????????????????????????????? Reader ?? Writer?? +InputStreamReader ʵִıļַֽOutputStreamWriter ʵַΪıļֽǶ̳ Reader Writer -???????????????????????????????????????????? +ǰַתΪֽڣǰֽϳַ ```java -byte[] bytes = str.getBytes(encoding); // ???? -String str = new String(bytes, encoding)?? // ???? +byte[] bytes = str.getBytes(encoding); // +String str = new String(bytes, encoding) // ``` -GBK ??????????? 2 ?????????? 1 ??????UTF-8 ??????????? 3 ?????????? 1 ??????Java ?????????? UTF-16be???????????? 2 ?????? +GBK Уռ 2 ֽڣӢռ 1 ֽڣUTF-8 Уռ 3 ֽڣӢռ 1 ֽڣJava ʹ˫ֽڱ UTF-16beĺӢĶռ 2 ֽڡ -?????????????????????????????????????? +ͽʹòͬı뷽ʽôͳ롣 -# ??????? +# -????????????????????????????????????? +лǽһתֽУ洢ʹ䡣 -??????ObjectOutputStream.writeObject() +лObjectOutputStream.writeObject() -????????ObjectInputStream.readObject() +лObjectInputStream.readObject() -?????????????? Serializable ?????????????????????ʦ?????????? +лҪʵ Serializable ӿڣֻһ׼ûκηҪʵ֡ -transient ??????????????????????? +transient ؼֿʹһЩԲᱻл -**ArrayList ????????????????**??ArrayList ?՛????????????? transient ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +**ArrayList лͷлʵ**ArrayList д洢ݵ transient εģΪǶ̬չģеĿռ䶼ʹã˾ͲҪеݶлͨдлͷлʹÿֻлݵDzݡ ``` private transient Object[] elementData; ``` -# ??????? +# -Java ?????????? +Java е֧֣ -1. InetAddress????????????????????????? IP ????? -2. URL???????????????? URL ??????????????????????????? -3. Sockets????? TCP ????????????? -4. Datagram????? UDP ????????????? +1. InetAddressڱʾϵӲԴ IP ַ +2. URLͳһԴλͨ URL ֱӶȡдϵݣ +3. Socketsʹ TCP Эʵͨţ +4. Datagramʹ UDP Эʵͨš ## 1. InetAddress -??????????????????????????????????????? InetAddress.getByName(String host)??InetAddress.getByAddress(byte[] addr)?? +ûйй캯ֻ̬ͨʵ InetAddress.getByName(String host)InetAddress.getByAddress(byte[] addr) ## 2. URL -???????? URL ???????????? +ֱӴ URL жȡֽ ```java URL url = new URL("http://www.baidu.com"); -InputStream is = url.openStream(); // ????? -InputStreamReader isr = new InputStreamReader(is, "utf-8"); // ????? +InputStream is = url.openStream(); // ֽ +InputStreamReader isr = new InputStreamReader(is, "utf-8"); // ַ BufferedReader br = new BufferedReader(isr); String line = br.readLine(); while (line != null) { @@ -137,56 +137,56 @@ is.close(); ## 3. Sockets -Socket ?????? +Socket ͨģ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/fa4101d7-19ce-4a69-a84f-20bbe64320e5.jpg) -- ServerSocket???????????? -- Socket????????? +- ServerSocket +- Socketͻ -??????????????? InputStream ?? OutputStream ????????????? +Ϳͻͨ InputStream OutputStream ## 4. Datagram -- DatagramPacket????????? -- DatagramSocket??????? +- DatagramPacketݰ +- DatagramSocketͨ # NIO -NIO ???????? I/O ???? ( ??????????????? ) ?????????????? ?????????????????????????????????? +NIO ʱ I/O ( ȡ ) תƻزϵͳ ҪԱȥƾͿԼٶȡ -## 1. ????? +## 1. -I/O ?? NIO ?????????????????????????????????????????I/O ??????????????????? NIO ??????????????? +I/O NIO ҪݴʹķʽǰᵽģI/O ķʽݣ NIO Կķʽݡ -???????? I/O ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? I/O ????????? + I/O һһֽڽдݣһһֽڵݣһһֽڵݡΪʽݴdzףӼԱÿֻ𵥸ӴƵһ֣ҲԼ򵥵ġһǣ I/O ͨ൱ -????????? I/O ???????????????????????????????????????????????????????????????????????????????????????? I/O ???????????? I/O ????????????????? +һ I/O ϵͳԿʽݣÿһһвһݿ顣鴦ݱȰҪöࡣ I/O ȱһЩ I/O еԺͼԡ -I/O ???? NIO ?????????????java.io.\* ????? NIO ????????????????????????????????? NIO ??????????? java.io.\* ??????????????????????????????????????????????????????????????? +I/O NIO Ѿܺõؼˣjava.io.\* Ѿ NIO Ϊʵˣ NIO һЩԡ磬 java.io.\* еһЩԿʽдݵķʹüʹڸϵͳУٶҲ졣 -## 2. ????????? +## 2. ͨ뻺 -### 2.1 ??? +### 2.1 ͨ -??? Channel ???? I/O ???????????????????????????????? +ͨ Channel Ƕԭ I/O еģ⣬ͨȡдݡ -????????????????????????????????????????(??????????? InputStream ???? OutputStream ??????)?? ???????????????????????????????????? +ͨIJ֮ͬڣֻһƶ(һ InputStream OutputStream ) ͨ˫ģڶдͬʱڶд -???????????????? +ͨͣ -- FileChannel?????????????? -- DatagramChannel????? UDP ????????????? -- SocketChannel????? TCP ????????????? -- ServerSocketChannel???????????????? TCP ?????????????????????????????? SocketChannel?? +- FileChannelļждݣ +- DatagramChannelͨ UDP дݣ +- SocketChannelͨ TCP дݣ +- ServerSocketChannelԼ½ TCP ӣÿһ½Ӷᴴһ SocketChannel -### 2.2 ?????? +### 2.2 -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +͸һͨж󶼱ȷŵУͬأͨжȡκݶҪСҲ˵ֱӶͨждݣȾ -??????????????????????????????????????????????????????????????????????????????/????? +ʵһ飬һ顣ṩ˶ݵĽṹʣһԸϵͳĶ/д̡ -??????????????????? +ͣ - ByteBuffer - CharBuffer @@ -197,50 +197,50 @@ I/O ???? NIO ?????????????java.io.\* ????? NIO ????????????????????????????????? - DoubleBuffer -## 3. ???????????? +## 3. ״̬ -- capacity??????????? -- position??????????????????? -- limit?????????????????? +- capacity +- positionǰѾдֽ +- limitԶдֽ -?????????????? +״̬ĸı̣ -1\. ????????? 8 ???????????????? position ? 0???? limit == capacity == 9??capacity ??????????????????????????? +1\. ½һСΪ 8 ֽڵĻʱ position Ϊ 0 limit == capacity == 9capacity ı䣬ۻ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1bea398f-17a7-4f67-a90b-9e2d243eaa9a.png) -2\. ???????????? 3 ???????????????????? position ?????? 3??limit ??????? +2\. ͨжȡ 3 ֽд뻺Уʱ position ƶΪ 3limit ֲ䡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4628274c-25b6-4053-97cf-d1239b44c43d.png) -3\. ??????????????????????????????????? flip() ??????????????? limit ???????? position?????? position ????? 0?? +3\. ڽд֮ͨǰҪȵ flip() limit Ϊǰ position position Ϊ 0 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/952e06bd-5a65-4cab-82e4-dd1536462f38.png) -4\. ?????????? 4 ?????????????????? position ??? 4?? +4\. ӻȡ 4 ֽڵУʱ position Ϊ 4 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b5bdcbe2-b958-4aef-9151-6ad963cb28b4.png) -5\. ?????????? clear() ??????????????????? position ?? limit ?????????????? +5\. Ҫ clear() ջʱ position limit Ϊλá ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/67bf5487-c45d-49b6-b9c0-a058d8c68902.png) -## 4. ???????? +## 4. дļʵ -1\. ?????????????? FileInputStream???????? FileInputStream ??????? FileChannel?? +1\. ΪҪȡļ FileInputStream֮ͨ FileInputStream ȡ FileChannel ```java FileInputStream fin = new FileInputStream("readandshow.txt"); FileChannel fic = fin.getChannel(); ``` -2\. ???????????? 1024 ?? Buffer +2\. һΪ 1024 Buffer ```java ByteBuffer buffer = ByteBuffer.allocate(1024); ``` -3\. ??????????? FileChannel ?? Buffer ???????????????? read() ???????? -1 +3\. ݴ FileChannel д뵽 Buffer УûݵĻ read() ᷵ -1 ```java int r = fcin.read(buffer); @@ -249,86 +249,86 @@ if (r == -1) { } ``` -4\. ???????????? FileOutputStream???????? FileOutputStream ?????? FileChannel +4\. ΪҪдļ FileOutputStream֮ͨ FileOutputStream ȡ FileChannel ```java FileOutputStream fout = new FileOutputStream("writesomebytes.txt"); FileChannel foc = fout.getChannel(); ``` -5\. ???? flip() ???? +5\. flip() лд ```java buffer.flip(); ``` -6\. ?? Buffer ????????????? FileChannel ?? +6\. Buffer еݶȡ FileChannel ```java foc.write(buffer); ``` -7\. ?????? clear() ????????? +7\. clear() û ```java buffer.clear(); ``` -## 5. ??????????? +## 5. -??????FileChannel ??????????????????????? Channel ????? +Ӧע⣬FileChannel лģʽ׽ Channel ԡ -### 5.1 ????? I/O +### 5.1 ʽ I/O -????? I/O ????? InputStream.read() ?????????????????????????????????????? ServerSocket.accept() ????????????????????????????????????????????????????????????????????????????????? +ʽ I/O ڵ InputStream.read() ʱһֱȵݵʱʱŻ᷵أڵ ServerSocket.accept() ʱҲһֱпͻӲŻ᷵أÿͻӹ󣬷˶һ߳ȥÿͻ˵ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/edc23f99-c46c-4200-b64e-07516828720d.jpg) -### 5.2 ??????? I/O +### 5.2 ʽ I/O -?????????????????????? I/O ??????????????? +һרŵ߳е I/O ¼ַ -?????????????????????????????????????????????? +¼ƣ¼ʱ򴥷ͬȥ¼ -???????????????? wait()??notify() ????????????????????????????????????????????????? +߳ͨţ֮߳ͨ wait()notify() ȷʽͨţ֤ÿлģν߳л ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7fcb2fb0-2cd9-4396-bc2d-282becf963c3.jpg) -## 6. ???????? +## 6. ׽ʵ ### 6.1 ServerSocketChannel -???????????????? ServerSocketChannel ????????????? +ÿһ˿ڶҪһ ServerSocketChannel ӡ ```java ServerSocketChannel ssc = ServerSocketChannel.open(); -ssc.configureBlocking(false); // ??????????? +ssc.configureBlocking(false); // Ϊ ServerSocket ss = ssc.socket(); InetSocketAddress address = new InetSocketAddress(ports[i]); -ss.bind(address); // ????? +ss.bind(address); // 󶨶˿ں ``` ### 6.2 Selectors -?? I/O ??? Selector ??????? I/O ???????? ?D ????????????????????????????????????????????????????????? +첽 I/O ͨ Selector עض I/O ¼Ȥ D ɶݵĵµ׽ӵȵȣڷ¼ʱϵͳ֪ᷢͨ -???? Selectors ?????????????????????? register() ??????register() ????????????????? Selector????????????? OP_ACCEPT?????????????????????? accept ???????????????????????????????????? + Selectors ֮󣬾ͿԶԲͬͨ register() register() ĵһ Selectorڶ OP_ACCEPTָҪ accept ¼Ҳµӽʱ¼ -SelectionKey ????????????? Selector ?????????????? Selector ????????????????????????????????????? SelectionKey ???????SelectionKey ????????????????????? +SelectionKey ͨڴ Selector ϵעᡣij Selector ֪ͨij¼ʱͨṩӦڸ¼ SelectionKey еġSelectionKey ȡͨעᡣ ```java Selector selector = Selector.open(); SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT); ``` -### 6.3 ????? +### 6.3 ѭ -???????????? Selector ?? select() ??????????????????????????????????????????????????????????????????????? select() ????????????????????????????? +ȣǵ Selector select() ֱһע¼һ߸¼ʱ select() ¼ -??????????????? Selector ?? selectedKeys() ?????????????????????? SelectionKey ???????? ???? ?? +ǵ Selector selectedKeys() ط¼ SelectionKey һ -??????????? SelectionKeys ??????????? SelectionKey ??????????????????? SelectionKey????????????????????? I/O ??????????????????? I/O ???? +ͨ SelectionKeys δÿ SelectionKey ¼ÿһ SelectionKeyȷʲô I/O ¼Լ¼ӰЩ I/O ```java int num = selector.select(); @@ -342,9 +342,9 @@ while (it.hasNext()) { } ``` -### 6.4 ?????????? +### 6.4 -????????????????????? ServerSocketChannel??????????????????????????????????????? SelectionKey ???? readyOps() ?????????????????????????? +ִеǽע ServerSocketChannelҽעǡա¼Ϊȷһ㣬Ƕ SelectionKey readyOps() 鷢ʲô͵¼ ```java if ((key.readyOps() & SelectionKey.OP_ACCEPT) @@ -354,39 +354,39 @@ if ((key.readyOps() & SelectionKey.OP_ACCEPT) } ``` -??????????? readOps() ?????????????????????????? +Կ϶˵ readOps() Ǹ¼µӡ -### 6.5 ??????????? +### 6.5 µ -?????????????????????????????????????????????????????????????????????????????? accept() ???????????? +Ϊ֪׽һڵȴԿ԰ȫؽҲ˵õ accept() ```java ServerSocketChannel ssc = (ServerSocketChannel)key.channel(); SocketChannel sc = ssc.accept(); ``` -??????????????? SocketChannel ??????????????????????????????????????????????????????????????????????? SocketChannel ??? Selector???????????? +һǽӵ SocketChannel ΪġڽӵĿΪ˶ȡ׽ֵݣǻ뽫 SocketChannel עᵽ Selectorϣʾ ```java sc.configureBlocking( false ); SelectionKey newKey = sc.register( selector, SelectionKey.OP_READ ); ``` -?????????? register() ?? OP_READ ???????? SocketChannel ??????? ??? ?????? ???? ??????? +עʹ register() OP_READ SocketChannel ע ȡ ӡ -### 6.6 ?????????? SelectionKey +### 6.6 ɾ SelectionKey -????? SelectionKey ???????????????????????????????????????????????? SelectionKey ????????????????????????????????????????????????????????????????????????????????????????????????????????????????? remove() ???????????????? SelectionKey?? +ڴ SelectionKey ֮ǼԷѭˡDZȽ SelectionKey ѡļɾûɾļôȻһļ֣ᵼdzٴδǵõ remove() ɾ SelectionKey ```java it.remove(); ``` -???????????????????????????????????????????(???????????? I/O ???)??? +ǿԷѭܴһ׽д(һ I/O ¼)ˡ -### 6.7 ????? I/O +### 6.7 I/O -?????????????????????????????????? I/O ????????????????????? Selector.select()????????????????? I/O ?????????? SelectionKey ???????? OP_READ ?????????????? +һ׽ֵݵʱᴥһ I/O ¼ᵼѭе Selector.select()һ߶ I/O ¼һΣ SelectionKey Ϊ OP_READ ¼ʾ ```java } else if ((key.readyOps() & SelectionKey.OP_READ) @@ -398,9 +398,9 @@ it.remove(); ``` -# ?????? +# ο -- Eckel B, ????? , ??? , ?? . Java ?????? [M]. ??????????? , 2002. -- [IBM: NIO ????](https://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.html) -- [ ??????? Java I/O ????????? ](https://www.ibm.com/developerworks/cn/java/j-lo-javaio/index.html) -- [NIO ??? IO ?????? ](http://blog.csdn.net/shimiso/article/details/24990499) +- Eckel B, ˶ , , . Java ˼ [M]. еҵ , 2002. +- [IBM: NIO ](https://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.html) +- [ Java I/O Ĺ ](https://www.ibm.com/developerworks/cn/java/j-lo-javaio/index.html) +- [NIO 봫ͳ IO ](http://blog.csdn.net/shimiso/article/details/24990499) diff --git a/notes/Java 基础语法.md b/notes/Java 基础语法.md index 05a53044..87fc192b 100644 --- a/notes/Java 基础语法.md +++ b/notes/Java 基础语法.md @@ -1,140 +1,140 @@ -* [????](#????) +* [](#) * [ final](#-final) - * [????????](#????????) - * [???????](#???????) -* [????](#????) + * [ʼ˳](#ʼ˳) + * [Ȩ](#Ȩ) +* [](#) * [Set](#set) * [Queue](#queue) * [Map](#map) -* [????](#????) -* [??](#??) -* [??????](#??????) +* [](#) +* [쳣](#쳣) +* [ο](#ο) -# ???? +# ## final -**final ????** +**final ** -?????????????????????????????????????????????????????????????????? +ΪDZʱҲʱʼܱıij -????????????final ???????????????????final ????????????????????????????????????????????????? +ڻͣfinal ʹֵ䣻öfinal ʹò䣬ҲͲ󣬵DZõĶǿ޸ĵġ -**final ????** +**final ** -???????????????????? +ܱาǡ -private ?????????????? final????????????????????????????? private ???????????????????????????????????????????????? +private ʽرָΪ finalжķͻеһ private ǩͬʱķǸǻ෽ˡ -**final ??** +**final ** -????????????? +಻̳С -## ???????? +## ʼ˳ -static ???????????????????????????????????????????????????????????????????????? +static ľ̬ڴֻһݣֻһʵʱʼһΣݵijʼ ```java -public static String staticField = "???????"; +public static String staticField = "̬"; ``` -static ????? static ???????????????????????????????????????????????????????????? +static static һһʵʱһΣĸȡڴе˳ ```java static { - System.out.println("??????????"); + System.out.println("̬ʼ"); } ``` -?????????????????????????????????????????????? +ͨݺͨijʼھ̬ݺ;̬ʼ֮ ```java -public String field = "????"; +public String field = ""; ``` ```java { - System.out.println("???????"); + System.out.println("ʼ"); } ``` -??????????????????????? +ǹ캯еݽгʼ ```java public InitialOrderTest() { - System.out.println("??????"); + System.out.println(""); } ``` -??????????????????????? +ڼ̳е£ʼ˳Ϊ -1. ?????????????????? -2. ?????????????????? -3. ???????????? -4. ???????????? -5. ???????????? -6. ???????????? +1. ̬ࣨݡ̬飩 +2. ̬ࣨݡ̬飩 +3. ࣨݡ飩 +4. ࣨ +5. ࣨݡ飩 +6. ࣨ -## ??????? +## Ȩ -Java ?????????????????????private??protected ??? public?????????????????????????????? +Java Ȩηprivateprotected Լ publicӷηʾɼ -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ԶеijԱֶԼϷηԱɼʾóԱĶʵóԱɼʾഴ󣬿԰൱еһԱȻʾһ࣬ͺˡ -protected ????????????????????????????????????????????????????????????????????????????? +protected γԱʾڼ̳ϵгԱɼηû壬Ϊûм̳ϵ -# ???? +# ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/114c49a6-72e3-4264-ae07-c564127094ac.png) -??????????? Collection ?? Map ?????Collection ??????? List??Set ??? Queue?? +Ҫ Collection Map ֣Collection ְ ListSet Լ Queue ## Set -- HashSet????? Hash ??????????????????????????? +- HashSetʹ Hash ʵֿ֣֧ٲңʧȥԣ -- TreeSet?????????????????????????????? HashSet?? +- TreeSetʹʵ֣򣬵DzЧʲ HashSet -- LinkedListHashSet?????? HashSet ?????????????????????????????????????????????? +- LinkedListHashSet HashSet IJЧʣڲʹάԪصIJ˳˾ԡ ## Queue -???????????LinkedList ?? PriorityQueue?????? LinkedList ????????? +ֻʵ֣LinkedList PriorityQueue LinkedList ֧˫С ## Map -- HashMap????? Hash ??? +- HashMapʹ Hash ʵ -- LinkedHashMap???????????????????????????????????LRU????? +- LinkedHashMap˳Ϊ˳ʹãLRU˳ -- TreeMap????????????? +- TreeMapںʵ -- ConcurrentHashMap??????? Map?????p??????? +- ConcurrentHashMap̰߳ȫ Map漰ͬ -# ???? +# -?????????? **Class** ?????????????????????????????????????????????????? .class ??????????????????? Class ???? +ÿ඼һ **Class** 󣬰йصϢһʱһͬ .class ļļݱ Class -????????? Class ?????????????????????????????? JVM ????????? Class.forName('com.mysql.jdbc.Driver.class') ?????????????????????????????? Class ???? +൱ Class ļءڵһʹʱŶ̬ص JVM Уʹ Class.forName('com.mysql.jdbc.Driver.class') ַʽļأ÷᷵һ Class -???????????????????????????????????????????????????????????????????? .class ??????????????????? +ṩʱϢʱżؽڱʱڸ .class ҲԼؽ -Class ?? java.lang.reflect ???????????????java.lang.reflect ???????? **Field**??**Method** ??? **Constructor** ????????? get() ?? set() ???????????? Field ??????????????????? invoke() ?????????? Method ???????????????????? Constructor ?????????? +Class java.lang.reflect һԷṩ֧֣java.lang.reflect **Field****Method** Լ **Constructor** ࡣʹ get() set() ȡ޸ Field ֶΣʹ invoke() Method ķ Constructor µĶ -IDE ?????????????????????????????????????????????????????????????????????????? +IDE ʹ÷ƻȡϢʹһĶʱֶܹΡ͹캯Ϣгûѡ -# ?? +# 쳣 -Throwable ????????????ʦ???????????????????????**Error** ?? **Exception**?????? Error ?????????????????? +Throwable ʾκοΪ쳣׳࣬Ϊ֣**Error** **Exception** Error ʾʱϵͳ -Exception ????????**?????** ?? **???????**???????????? try...catch... ??????????????????????????????????????????????????? 0 ?????? Arithmetic Exception??????????????????????? +Exception Ϊ֣**ܼ쳣** **ܼ쳣**ܼ쳣Ҫ try...catch... 䲶񲢽дҿԴ쳣лָܼ쳣dzʱ 0 Arithmetic Exceptionʱ޷ָ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/48f8f98e-8dfd-450d-8b5b-df4688f0d377.jpg) -# ?????? +# ο -- Eckel B, ????? , ??? , ?? . Java ?????? [M]. ??????????? , 2002. -- [Java ????????? ](https://segmentfault.com/a/1190000004527951) +- Eckel B, ˶ , , . Java ˼ [M]. еҵ , 2002. +- [Java ʼ˳ ](https://segmentfault.com/a/1190000004527951) diff --git a/notes/Java 容器.md b/notes/Java 容器.md index 8bf48f09..8bbc9b45 100644 --- a/notes/Java 容器.md +++ b/notes/Java 容器.md @@ -1,78 +1,78 @@ -* [????](#????) +* [](#) * [1. List](#1-list) * [2. Set](#2-set) * [3. Queue](#3-queue) * [4. Map](#4-map) - * [5. Java 1.0/1.1 ????](#5-java-1011-????) -* [???????????](#???????????) - * [1. ????????](#1-????????) - * [2. ????????](#2-????????) -* [???](#???) -* [??????](#??????) + * [5. Java 1.0/1.1 ](#5-java-1011-) +* [еģʽ](#еģʽ) + * [1. ģʽ](#1-ģʽ) + * [2. ģʽ](#2-ģʽ) +* [ɢ](#ɢ) +* [Դ](#Դ) * [1. ArraList](#1-arralist) - * [2. Vector ?? Stack](#2-vector-??-stack) + * [2. Vector Stack](#2-vector--stack) * [3. LinkedList](#3-linkedlist) * [4. TreeMap](#4-treemap) * [5. HashMap](#5-hashmap) * [6. LinkedHashMap](#6-linkedhashmap) * [7. ConcurrentHashMap](#7-concurrenthashmap) -* [??????](#??????) +* [ο](#ο) -# ???? +# ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ebf03f56-f957-4435-9f8f-0f605661484d.jpg) -??????????? Collection ?? Map ?????Collection ??????? List??Set ??? Queue?? +Ҫ Collection Map ֣Collection ְ ListSet Լ Queue ## 1. List -- ArrayList??????????????????????????? +- ArrayListڶ̬ʵ֣֧ʣ -- LinkedList????????????????????????????????????????????????????????????????????LinkedList ??????????????????????? +- LinkedList˫ѭʵֻ֣˳ʣǿԿٵмɾԪءˣLinkedList ջк˫˶С ## 2. Set -- HashSet?????? Hash ??????????????????????????? +- HashSet Hash ʵֿ֣֧ٲңʧȥԣ -- TreeSet????????????????????????????????? HashSet?? +- TreeSetںʵ֣򣬵DzЧʲ HashSet -- LinkedListHashSet?????? HashSet ?????????????????????????????????????????????? +- LinkedListHashSet HashSet IJЧʣڲʹάԪصIJ˳˾ԡ ## 3. Queue -???????????LinkedList ?? PriorityQueue?????? LinkedList ?????????PriorityQueue ??????????? +ֻʵ֣LinkedList PriorityQueue LinkedList ֧˫УPriorityQueue ǻڶѽṹʵ֡ ## 4. Map -- HashMap?????? Hash ??? +- HashMap Hash ʵ -- LinkedHashMap??????????????????????????????????????????????LRU????? +- LinkedHashMapʹάԪص˳˳Ϊ˳ʹãLRU˳ -- TreeMap????????????? +- TreeMapںʵ -- ConcurrentHashMap??????? Map?????p?????? HashTable ????????? +- ConcurrentHashMap̰߳ȫ Map漰 HashTable ͬ -## 5. Java 1.0/1.1 ???? +## 5. Java 1.0/1.1 -?????????????????????????????????????????????? +ھɵǾӦʹǣֻҪǽ˽⡣ -- Vector???? ArrayList ?????????????????? +- Vector ArrayList ƣ̰߳ȫ -- HashTable???? HashMap ?????????????????? +- HashTable HashMap ƣ̰߳ȫ -# ??????????? +# еģʽ -## 1. ???????? +## 1. ģʽ -????????????????????????????? Iterator ??????????????????????????????????????? +ӸͼԿÿ඼һ Iterator 󣬿ͨеԪء -[Java ????????? ](https://github.com/CyC2018/InterviewNotes/blob/master/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F.md#92-java-%E5%86%85%E7%BD%AE%E7%9A%84%E8%BF%AD%E4%BB%A3%E5%99%A8) +[Java еĵģʽ ](https://github.com/CyC2018/InterviewNotes/blob/master/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F.md#92-java-%E5%86%85%E7%BD%AE%E7%9A%84%E8%BF%AD%E4%BB%A3%E5%99%A8) -## 2. ???????? +## 2. ģʽ -java.util.Arrays#asList() ????????????????? List ????? +java.util.Arrays#asList() ԰תΪ List ͡ ```java List list = Arrays.asList(1, 2, 3); @@ -80,44 +80,44 @@ java.util.Arrays#asList() ????????????????? List ????? list = Arrays.asList(arr); ``` -# ??? +# ɢ -??? hasCode() ?????????????????????????? +ʹ hasCode() ɢֵʹõǶĵַ -?? equals() ????????????????????????????????????????????????????????????????????????????? + equals() жǷȵģȵɢֵһҪͬɢֵͬһȡ -?????????????????????? +ȱʣ -1. ????? -2. ????? -3. ?????? -4. ??????????? x.equals(y)????????? -5. ???ʦ??? null ????? x ???? x.equals(nul) ?????? false +1. Է +2. Գ +3. +4. һԣε x.equals(y)䣩 +5. κβ null Ķ x x.equals(nul) Ϊ false -# ?????? +# Դ -????????? [ ?? - ???? ](https://github.com/CyC2018/InterviewNotes/blob/master/notes/%E7%AE%97%E6%B3%95.md#%E7%AC%AC%E4%B8%89%E7%AB%A0-%E6%9F%A5%E6%89%BE) ??????????????????????????? +Ķ [ 㷨 - ](https://github.com/CyC2018/InterviewNotes/blob/master/notes/%E7%AE%97%E6%B3%95.md#%E7%AC%AC%E4%B8%89%E7%AB%A0-%E6%9F%A5%E6%89%BE) ֣ԼԴкܴ -????????[OpenJDK 1.7](http://download.java.net/openjdk/jdk7) +Դأ[OpenJDK 1.7](http://download.java.net/openjdk/jdk7) ## 1. ArraList [ArraList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/ArrayList.java) -????? RandomAccess ??????????????????????????????????? ArrayList ?????????????? +ʵ RandomAccess ӿڣ֧ʣȻģΪ ArrayList ǻʵֵġ ```java public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable ``` -??????????????????????????? transient ??????????????????????????????????????????????????????????????? writeObject() ?? readObject()?? +ʵ֣Ԫصʹ transient ΣΪ鲻һλöռԪأҲûҪȫлҪд writeObject() readObject() ```java private transient Object[] elementData; ``` -?????????? 10 +ĬϴСΪ 10 ```java public ArrayList(int initialCapacity) { @@ -132,7 +132,7 @@ public ArrayList() { } ``` -??????????? System.arraycopy() ????????????????????????????????????????????????????????????????????????? +ɾԪʱ System.arraycopy() ԪؽиƣɾɱܸߣڴʱָŵСٸƲִд ```java public E remove(int index) { @@ -150,9 +150,9 @@ public E remove(int index) { } ``` -?????????? ensureCapacity() ????????????????????????????????????????????????????????? 1.5 ???? +Ԫʱʹ ensureCapacity() ֤㹻ʱҪݣʹΪ 1.5 -modCount ??????? ArrayList ??????????????????????? add() ?? addAll() ?????????? ensureCapacity()?????????? ensureCapacity() ?? modCount ???????? +modCount ¼ ArrayList 仯ĴΪÿڽ add() addAll() ʱҪ ensureCapacity()ֱ ensureCapacity() ж modCount ޸ġ ```java public void ensureCapacity(int minCapacity) { @@ -190,7 +190,7 @@ private static int hugeCapacity(int minCapacity) { } ``` -???????????????????????????????????? modCount ??????????????????? ConcurrentModificationException?? +ڽлߵȲʱҪȽϲǰ modCount Ƿı䣬ıҪ׳ ConcurrentModificationException ```java private void writeObject(java.io.ObjectOutputStream s) @@ -213,20 +213,20 @@ private void writeObject(java.io.ObjectOutputStream s) } ``` -**?? Vector ??????** +** Vector ** -1. Vector ?? ArrayList ?????????????????????????? Vector ????????????????? ArrayList ????????????????? ArrayList ?????? Vector??????????????????????????????? -2. Vector ???????????????? 2 ??????? ArrayList ?? 1.5 ???? +1. Vector ArrayList ȫͬģΨһ Vector ͬģ˿ͱ ArrayList Ҫ󣬷Ҫʹ ArrayList VectorΪͬȫɳԱԼƣ +2. Vector ÿС 2 ռ䣬 ArrayList 1.5 -????????????? ArrayList????????? Collections.synchronizedList(new ArrayList<>()); ?????????????? ArrayList?????????? concurrent ????????? CopyOnWriteArrayList ?? +Ϊʹ̰߳ȫ ArrayListʹ Collections.synchronizedList(new ArrayList<>()); һ̰߳ȫ ArrayListҲʹ concurrent µ CopyOnWriteArrayList ࣻ -**?? LinkedList ??????** +** LinkedList ** -1. ArrayList ??????????????LinkedList ?????????????????? -2. ArrayList ???????????LinkedList ?????? -3. LinkedList ???????????????????? +1. ArrayList ڶ̬ʵ֣LinkedList ˫ѭʵ֣ +2. ArrayList ֧ʣLinkedList ֧֣ +3. LinkedList λɾԪظ졣 -## 2. Vector ?? Stack +## 2. Vector Stack [Vector.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/Vector.java) @@ -242,13 +242,13 @@ private void writeObject(java.io.ObjectOutputStream s) [HashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java) -??????????????????? +ʹͻ -??????? capacity ? 16????????????????????? 2 ????????????? Entry[] table ?????????size ????????????????? +Ĭ capacity Ϊ 16Ҫע뱣֤Ϊ 2 Ĵη Entry[] table ijȣsize ʵʹ -threshold ?????? size ????????size ?????? threshold?????????????????????????????? +threshold 涨һ size ٽֵsize С thresholdڵڣͱݲ -threshold = capacity * load_factor?????? load_factor ? table ????????????????load_factor ???????????????????????????????????????? +threshold = capacity * load_factor load_factor Ϊ table ܹʹõıload_factor ᵼ¾۴صij֣ӶӰѯͲЧʣ㷨ʼǡ ```java static final int DEFAULT_INITIAL_CAPACITY = 16; @@ -268,7 +268,7 @@ final float loadFactor; transient int modCount; ``` -??????????????????????????????????????? capacity ???????????? +ԪشпԿҪʱ capacity Ϊԭ ```java void addEntry(int hash, K key, V value, int bucketIndex) { @@ -279,7 +279,7 @@ void addEntry(int hash, K key, V value, int bucketIndex) { } ``` -Entry ??????????????????????? next ??????????????? +Entry ʾһֵԪأе next ָлʱʹá ```java static class Entry implements Map.Entry { @@ -290,7 +290,7 @@ static class Entry implements Map.Entry { } ``` -get() ???????????????????key ? null ?? ??? null??????????? HashMap ??????? null ??????? +get() Ҫֳkey Ϊ null Ϊ nullпԿ HashMap null Ϊ ```java public V get(Object key) { @@ -306,7 +306,7 @@ public V get(Object key) { } ``` -put() ???????????? key ???? null ????????????????????????????? key ? null ??????????????? key ? null ???????????????????? 0 ?????????? null ??????? hash ??????????????????????????? +put() ҲҪ key ǷΪ null ͬĴҪעû key Ϊ null ļֵԣ²һ key Ϊ null ļֵʱĬǷ 0 λãΪ null ܼ hash ֵҲ޷֪Ӧ÷ĸϡ ```java public V put(K key, V value) { @@ -354,8 +354,8 @@ private V putForNullKey(V value) { [ConcurrentHashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java) -[ ??? ConcurrentHashMap ?????????????? ](https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/) +[ ̽ ConcurrentHashMap ߲Եʵֻ ](https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/) -# ?????? +# ο -- Java ?????? +- Java ˼ diff --git a/notes/Leetcode 题解.md b/notes/Leetcode 题解.md index d28a471f..0727747e 100644 --- a/notes/Leetcode 题解.md +++ b/notes/Leetcode 题解.md @@ -1,67 +1,67 @@ -* [?????](#?????) - * [???????](#???????) - * [??????](#??????) - * [????](#????) - * [????](#????) - * [???????](#???????) - * [??????](#??????) - * [?????](#?????) - * [????](#????) +* [㷨˼](#㷨˼) + * [ֲ](#ֲ) + * [̰˼](#̰˼) + * [˫ָ](#˫ָ) + * [](#) + * [ѡ](#ѡ) + * [](#) + * [Ͱ](#Ͱ) + * [](#) * [BFS](#bfs) * [DFS](#dfs) * [Backtracking](#backtracking) - * [????](#????) - * [???](#???) - * [????](#????) - * [???????](#???????) - * [??????](#??????) - * [??????????](#??????????) - * [????????????](#????????????) - * [???????????](#???????????) - * [0-1 ????](#0-1-????) - * [????????](#????????) - * [???????](#???????) - * [????????](#????????) - * [???](#???) - * [????](#????) - * [??????](#??????) - * [???????](#???????) - * [???](#???) - * [????????????](#????????????) - * [????????](#????????) - * [??????????](#??????????) - * [????](#????) -* [????????](#????????) - * [??????](#??????) - * [?????](#?????) - * [?????](#?????) - * [?????????](#?????????) - * [???????](#???????) - * [????](#????) - * [??](#??) - * [???](#???) - * [?????](#?????) - * [????????](#????????) + * [](#) + * [ݹ](#ݹ) + * [̬滮](#̬滮) + * [ָ](#ָ) + * [·](#·) + * [쳲](#쳲) + * [](#) + * [ϵ](#ϵ) + * [0-1 ](#0-1-) + * [](#) + * [ַ༭](#ַ༭) + * [](#) + * [ѧ](#ѧ) + * [](#) + * [Լ](#Լ) + * [ת](#ת) + * [׳](#׳) + * [ַӷ](#ַӷ) + * [](#) + * [ͶƱ](#ͶƱ) + * [](#) +* [ݽṹ](#ݽṹ) + * [ջͶ](#ջͶ) + * [ϣ](#ϣ) + * [ַ](#ַ) + * [](#) + * [](#) + * [](#) + * [](#) + * [ݹ](#ݹ) + * [α](#α) + * [ǰк](#ǰк) * [BST](#bst) * [Trie](#trie) - * [?](#?) - * [????](#????) -* [??????](#??????) + * [ͼ](#ͼ) + * [λ](#λ) +* [ο](#ο) -# ????? +# 㷨˼ -## ??????? +## ֲ -???????????????????????????????????? +ֲ˼򵥣ʵʱһЩҪעϸڣ -1. ????? mid ???????? mid = (l + h) / 2 ??????????? l + h ???????????????????? mid = l + (h - l) / 2 ?? +1. ڼ mid ʱʹ mid = (l + h) / 2 ַʽΪ l + h ܻᵼ¼ӷӦʹ mid = l + (h - l) / 2 -2. ?? h ?????????????????????????? l <= h ???h = mid - 1???????????? l < h ???h = mid?? -??????????????????? l <= h ?????? h = mid??????????????????????????? l = 1??h = 1????? mid ????? 1??????????????? h = mid ??????????????????????????? l < h ????? h = mid - 1??????????????????????????????? 1,2,3 ??????? 1 ????? l = 0??h = 2??mid = 1???? key < arr[mid] ??? h = mid - 1 = 0?????????????????????????????? +2. h ĸֵѭйأѭΪ l <= h ʱh = mid - 1ѭΪ l < h ʱh = mid +£ѭΪ l <= h ʱ h = midѭ޷˳ l = 1h = 1ʱ mid Ҳ 1ʱִ h = mid ôͻѭѭΪ l < h h = mid - 1ҵ 1,2,3 Ҫ 1 ʼ l = 0h = 2mid = 1ж key < arr[mid] ִ h = mid - 1 = 0ʱѭ˳ֱӰѲҵˡ -3. l ???????? l = mid + 1?? +3. l ĸֵһ㶼Ϊ l = mid + 1 ```java public int search(int key, int[] arr) { @@ -76,11 +76,11 @@ public int search(int key, int[] arr) { } ``` -**???** +**󿪷** [Leetcode : 69. Sqrt(x) (Easy)](https://leetcode.com/problems/sqrtx/description/) -????? x ????? sqrt ????? 0 \~ x ??????????? sqrt == x / sqrt ?????????????????? 0 \~ x ?????? sqrt?? +һ x Ŀ sqrt һ 0 \~ x ֮䣬 sqrt == x / sqrt öֲ 0 \~ x ֮ sqrt ```java public int mySqrt(int x) { @@ -97,7 +97,7 @@ public int mySqrt(int x) { } ``` -**?????** +**Ӳ** [Leetcode : 441. Arranging Coins (Easy)](https://leetcode.com/problems/arranging-coins/description/) @@ -105,17 +105,17 @@ public int mySqrt(int x) { n = 8 The coins can form the following rows: -?? -?? ?? -?? ?? ?? -?? ?? + + + + Because the 4th row is incomplete, we return 3. ``` -??????????? i ?? i ???????????????????? +Ŀ i а i ͳܹе -???? h ?????? l????????????????????????? + h lΪڵӲһвȥ ```java public int arrangeCoins(int n) { @@ -131,7 +131,7 @@ public int arrangeCoins(int n) { } ``` -?????????????????????????? +Բöֲңֱ۵Ľⷨ£ ```java public int arrangeCoins(int n) { @@ -144,18 +144,18 @@ public int arrangeCoins(int n) { } ``` -**????????? Single Element** +** Single Element** [Leetcode : 540. Single Element in a Sorted Array (Medium)](https://leetcode.com/problems/single-element-in-a-sorted-array/description/) -???????????????????????????????????????????????? +ĿһֻһΣҳ ```java public int singleNonDuplicate(int[] nums) { int l = 0, h = nums.length - 1; while(l < h) { int m = l + (h - l) / 2; - if(m % 2 == 1) m--; // ??? l/h/m ????????????????????????? ???? + if(m % 2 == 1) m--; // ֤ l/h/m żλʹòСһֱ if(nums[m] == nums[m + 1]) l = m + 2; else h = m; } @@ -163,19 +163,19 @@ public int singleNonDuplicate(int[] nums) { } ``` -## ?????? +## ̰˼ -???????????????????????????????????????????????? +̰˼뱣֤ÿβǾֲŵģõĽȫŵġ -**???????** +**** [Leetcode : 455. Assign Cookies (Easy)](https://leetcode.com/problems/assign-cookies/description/) -??????????????????????????????????????????????????????????????????????????????????????????????????????????? +ĿÿӶһȣÿɶһСֻбɵĴСһӵȣúӲŻ㡣Իĺ -????????????????????????????????????????????????????????????????????????????????????????????????????? +ΪСĺ׵õ㣬СӡһӵıӦСúӣɾȱȽϴĺӡ -??????????????????????????????? i ?????????? m ???????????? i ????????????????? m ???????????????? i ??????????????????????????????????? k ???????????????????????????? i ?????????? n ?????????????????????? m ??????????????????????????????? k ?????????????????? m ???????????? n ???????????????????????????????????????????????????????????? +֤ijѡУ̰IJѡ i ӷ m ɣҵ i С m Ϊ i ӵСɣ̰IJտ k ӡŲѡи i ӷ n ɣɴڵ m ɣôŲҪ k ӡǷʹõ m ȥ n ȫӰĽ̰IJԾŲԣ̰IJŵġ ```java public int findContentChildren(int[] g, int[] s) { @@ -190,7 +190,7 @@ public int findContentChildren(int[] g, int[] s) { } ``` -**??????????** +**Ͷڴ** [Leetcode : 452. Minimum Number of Arrows to Burst Balloons (Medium)](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/) @@ -202,7 +202,7 @@ Output: 2 ``` -???????????????????????????????????????????????????????????????????????????????????????? +Ŀһˮƽϰڷţصڴֱᣬʹ·ϵ򶼻ƣСͶڴ ```java public int findMinArrowShots(int[][] points) { @@ -221,13 +221,13 @@ public int findMinArrowShots(int[][] points) { } ``` -**????????????** +**Ʊ** [Leetcode : 122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/) -??????????????????????????????????? +ƱȻ۹ƱӶò۴档 -??????????? [a, b, c, d]??????? a <= b <= c <= d ????????????? d - a = (d - c) + (c - b) + (b - a) ???????????? prices[i] ?? prices[i] - prices[i-1] ???????? prices[i] - prices[i-1] ?????????????????????????????????????? +һ [a, b, c, d] a <= b <= c <= d ôΪ d - a = (d - c) + (c - b) + (b - a) ʵһ prices[i] prices[i] - prices[i-1] ôͰ prices[i] - prices[i-1] ӵУӶھֲŵҲ֤ȫš ```java public int maxProfit(int[] prices) { @@ -239,7 +239,7 @@ public int maxProfit(int[] prices) { } ``` -**???????** +**ֲ** [Leetcode : 605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/) @@ -248,7 +248,7 @@ Input: flowerbed = [1,0,0,0,1], n = 1 Output: True ``` -?????????????????????????????????? +Ŀ֮Ҫһλļ ```java public boolean canPlaceFlowers(int[] flowerbed, int n) { @@ -266,15 +266,15 @@ public boolean canPlaceFlowers(int[] flowerbed, int n) { } ``` -**???????????????????** +**޸һΪǵݼ** [Leetcode : 665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/) -??????????????????????????????????????????? +Ŀжһֻܲ޸һͳΪǵݼ顣 -?? nums[i] < nums[i - 1] ??????????????????? nums[i - 1] = nums[i]??????????? nums[i] = nums[i - 1] ???????? nums[i] ??????????????????? nums[i + 1] ???????????? nums[i] ???? + nums[i] < nums[i - 1] £ȿ nums[i - 1] = nums[i]Ϊ޸ nums[i] = nums[i - 1] Ļô nums[i] ôпܱ nums[i + 1] Ҫʹ nums[i] С -?????? nums[i] < nums[i - 2] ???????????? nums[i - 1] ??????????????????????????? nums[i] = nums[i - 1] ???? + nums[i] < nums[i - 2] £ֻ޸ nums[i - 1] Ϊǵݼֻͨ޸ nums[i] = nums[i - 1] С ```java public boolean checkPossibility(int[] nums) { @@ -290,7 +290,7 @@ public boolean checkPossibility(int[] nums) { } ``` -**?????????** +**жǷΪӴ** [Leetcode : 392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/) @@ -309,7 +309,7 @@ public boolean isSubsequence(String s, String t) { } ``` -**????????????????????????** +**ַָʹַͬһ** [Leetcode : 763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/) @@ -333,7 +333,7 @@ public List partitionLabels(String S) { } ``` -**??????????????????** +**ߺ** ```html Input: @@ -343,9 +343,9 @@ Output: [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]] ``` -???????????????? (h, k) ??????h ???????k ????????????? k ??????????????????????????? +һѧ (h, k) h ʾߣk ʾǰ k ѧ߱߻ߺһߡ -????????????k ???????????????????????? k ?????? +߽k ֵȻź˳еĵ k λС ```java public int[][] reconstructQueue(int[][] people) { @@ -374,17 +374,17 @@ public int[][] reconstructQueue(int[][] people) { ``` -## ???? +## ˫ָ -????????????????????????????????????????????? +˫ָҪڱ飬ָָͬԪأӶЭͬ -**?????????????????????????????????????? 0** +**һѾвҳʹǵĺΪ 0** -[Leetcode ??167. Two Sum II - Input array is sorted (Easy)](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/) +[Leetcode 167. Two Sum II - Input array is sorted (Easy)](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/) -?????????????????????????????????????????????????????????????????????????????????????? +ʹ˫ָ룬һָָԪؽСֵһָָԪؽϴֵָСԪصָͷβָϴԪصָβͷ -?????????????????? sum == target??????????????????? sum > target?????????????? sum ???????? sum < target?????????????? sum ?????? +ָָԪصĺ sum == targetôõҪĽ sum > targetƶϴԪأʹ sum СһЩ sum < targetƶСԪأʹ sum һЩ ```java public int[] twoSum(int[] numbers, int target) { @@ -399,21 +399,21 @@ public int[] twoSum(int[] numbers, int target) { } ``` -????????????????????? 0 ??????[???????????????? P351](#) +չԪغΪ 0 ⣺[Աָ P351](#) -**?????????????????????????A??** +**ۼӺΪֵ鳤** -[???????????????? P354/P355/P358](#) +[Աָ P354/P355/P358](#) -**??????????????????????** +**кжźľвԪ** -[???????????????? P347](#) +[Աָ P347](#) -**????????????????** +**תַеԪַ** [Leetcode : 345. Reverse Vowels of a String (Easy)](https://leetcode.com/problems/reverse-vowels-of-a-string/description/) -???????????????????????????????????????????????????????????? +ʹ˫ָ룬ָתԪַһָͷβһָβͷ ```java private HashSet vowels = new HashSet<>(Arrays.asList('a','e','i','o','u','A','E','I','O','U')); @@ -442,11 +442,11 @@ public String reverseVowels(String s) { } ``` -**?????????** +**ƽ** [Leetcode : 633. Sum of Square Numbers (Easy)](https://leetcode.com/problems/sum-of-square-numbers/description/) -????????????????????????????? 5 = 12 + 22?? +жһǷΪƽͣ 5 = 12 + 22 ```java public boolean judgeSquareSum(int c) { @@ -461,7 +461,7 @@ public boolean judgeSquareSum(int c) { } ``` -**??????????????????????????** +**ַɾһַ** [Leetcode : 680. Valid Palindrome II (Easy)](https://leetcode.com/problems/valid-palindrome-ii/description/) @@ -489,15 +489,15 @@ private boolean isPalindrome(String s, int l, int r){ } ``` -**?????????????** +**鲢** [Leetcode : 88. Merge Sorted Array (Easy)](https://leetcode.com/problems/merge-sorted-array/description/) -?????????????????? +ѹ鲢浽һϡ ```java public void merge(int[] nums1, int m, int[] nums2, int n) { - int i = m - 1, j = n - 1; // ???????????????????? nums1 ????????????????????? + int i = m - 1, j = n - 1; // Ҫβʼ nums1 Ϲ鲢õֵḲǻδй鲢Ƚϵֵ int idx = m + n - 1; while(i >= 0 || j >= 0){ if(i < 0) nums1[idx] = nums2[j--]; @@ -509,11 +509,11 @@ public void merge(int[] nums1, int m, int[] nums2, int n) { } ``` -**?????????????** +**жǷڻ** [Leetcode : 141. Linked List Cycle (Easy)](https://leetcode.com/problems/linked-list-cycle/description/) -????????????????????????????????????????????????????????????????????????????? +ʹ˫ָ룬һָÿƶһڵ㣬һָÿƶڵ㣬ڻôָһ ```java public boolean hasCycle(ListNode head) { @@ -529,7 +529,7 @@ public boolean hasCycle(ListNode head) { } ``` -**????????** +**** [Leetcode : 524. Longest Word in Dictionary through Deleting (Medium)](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/description/) @@ -559,40 +559,40 @@ public String findLongestWord(String s, List d) { } ``` -## ???? +## -### ??????? +### ѡ -?????????? **Kth Element** ?????????? O(n) ??????O(1) ??????????????? +һ **Kth Element** ⣬ O(n) ʱ临ӶȣO(1) ռ临Ӷ⹤ -???????????????????????????????????????????????????? O(n2)?? +һѡһҪȴ飬ʱ临ӶΪ O(n2) -### ?????? +### -????????????? **TopK Elements** ???????????????? K ?????????????? TopK Elements??????????????????? Kth Element ??????????????????????? Kth Element????????????????? TopK Elements ?????????? Kth Element ??????????????????????? Kth Element ???????? TopK Elements?????????????????????????????? Kth Element ?? TopK Elements ???????????????????? + **TopK Elements** ⣬ͨάһСΪ K ĶѣеԪؾ TopK ElementsȻҲ Kth Element ⣬ΪѵǸԪؾ Kth ElementѡҲ TopK Elements ⣬Ϊҵ Kth Element ֮ٱһ飬Сڵ Kth Element Ԫض TopK ElementsԿѡͶ򶼿 Kth Element TopK Elements ⣬ֻпҪ⡣ **Kth Element** [Leetocde : 215. Kth Largest Element in an Array (Medium)](https://leetcode.com/problems/kth-largest-element-in-an-array/description/) -???????[Solution explained](https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60294/Solution-explained) +ο[Solution explained](https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60294/Solution-explained) -- ????? O(nlgn)??????? O(1) ???????? -- ????? O(nlgk)??????? O(k) ?????????? -- ????? O(n)??????? O(1) ????QuickSelect +- ʱ临Ӷ O(nlgn)ռ临Ӷ O(1) ⷨ +- ʱ临Ӷ O(nlgk)ռ临Ӷ O(k) ⷨ +- ʱ临Ӷ O(n)ռ临Ӷ O(1) ⷨQuickSelect **ToK Elements** -[???????????????? P336](#) +[Աָ P336](#) -### ????? +### Ͱ -**?????????????? k ????** +**ҳƵ k ** [Leetcode : 347. Top K Frequent Elements (Medium)](https://leetcode.com/problems/top-k-frequent-elements/description/) -????? +Ͱ ```java public List topKFrequent(int[] nums, int k) { @@ -619,29 +619,29 @@ public List topKFrequent(int[] nums, int k) { } ``` -## ???? +## -??????????????????????????????????????????????????????????? +͹㷺ͼУǵӦԶԶֹˡ ### BFS ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4ff355cf-9a7f-4468-af43-e5b02038facc.jpg) -??????????????????????????????????????????? 0 ???????????? 6??2??1 ?? 5 ????????? +еһһؽбӽڵ 0 621 5 ĸ½ڵ㡣 -?????? 6 ??????????????? 4 ???? 2 ?????????????????????? 1 ?????????????????????? 5 ???????????? 3 ?? 4 ??????????????????????4 ?? 3 ?? + 6 ʼõڵ 4 2 ʼûһڵ㣻 1 ʼûһڵ㣻 5 ʼõ 3 4 ڵ㡣һܹõ½ڵ㣺4 3 -???????????????????????????????? +½ڵı -??????????????????????????????????????? di ????? i ????????????????????????????????????????????? i ?????????? j???? di<=dj?????????????????????????? **?????** ???????????????????????????????????????????????????????????????????????????????????????? +ԿÿһֱĽڵ㶼ڵ·ͬ di ʾ i ڵڵ·ȣƵһۣȱĽڵ i Ľڵ j di<=djۣ· **Ž** ⣺һαĿĽڵ㣬·Ϊ·֮ٱĿĽڵ㣬·Ͳ· -???????? BFS ???????????????? +ڳʵ BFS ʱҪ⣺ -- ???????????????????? -- ??????????????????????????????????????? +- У洢ÿһֱĽڵ +- ǣڱýڵ㣬Ӧýǣֹظ -**??????????????????????????????** +**дԭ㵽ض·** ```html [[1,1,0,1], @@ -685,17 +685,17 @@ private class Position { ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f7f7e3e5-7dd4-4173-9999-576b9e2ac0a2.png) -???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? 0 ?????????????????????? 6 ???????????? 6 ??????????????? 4??????????????????????????????????????????????????????? 0 ?????????????????? 0 ??????????????? 2?????????????? +һһÿһ½ڵ㣬Ҫöȴ洢Աһʱٱڱһ½ڵʱ½ڵбӽڵ 0 ʼõ½ڵ 6 ʱ½ڵ 6 бõ½ڵ 4˷ַʽ½ڵ㣬ֱû½ڵˣʱءصڵ 0 ǣԸڵ 0 бõ½ڵ 2Ȼϲ衣 -???????????????? DFS ????????????????????????????????????????DFS ????????????? **?????** ???? +һڵʹ DFS һͼбʱܹĽڵ㶼ǴӳʼڵɴģDFS **ɴ** ⡣ -???????? DFS ???????????????? +ڳʵ DFS ʱҪ⣺ -- ??????????????????????????????????????????????????????????????? -- ?????? BFS ????????????????????????????? +- ջջ浱ǰڵϢ½ڵ㷵ʱܹǰڵ㡣Ҳʹõݹջ +- ǣ BFS һͬҪѾýڵбǡ -**??????????????** +**ͨ** [Leetcode : 695. Max Area of Island (Easy)](https://leetcode.com/problems/max-area-of-island/description/) @@ -731,7 +731,7 @@ private int dfs(int[][] grid, int i, int j){ } ``` -**??????????** +**ͼͨ** [Leetcode : 547. Friend Circles (Medium)](https://leetcode.com/problems/friend-circles/description/) @@ -771,7 +771,7 @@ private void dfs(int[][] M, int i, boolean[] hasFind) { } ``` -**?????????????????** +**еͨ** [Leetcode : 200. Number of Islands (Medium)](https://leetcode.com/problems/number-of-islands/description/) @@ -812,7 +812,7 @@ private void dfs(char[][] grid, int i, int j) { } ``` -**????????????????????????** +**дӸҶӵ·** [Leetcode : 257. Binary Tree Paths (Easy)](https://leetcode.com/problems/binary-tree-paths/description/) @@ -847,7 +847,7 @@ private void dfs(TreeNode root, String prefix, List ret){ } ``` -**?????????** +**** [Leetcode : 130. Surrounded Regions (Medium)](https://leetcode.com/problems/surrounded-regions/description/) @@ -865,9 +865,9 @@ X X X X X O X X ``` -????????????? 'X' ?? 'O' ???? 'X'?? +Ŀʹñ 'X' 'O' תΪ 'X' -?????????????????????????????? +ѡ࣬ʣµľˡ ```java private int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; @@ -902,7 +902,7 @@ private void dfs(char[][] board, int r, int c) { } ``` -**????????????????????** +**ܵ** [Leetcode : 417. Pacific Atlantic Water Flow (Medium)](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) @@ -921,7 +921,7 @@ Return: [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix). ``` -?????????????????????????????????????????????????????????????????????????????????????????????????????? +Ŀߺϱ̫ƽұߺ±ǴڲִΣθߵĵطˮܹ͵ĵطˮܹ̫ƽʹеط ```java private int m, n; @@ -967,21 +967,21 @@ private void dfs(int r, int c, boolean[][] canReach) { } ``` -**N ???** +**N ʺ** [Leetcode : 51. N-Queens (Hard)](https://leetcode.com/problems/n-queens/description/) ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1f080e53-4758-406c-bb5f-dbedf89b63ce.jpg) -??????????? n\*n ???????? n ???????????????????????????????????????????? n ???? +Ŀ n\*n ľаڷ n ʺ󣬲ÿʺͬһУͬһУͬһԽϣҪе n ʺ⡣ -???????????????????????????????????????????????????????????????????????????????????????????????45 ????????????? 135 ??????????? +һһеذڷţȷһеǸʺӦðһʱҪȷijһǷϷֱΪб顢45 ȶԽ߱ 135 ȶԽ߱顣 -45 ????????????????? 2\*n - 1??????????????? (r,c) ???????????????? r + c?? +45 ȶԽ߱άΪ 2\*n - 1ͨͼȷ (r,c) λڵ±Ϊ r + c ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/85583359-1b45-45f2-9811-4f7bb9a64db7.jpg) -135 ??????????????????? 2\*n - 1??(r,c) ???????????????? n - 1 - (r - c)?? +135 ȶԽ߱άҲ 2\*n - 1(r,c) λڵ±Ϊ n - 1 - (r - c) ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9e80f75a-b12b-4344-80c8-1f9ccc2d5246.jpg) @@ -1032,11 +1032,11 @@ private void backstracking(int row) { ### Backtracking -?????? DFS ?????????????????????????????????????? **???????** ?????????? { 'a','b','c' } ????????????????????????????????????????? + DFS һ֣ڱͼĽڵϣ **** ⣬ { 'a','b','c' } ַַеõַ -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ڳʵʱҪעԪؽбǵ⡣ʹõݹʵֵĻݣڷһԪؽµĵݹãʱҪԪرΪѾʣڼݹʱظʸԪأڵݹ鷵ʱҪԪرΪδʣΪֻҪ֤һݹвͬʱһԪأڲͬĵݹǿԷѾʹDzڵǰݹеԪء -**??????????** +**ּ** [Leetcode : 17. Letter Combinations of a Phone Number (Medium)](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/) @@ -1070,7 +1070,7 @@ private void combination(String prefix, String digits, int offset, List } ``` -**???????????????** +**ھѰַ** [Leetcode : 79. Word Search (Medium)](https://leetcode.com/problems/word-search/description/) @@ -1125,7 +1125,7 @@ private boolean dfs(char[][] board, String word, int start, int r, int c) { } ``` -**IP ???????** +**IP ַ** [Leetcode : 93. Restore IP Addresses(Medium)](https://leetcode.com/problems/restore-ip-addresses/description/) @@ -1160,11 +1160,11 @@ private void doRestore(int k, String path, String s) { } ``` -**????** +**** [Leetcode : 46. Permutations (Medium)](https://leetcode.com/problems/permutations/description/) -??????????????????????????? +ĿҳһС ```java public List> permute(int[] nums) { @@ -1192,7 +1192,7 @@ private void backtracking(List permuteList, boolean[] visited, int[] nu } ``` -**?????????????** +**ظ** [Leetcode : 47. Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/) @@ -1201,7 +1201,7 @@ private void backtracking(List permuteList, boolean[] visited, int[] nu [[1,1,2], [1,2,1], [2,1,1]] ``` -?????????? Permutations ?????????????????????????????????????????????????????????????????????????????????????????????? +ʵϣ Permutations ͬҪȻһԪʱжԪǷǰһԪأڣǰһԪػδʣôԪء ```java public List> permuteUnique(int[] nums) { @@ -1231,11 +1231,11 @@ private void backtracking(List permuteList, boolean[] visited, int[] nu } ``` -**???** +**** [Leetcode : 77. Combinations (Medium)](https://leetcode.com/problems/combinations/description/) -?????????????? 1 \~ n ????? k ????????????????? +Ŀҳ 1 \~ n ȡ k Ԫصпܵϡ ```java public List> combine(int n, int k) { @@ -1247,20 +1247,20 @@ public List> combine(int n, int k) { private void backtracking(int start, int n, int k, List combineList, List> ret){ if(k == 0){ - ret.add(new ArrayList(combineList)); // ??????????????? List + ret.add(new ArrayList(combineList)); // Ҫ¹һ List return; } - for(int i = start; i <= n - k + 1; i++){ // ??? + for(int i = start; i <= n - k + 1; i++){ // ֦ - combineList.add(i); // ?? i ????????? + combineList.add(i); // i Ϊѷ backtracking(i + 1, n, k - 1, combineList, ret); - combineList.remove(combineList.size() - 1); // ?? i ???????? + combineList.remove(combineList.size() - 1); // i Ϊδ } } ``` -**??????** +**** [Leetcode : 39. Combination Sum (Medium)](https://leetcode.com/problems/combination-sum/description/) @@ -1295,7 +1295,7 @@ A solution set is: } ``` -**???????????????** +**ظ** [Leetcode : 40. Combination Sum II (Medium)](https://leetcode.com/problems/combination-sum-ii/description/) @@ -1339,11 +1339,11 @@ private void doCombination(int[] candidates, int target, int start, List> ret; @@ -1366,13 +1366,13 @@ private void backtracking(int startIdx, int size, int[] nums) { for (int i = startIdx; i < nums.length; i++) { subsetList.add(nums[i]); - backtracking(i + 1, size, nums); // startIdx ????????????? subset ?????????????? + backtracking(i + 1, size, nums); // startIdx ΪһԪأʹ subset еԪض subsetList.remove(subsetList.size() - 1); } } ``` -**????????????** +**ظӼ** [Leetcode : 90. Subsets II (Medium)](https://leetcode.com/problems/subsets-ii/description/) @@ -1423,7 +1423,7 @@ private void backtracking(int startIdx, int size, int[] nums) { } ``` -**????????????????????????** +**ַָʹÿֶǻ** [Leetcode : 131. Palindrome Partitioning (Medium)](https://leetcode.com/problems/palindrome-partitioning/description/) @@ -1458,7 +1458,7 @@ private boolean isPalindrome(String s, int begin, int end) { } ``` -**????** +**** [Leetcode : 37. Sudoku Solver (Hard)](https://leetcode.com/problems/sudoku-solver/description/) @@ -1514,9 +1514,9 @@ private int cubeNum(int i, int j) { } ``` -## ???? +## -**????????????** +**ʽ** [Leetcode : 241. Different Ways to Add Parentheses (Medium)](https://leetcode.com/problems/different-ways-to-add-parentheses/description/) @@ -1554,21 +1554,21 @@ public List diffWaysToCompute(String input) { } ``` -## ??? +## ݹ -??????????????????????? +ԭʼֽɽС⡣ -## ???? +## ̬滮 -?????????????????????????????????????????????????????????? +ԭɶ⣬ʱ򱣴Ľ⣬ʹֻһΡ -### ??????? +### ָ -**??????????????** +**ָ˻** [Leetcode : 343. Integer Break (Medim)](https://leetcode.com/problems/integer-break/description/) -?????????For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). +ĿFor example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). ```java public int integerBreak(int n) { @@ -1583,15 +1583,15 @@ public int integerBreak(int n) { } ``` -**????????????????** +**ƽָ** [Leetcode : 279. Perfect Squares(Medium)](https://leetcode.com/problems/perfect-squares/description/) -?????????For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. +ĿFor example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. ```java public int numSquares(int n) { - List squares = new ArrayList<>(); // ??? n ??????? + List squares = new ArrayList<>(); // 洢С n ƽ int diff = 3; while(square <= n) { squares.add(square); @@ -1611,11 +1611,11 @@ public int numSquares(int n) { } ``` -**???????????????????** +**ָĸַ** [Leetcode : 91. Decode Ways (Medium)](https://leetcode.com/problems/decode-ways/description/) -?????????Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12). +ĿGiven encoded message "12", it could be decoded as "AB" (1 2) or "L" (12). ```java public int numDecodings(String s) { @@ -1635,13 +1635,13 @@ public int numDecodings(String s) { } ``` -### ?????? +### · -**???????????** +**·** [Leetcode : 62. Unique Paths (Medium)](https://leetcode.com/problems/unique-paths/description/) -???????????????????????????????????????????????????????? +ĿͳƴӾϽǵ½ǵ·ÿֻƶ ```java public int uniquePaths(int m, int n) { @@ -1656,11 +1656,11 @@ public int uniquePaths(int m, int n) { } ``` -**???????????** +**С·** [Leetcode : 64. Minimum Path Sum (Medium)](https://leetcode.com/problems/minimum-path-sum/description/) -??????????????????????????????????????????????????????? +ĿӾϽǵ½ǵС·ͣÿֻƶ ```java public int minPathSum(int[][] grid) { @@ -1678,29 +1678,29 @@ public int minPathSum(int[][] grid) { } ``` -### ?????????? +### 쳲 -????????????????? 4 ?????????????????? N ????????????????????????????????? O(lgN)?? +Աָϵ 4 µķ쳲 N תΪ˷㣬Ӷʱ临ӶСΪ O(lgN) -**?????** +**¥** [Leetcode : 70. Climbing Stairs (Easy)](https://leetcode.com/problems/climbing-stairs/description/) -??????????? N ??????????????????????????????????????????? +Ŀ N ¥ݣÿοһ׻ףж¥ݵķ -??????????? dp ???????????????????????????????? 1 ???????dp[i] ???????? i ???????????????? i ?????????? i-1 ?? i-2 ????????????????????? i ????????????????? i-1 ??? i-2 ??????????????? +һ dp 洢¥ݵķΪ˷ۣ± 1 ʼdp[i] ʾߵ i ¥ݵķĿ i ¥ݿԴӵ i-1 i-2 ¥һߵ i ¥ݵķΪߵ i-1 ͵ i-2 ¥ݵķ֮͡
dp[i] = dp[i-1] + dp[i-2]
-dp[N] ??????? +dp[N] Ϊ -????? dp[i] ??? dp[i - 1] ?? dp[i - 2] ??????????????????????? dp[i - 1] ?? dp[i - 2] ????????????? O(n) ????????? O(1) ?????? +ǵ dp[i] ֻ dp[i - 1] dp[i - 2] йأ˿ֻ洢 dp[i - 1] dp[i - 2] ɣʹԭ O(n) ռ临ӶŻΪ O(1) Ӷȡ ```java public int climbStairs(int n) { if(n == 1) return 1; if(n == 2) return 2; - // ???????????????? + // ǰһ¥ݡһ¥ int pre1 = 2, pre2 = 1; for(int i = 2; i < n; i++){ int cur = pre1 + pre2; @@ -1711,27 +1711,27 @@ public int climbStairs(int n) { } ``` -**??????** +**ĸţ** -[????????????????-P181](#) +[Աָ-P181](#) -?????????????????????????????? 1 ??????????????????????????? 1 ????????????????????????????? 3 ??????????????????????????? N???? N ???????????? +Ŀũгĸţÿ궼 1 ͷСĸţԶһ 1 ֻСĸţӵڶ꿪ʼĸţʼСĸţÿֻСĸţ 3 ֿ֮Сĸţ N N ţ -?? i ???????????????? + i ţΪ
dp[i] = dp[i-1] + dp[i-3]
-**???????** +**ǿ** [Leetcode : 198. House Robber (Easy)](https://leetcode.com/problems/house-robber/description/) -????????????????????????????????????????????????????? +ĿһסDzڽס -???? dp ????????????????????????? dp[i] ????????? i ??????????????????????????????????????????????????? i ??????????????? i - 2 ?? i - 3 ??????????? + dp 洢 dp[i] ʾ i סʱڲڽס˵ i סôֻ i - 2 i - 3 ס ![](http://latex.codecogs.com/gif.latex?\\\\dp[i]=max(dp[i-2],dp[i-3])+nums[i]) -O(n) ????????????? +O(n) ռ临Ӷʵַ ```java public int rob(int[] nums) { @@ -1750,7 +1750,7 @@ public int rob(int[] nums) { } ``` -O(1) ????????????? +O(1) ռ临Ӷʵַ ```java public int rob(int[] nums) { @@ -1769,7 +1769,7 @@ public int rob(int[] nums) { } ``` -**??????????????** +**ǿڻν** [Leetcode : 213. House Robber II (Medium)](https://leetcode.com/problems/house-robber-ii/description/) @@ -1797,39 +1797,39 @@ private int rob(int[] nums, int s, int e) { ``` -**???????** +**ż** -??????????? N ?? ?? ?? ?????????????????????????????? +Ŀ N ŷ⣬DZңװŵķʽ -??????????? dp ????????????dp[i] ???? i ???????????????????????? i ????????? j ???????????? j ????????? k ???????????? i ?? k ?????????????????? +һ dp 洢ʽdp[i] ʾǰ i źŷĴʽ i װ j ŷ棬 j װ k ŷ档 i k Ƿȣ -?? i==k?????? i ?? k ?????????????????????????????????? i-2 ?????? dp[i-2] ????????????????? j ?? i-1 ???????????? (i-1)\*dp[i-2] ???????????? + i==k i k źǵźŷȷλã i-2 dp[i-2] ִװŵķʽ j i-1 ȡֵ˹ (i-1)\*dp[i-2] ִװŷʽ -?? i != k?????? i ?? j ?????? i ?????????????????????? i-1 ?????? dp[i-1] ???????????????? j ?? i-1 ???????????? (n-1)\*dp[i-1] ???????????? + i != k i j ź󣬵 i źŷȷλã i-1 dp[i-1] ִװŷʽ j i-1 ȡֵ˹ (n-1)\*dp[i-1] ִװŷʽ -??????????????????????????????? +װʽΪ
dp[i] = (i-1) \* dp[i-2] + (i-1) \* dp[i-1]
-dp[N] ??????? +dp[N] Ϊ -????????????????dp[i] ??? dp[i-1] ?? dp[i-2] ????????????????????????? dp[i-1] ?? dp[i-2]?? +¥һdp[i] ֻ dp[i-1] dp[i-2] йأҲֻ洢 dp[i-1] dp[i-2] -### ???????????? +###  -?????????? {S1, S2,...,Sn} ????????????????????? {Si1, Si2,..., Sim}?????? i1??i2 ... im ???????????????????????????????????????????????????????????**??????**?? +֪һ {S1, S2,...,Sn} ȡµ {Si1, Si2,..., Sim} i1i2 ... im ֵиȻԭеȺ˳򣬳Ϊԭеһ**** -????????????????? ix > iy ???Six > Siy???????????????????**??????????**?? +У± ix > iy ʱSix > SiyΪԭеһ**** -??????????? dp ?????????????????dp[n] ????? Sn ???????????????????????????????????????? {Si1, Si2,...,Sim}????? im < n ???? Sim < Sn ????? {Si1, Si2,..., Sim, Sn} ??????????????????????????????? 1?????????????????????????????????????????????????????????????????????????????? Sn ????????? Sn ???????????????????? dp[n] = max{ dp[i]+1 | Si < Sn && i < n} ?? +һ dp 洢еijȣdp[n] ʾ Sn βегȡһ {Si1, Si2,...,Sim} im < n Sim < Sn ʱ {Si1, Si2,..., Sim, Sn} ΪһУеij 1ĵУǸоҪҵģڳĵϼ Sn ͹ Sn ΪβС dp[n] = max{ dp[i]+1 | Si < Sn && i < n} -??????? dp[n] ???????????????????????????????????? {Sn} ????????????????????????????????????????? dp[n] ??? 1?????? +Ϊ dp[n] ʱ޷ҵһĵУʱ {Sn} ͹˵УҪǰⷽ޸ģ dp[n] СΪ 1 ![](http://latex.codecogs.com/gif.latex?\\\\dp[n]=max\{1,dp[i]+1|S_iN ???????? dp[N] ??????????????????????????????? dp ?????????????????????????? max{ dp[i] | 1 <= i <= N} ??????? +һΪ N Увһ SN Ϊβ dp[N] ееijȣҪ dp ҳֵҪĽ max{ dp[i] | 1 <= i <= N} Ϊ -**????????????** +**** [Leetcode : 300. Longest Increasing Subsequence (Medium)](https://leetcode.com/problems/longest-increasing-subsequence/description/) @@ -1852,7 +1852,7 @@ public int lengthOfLIS(int[] nums) { } ``` -????????????? O(n2) ??????????????????????????? O(nlogn)????????? tails ??????? tails[i] ?????? i + 1 ??????????????????????????????????? [4,5,6,3]???? +Ͻⷨʱ临ӶΪ O(n2) ʹöֲʹʱ临ӶȽΪ O(nlogn)һ tails 飬 tails[i] 洢Ϊ i + 1 еһԪأ [4,5,6,3] ```html len = 1 : [4], [5], [6], [3] => tails[0] = 3 @@ -1860,9 +1860,9 @@ len = 2 : [4, 5], [5, 6] => tails[1] = 5 len = 3 : [4, 5, 6] => tails[2] = 6 ``` -?????????? x??????????? tails ?????????????????????? tails ?????? tails[i-1] < x <= tails[i]????????? tails[i] = x ?? +һԪ x tails еֵôӵ tails 棻 tails[i-1] < x <= tails[i]ô tails[i] = x -??????? tails ???g?????????????? Si ?? tails ??????????????????????? +Կ tails 鱣ڲ Si λ tails λʱͿʹöֲҡ ```java public int lengthOfLIS(int[] nums) { @@ -1888,13 +1888,13 @@ private int binarySearch(int[] nums, int sIdx, int eIdx, int key){ } ``` -**???????????** +**ڶ** [Leetcode : 376. Wiggle Subsequence (Medium)](https://leetcode.com/problems/wiggle-subsequence/description/) -?????? O(n) ???????? +Ҫʹ O(n) ʱ临Ӷ⡣ -????????? up ?? down?? +ʹ״̬ up down ```java public int wiggleMaxLength(int[] nums) { @@ -1909,17 +1909,17 @@ public int wiggleMaxLength(int[] nums) { } ``` -### ??????????? +### ϵ -?????????????? S1 ?? S2?????????????????????? + S1 S2ҳĹС -?????????????? dp ????????????????????????? dp[i][j] ??? S1 ??? i ??????? S2 ??? j ????????????????????????? S1i ?? S2j ???????????????????? +һά dp 洢еijȣ dp[i][j] ʾ S1 ǰ i ַ S2 ǰ j ַеijȡ S1i S2j ֵǷȣΪ -?? ?? S1i==S2j ???????????? S1 ??? i-1 ??????? S2 ??? j-1 ??????????????????????????? S1i ????????????????????? 1 ???? dp[i][j] = dp[i-1][j-1] + 1?? + S1i==S2j ʱô S1 ǰ i-1 ַ S2 ǰ j-1 ַеĻټ S1i ֵгȼ 1 dp[i][j] = dp[i-1][j-1] + 1 -?? ?? S1i != S2j ??????????????????? S1 ??? i-1 ??????? S2 ??? j ??????????????????? S1 ??? i ??????? S2 ??? j-1 ?????????????????????????????? dp[i][j] = max{ dp[i-1][j], dp[i][j-1] }?? + S1i != S2j ʱʱΪ S1 ǰ i-1 ַ S2 ǰ j ַУ S1 ǰ i ַ S2 ǰ j-1 ַУǵߣ dp[i][j] = max{ dp[i-1][j], dp[i][j-1] } -??????????????????????????? +ϣϵе״̬תƷΪ ![](http://latex.codecogs.com/gif.latex?\\\\ dp[i][j]=\left\{ @@ -1929,13 +1929,13 @@ max(dp[i-1][j],dp[i][j-1])&&{S1_i<>S2_j} \end{array}\right. ) -???????? N ?????? S1 ?? ????? M ?????? S2??dp[N][M] ???????? S1 ?????? S2 ????????????????? +ڳΪ N S1 Ϊ M S2dp[N][M] S1 S2 гȡ -???????????????????????????????????????? +ȣ²ͬ㣺 -?? ????????????????????????????????? -?? ????????????????dp[i] ????? Si ??????????????????????????????? Si ??????????????????dp[i][j] ??? S1 ??? i ??????? S2 ??? j ??????????????????????????????? S1i ?? S2j ?? -?? ???? 2 ???????????????????????????? dp[N][M] ????????????????????????? dp[N] ?????????????? SN ???????????????????????????????????????????????????? dp ????????????? + ԵУǵС + Уdp[i] ʾ Si Ϊβгȣб Si Уdp[i][j] ʾ S1 ǰ i ַ S2 ǰ j ַгȣһ S1i S2j + 2 սʱ dp[N][M] ս⣬ dp[N] ս⣬Ϊ SN ΪβвһУҪһ dp ҵߡ ```java public int lengthOfLCS(int[] nums1, int[] nums2) { @@ -1951,18 +1951,18 @@ public int lengthOfLCS(int[] nums1, int[] nums2) { } ``` -### 0-1 ???? +### 0-1 -?????????? N ????????????????????????????????????????????????? w ???? v?? +һΪ N ıҪװƷļֵЩƷԣ w ͼֵ v -?????????????? dp ???????????? dp[i][j] ???????????? j ????????? i ???????????????????? i ????????? w?????? v??????? i ??????????????????????????????????? +һά dp 洢ֵ dp[i][j] ʾ j £ǰ i Ʒܴﵽֵ i ƷΪ wֵΪ vݵ i ƷǷӵУԷۣ -?? ?? i ??????????????????????????? j ??? i ??????????????????????????? j ??? i-1 ??????????????dp[i][j] = dp[i-1][j]?? -?? ?? i ???????????????dp[i][j] = dp[i-1][j-w] + v?? + i Ʒûӵ j ǰ i Ʒֵ j ǰ i-1 Ʒֵdp[i][j] = dp[i-1][j] + i ƷӵУdp[i][j] = dp[i-1][j-w] + v -?? i ??????????????????????????????????????????? + i ƷҲԲӣȡֵ -?????0-1 ????????????????? +ϣ0-1 ״̬תƷΪ ![](http://latex.codecogs.com/gif.latex?\\\\dp[i][j]=max(dp[i-1][j],dp[i-1][j-w]+v)) @@ -1984,18 +1984,18 @@ public int knapsack(int W, int N, int[] weights, int[] values) { } ``` -**??????** +**ռŻ** -?????????????? 0-1 ??????????????????????????????? i ?????????????? i-1 ??????????????????? dp ?????????????? dp[j] ??????? dp[i-1][j] ??????? dp[i][j]??????? +ڳʵʱԶ 0-1 Ż۲״̬תƷ֪̿ǰ i Ʒ״̬ǰ i-1 Ʒ״̬йأ˿Խ dp Ϊһά飬 dp[j] ȿԱʾ dp[i-1][j] ҲԱʾ dp[i][j]ʱ ![](http://latex.codecogs.com/gif.latex?\\\\dp[j]=max(dp[j],dp[j-w]+v)) -??? dp[j-w] ??? dp[i-1][j-w]???????????? dp[i][j-w] ????? dp[i-1][j-w] ????????????????? dp[i][j] ????? dp[i][j-w]???????????????????????????? +Ϊ dp[j-w] ʾ dp[i-1][j-w]˲ dp[i][j-w] ֹ dp[i-1][j-w] ǡҲ˵Ҫȼ dp[i][j] ټ dp[i][j-w]ڳʵʱҪѭ⡣ -**????????????????** +**޷ʹ̰㷨Ľ** -0-1 ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? 5 ?????????????????? 0 ???????? 1?????????????? 16????????? 2 ?????????????????? 1 ????? 2?????? 22. +0-1 ޷ʹ̰㷨⣬Ҳ˵ܰԼ۱ߵƷﵽţΪַʽɱռ˷ѣӶ޷ﵽšƷһΪ 5 ıƷ 0 Ʒ 1ôֻܴŵļֵΪ 16˷˴СΪ 2 Ŀռ䡣ŵķʽǴƷ 1 Ʒ 2ֵΪ 22. | id | w | v | v/w | | --- | --- | --- | --- | @@ -2003,23 +2003,23 @@ public int knapsack(int W, int N, int[] weights, int[] values) { | 1 | 2 | 10 | 5 | | 2 | 3 | 12 | 4 | -**????** +**** -??????????????????????????????? 0-1 ?????????????????????????? 1/2/4... ????????????????????????????????????????????? +ȫƷ޸תΪ 0-1 ÿƷͼֵΪ 1/2/4... ǶһƷȻһƷֻһΡ -?????????????????????????????????? 0-1 ?????? +رƷƣͬתΪ 0-1 -????????????????????????????????????????????????????? +άñƷͬʱơ -??????????????????????????? +Ʒ֮໥Լ -**?????????????????????** +**Ϊȵ** [Leetcode : 416. Partition Equal Subset Sum (Medium)](https://leetcode.com/problems/partition-equal-subset-sum/description/) -????????????????? sum/2 ?? 0-1 ???????????????????????????????????????????????????? +ԿһСΪ sum/2 0-1 ⣬Ҳвͬĵطûмֵԣұ뱻 -??????????????????? +ʵʹ˿ռŻ ```java public boolean canPartition(int[] nums) { @@ -2046,7 +2046,7 @@ public boolean canPartition(int[] nums) { } ``` -**???????????????** +**ַбָ** [Leetcode : 139. Word Break (Medium)](https://leetcode.com/problems/word-break/description/) @@ -2073,7 +2073,7 @@ public boolean wordBreak(String s, List wordDict) { } ``` -**?????????????????????????????????** +**ıһʹǵĺΪһ** [Leetcode : 494. Target Sum (Medium)](https://leetcode.com/problems/target-sum/description/) @@ -2091,7 +2091,7 @@ Explanation: There are 5 ways to assign symbols to make the sum of nums be target 3. ``` -????????????? subset sum ?????????? 0-1 ????????????????????????????????????P ?? N?????? P ????????N ?????????????????? +תΪ subset sum ⣬Ӷʹ 0-1 ķ⡣Խ֣P N P ʹţN ʹøţƵ ```html sum(P) - sum(N) = target @@ -2099,7 +2099,7 @@ sum(P) + sum(N) + sum(P) - sum(N) = target + sum(P) + sum(N) 2 * sum(P) = target + sum(nums) ``` -????????????????????????????????????? (target + sum(nums))/2???????????? +ֻҪҵһӼǶȡţҺ͵ (target + sum(nums))/2֤ڽ⡣ ```java public int findTargetSumWays(int[] nums, int S) { @@ -2127,7 +2127,7 @@ private int subsetSum(int[] nums, int targetSum) { } ``` -**01????????????????** +**01ַַ** [Leetcode : 474. Ones and Zeroes (Medium)](https://leetcode.com/problems/ones-and-zeroes/description/) @@ -2135,10 +2135,10 @@ private int subsetSum(int[] nums, int targetSum) { Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3 Output: 4 -Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are ??10,??0001??,??1??,??0?? +Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are 10,0001,1,0 ``` -??????????????? 0-1 ??????????????????????0 ???????? 1 ???????? +һάõ 0-1 ⣬С0 1 ```java public int findMaxForm(String[] strs, int m, int n) { @@ -2164,13 +2164,13 @@ public int findMaxForm(String[] strs, int m, int n) { } ``` -**?????** +**Ǯ** [Leetcode : 322. Coin Change (Medium)](https://leetcode.com/problems/coin-change/description/) -?????????????????????????????????????????????????????????????????????????????? +ĿһЩӲңҪЩӲɸǮʹӲ١Ӳҿظʹá -?????????????????????????????? 0-1?????????????????????????????????? 0 ???????????????????? +һȫ⣬ȫ 0-1ʵΨһIJͬǣڶѭǴ 0 ʼģǴβʼ ```java public int coinChange(int[] coins, int amount) { @@ -2188,7 +2188,7 @@ public int coinChange(int[] coins, int amount) { } ``` -**??????** +**ܺ** [Leetcode : 377. Combination Sum IV (Medium)](https://leetcode.com/problems/combination-sum-iv/description/) @@ -2225,7 +2225,7 @@ public int combinationSum4(int[] nums, int target) { } ``` -**????????????????** +**ֻܽεĹƱ** [Leetcode : 123. Best Time to Buy and Sell Stock III (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/) @@ -2243,7 +2243,7 @@ public int maxProfit(int[] prices) { } ``` -**?????? k ????????** +**ֻܽ k εĹƱ** [Leetcode : 188. Best Time to Buy and Sell Stock IV (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/) @@ -2274,13 +2274,13 @@ public int maxProfit(int k, int[] prices) { } ``` -### ???????? +### -**?????????** +**** [Leetcode : 303. Range Sum Query - Immutable (Easy)](https://leetcode.com/problems/range-sum-query-immutable/description/) -?????? i \~ j ???????????? sum[j] - sum[i-1]?????? sum[i] ? 0 \~ j ???? + i \~ j ĺͣתΪ sum[j] - sum[i-1] sum[i] Ϊ 0 \~ j ĺ͡ ```java class NumArray { @@ -2299,11 +2299,11 @@ class NumArray { } ``` -**???????????** +**ĺ** [Leetcode : 53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/) -?? sum[i] ??? num[i] ??????????????????????? sum[i-1] ??? sum[i] ???????? sum[i-1] ?? 0??????? num[i] ?????????????????????????????????????????????????? num[i] ???? + sum[i] Ϊ num[i] Ϊβĺͣ sum[i-1] õ sum[i] ֵ sum[i-1] С 0ô num[i] Ϊβ鲻ܰǰݣΪǰIJ֣ôһ num[i] С ```java public int maxSubArray(int[] nums) { @@ -2319,7 +2319,7 @@ public int maxSubArray(int[] nums) { } ``` -**????????????????????** +**еȲĸ** [Leetcode : 413. Arithmetic Slices (Medium)](https://leetcode.com/problems/arithmetic-slices/description/) @@ -2329,7 +2329,7 @@ A = [1, 2, 3, 4] return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself. ``` -???? (1,2,3,4)????????????????????????????????? (1,2,3,4,5)??????????????????????? (1,2,3,4) ?????????????????? (1,2,3,4,5)????? dp[i] = dp[i - 1] + 1?? + (1,2,3,4)ɵķʽ (1,2,3,4,5)ɵķʽ (1,2,3,4) ⻹һ֣ (1,2,3,4,5) dp[i] = dp[i - 1] + 1 ```java public int numberOfArithmeticSlices(int[] A) { @@ -2348,13 +2348,13 @@ public int numberOfArithmeticSlices(int[] A) { } ``` -### ??????? +### ַ༭ -**?????????????????????????** +**ɾַַʹ** [Leetcode : 583. Delete Operation for Two Strings (Medium)](https://leetcode.com/problems/delete-operation-for-two-strings/description/) -????????????????????????????????????? +תΪַ⡣ ```java public int minDistance(String word1, String word2) { @@ -2372,21 +2372,21 @@ public int minDistance(String word1, String word2) { ``` -**????????????????????????** // TODO +**޸һַΪһַ** // TODO [Leetcode : 72. Edit Distance (Hard)](https://leetcode.com/problems/edit-distance/description/) -**??????????????????????????**// TODO +**޸һַʹΪַ**// TODO -[????????????????-?????](#) +[Աָ-ַ](#) -### ???????? +### -**???????????????** +**ҪȴڵĹƱ** [Leetcode : 309. Best Time to Buy and Sell Stock with Cooldown(Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/) -??????????????????????????????? +Ŀ֮Ҫһȴʱ䡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ac9b31ec-cef1-4880-a875-fc4571ca10e1.png) @@ -2417,11 +2417,11 @@ public int maxProfit(int[] prices) { ``` -**???? 0 \~ n ???????????????? 1 ?????** +**ͳƴ 0 \~ n ÿĶƱʾ 1 ĸ** [Leetcode : 338. Counting Bits (Medium)](https://leetcode.com/problems/counting-bits/description/) -???????? 6(110)????????????????? 2(10) ????????? 1 ????? dp[i] = dp[i&(i-1)] + 1; + 6(110)Կ 2(10) ǰһ 1 dp[i] = dp[i&(i-1)] + 1; ```java public int[] countBits(int num) { @@ -2433,11 +2433,11 @@ public int maxProfit(int[] prices) { } ``` -**?????????????????????** +**һܹɵ** [Leetcode : 646. Maximum Length of Pair Chain (Medium)](https://leetcode.com/problems/maximum-length-of-pair-chain/description/) -???? (a, b) ?? (c, d) ????? b < c????????????????????? + (a, b) (c, d) b < cǿԹһ ```java public int findLongestChain(int[][] pairs) { @@ -2464,13 +2464,13 @@ public int findLongestChain(int[][] pairs) { } ``` -**???????????????????** +**۳Ʊ** [Leetcode : 121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) -??????????? +ֻһνס -??????????????????????????????????????????????????????????????????????? +ֻҪ¼ǰС۸񣬽С۸Ϊ۸Ȼ󽫵ǰļ۸Ϊ۳۸񣬲鿴۸Ƿǵǰ۸ ```java public int maxProfit(int[] prices) { @@ -2486,7 +2486,7 @@ public int maxProfit(int[] prices) { } ``` -**??????????** +**ճַ** [Leetcode : 650. 2 Keys Keyboard (Medium)](https://leetcode.com/problems/2-keys-keyboard/description/) @@ -2516,30 +2516,30 @@ public int minSteps(int n) { } ``` -## ??? +## ѧ -### ???? +### -**???????** +**ֽ** -????????????????????????????? 84 = 22 \* 31 \* 50 \* 71 \* 110 \* 130 \* 170 \* ?? +ÿһԷֽij˻ 84 = 22 \* 31 \* 50 \* 71 \* 110 \* 130 \* 170 \* -**????** +**** -?? x = 2m0 \* 3m1 \* 5m2 \* 7m3 \* 11m4 \* ?? -?? y = 2n0 \* 3n1 \* 5n2 \* 7n3 \* 11n4 \* ?? + x = 2m0 \* 3m1 \* 5m2 \* 7m3 \* 11m4 \* + y = 2n0 \* 3n1 \* 5n2 \* 7n3 \* 11n4 \* -??? x ???? y??y mod x == 0????????????? i??mi <= ni?? + x yy mod x == 0 imi <= ni -x ?? y ?? **??????** ???gcd(x,y) = 2min(m0,n0) \* 3min(m1,n1) \* 5min(m2,n2) \* ... +x y **Լ** Ϊgcd(x,y) = 2min(m0,n0) \* 3min(m1,n1) \* 5min(m2,n2) \* ... -x ?? y ?? **????????** ???lcm(x,y) = 2max(m0,n0) \* 3max(m1,n1) \* 5max(m2,n2) \* ... +x y **С** Ϊlcm(x,y) = 2max(m0,n0) \* 3max(m1,n1) \* 5max(m2,n2) \* ... -**????????????** +**** [Leetcode : 204. Count Primes (Easy)](https://leetcode.com/problems/count-primes/description/) -???????????????????????????????????????????????????????? +˹ɸÿҵһʱܱų ```java public int countPrimes(int n) { @@ -2548,7 +2548,7 @@ public int countPrimes(int n) { for(int i = 2; i < n; i++){ if(notPrimes[i]) continue; cnt++; - // ?? i * i ??????????? k < i????? k * i ?????????????????? + // i * i ʼΪ k < iô k * i ֮ǰѾȥ for(long j = (long) i * i; j < n; j += i){ notPrimes[(int) j] = true; } @@ -2557,7 +2557,7 @@ public int countPrimes(int n) { } ``` -### ?????? +### Լ ```java int gcd(int a, int b) { @@ -2566,7 +2566,7 @@ int gcd(int a, int b) { } ``` -??????????????????????????? +󹫱Ϊij˻Լ ```java int lcm(int a, int b){ @@ -2574,22 +2574,22 @@ int lcm(int a, int b){ } ``` -???????????????????????? a % b ??????????????????????????? [ ????????2.7]() ????????????????????????I???? +Լ⣬ΪҪ a % b DZȽϺʱģʹ [ ֮2.7]() ķüλ滻 -???? a ?? b ???????? f(a, b)???? + a b Լ f(a, b)У -1\. ??? a ?? b ????????f(a, b) = 2\*f(a/2, b/2); -2\. ??? a ????? b ????????f(a, b) = f(a/2, b); -3\. ??? b ????? a ????????f(a, b) = f(a, b/2); -4\. ??? a ?? b ?????????f(a, b) = f(a, a-b); +1\. a b Ϊżf(a, b) = 2\*f(a/2, b/2); +2\. a ż b f(a, b) = f(a/2, b); +3\. b ż a f(a, b) = f(a, b/2); +4\. a b Ϊf(a, b) = f(a, a-b); -?? 2 ??? 2 ?????????????????? + 2 ͳ 2 תΪλ -### ??????? +### ת -Java ?? static String toString(int num, int radix) ???????????????? redix ??????????????? +Java static String toString(int num, int radix) ԽһװΪ redix Ʊʾַ -**7 ????** +**7 ** [Leetcode : 504. Base 7 (Easy)](https://leetcode.com/problems/base-7/description/) @@ -2605,7 +2605,7 @@ public String convertToBase7(int num) { } ``` -**16 ????** +**16 ** [Leetcode : 405. Convert a Number to Hexadecimal (Easy)](https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/) @@ -2622,15 +2622,15 @@ public String toHex(int num) { } ``` -### ??? +### ׳ -**???????????? 0** +**ͳƽ׳βжٸ 0** [Leetcode : 172. Factorial Trailing Zeroes (Easy)](https://leetcode.com/problems/factorial-trailing-zeroes/description/) -???? 0 ?? 2 * 5 ??????2 ????????????? 5 ????????????????????? 5 ???? +β 0 2 * 5 2 Զ 5 ֻҪͳжٸ 5 ɡ -????????? N?????????? 5 ????????N/5 + N/52 + N/53 + ...?????? N/5 ????????? N ?????? 5 ???????????? 5??N/52 ????????? N ?????? 52 ????????????? 5 ...?? +һ N 5 ĸΪN/5 + N/52 + N/53 + ... N/5 ʾ N 5 ıһ 5N/52 ʾ N 52 ıٹһ 5 ... ```java public int trailingZeroes(int n) { @@ -2638,11 +2638,11 @@ public int trailingZeroes(int n) { } ``` -????????? N! ?????????????? 1 ??????????????? 2 ?????????????? [ ????????2.2](#) ???????????? 5 ?????2 ?????? N/2 + N/22 + N/23 + ... +ͳƵ N! ĶƱʾλ 1 λãֻҪͳжٸ 2 ɣĿ [ ֮2.2](#) жٸ 5 һ2 ĸΪ N/2 + N/22 + N/23 + ... -### ???????????? +### ַӷ -**????????** +**Ƽӷ** [Leetcode : 67. Add Binary (Easy)](https://leetcode.com/problems/add-binary/description/) @@ -2661,11 +2661,11 @@ public String addBinary(String a, String b) { } ``` -**????????** +**ַӷ** [Leetcode : 415. Add Strings (Easy)](https://leetcode.com/problems/add-strings/description/) -????????????????????????? +ĿֵַΪǸ ```java public String addStrings(String num1, String num2) { @@ -2681,23 +2681,23 @@ public String addStrings(String num1, String num2) { } ``` -### ???????? +### -**??????????????????????????** +**ıԪʹеԪض** [Leetcode : 462. Minimum Moves to Equal Array Elements II (Medium)](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/) -???????????????????????????????????????????????? +ĿÿοԶһԪؼһ߼һСĸı -????????????????????????????????????????????????????????????? +Ǹ͵⣬ƶСķʽԪضƶλ£ -?? m ???????a ?? b ?? m ??????????????? b > a???? a ?? b ???????????????????? b - a?????????? (b - m) + (m - a)???????????????????????????????????? + m Ϊλa b m ߵԪأ b > aҪʹ a b ȣܹƶĴΪ b - aֵ (b - m) + (m - a)Ҳǰƶλƶ -?????A??? N?????????? N/2 ?? a ?? b ????????????????? m ???? +鳤Ϊ Nҵ N/2 a b ϣʹǶƶ m λá -**?? 1** +**ⷨ 1** -????????????O(NlgN) +ʱ临ӶȣO(NlgN) ```java public int minMoves2(int[] nums) { @@ -2713,9 +2713,9 @@ public int minMoves2(int[] nums) { } ``` -**?? 2** +**ⷨ 2** -???????????????????????? O(N) +ʹÿҵλʱ临Ӷ O(N) ```java public int minMoves2(int[] nums) { @@ -2747,13 +2747,13 @@ private void swap(int[] nums, int i, int j) { } ``` -### ?????????? +### ͶƱ -**??????????????? n / 2 ?????** +**гִ n / 2 Ԫ** [Leetcode : 169. Majority Element (Easy)](https://leetcode.com/problems/majority-element/description/) -?????????????????????????????????? n / 2 +ȶмǸִһ n / 2 ```java public int majorityElement(int[] nums) { @@ -2762,7 +2762,7 @@ public int majorityElement(int[] nums) { } ``` -???????? Boyer-Moore Majority Vote Algorithm ????????????????????? O(n)???????????????????? cnt ????????????????????????????????????????????????? cnt--????????????? i ???????? cnt == 0 ?????? i ???????? majority???????? majority????????????????? i / 2 ???????????? i / 2 ??? cnt ?????????? 0 ????????? n - i ???????majority ????????? (n - i) / 2?????????????????? majority?? + Boyer-Moore Majority Vote Algorithm ⣬ʹʱ临ӶΪ O(n)ô㷨ʹ cnt ͳһԪسֵĴԪغͳԪزʱ cnt--ǰ i Ԫأ cnt == 0 ˵ǰ i Ԫû majority majoritydzֵĴ i / 2 Ϊ i / 2 Ļ cnt һΪ 0 ʱʣµ n - i ԪУmajority Ŀ (n - i) / 2˼Ҿҳ majority ```java public int majorityElement(int[] nums) { @@ -2779,22 +2779,22 @@ public int majorityElement(int[] nums) { } ``` -**??????????????? n / k ?????** +**гִ n / k Ԫ** -[???????????????? P343](#) +[Աָ P343](#) -? k - 1 ????? +ѡ k - 1 ѡ -### ???? +### -**?????** +**ƽ** [Leetcode : 367. Valid Perfect Square (Easy)](https://leetcode.com/problems/valid-perfect-square/description/) -???????1,4,9,16,.. -?????3,5,7,... +ƽУ1,4,9,16,.. +3,5,7,... -???????????????????????????? 1 ???????????? +ΪȲУʹԿԵõ 1 ʼƽС ```java public boolean isPerfectSquare(int num) { @@ -2807,7 +2807,7 @@ public boolean isPerfectSquare(int num) { } ``` -**3 ?? n ??** +**3 n η** [Leetcode : 326. Power of Three (Easy)](https://leetcode.com/problems/power-of-three/description/) @@ -2817,7 +2817,7 @@ public boolean isPowerOfThree(int n) { } ``` -**?????????????????????** +**ҳеij˻** [Leetcode : 628. Maximum Product of Three Numbers (Easy)](https://leetcode.com/problems/maximum-product-of-three-numbers/description/) @@ -2847,13 +2847,13 @@ public int maximumProduct(int[] nums) { } ``` -**???????** +**˻** [Leetcode : 238. Product of Array Except Self (Medium)](https://leetcode.com/problems/product-of-array-except-self/description/) -??????????????????????????????????????????????????????????????????????????????? +Ŀһ飬һ飬ÿԪΪԭʼг˸λϵԪ֮Ԫصij˻ -???????????? O(n)???????????????? +ĿҪʱ临ӶΪ O(n)Ҳʹó ```java public int[] productExceptSelf(int[] nums) { @@ -2872,13 +2872,13 @@ public int[] productExceptSelf(int[] nums) { } ``` -# ???????? +# ݽṹ -## ?????? +## ջͶ -**?????????** +**ջʵֶ** -???????? +һջʵ֣ ```java class MyQueue { @@ -2909,7 +2909,7 @@ class MyQueue { } ``` -????????? +ջʵ֣ ```java class MyQueue { @@ -2944,7 +2944,7 @@ class MyQueue { } ``` -**?????????** +**öʵջ** [Leetcode : 225. Implement Stack using Queues (Easy)](https://leetcode.com/problems/implement-stack-using-queues/description/) @@ -2959,7 +2959,7 @@ class MyStack { public void push(int x) { queue.add(x); - for(int i = 1; i < queue.size(); i++){ // ??? + for(int i = 1; i < queue.size(); i++){ // ת queue.add(queue.remove()); } } @@ -2978,11 +2978,11 @@ class MyStack { } ``` -**????** +**Сֵջ** [Leetcode : 155. Min Stack (Easy)](https://leetcode.com/problems/min-stack/description/) -????????????????????????????? +ջʵ֣һ洢ݣһ洢Сֵ ```java class MinStack { @@ -3025,9 +3025,9 @@ class MinStack { } ``` -?????????????????????????????????????????????????????????????????????? ????????3.7?? +ʵСֵ⣬Ƚʹջʵ֣ȻͽתΪСֵջ ֮3.7 -**?????????????** +**ջʵƥ** [Leetcode : 20. Valid Parentheses (Easy)](https://leetcode.com/problems/valid-parentheses/description/) @@ -3057,7 +3057,7 @@ public boolean isValid(String s) { } ``` -**?????????????????????????????** +**бȵǰԪشһԪصľ** ```html Input: [73, 74, 75, 71, 69, 72, 76, 73] @@ -3066,7 +3066,7 @@ Output: [1, 1, 4, 2, 1, 1, 0, 0] [Leetcode : 739. Daily Temperatures (Medium)](https://leetcode.com/problems/daily-temperatures/description/) -???????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʹջ洢δԪءԱ֤ջԪصһijԪشԪؽջУǸԪѾҵԪأ˻ջ ```java public int[] dailyTemperatures(int[] temperatures) { @@ -3084,7 +3084,7 @@ public int[] dailyTemperatures(int[] temperatures) { } ``` -**??????????????????????** +**һȵǰ** [Leetcode : 496. Next Greater Element I (Easy)](https://leetcode.com/problems/next-greater-element-i/description/) @@ -3093,7 +3093,7 @@ Input: nums1 = [4,1,2], nums2 = [1,3,4,2]. Output: [-1,3,-1] ``` -???????????? Stack ??????????????????????????????????????????????????????????????????????????????? +ڱʱ Stack еǰջԪĴ˵ջԪصһǵǰԪء ```java public int[] nextGreaterElement(int[] nums1, int[] nums2) { @@ -3114,7 +3114,7 @@ public int[] nextGreaterElement(int[] nums1, int[] nums2) { } ``` -**?????????????????????????** +**ѭһȵǰԪش** [Leetcode : 503. Next Greater Element II (Medium)](https://leetcode.com/problems/next-greater-element-ii/description/) @@ -3134,28 +3134,28 @@ public int[] nextGreaterElements(int[] nums) { ``` -## ????? +## ϣ -???? Hash Table ??????????????????????????????????????????????????????????????????????????? Hash Table ???????????????? + Hash Table ԿٲһԪǷڵ⣬ҪһĿռ洢ȿʱ临Ӷȵ£ Hash Table ֿռ任ȡʱ -Java ?? **HashSet** ???????????????? O(1) ??????????????????????? +Java е **HashSet** ڴ洢һϣ O(1) ʱ临ӶȲԪǷڼС -????????????????????????????????????????????????????????????????????????????????????????? 26 ?????????????????????????????????? O(1)?? +ԪҷΧôһ洢һԪǷڣֻСдַԪأͿһΪ 26 IJ洢һַϣʹÿռ临ӶȽΪ O(1) -Java ?? **HashMap** ??????????????????????????????????? +Java е **HashMap** ҪӳϵӶԪϵ -?????????????????????????????????? HashMap ?????????????????????????????????????????? url ??????[Leetcdoe : 535. Encode and Decode TinyURL (Medium)](https://leetcode.com/problems/encode-and-decode-tinyurl/description/)???????? HashMap ??????????? url ???? url ???????????????????? url???????????? url ????? url ????????????????? +ڶһݽѹתʱ HashMap ԰ԭʼݺתϵһ url ϵͳУ[Leetcdoe : 535. Encode and Decode TinyURL (Medium)](https://leetcode.com/problems/encode-and-decode-tinyurl/description/) HashMap ͿԴ洢 url ԭʼ url ӳ䣬ʹòʾ򻯵 urlҲԸݼ򻯵 url õԭʼ url ӶλȷԴ -HashMap ??????????????????????????????????????????? HashSet ????????????????????????????????????????????? +HashMap ҲԪؽмͳƣʱΪԪأֵΪ HashSet ƣԪҷΧ󣬿ͳơ -**????????????????????** +**еΪֵ** [Leetcode : 1. Two Sum (Easy)](https://leetcode.com/problems/two-sum/description/) -??????????????????????????????????????????????????????????? O(nlgn)???????? O(1)?? +ȶȻʹ˫ָ뷽߶ֲҷʱ临ӶΪ O(nlgn)ռ临ӶΪ O(1) -?? HashMap ???????????????????????? nums[i] ????? HashMap ???????? target - nums[i] ???????????? target - nums[i] ??????????? i ???????????????????????????? O(n)???????? O(n)??????????????? + HashMap 洢Ԫغӳ䣬ڷʵ nums[i] ʱж HashMap Ƿ target - nums[i] ˵ target - nums[i] ڵ i Ҫҵ÷ʱ临ӶΪ O(n)ռ临ӶΪ O(n)ʹÿռȡʱ䡣 ```java public int[] twoSum(int[] nums, int target) { @@ -3168,9 +3168,9 @@ public int[] twoSum(int[] nums, int target) { } ``` -**????????** +**г** -??????????????????????????? 1 +гСֻΪ 1 [Leetcode : 594. Longest Harmonious Subsequence (Easy)](https://leetcode.com/problems/longest-harmonious-subsequence/description/) @@ -3190,15 +3190,15 @@ public int findLHS(int[] nums) { } ``` -## ????? +## ַ -**????????????????????????????** +**ַİַǷȫͬ** [Leetcode : 242. Valid Anagram (Easy)](https://leetcode.com/problems/valid-anagram/description/) -???????????????????? 26 ????????????? Hash Table ??????????????????????????????????????????????????? +ַֻСдַܹ 26 Сдַ Hash Table ӳִַΪֵΧС˿ӳ䡣 -?????? 26 ???????????????????????????????????????????????????????????????? +ʹóΪ 26 ֵַַͳƣȽֵַַǷͬ ```java public boolean isAnagram(String s, String t) { @@ -3210,13 +3210,13 @@ public boolean isAnagram(String s, String t) { } ``` -**????????** +**ַͬ** [Leetcode : 205. Isomorphic Strings (Easy)](https://leetcode.com/problems/isomorphic-strings/description/) -???? "egg" ?? "add" ???????????????? + "egg" "add" ַͬ -????????????????????????????????????????????????????????????????? +¼һַϴγֵλãַijַϴγֵλһôͬ ```java public boolean isIsomorphic(String s, String t) { @@ -3233,30 +3233,30 @@ public boolean isIsomorphic(String s, String t) { } ``` -**???????????????????????????????????** +**һַϿɵĻַ󳤶** [Leetcode : 409. Longest Palindrome](https://leetcode.com/problems/longest-palindrome/description/) -?????? 128 ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʹóΪ 128 ͳÿֵַĸÿַżɻַΪַмǸַԵ֣еַͰŵм䡣 ```java public int longestPalindrome(String s) { - int[] cnts = new int[128]; // ascii ????? 128 ?? + int[] cnts = new int[128]; // ascii ܹ 128 for(char c : s.toCharArray()) cnts[c]++; int ret = 0; for(int cnt : cnts) ret += (cnt / 2) * 2; - if(ret < s.length()) ret ++; // ????????? s ???????????????????????????????????????????? + if(ret < s.length()) ret ++; // s һеδʹõַڣ԰ַŵĵм return ret; } ``` -**???????????????????** +**жһǷǻ** [Leetcode : 9. Palindrome Number (Easy)](https://leetcode.com/problems/palindrome-number/description/) -???????????????????????????????????????? +Ҫʹöռ䣬ҲͲܽתΪַжϡ -??????????????????????????????????????????????????????? +ֳ֣ұDzҪתãȻжǷȡ ```java public boolean isPalindrome(int x) { @@ -3272,18 +3272,18 @@ public boolean isPalindrome(int x) { } ``` -**???????????** +**ַ** [Leetcode : 647. Palindromic Substrings (Medium)](https://leetcode.com/problems/palindromic-substrings/description/) -??????????????????????????????????????????? +Ǵַijһλʼȥչַ ```java private int cnt = 0; public int countSubstrings(String s) { for(int i = 0; i < s.length(); i++) { - extendSubstrings(s, i, i); // ???????? - extendSubstrings(s, i, i + 1); // ??????? + extendSubstrings(s, i, i); // + extendSubstrings(s, i, i + 1); // ż } return cnt; } @@ -3297,7 +3297,7 @@ private void extendSubstrings(String s, int start, int end) { } ``` -**??????????????????? 1 ?? ???? 0 ????????????????????** +**ͳƶַ 1 0 ַͬ** ```html Input: "00110011" @@ -3323,48 +3323,48 @@ public int countBinarySubstrings(String s) { } ``` -**??????????????** +**ַѭλ** -[ ????????3.1](#) +[ ֮3.1](#) -????????????? s1 ?? s2 ??????? s2 ???????? s1 ??????????????????????? +ַ s1 s2 Ҫж s2 Ƿܹ s1 ѭλõַ ```html s1 = AABCD, s2 = CDAA Return : true ``` -s1 ??????????????? s1s1 ?????????????????? s2 ????? s1s1 ????????????? +s1 ѭλĽ s1s1 ַֻҪж s2 Ƿ s1s1 ַɡ -**??????????** +**ַѭλ** -[ ????????2.17](#) +[ ֮2.17](#) -????????????????? k ?? +ַѭƶ k λ -???? abcd123 ??????? 3 ??? 123abcd + abcd123 ƶ 3 λ õ 123abcd -?? abcd123 ?? abcd ?? 123 ?????????? dcba321?????????????????????????123abcd?? + abcd123 е abcd 123 򣬵õ dcba321Ȼַ򣬵õ123abcd -**????????????** +**ַеʵķת** -[????????????????](#) +[Աָ](#) -???L "I am a student" ????? "student a am I" +罫 "I am a student" ת "student a am I" -????????????????????????????? +ÿȻַ -## ????????? +## -**???????** // TODO +**ת** // TODO -[??????????? P114](#) +[ԱԽ P114](#) -**???????** //TODO +**֮ʹӡ** //TODO -[???????????????? P335](#) +[Աָ P335](#) -**???????? 0 ????** +**е 0 Ƶĩβ** [Leetcode : 283. Move Zeroes (Easy)](https://leetcode.com/problems/move-zeroes/description/) @@ -3381,20 +3381,20 @@ s1 ????????? } ``` -**???????????? [1, n] ???????????????I?????????????????????????????** +**һԪ [1, n] ֮䣬һ滻Ϊһҳʧظ** [Leetcode : 645. Set Mismatch (Easy)](https://leetcode.com/problems/set-mismatch/description/) -?????????????????????????????????????? O(nlogn)??????????? O(n) ????????O(1) ?????????? +ֱӵķȶַʱ临ӶΪ O(nlogn) O(n) ʱ临ӶȡO(1) ռ临Ӷ⡣ -?????????????????????????????????????????????????? +Ҫ˼ͨԪأʹϵԪȷλϡ -???????????? i ????????? i + 1 ???????????? i ?? nums[i] - 1 ?????????? num[i] - 1 ?????? nums[i] ?????????????????????????????????????????????????????? i ???????????????????????????????????????????????????????????????????????????????????? nums[i] != nums[nums[i] - 1 ?????? +飬 i λϵԪز i + 1 ôͽ i λ nums[i] - 1 λϵԪأʹ num[i] - 1 ԪΪ nums[i] ҲǸλԪȷġҪѭУΪһνû취ʹõ i λϵԪȷġҪԪؿܾظԪأôѭͿԶȥֹѭķǼ nums[i] != nums[nums[i] - 1 -????????? +Ŀ -- [Leetcode :448. Find All Numbers Disappeared in an Array (Easy)](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/)??????????????? -- [Leetcode : 442. Find All Duplicates in an Array (Medium)](https://leetcode.com/problems/find-all-duplicates-in-an-array/description/)?????????????????? +- [Leetcode :448. Find All Numbers Disappeared in an Array (Easy)](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/)ѰжʧԪ +- [Leetcode : 442. Find All Duplicates in an Array (Medium)](https://leetcode.com/problems/find-all-duplicates-in-an-array/description/)ѰظԪء ```java public int[] findErrorNums(int[] nums) { @@ -3416,11 +3416,11 @@ private void swap(int[] nums, int i, int j){ } ``` -**????????????????????????? [0, n-1] ???** +**ҳظֵ [0, n-1] ֮** [Leetcode : 287. Find the Duplicate Number (Medium)](https://leetcode.com/problems/find-the-duplicate-number/description/) -?????????? +ֲҽⷨ ```java public int findDuplicate(int[] nums) { @@ -3438,7 +3438,7 @@ public int findDuplicate(int[] nums) { } ``` -???????????????????????????????? +˫ָⷨлҳڣ ```java public int findDuplicate(int[] nums) { @@ -3457,11 +3457,11 @@ public int findDuplicate(int[] nums) { } ``` -### ??????? +### -????????????????????????? +ָкзֱľ -?????????????????????????????? +һʹöֲҷ ```html [ @@ -3471,7 +3471,7 @@ public int findDuplicate(int[] nums) { ] ``` -**??????????** +**** [Leetocde : 240. Search a 2D Matrix II (Medium)](https://leetcode.com/problems/search-a-2d-matrix-ii/description/) @@ -3489,7 +3489,7 @@ public boolean searchMatrix(int[][] matrix, int target) { } ``` -**???????? Kth Element** +** Kth Element** [Leetcode : 378. Kth Smallest Element in a Sorted Matrix ((Medium))](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/) @@ -3504,9 +3504,9 @@ k = 8, return 13. ``` -???????[Share my thoughts and Clean Java Code](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173) +ο[Share my thoughts and Clean Java Code](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173) -?????????? +ֲҽⷨ ```java public int kthSmallest(int[][] matrix, int k) { @@ -3527,14 +3527,14 @@ public int kthSmallest(int[][] matrix, int k) { } ``` -????? +ѽⷨ ```java public int kthSmallest(int[][] matrix, int k) { int m = matrix.length, n = matrix[0].length; PriorityQueue pq = new PriorityQueue(); for(int j = 0; j < n; j++) pq.offer(new Tuple(0, j, matrix[0][j])); - for(int i = 0; i < k - 1; i++) { // ???????? k - 1 ?????????????????????? k ???? + for(int i = 0; i < k - 1; i++) { // Сѣȥ k - 1 ѶԪأʱѶԪؾǵ k Tuple t = pq.poll(); if(t.x == m - 1) continue; pq.offer(new Tuple(t.x + 1, t.y, matrix[t.x + 1][t.y])); @@ -3555,25 +3555,25 @@ class Tuple implements Comparable { } ``` -## ???? +## -**??????????????** +**жĽ** [Leetcode : 160. Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/description/) ```html -A: a1 ?? a2 - ?K - c1 ?? c2 ?? c3 - ?J -B: b1 ?? b2 ?? b3 +A: a1 a2 + K + c1 c2 c3 + J +B: b1 b2 b3 ``` -????????? O(n) ?????? O(1) +Ҫʱ临ӶΪ O(n) ռ临ӶΪ O(1) -?? A ?????? a + c??B ?????? b + c?????? c ?????????????????? a + c + b = b + c + a?? + A ijΪ a + cB ijΪ b + c c Ϊβֳȣ֪ a + c + b = b + c + a -?????? A ??????????????????????????????? B ???????????????? B?????????????? B ??????????????????????????????? A ???????????????? A???????????????? A ?? B ????????????????????????? + A ָʵβʱ B ͷʼ Bͬأ B ָʵβʱ A ͷʼ AܿƷ A B ָͬʱʵ㡣 ```java public ListNode getIntersectionNode(ListNode headA, ListNode headB) { @@ -3587,19 +3587,19 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) { } ``` -??????????????????????????????????? ????????3.6 ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ֻжǷڽ㣬ôһ⣬ ֮3.6 ⡣ֽⷨѵһĽβӵڶĿͷڶǷڻֱӱȽϵһһڵ͵ڶһڵǷͬ -**?????** +**ת** [Leetcode : 206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/) -???????????????? +ͷ巨ܹ򹹽 ```java public ListNode reverseList(ListNode head) { - ListNode newHead = null; // ??? null ????????????? + ListNode newHead = null; // Ϊ null ΪĽβ while(head != null){ ListNode nextNode = head.next; head.next = newHead; @@ -3610,11 +3610,11 @@ public ListNode reverseList(ListNode head) { } ``` -**??????????????** +**鲢** [Leetcode : 21. Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/) -??????????????????????????????????????????????????????????????????????????????????????????? +һõݹ鷽ʽ壺ǿսڵ㣬һֵһָһָ룬˺ܶõݹ ```java public ListNode mergeTwoLists(ListNode l1, ListNode l2) { @@ -3632,7 +3632,7 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) { } ``` -**?????????????????????** +**ɾظڵ** [Leetcode : 83. Remove Duplicates from Sorted List (Easy)](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/) @@ -3644,11 +3644,11 @@ public ListNode deleteDuplicates(ListNode head) { } ``` -**????????** +**** [Leetcode : 234. Palindrome Linked List (Easy)](https://leetcode.com/problems/palindrome-linked-list/description/) -?????????????????????????????? +г룬ѺηתȻȽǷȡ ```java public boolean isPalindrome(ListNode head) { @@ -3659,11 +3659,11 @@ public boolean isPalindrome(ListNode head) { fast = fast.next.next; } - if(fast != null){ // ???????? slow ??????????? + if(fast != null){ // żڵ㣬 slow ָһڵ slow = slow.next; } - cut(head, slow); // ?????????? + cut(head, slow); // г ListNode l1 = head, l2 = slow; l2 = reverse(l2); return isEqual(l1, l2); @@ -3695,9 +3695,9 @@ private boolean isEqual(ListNode l1, ListNode l2){ } ``` -**??????????????** +**ɾڵ** -[????????3.4]() +[֮3.4]() ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2c968ec5-0967-49ce-ac06-f8f5c9ab33bc.jpg) @@ -3706,7 +3706,7 @@ B.val = C.val; B.next = C.next; ``` -**??????????????** +**Ԫذżۼ** [Leetcode : 328. Odd Even Linked List (Medium)](https://leetcode.com/problems/odd-even-linked-list/description/) @@ -3727,13 +3727,13 @@ public ListNode oddEvenList(ListNode head) { } ``` -## ?? +## -### ??? +### ݹ -?????????????????????????????????????????????????????????????????????????????? +һҪôǿҪôָ룬ÿָָһһֵݹṹܶʹõݹ -**??????** +**ĸ߶** [Leetcode : 104. Maximum Depth of Binary Tree (Easy)](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/) @@ -3744,21 +3744,21 @@ public int maxDepth(TreeNode root) { } ``` -**?????** +**ת** [Leetcode : 226. Invert Binary Tree (Easy)](https://leetcode.com/problems/invert-binary-tree/description/) ```java public TreeNode invertTree(TreeNode root) { if(root == null) return null; - TreeNode left = root.left; // ???????????? left ??????????????? + TreeNode left = root.left; // IJı left ָ룬ȱ root.left = invertTree(root.right); root.right = invertTree(left); return root; } ``` -**???????** +**鲢** [Leetcode : 617. Merge Two Binary Trees (Easy)](https://leetcode.com/problems/merge-two-binary-trees/description/) @@ -3774,11 +3774,11 @@ public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { } ``` -**?????????????????** +**ж·Ƿһ** [Leetcdoe : 112. Path Sum (Easy)](https://leetcode.com/problems/path-sum/description/) -??????????????????? root ?? leaf ????????? +Ŀ·ͶΪ root leaf нڵĺ ```java public boolean hasPathSum(TreeNode root, int sum) { @@ -3788,13 +3788,13 @@ public boolean hasPathSum(TreeNode root, int sum) { } ``` -**???????????????????????** +**ͳ·͵һ·** [Leetcode : 437. Path Sum III (Easy)](https://leetcode.com/problems/path-sum-iii/description/) -?????????????????? root ??????? leaf ??????????????? +Ŀ·һ root ͷ leaf βDZ -pathSumStartWithRoot() ?????????????????????????? +pathSumStartWithRoot() ͳijڵ㿪ͷ· ```java public int pathSum(TreeNode root, int sum) { @@ -3812,7 +3812,7 @@ private int pathSumStartWithRoot(TreeNode root, int sum){ } ``` -**??????** +**ĶԳ** [Leetcode : 101. Symmetric Tree (Easy)](https://leetcode.com/problems/symmetric-tree/description/) @@ -3830,11 +3830,11 @@ private boolean isSymmetric(TreeNode t1, TreeNode t2){ } ``` -**?????** +**ƽ** [Leetcode : 110. Balanced Binary Tree (Easy)](https://leetcode.com/problems/balanced-binary-tree/description/) -????????????????????????????? 1 +Ŀ߶ȲǷСڵ 1 ```java private boolean result = true; @@ -3853,11 +3853,11 @@ public int maxDepth(TreeNode root) { } ``` -**????** +**С·** [Leetcode : 111. Minimum Depth of Binary Tree (Easy)](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/) -????????????????????????????? +Ŀĸڵ㵽ҶӽڵС ```java public int minDepth(TreeNode root) { @@ -3869,7 +3869,7 @@ public int minDepth(TreeNode root) { } ``` -**????????????** +**ͳҶӽڵĺ** [Leetcode : 404. Sum of Left Leaves (Easy)](https://leetcode.com/problems/sum-of-left-leaves/description/) @@ -3886,11 +3886,11 @@ private boolean isLeaf(TreeNode node){ } ``` -**????????** +**޼һ** [Leetcode : 669. Trim a Binary Search Tree (Easy)](https://leetcode.com/problems/trim-a-binary-search-tree/description/) -????????????????? L \~ R ?????? +Ŀֵֻ L \~ R ֮Ľڵ ```java public TreeNode trimBST(TreeNode root, int L, int R) { @@ -3903,7 +3903,7 @@ public TreeNode trimBST(TreeNode root, int L, int R) { } ``` -**????** +**** [Leetcode : 572. Subtree of Another Tree (Easy)](https://leetcode.com/problems/subtree-of-another-tree/description/) @@ -3923,11 +3923,11 @@ private boolean isSame(TreeNode s, TreeNode t){ } ``` -**??????????????????????** +**й** [Leetcode : 108. Convert Sorted Array to Binary Search Tree (Easy)](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/) -???????????BST?????????????????????????????????????????? +BSTڵڵнڵ㣬Сڵнڵ㡣 ```java public TreeNode sortedArrayToBST(int[] nums) { @@ -3944,7 +3944,7 @@ private TreeNode toBST(int[] nums, int sIdx, int eIdx){ } ``` -**??????????** +**ڵ·** ```html 1 @@ -3975,7 +3975,7 @@ private int depth(TreeNode root) { } ``` -**????????????????** +**ҳеڶСĽڵ** [Leetcode : 671. Second Minimum Node In a Binary Tree (Easy)](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/) @@ -3990,7 +3990,7 @@ Input: Output: 5 ``` -???????????? 0 ???? 2 ??????????????????????????????? +һڵҪô 0 2 ӽڵ㣬ӽڵ㣬ôڵСĽڵ㡣 ```java public int findSecondMinimumValue(TreeNode root) { @@ -4006,7 +4006,7 @@ public int findSecondMinimumValue(TreeNode root) { } ``` -**??????????????????????** +**Ѱڵ** [Leetcode : 235. Lowest Common Ancestor of a Binary Search Tree (Easy)](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/) @@ -4018,7 +4018,7 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { } ``` -**???????????** +**** [Leetcode : 236. Lowest Common Ancestor of a Binary Tree (Medium) ](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/) @@ -4031,7 +4031,7 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { } ``` -**??????????????????** +**ͬڵֵ·** [Leetcode : 687. Longest Univalue Path (Easy)](https://pomotodo.com/app/) @@ -4063,7 +4063,7 @@ private int dfs(TreeNode root){ } ``` -**???????** +**** [Leetcode : 337. House Robber III (Medium)](https://leetcode.com/problems/house-robber-iii/description/) @@ -4082,11 +4082,11 @@ public int rob(TreeNode root) { } ``` -### ????? +### α -??? BFS???????????????????????????????????????? ??????????????????????????????????????????????????????????????????????????????????????????? +ʹ BFSҪʹֱ洢ǰĽڵһĽڵ㣬 ΪڿʼһĽڵʱǰеĽڵǵǰĽڵֻҪƱôڵܱ֤αĶǵǰĽڵ㡣 -**????????????????????** +**һÿڵƽ** [637. Average of Levels in Binary Tree (Easy)](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/) @@ -4111,7 +4111,7 @@ public List averageOfLevels(TreeNode root) { } ``` -**???????????** +**õ½ǵĽڵ** [Leetcode : 513. Find Bottom Left Tree Value (Easy)](https://leetcode.com/problems/find-bottom-left-tree-value/description/) @@ -4128,7 +4128,7 @@ public int findBottomLeftValue(TreeNode root) { } ``` -### ???????? +### ǰк ```html 1 @@ -4138,16 +4138,16 @@ public int findBottomLeftValue(TreeNode root) { 4 5 6 ``` -????????[1 2 3 4 5 6] -?????????[1 2 4 5 3 6] -??????????[4 2 5 1 3 6] -??????????[4 5 2 6 3 1] +α˳[1 2 3 4 5 6] +ǰ˳[1 2 4 5 3 6] +˳[4 2 5 1 3 6] +˳[4 5 2 6 3 1] -???????? BFS ???????????? BFS ????????????????????????????????????? DFS ???? +αʹ BFS ʵ֣õľ BFS һһԣǰ򡢺 DFS ʵ֡ -????????????????????????????????????????????? +ǰ򡢺ֻڶԽڵʵ˳һ㲻ͬͬ -?? ??? + ǰ ```java void dfs(TreeNode root){ @@ -4157,7 +4157,7 @@ void dfs(TreeNode root){ } ``` -?? ???? + ```java void dfs(TreeNode root){ @@ -4167,7 +4167,7 @@ void dfs(TreeNode root){ } ``` -?? ???? + ```java void dfs(TreeNode root){ @@ -4177,7 +4177,7 @@ void dfs(TreeNode root){ } ``` -**????????????????????** +**ǵݹʵֶǰ** [Leetcode : 144. Binary Tree Preorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-preorder-traversal/description/) @@ -4191,17 +4191,17 @@ public List preorderTraversal(TreeNode root) { TreeNode node = stack.pop(); ret.add(node.val); if (node.right != null) stack.push(node.right); - if (node.left != null) stack.push(node.left); // ?????????????????????????????????????????????? + if (node.left != null) stack.push(node.left); // Ϊջ } return ret; } ``` -**?????????????????????** +**ǵݹʵֶĺ** [Leetcode : ### 145. Binary Tree Postorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-postorder-traversal/description/) -??????? root -> left -> right?????????? left -> right -> root?????????????????? root -> right -> left??????????????????????????? +ǰΪ root -> left -> rightΪ left -> right -> root޸ǰΪ root -> right -> leftô˳ͺͺ෴ ```java public List postorderTraversal(TreeNode root) { @@ -4220,7 +4220,7 @@ public List postorderTraversal(TreeNode root) { } ``` -**?????????????????????** +**ǵݹʵֶ** [Leetcode : 94. Binary Tree Inorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-inorder-traversal/description/) @@ -4230,7 +4230,7 @@ public List inorderTraversal(TreeNode root) { Stack stack = new Stack<>(); TreeNode cur = root; while(cur != null || !stack.isEmpty()) { - while(cur != null) { // ??????????????? + while(cur != null) { // ģݹջIJ stack.add(cur); cur = cur.left; } @@ -4242,19 +4242,19 @@ public List inorderTraversal(TreeNode root) { } ``` -**???????????????????????????????** //TODO +**ʹǰؽ** //TODO ### BST -??????? BST ??????????????? +Ҫ BST ص㡣 -**?? BST ??????????????????????????????** +** BST Ѱڵ㣬ʹǵĺΪһֵ** [653. Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/) -????????????????????????????????????????????? +ʹõ֮˫ָвҡ -?????????????????????????????????????????????????????????????????????????????? +Ӧע⵽һⲻ÷ֱ˼룬ΪĽڵֱܷС ```java public boolean findTarget(TreeNode root, int k) { @@ -4278,11 +4278,11 @@ private void inOrder(TreeNode root, List nums){ } ``` -**?? BST ???????????????????????** +** BST вСڵ֮ľֵ** [Leetcode : 530. Minimum Absolute Difference in BST (Easy)](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/) -???? BST ????????????????????????????????????????????????????????????? + BST Ϊʣٽڵ֮ľֵȡСֵ ```java private int minDiff = Integer.MAX_VALUE; @@ -4302,11 +4302,11 @@ private void inorder(TreeNode node){ } ``` -**?? BST ????????????????????????** +** BST ÿڵֵϱĽڵֵ** [Leetcode : Convert BST to Greater Tree (Easy)](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) -????????????? +ȱ ```java private int sum = 0; @@ -4331,7 +4331,7 @@ private void traver(TreeNode root) { } ``` -**??? BST ?????????????** +**Ѱ BST гִĽڵ** ```java private int cnt = 1; @@ -4369,11 +4369,11 @@ private void inorder(TreeNode node){ } ``` -**??? BST ??? k ?????** +**Ѱ BST ĵ k Ԫ** [Leetcode : 230. Kth Smallest Element in a BST (Medium)](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/) -?????? +ݹⷨ ```java public int kthSmallest(TreeNode root, int k) { @@ -4389,7 +4389,7 @@ private int count(TreeNode node) { } ``` -??????????? +ⷨ ```java private int cnt = 0; @@ -4417,9 +4417,9 @@ private void inorder(TreeNode node, int k) { ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5c638d59-d4ae-4ba4-ad44-80bdc30f38dd.jpg) -Trie???????????????????????????????????????????????????????? +Trieֳǰ׺ֵжַǷڻǷijַǰ׺ -**?????? Trie** +**ʵһ Trie** [Leetcode : 208. Implement Trie (Prefix Tree) (Medium)](https://leetcode.com/problems/implement-trie-prefix-tree/description/) @@ -4479,7 +4479,7 @@ class Trie { } ``` -**?????? Trie????????????** +**ʵһ Trieǰ׺** [Leetcode : 677. Map Sum Pairs (Medium)](https://leetcode.com/problems/map-sum-pairs/description/) @@ -4536,13 +4536,13 @@ class MapSum { } ``` -## ? +## ͼ -## ???? +## λ -**1. ???????** +**1. ԭ** -0s ??? ??? 0 ??1s ?????? 1?? +0s ʾ һ 0 1s ʾһ 1 ``` x ^ 0s = x x & 0s = 0 x | 0s = x @@ -4550,33 +4550,33 @@ x ^ 1s = \~x x & 1s = x x | 1s = 1s x ^ x = 0 x & x = x x | x = x ``` -?? ???? x ^ 1s = \~x ???????????????????????? x ^ x = 0 ???????????????????????????????????????????????? -?? ???? x & 0s = 0 ?? x & 1s = x ?????????????????????????? num ?? mask ??00111100 ???????????????? num ???? mask ?? 1 ???????????? -?? ???? x | 0s = x ?? x | 1s = 1s ????????????????????????? num ?? mask??00111100 ????????????? num ???? mask ?? 1 ????????????????? 1 ?? + x ^ 1s = \~x ص㣬Խλʾת x ^ x = 0 ص㣬Խظȥֻһ + x & 0s = 0 x & 1s = x ص㣬ʵһ num mask 00111100 λֻ num mask 1 Ӧλ + x | 0s = x x | 1s = 1s ص㣬ʵòһ num mask00111100 λ num mask 1 ӦλΪ 1 -\>\> n ????????????????? 2n?? -\>\>\> n ????????????????? 0?? -<< n ????????????????? 2n?? +\>\> n Ϊƣ൱ڳ 2n +\>\>\> n Ϊ޷ƣ߻Ჹ 0 +<< n Ϊƣ൱ڳ 2n -n&(n-1) ??????????? n ???????????????????????????????? 10110**100**????? 1 ??? 10110**011**???????????????? 10110**000**?? +n&(n-1) λȥ n λʾ͵һλڶƱʾ 10110**100**ȥ 1 õ 10110**011**õ 10110**000** -n-n&(\~n+1) ??????????? n ?????????????????? +n-n&(\~n+1) ȥ n λʾߵһλ -n&(-n) ???????? n ??????????????????-n ??? n ?????? 1????????????? 10110**100**??-n ??? 01001**100**???????? 00000**100** +n&(-n) õ n λʾ͵һλ-n õ n ķ 1ڶƱʾ 10110**100**-n õ 01001**100**õ 00000**100** -**2. mask ????** +**2. mask ** -???? 111111111???? 0 ????????\~0?? +Ҫȡ 111111111 0 ȡɣ\~0 -??????? i ? 1 ?? mask???? 1 ??????? i ?????1<<i ?????? 1<<5 ?????? 5 ? 1 ?? mask ??00010000?? +Ҫõֻе i λΪ 1 mask 1 ƶ i λɣ1<<i 1<<5 õֻе 5 λΪ 1 mask 00010000 -???? 1 ?? i ? 1 ?? mask??1<<(i+1)-1 ????????L 1<<(4+1)-1 = 00010000-1 = 00001111?? +Ҫõ 1 i λΪ 1 mask1<<(i+1)-1 ɣ罫 1<<(4+1)-1 = 00010000-1 = 00001111 -???? 1 ?? i ? 0 ?? mask???? 1 ?? i ? 1 ?? mask ??????? \~(1<<(i+1)-1)?? +Ҫõ 1 i λΪ 0 maskֻ轫 1 i λΪ 1 mask ȡ \~(1<<(i+1)-1) -**3. ????????** +**3. λ** -?? ????? i + ȡ i λ num & 00010000 != 0 @@ -4584,7 +4584,7 @@ num & 00010000 != 0 (num & (1 << i)) != 0; ``` -?? ???? i ????? 1 + i λΪ 1 num | 00010000 @@ -4592,7 +4592,7 @@ num | 00010000 num | (1 << i); ``` -?? ???? i ???? 0 + i λΪ 0 num & 11101111 @@ -4600,7 +4600,7 @@ num & 11101111 num & (\~(1 << i)) ``` -?? ????????? i ???? 0 + λ i λΪ 0 num & 00001111 @@ -4608,7 +4608,7 @@ num & 00001111 num & ((1 << i) - 1); ``` -?? ???? 0 ???? i ???? 0 + 0 λ i λΪ 0 num & 11110000 @@ -4616,27 +4616,27 @@ num & 11110000 num & (\~((1 << (i+1)) - 1)); ``` -?? ???? i ????? 0 ???? 1 + i λΪ 0 1 -????? i ??????? v ???? i ??????????? +Ƚ i λ㣬Ȼ v i λִСλ㡣 ```java (num & (1 << i)) | (v << i); ``` -**4. Java ??????** +**4. Java еλ** ```html -static int Integer.bitCount() // ??? 1 ?????? -static int Integer.highestOneBit() // ?????? -static String toBinaryString(int i) // ?????????????????? +static int Integer.bitCount() // ͳ 1 +static int Integer.highestOneBit() // λ +static String toBinaryString(int i) // תλƱʾַ ``` -**?????????????????????????** +**ͳĶƱʾжλͬ** [Leetcode : 461. Hamming Distance (Easy)](https://leetcode.com/problems/hamming-distance/) -???????????????????????????????? 1 ?????????? 1 ???? +ͬһλΪ 1 ͳжٸ 1 ɡ ```java public int hammingDistance(int x, int y) { @@ -4650,7 +4650,7 @@ public int hammingDistance(int x, int y) { } ``` -??????? Integer.bitcount() ????? 1 ????????? +ʹ Integer.bitcount() ͳ 1 ĸ ```java public int hammingDistance(int x, int y) { @@ -4658,7 +4658,7 @@ public int hammingDistance(int x, int y) { } ``` -**?????????????** +**תһıλ** [Leetcode : 190. Reverse Bits (Easy)](https://leetcode.com/problems/reverse-bits/description/) @@ -4674,9 +4674,9 @@ public int reverseBits(int n) { } ``` -**??????????????????????** +**ö** -[???????????????? ??P317](#) +[Աָ P317](#) ```java a = a ^ b; @@ -4684,13 +4684,13 @@ b = a ^ b; a = a ^ b; ``` -?? c = a ^ b????? b ^ c = b ^ b ^ a = a??a ^ c = a ^ a ^ b = b?? + c = a ^ bô b ^ c = b ^ b ^ a = aa ^ c = a ^ a ^ b = b -**???????????? 4 ?? n ??** +**жһDz 4 n η** [Leetcode : 342. Power of Four (Easy)](https://leetcode.com/problems/power-of-four/) -??????????????????????????? 1 ????????? 0 ?????? 16 ?? 10000?????????? 1 ??????? 2 ?????????????????????????????????????????????????? +ƱʾֻһλΪ 1 ĶΪ 0 16 10000ÿΰ 1 ƶ 2 λܹ֣ȻȽϹҪжϵǷͬ ```java public boolean isPowerOfFour(int num) { @@ -4703,7 +4703,7 @@ public boolean isPowerOfFour(int num) { } ``` -??????? Java ?? Integer.toString() ?????????????? 4 ??????????????????????????????? 1 ????? +Ҳ Java Integer.toString() תΪ 4 ʽַȻжַǷ 1 ͷ ```java public boolean isPowerOfFour(int num) { @@ -4711,11 +4711,11 @@ public boolean isPowerOfFour(int num) { } ``` -**???????????? 2 ?? n ??** +**жһDz 2 n η** [Leetcode : 231. Power of Two (Easy)](https://leetcode.com/problems/power-of-two/description/) -????????? Power of Four ??????????? 2 ?? n ????????????????????????? 1 ????? +ͬ Power of Four ķ 2 n η⣬ĶƱʾֻһ 1 ڡ ```java public boolean isPowerOfTwo(int n) { @@ -4723,7 +4723,7 @@ public boolean isPowerOfTwo(int n) { } ``` -???? 1000 & 0111 == 0 ??????????????????? + 1000 & 0111 == 0 ʣõ½ⷨ ```java public boolean isPowerOfTwo(int n) { @@ -4731,13 +4731,13 @@ public boolean isPowerOfTwo(int n) { } ``` -**????????????????????** +**ΨһһظԪ** [Leetcode : 136. Single Number (Easy)](https://leetcode.com/problems/single-number/description/) -?????????????????? 0??????????????????????????????????????????????? +ͬĽΪ 0ĽǵֵǸ -???????[Leetcode : 389. Find the Difference (Easy)](https://leetcode.com/problems/find-the-difference/description/)??????????????????????????????????????????? O(1) ????????????????????? HashSet?? +ƵУ[Leetcode : 389. Find the Difference (Easy)](https://leetcode.com/problems/find-the-difference/description/)ַһַͬʹ O(1) Ŀռ临Ӷ⣬Ҫʹ HashSet ```java public int singleNumber(int[] nums) { @@ -4747,22 +4747,22 @@ public int singleNumber(int[] nums) { } ``` -**??????????????????** +**вظԪ** [Leetcode : 260. Single Number III (Medium)](https://leetcode.com/problems/single-number-iii/description/) -????????????????????????????????????? +ȵԪλʾϱضһλڲͬ -?????????????????????????????????????????????????? +ԪõĽΪظԪĽ -diff &= -diff ????? diff ?????? 0 ????????????????????????????????????????????????????????????????????????????? +diff &= -diff õ diff Ҳ಻Ϊ 0 λҲDzظԪλʾҲ಻ͬһλһλͿԽԪֿ ```java public int[] singleNumber(int[] nums) { int diff = 0; for(int num : nums) diff ^= num; - // ???????? + // õһλ diff &= -diff; int[] ret = new int[2]; for(int num : nums) { @@ -4773,11 +4773,11 @@ public int[] singleNumber(int[] nums) { } ``` -**??????????????????????????? 0 ?? 1** +**жһλʾǷ񲻻 0 1** [Leetcode : 693. Binary Number with Alternating Bits (Easy)](https://leetcode.com/problems/binary-number-with-alternating-bits/description/) -???? 10101 ?????????????????????????? 1 ??? 1010 ????????????????????????????????? 11111?? + 10101 λʾƶ 1 λõ 1010 ÿλͬõĽΪ 11111 ```java public boolean hasAlternatingBits(int n) { @@ -4786,13 +4786,13 @@ public boolean hasAlternatingBits(int n) { } ``` -**????????????** +**һIJ** [Leetcode : 476. Number Complement (Easy)](https://leetcode.com/problems/number-complement/description/) -????????????????? 0 ???? +ǶƱʾе 0 -???? 00000101??????????????? 00000111 ?????????????????????????????? 00000111?? + 00000101ҪԽ 00000111 ôתΪ 00000111 ```java public int findComplement(int num) { @@ -4804,7 +4804,7 @@ public int findComplement(int num) { } ``` -???????? Java ?? Integer.highestOneBit() ????????????? 1 ?????? + Java Integer.highestOneBit() ú 1 ```java public int findComplement(int num) { @@ -4815,7 +4815,7 @@ public int findComplement(int num) { } ``` -???? 10000000 ?????????????? 11111111?????????????????? + 10000000 Ҫչ 11111111· ```html mask |= mask >> 1 11000000 @@ -4835,11 +4835,11 @@ public int findComplement(int num) { } ``` -**???????????** +**ʵļӷ** [Leetcode : 371. Sum of Two Integers (Easy)](https://leetcode.com/problems/sum-of-two-integers/description/) -a ^ b ????????????????????????(a & b) << 1 ????????????????????? (a & b) << 1 ?????????? 0??????????????????? 0 ?????????????????? 0?????????? +a ^ b ʾûпǽλĺͣ(a & b) << 1 ǽλݹֹԭ (a & b) << 1 ұ߻һ 0ôݹ飬λұߵ 0 ࣬λΪ 0ݹֹ ```java public int getSum(int a, int b) { @@ -4847,17 +4847,17 @@ public int getSum(int a, int b) { } ``` -**??????????** +**ʵ˷** -[???????????????? P319](#) +[Աָ P319](#) -**??????????????** +**ַ˻** [Leetcode : 318. Maximum Product of Word Lengths (Medium)](https://leetcode.com/problems/maximum-product-of-word-lengths/description/) -?????????????????????????????????????????????????????????????????????????????????????????????????? +ĿַַֻСдַַַȵ˻Ҫַַܺͬ -???????????????????????????????????????????????????????????????? 26 ????????????? 32 ???????????????????????? +˼·ҪжַǷַַֻͬСдַܹ 26 λ˿һ 32 λ洢ÿַǷֹ ```java public int maxProduct(String[] words) { @@ -4881,14 +4881,14 @@ public int maxProduct(String[] words) { } ``` -# ?????? +# ο - [Leetcode](https://leetcode.com/problemset/algorithms/?status=Todo) -- ??? Offer -- ??????????? -- ?????? -- ???????????????? -- ????????????? -- ?? -- ????????????????????? +- ָ Offer +- ԱԽ +- ֮ +- Աָ +- ݽṹ㷨 +- 㷨 +- ̳лָ diff --git a/notes/Linux.md b/notes/Linux.md index 3de0ef8a..dc817fd6 100644 --- a/notes/Linux.md +++ b/notes/Linux.md @@ -1,41 +1,41 @@ -* [?????????????](#?????????????) - * [????](#????) - * [???](#???) - * [??????](#??????) - * [?????](#?????) +* [òԼ](#òԼ) + * [](#) + * [ػ](#ػ) + * [鿴](#鿴) + * [鿴˿](#鿴˿) * [PATH](#path) - * [?????](#?????) + * [еȼ](#еȼ) * [sudo](#sudo) * [GNU](#gnu) - * [????????](#????????) - * [???????ѷ](#???????ѷ) -* [????](#????) - * [??????????](#??????????) - * [??????](#??????) + * [](#) + * [а汾](#а汾) +* [](#) + * [̵ļ](#̵ļ) + * [](#) * [1. MBR](#1-mbr) * [2. GPT](#2-gpt) - * [??????????](#??????????) + * [](#) * [1. BIOS](#1-bios) * [2. UEFI](#2-uefi) - * [????](#????) -* [??????????????](#??????????????) - * [?????????](#?????????) - * [?????????????????](#?????????????????) - * [1. ?????????????](#1-?????????????) - * [2. ???????????](#2-???????????) - * [3. ??????](#3-??????) - * [???????](#???????) - * [?????????](#?????????) - * [??????](#??????) -* [???????](#???????) - * [??????](#??????) - * [????????????????](#????????????????) + * [](#) +* [ļȨĿ¼](#ļȨĿ¼) + * [ļȨ޸](#ļȨ޸) + * [ļԼȨ޵޸](#ļԼȨ޵޸) + * [1. ޸ļȺ](#1-޸ļȺ) + * [2. ޸ļӵ](#2-޸ļӵ) + * [3. ޸Ȩ](#3-޸Ȩ) + * [Ŀ¼Ȩ](#Ŀ¼Ȩ) + * [ļĬȨ](#ļĬȨ) + * [Ŀ¼](#Ŀ¼) +* [ļĿ¼](#ļĿ¼) + * [ļʱ](#ļʱ) + * [ļĿ¼Ļ](#ļĿ¼Ļ) * [1. ls](#1-ls) * [2. cp](#2-cp) * [3. rm](#3-rm) * [4. mv](#4-mv) - * [??????????](#??????????) + * [ȡļ](#ȡļ) * [1. cat](#1-cat) * [2. tac](#2-tac) * [3. more](#3-more) @@ -44,105 +44,105 @@ * [6. tail](#6-tail) * [7. od](#7-od) * [8. touch](#8-touch) - * [????????????](#????????????) + * [ָļ](#ָļ) * [1. which](#1-which) * [2. whereis](#2-whereis) * [3. locate](#3-locate) * [4. find](#4-find) - * [4.1 ???????????](#41-???????????) - * [4.2 ????????????????????????](#42-????????????????????????) - * [4.3 ???????????????????](#43-???????????????????) -* [???????????](#???????????) - * [??????????](#??????????) + * [4.1 ʱйصѡ](#41-ʱйصѡ) + * [4.2 ļӵߺȺйصѡ](#42-ļӵߺȺйصѡ) + * [4.3 ļȨ޺йصѡ](#43-ļȨ޺йصѡ) +* [ļϵͳ](#ļϵͳ) + * [ļϵͳ](#ļϵͳ) * [inode](#inode) - * [???? inode ?? block](#????-inode-??-block) - * [????????????????](#????????????????) - * [1. ???????](#1-???????) - * [2. ????????](#2-????????) -* [???????](#???????) - * [???](#???) + * [Ŀ¼ inode block](#Ŀ¼-inode--block) + * [ʵ](#ʵ) + * [1. ʵ](#1-ʵ) + * [2. ](#2-) +* [ѹ](#ѹ) + * [ѹ](#ѹ) * [1. gzip](#1-gzip) * [2. bzip2](#2-bzip2) * [3. xz](#3-xz) - * [???](#???) + * [](#) * [BASH](#bash) - * [Bash????](#bash????) - * [????????](#????????) - * [??????????](#??????????) - * [???????????](#???????????) - * [???????](#???????) - * [1. ??????cut](#1-??????cut) - * [2. ????????sort??uniq](#2-????????sort??uniq) - * [3. ???????????tee](#3-???????????tee) - * [4. ?????????tr??col??expand??join??paste](#4-?????????tr??col??expand??join??paste) - * [5. ???????split](#5-???????split) -* [??????????????????????](#??????????????????????) + * [Bash](#bash) + * [](#) + * [ָ˳](#ָ˳) + * [ض](#ض) + * [ָ](#ָ) + * [1. ȡָcut](#1-ȡָcut) + * [2. sortuniq](#2-sortuniq) + * [3. ˫ضtee](#3-˫ضtee) + * [4. ַתָtrcolexpandjoinpaste](#4-ַתָtrcolexpandjoinpaste) + * [5. ָsplit](#5-ָsplit) +* [ʾļʽ](#ʾļʽ) * [grep](#grep) * [printf](#printf) * [awk](#awk) -* [vim ??????](#vim-??????) -* [??????](#??????) +* [vim ģʽ](#vim-ģʽ) +* [ο](#ο) -# ????????????? +# òԼ -## ???? +## **1. --help** -?????????????????? +ָĻ÷ѡܡ **2. man** -man ?? manual ??????????????????????????? +man manual дָľϢʾ -????? man date ????? DATE(1) ?????????????????????????????????????????????? +ִ man date ʱ DATE(1) ֣еִָͣõּ£ -| ???? | ???? | +| | | | -- | -- | -| 1 | ????? shell ?????????????????????????? | -| 5 | ??????? | -| 8 | ????????????????????? | +| 1 | û shell пԲָ߿ִļ | +| 5 | ļ | +| 8 | ϵͳԱʹõĹָ | **3. info** -info ?? man ????????? info ???????????????????????????????? +info man ƣ info ĵֳһҳ棬ÿҳԽת -## ??? +## ػ -**1. ???????????? sync** +**1. ͬд sync** -????????????????????????????????????????????????????????????????????? sync ????????? +Ϊ˼ӿԴļĶдٶȣλڴеļݲͬϣ˹ػ֮ǰҪȽ sync ͬ **2. shutdown** ```html -# /sbin/shutdown [-krhc] [???] [??????] --k ?? ??????????????????????????????????? --r ?? ??????????????????????? --h ?? ?????????????????????? --c ?? ??????????? shutdown ??????? +# /sbin/shutdown [-krhc] [ʱ] [ѶϢ] +-k ػֻǷ;ѶϢ֪ͨߵû +-r ϵͳķͣ +-h ϵͳķͣػ +-c ȡѾڽе shutdown ָ ``` -**3. ??????????** +**3. ػָ** -reboot??halt??poweroff?? +reboothaltpoweroff -## ?????? +## 鿴 -ps ?? +ps ָ -????? theadx ?????????? +鿴 theadx Ϣ ```html ps aux | grep threadx ``` -## ????? +## 鿴˿ -netstat ?? +netstat ָ -???????? 80 ??????? +鿴˿ 80 Ƿռã ```html netstat -anp | grep 80 @@ -150,345 +150,345 @@ netstat -anp | grep 80 ## PATH -????????????? PATH ??????????????????????????? : ????? +ڻ PATH ִļ··֮ : ָ ```html /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/dmtsai/.local/bin:/home/dmtsai/bin ``` -## ????? +## еȼ -0??????? -1????????????????????root???? -2?????????????????? -3?????????????????????????????????????????? -4??????????? -5???????????? X-windows ?????????????? -6???????????????????? +0ػģʽ +1ûģʽƽroot룩 +2ֵ֧Ķûģʽ +3ֵ֧Ķûģʽıģʽõģʽ +4δʹ +5ֵ֧ X-windows ֶ֧ûģʽ棩 +6ϵͳ ## sudo -??? sudo ????????????? root ????????????????? /etc/sudoers ?????????? +ʹ sudo һûʹ root ִеû /etc/sudoers вʹøָ ## GNU -GNU ?????????????????????????????????????????????????? GNU???????????????? GPL ????????????? GPL ???? GNU ???????????????????????? +GNU ƻΪūƻĿǴһȫɵIJϵͳΪ GNUȫ GPL ʽ GPL ȫΪ GNU ͨùЭ飬ݣ -- ???????????????????? -- ??????????? -- ?????????????????????????? +- κĿд˳ɣ +- ٸƵɣ +- Ľ˳򣬲Ľɡ -## ???????? +## -RPM ?? DPKG ???????????????????????RPM ???? Redhat Package Manager???????? Red Hat ????????????? GNU ???????????????????? Linux ?? (RHEL) ?????????????? RPM ????????????? Debian ?????? (UBUNTU) ?? DEB ???????????? DPKG?????? Debian Package??????????? RPM ????? +RPM DPKG ΪߡRPM ȫΪ Redhat Package Manager Red Hat ˾ƶʵʩ GNU ԴϵͳܲΪܶ Linux ϵͳ (RHEL) ļȶ׼ RPM оǻ Debian ϵͳ (UBUNTU) DEB ߣ DPKGȫΪ Debian Packageܷ RPM ơ -YUM ???? RPM ?????????????????????????????????????????????????? RPM ???????????????????????????????????????????????????????????????????????????????????????????????????YUM ??????????????????????????????????? +YUM RPM ߣָܹԴռ䣨Ŀ¼ȣԶĿ RPM ҰװܣԶϵءװ뷱ֶءװÿһҪ⣬YUM һǽϵͳ -## ???????ѷ +## а汾 -Linux ?????????????? Linux ???????????????? +Linux аԤȼɺõ Linux ں˼Ӧ -**???? DPKG** +** DPKG** -??????? +ҵа - Ubuntu -???????? +а - Debian -**???? RPM** +** RPM** -??????? +ҵа - Red Hat -???????? +а - Fedora - CentOS -# ???? +# -## ?????????? +## ̵ļ -Linux ???????????????????????? +Linux ÿӲһļ -???????????????? +̵ļ -- SCSI/SATA/USB ?????/dev/sd[a-p] -- IDE ?????/dev/hd[a-d] +- SCSI/SATA/USB ̣/dev/sd[a-p] +- IDE ̣/dev/hd[a-d] -?????????????????????????????????????????????????????????? +ļŵȷ̲˳йأIJλ޹ء -## ?????? +## -?????????????????????????????????? MBR ???????????????????????? GPT ?????? +̷Ҫָʽһƽ϶ MBR һǽƽٵ GPT ### 1. MBR -MBR ????????????????????????????????????Master boot record, MBR??????????partition table???????? MBR ? 446 bytes??partition table ? 64 bytes?? +MBR УһҪУҪ¼Master boot record, MBRpartition table MBR ռ 446 bytespartition table ռ 64 bytes -????????? 64 bytes???????? 4 ?????????? 4 ???????????????Primary?????????????Extended????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ֻ 64 bytesֻܴ洢 4 4 ΪPrimaryչExtendedչֻһռ¼Լ¼ķͨչԷֳ֣ЩΪ߼ -Linux ???????????????????????????????????????????+???????? /dev/sda1????????????????? 5 ????? +Linux ҲѷļļʽΪļ+ţ /dev/sda1ע⣬߼ıŴ 5 ʼ ### 2. GPT -????????????????????????? 512bytes ?????????? 4k??GPT ??????????????????????????????????????Logical Block Address, LBA???? +ͬĴвͬС 512bytes ´̵ 4kGPT Ϊ˼д̣ڶʹ߼ַLogical Block Address, LBA -GPT ?? 1 ?????????? MBR?????????? 33 ????????????????????????? 33 ???????????????????????? +GPT 1 ¼ MBR 33 ¼Ϣ 33 ڶԷϢбݡ -GPT ????????????????????????????????? 128 ???????? +GPT ûչԷ 128 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a5c25452-6fa5-49e7-9322-823077442775.jpg) -## ?????????? +## ### 1. BIOS -BIOS ??????????????????????????????????????????????????????????????????? MBR???? MBR ??????????????????????????????????????????????????? +BIOS ǿʱִеĵһ֪ԿĴ̣ȡ̵һ MBR MBR ִеĿĻزϵͳĺļ -MBR ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? MBR ???????????????????????????????????????????????????????????????????????????????? +MBR еĿṩ¹ܣѡļԼתתܿʵ˶ֻҪһϵͳĿװϣ MBR еĿʱͿѡǰIJϵͳתӶһϵͳ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f900f266-a323-42b2-bc43-218fdb8811a8.jpg) -???????????????????? Windows ???? Linux???????? Windows ?????? MBR???? Linux ???????????????????? MBR ??????????????????????????????????????????????????? +װȰװ Windows ٰװ LinuxΪװ Windows ʱḲǵ MBR Linux ѡ񽫿װ MBR ҿÿѡ ### 2. UEFI -UEFI ????? BIOS ???????????????????? +UEFI BIOS ˵ܸΪȫ棬ҲΪȫ -## ???? +## -????????????????????????????????????????????????????????? +Ŀ¼ΪĽ㣬Ҳ˵Ŀ¼֮ͿԶȡݡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/249f3bb1-feee-4805-a259-a72699d638ca.jpg) -# ?????????????? +# ļȨĿ¼ -## ????????? +## ļȨ޸ -???????????????????????????????????????????????????????? +ûΪ֣ļӵߡȺԼˣԲͬûвͬļȨޡ -??? ls ????????????????????????????????? drwxr-xr-x. 3 root root 17 May 6 00:14 .config???????????????????? +ʹ ls 鿴һļʱһļϢ drwxr-xr-x. 3 root root 17 May 6 00:14 .configݵĽ£ -- drwxr-xr-x?????????????????? 1 ????????????? 9 ?????????? -- 3?????????? -- root??????????? -- root???????? -- 17????????? -- May 6 00:14??????????????? -- .config????????? +- drwxr-xr-xļԼȨޣ 1 λΪļֶΣ 9 λΪļȨֶΡ +- 3 +- rootļӵߣ +- rootȺ飻 +- 17ļС +- May 6 00:14ļ޸ĵʱ䣻 +- .configļ -??????????????????? +ļͼ京У -- d?????? -- -??????? -- l??????????? +- dĿ¼ +- -ļ +- lļ -9 ?????????????? 3 ??????? 3 ???????????????????????????????????????????????????? 3 ???? r??w??x ????????????????????? +9 λļȨֶУÿ 3 Ϊһ飬 3 飬ÿһֱļӵߡȺԼ˵ļȨޡһȨе 3 λֱΪ rwx ȨޣʾɶдִС -## ????????????????? +## ļԼȨ޵޸ -### 1. ????????????? +### 1. ޸ļȺ ```html # chgrp [-R] groupname dirname/filename --R???????? +-Rݹ޸ ``` -### 2. ??????????? +### 2. ޸ļӵ -????????????????????????????????????? +޸ļӵߣҲ޸ļȺ顣 ```html -# chown [-R] ?????:????? dirname/filename +# chown [-R] û:Ⱥ dirname/filename ``` -### 3. ?????? +### 3. ޸Ȩ -?????????????????????????????????? 3 ????????????????????????????????? 4??2??1????????????????????? r??4??w??2??x??1?? +ԽһȨʾʱһȨ޵ 3 λֵλÿλȨֵΪ 421ÿȨ޶ӦȨֵΪ r4w2x1 ```html # chmod [-R] xyz dirname/filename ``` -???????? .bashrc ???????????? -rwxr-xr--?? + .bashrc ļȨ޸Ϊ -rwxr-xr-- ```html # chmod 754 .bashrc ``` -?????????????څ???? +Ҳʹ÷趨Ȩޡ ```html # chmod [ugoa] [+-=] [rwx] dirname/filename -- u??????? -- g????????? -- o???????? -- a???????? -- +???????? -- -???????? -- =???څ??? +- uӵ +- gȺ +- o +- a +- +Ȩ +- -ƳȨ +- =趨Ȩ ``` -??????? .bashrc ??????????????????? +Ϊ .bashrc ļûдȨޡ ```html # chmod a+w .bashrc ``` -## ??????? +## Ŀ¼Ȩ -?????????????????????????????????????????????????????????? w ?????????????????????? +ļǴ洢һļУǴ洢һļڵĿ¼Сˣӵļ w Ȩ޲ܶļ޸ġ -???????????????????????????????????????????? r ????????????????w ????????????????????????????????????????????????????????x ??????????????????????x ????? r ?? w ?????????????????????????????????????????????????????????????????? +Ŀ¼洢ļбһĿ¼ȨҲǶļбȨޡˣĿ¼ r ȨޱʾԶȡļбw Ȩޱʾ޸ļб˵ɾļļ޸ģx Ȩ޿øĿ¼ΪĿ¼x Ȩ r w Ȩ޵ĻʹһĿ¼ΪĿ¼Ҳû취ȡļбԼļб޸ˡ -## ????????? +## ļĬȨ -?????????????????????????????? 666 ??????? -rw-rw-rw- ?? -????????????????????????????????????????????? 777 ??????? drwxrwxrwx?? +ļĬȨޣļĬûпִȨޣΪ 666 Ҳ -rw-rw-rw- +Ŀ¼ĬȨޣĿ¼Ҫܹ룬ҲDZӵпִȨޣΪ 777 Ҳ drwxrwxrwx -??????? umask ???????????????????????????????????????????? 002 ??????????????????????? 2 ??????????????????????????????????? -rw-rw-r-- ?? +ͨ umask û߲鿴ļĬȨޣͨʽʾ 002 ʾûȨȥһ 2 ȨޣҲдȨޣ˽ļʱĬϵȨΪ -rw-rw-r-- -## ?????? +## Ŀ¼ -??????? Linux ???ѷ????????????????Filesystem Hierarchy Standard (FHS) ??? Linux ?????????????????????????? +Ϊʹͬ Linux а汾Ŀ¼ṹһԣFilesystem Hierarchy Standard (FHS) 涨 Linux Ŀ¼ṹĿ¼£ -- / (root, ????) -- /usr (unix software resource)???????????????????????????? -- /var (variable)????????????????????????????? +- / (root, Ŀ¼) +- /usr (unix software resource)ϵͳĬᰲװĿ¼ +- /var (variable)ϵͳйеļ -??????????????? +Ŀ¼£ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/27ace615-558f-4dfb-8ad4-7ac769c10118.jpg) -# ??????? +# ļĿ¼ -## ?????? +## ļʱ -1. modification time (mtime)???????????????????? -2. status time (ctime)?????????????????????????????? -3. access time (atime)???????????????? +1. modification time (mtime)ļݸ¾ͻ£ +2. status time (ctime)ļ״̬Ȩޡԣ¾ͻ£ +3. access time (atime)ȡļʱͻ¡ -## ???????????????? +## ļĿ¼Ļ ### 1. ls -?????????????????????????????????????????? +гļĿ¼ϢĿ¼Ϣаļ ```html # ls [-aAdfFhilnrRSt] file|dir --a ???????????? --d ???????????? --l ???????????????????????????????????????? +-a гȫļ +-d гĿ¼ +-l ԳݴгļȨ޵ȵ ``` ### 2. cp -????????? +Ʋ -?????????????????????????????????????? +ԴļϣĿļһҪĿ¼С ```html cp [-adfilprsu] source destination --a ?????? -dr --preserve=all ??????????? dr ?????????? --d ????????????????????????????????????????????? --i ????????????????????????????????? --p ????????????????????? --r ???????????? --u ??destination ?? source ?????? destination???? destination ???????????????? ---preserve=all ?????? -p ??????????????????? SELinux ??????, links, xattr ????????? +-a ൱ -dr --preserve=all ˼ dr ο˵ +-d ԴļΪļļԶļ +-i ĿļѾʱڸǰѯ +-p ͬļһƹȥ +-r ݹ +-u destination source ɲŸ destination destination ڵ²Ÿ +--preserve=all -p Ȩز⣬ SELinux , links, xattr Ҳ ``` ### 3. rm -????????? +Ƴ ```html -# rm [-fir] ??????? --r ???????? +# rm [-fir] ļĿ¼ +-r ݹɾ ``` ### 4. mv -????????? +ƶ ```html # mv [-fiu] source destination # mv [options] source1 source2 source3 .... directory --f ?? force ???????????????????????????????????????? +-f force ǿƵ˼ĿļѾڣѯʶֱӸ ``` -## ?????????? +## ȡļ ### 1. cat -??????????? +ȡļݡ ```html # cat [-AbEnTv] filename --n ?????????? ?????????????????? -b ?????? +-n ӡкţ ͬհҲкţ -b ѡͬ ``` ### 2. tac -?? cat ???????????????????????? + cat ķһпʼӡ ### 3. more -???????????????????????????????? +һҳһҳ鿴ļݣı༭ơ ### 4. less -?? more ????? + more ơ ### 5. head -??????????????? +ȡļǰС ```html # head [-n number] filename --n ?????????????????????????? +-n ֣ʾе˼ ``` ### 6. tail -?? head ????????????????????? + head ķֻȡǺС ### 7. od -?????????????????????????????????????? +ַʮƵʽʾļ ### 8. touch -?????????????????????? +޸ļʱ߽ļ ```html # touch [-acdmt] filename --a ?? ???? atime --c ?? ???? ctime????????????????????????? --m ?? ???? mtime --d ?? ????????????????????????????????????????? --date="????????" --t ????????????????????????????????????[YYYYMMDDhhmm] +-a atime +-c ctimeļ򲻽ļ +-m mtime +-d ԽµڶĿǰڣҲʹ --date="ڻʱ" +-t ԽµʱĿǰʱ䣬ʽΪ[YYYYMMDDhhmm] ``` -## ???????????? +## ָļ ### 1. which -????????? +ָ ```html # which [-a] command --a ??????????????????????????? +-a ָгֻеһ ``` ### 2. whereis -whereis ?????????????????????????????????????? +whereis ļٶȱȽϿ죬ΪֻضĿ¼ ```html # whereis [-bmsu] dirname/filename @@ -496,114 +496,114 @@ whereis ?????????????????????????????????????? ### 3. locate -locate ????????????????????????????? +locate ùؼֻʽ -locate ??? /var/lib/mlocate/ ???????????????????????????????????????????????????? locate ????????????????????? updatedb ??????????????? +locate ʹ /var/lib/mlocate/ ݿ洢ڴУÿһΣ޷ locate ½ļʹ updatedb ݿ⡣ ```html # locate [-ir] keyword --r??????????? +-rʽ ``` ### 4. find -find ????????????????????????????? +find ʹļԺȨ޽ ```html # find filename [option] ``` -#### 4.1 ??????????? +#### 4.1 ʱйصѡ ```html --mtime n ?????? n ?????????????????????? --mtime +n ?????? n ????(???? n ????)???????????? --mtime -n ?????? n ?????(?? n ????)???????????? --newer file ?? ???? file ???????? +-mtime n г n ǰһ޸Ĺݵļ +-mtime +n г n ֮ǰ( n 챾)޸Ĺݵļ +-mtime -n г n ֮( n 챾)޸Ĺݵļ +-newer file г file µļ ``` -+4??4 ?? -4 ?????????????? ++44 -4 ָʾʱ䷶Χ£ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/658fc5e7-79c0-4247-9445-d69bf194c539.png) -#### 4.2 ???????????????????????? +#### 4.2 ļӵߺȺйصѡ ```html -uid n -gid n -user name -group name --nouser ???????????????? /etc/passwd ????? --nogroup?????????????????? /etc/group ????? +-nouser ӵ߲ /etc/passwd ļ +-nogroupȺ鲻 /etc/group ļ ``` -#### 4.3 ??????????????????? +#### 4.3 ļȨ޺йصѡ ```html -name filename --size [+-]SIZE??????? SIZE ?????(+)??(-)?????????? SIZE ??????c: ???? byte??k: ???? 1024bytes??????????? 50KB??????????????? -size +50k +-size [+-]SIZEѰ SIZE Ҫ(+)С(-)ļ SIZE ĹУc: bytek: 1024bytesԣҪұ 50KBҪļ -size +50k -type TYPE --perm mode ???????????? mode ????? --perm -mode ???????????? mode ????? --perm /mode ??????????????? mode ????? +-perm mode Ȩ޵ mode ļ +-perm -mode Ȩް mode ļ +-perm /mode Ȩްһ mode ļ ``` -# ??????????? +# ļϵͳ -## ?????????? +## ļϵͳ -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ԷиʽΪڷϽļϵͳһֻܸͨʽΪһļϵͳǴеȼԽһʽΪļϵͳֻļϵͳܱأܱء -??????????????????? +ļϵͳṹ -1. superblock????????????????????????? inode ?? block ????????????????????????????????????????????? -2. inode?????????????? inode???????????????????????????????????? block ???? -3. block????????????????????????????????? block?? +1. superblock¼ļϵͳϢ inode block ʹʣԼļϵͳĸʽϢȣ +2. inodeһļռһ inode¼ļԣͬʱ¼ļڵ block 룻 +3. block¼ļݣļ̫ʱռö block -????????????????????????? inode ??????????????????????? block?????????? block ????????????? +Ҫȡһļʱ inode ȥļڵ blockȻ block ݶ -????????????????????????? block ???????? +Ƭָһļڵ block ڷɢ -Ext2 ???????????????????????????????????? block ????????????????????????????? block ????????? +Ext2 ļϵͳʹļṹڴ֮ϼ block ȺĸҲǽһļϵͳΪ block Ⱥ飬 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1974a836-aa6b-4fb8-bce1-6eb11969284a.jpg) ## inode -Ext2 ????????? block ???? 1k??2k ?? 4k ?????????? block ??????????????????????? inode ??????? 128 bytes?? +Ext2 ļϵͳֵ֧ block С 1k2k 4k ֣ͬ block С˵һļĴСÿ inode Сǹ̶Ϊ 128 bytes -inode ????????????????? block????????? block ????????????????????????? block??????? inode ?????????????????????? block?????????????????????????????????? block ???????????????? block ?????????????? block?? +inode м¼ļڵ blockÿ block dzСһļ㶼Ҫʮ blockһ inode Сޣ޷ֱô block˼ӡ˫ӡãʹ block СҲ block ¼ļ block ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/89091427-7b2b-4923-aff6-44681319a8aa.jpg) -inode ???????????????? +inode Ϣ -- ???????????(read/write/excute)?? -- ?????????????????(owner/group)?? -- ????????????? -- ????????????????????(ctime)?? -- ???????????(atime)?? -- ??????????(mtime)?? -- ???????????????(flag)???? SetUID...?? -- ????????????????? (pointer)?? +- ļĴȡģʽ(read/write/excute) +- ļӵȺ(owner/group) +- ļ +- ļ״̬ıʱ(ctime) +- һεĶȡʱ(atime) +- ޸ĵʱ(mtime) +- ļԵ(flag) SetUID... +- ļݵָ (pointer) -## ???? inode ?? block +## Ŀ¼ inode block -???????????????????? inode ????????? block??block ???????????????????????? inode ????????????????????????? inode ????????????????????????????????????????????????????????????????????? w ?????? +һĿ¼ʱһ inode һ blockblock ¼Ŀ¼ļ inode ԼļԿļ inode ¼ļļ¼Ŀ¼УļɾļļЩĿ¼ w Ȩйء -## ???????????????? +## ʵ ```html # ln [-sf] source_filename dist_filename --s ??????? hard link???? -s ? symbolic link --f ????????5????????????????????? +-s Ĭ hard link -s Ϊ symbolic link +-f Ŀ5ļʱɾĿļ ``` -### 1. ??????? +### 1. ʵ -hard link ?????????????????????????????????????????????? inode ?????????????????????????????????????????? 0?? +hard link ֻijĿ¼һĿʹĿӵļ inode ϡɾһĿļǴڣֻҪΪ 0 -????????????????? Filesystem?????????????????? +ƣܿԽ FilesystemܶĿ¼ӡ ```html # ln /etc/crontab . @@ -612,11 +612,11 @@ hard link ?????????????????????????????????????????????? inode ????????????????? 34474855 -rw-r--r--. 2 root root 451 Jun 10 2014 /etc/crontab ``` -### 2. ???????? +### 2. -symbolic link ???????? Windows ???????????????????????????????????????????????????????????????????????????????????????? +symbolic link Ϊ Windows ĿݷʽͨһļļݵĶȡָӵǸļԴļɾˣļʹ򲻿ˡ -symbolic link ???????????????? +symbolic link ΪĿ¼ӡ ```html # ll -i /etc/crontab /root/crontab2 @@ -624,118 +624,118 @@ symbolic link ???????????????? 53745909 lrwxrwxrwx. 1 root root 12 Jun 23 22:31 /root/crontab2 -> /etc/crontab ``` -# ??????? +# ѹ -## ??? +## ѹ -Linux ????????????????????????????????? +Linux кܶѹļչ£ -| ????? | ??????? | +| չ | ѹ | | -- | -- | | \*.Z | compress | |\*.zip | zip | |\*.gz | gzip| |\*.bz2 | bzip2 | |\*.xz | xz | -|\*.tar | tar ????????????????????? | -|\*.tar.gz | tar ????????????????? gzip ????? | -|\*.tar.bz2 | tar ????????????????? bzip2 ????? | -|\*.tar.xz | tar ????????????????? xz ????? | +|\*.tar | tar ݣûоѹ | +|\*.tar.gz | tar ļ gzip ѹ | +|\*.tar.bz2 | tar ļ bzip2 ѹ | +|\*.tar.xz | tar ļ xz ѹ | ### 1. gzip -gzip ?? Linux ?????????????????? compress??zip ?? gzip ???????????? +gzip Linux ʹѹָԽ⿪ compresszip gzip ѹļ -???? gzip ????????????????????? + gzip ѹԴļͲˡ -?? 9 ???????????????????? + 9 ͬѹȼʹá -??????? zcat??zmore??zless ?????????????????? +ʹ zcatzmorezless ȡѹļݡ ```html $ gzip [-cdtv#] filename --c ??????????????????????? --d ??????? --t ?????????????????? --v ?????????????? --# ?? # ???????????????????????????????????????????6 +-c ѹĻ +-d ѹ +-t ѹļǷ +-v ʾѹȵϢ +-# # Ϊֵ˼ѹȼԽѹԽߣĬΪ6 ``` ### 2. bzip2 -???? gzip ??????????? +ṩ gzip ߵѹȡ -??????bzcat??bzmore??bzless??bzgrep?? +鿴bzcatbzmorebzlessbzgrep ```html $ bzip2 [-cdkzv#] filename --k ?????????? +-k Դļ ``` ### 3. xz -???? bzip2 ??????????? +ṩ bzip2 ѵѹȡ -?????????gzip??bzip2??xz ?????????????????????????????????????????????? +Կgzipbzip2xz ѹȲŻҪע⣬ѹԽߣѹʱҲԽ -??????xzcat??xzmore??xzless??xzgrep?? +鿴xzcatxzmorexzlessxzgrep ```html $ xz [-dtlkc#] filename ``` -## ??? +## -????????????????????????????????????????????????????????tar ???????????????????????? gip??bzip2??xz ????????????????? +ѹָֻܶһļѹܹļһļtar ڴҲʹ gipbzip2xz ļѹ ```html -$ tar [-z|-j|-J] [cv] [-f ?????tar???] filename... ==?????? -$ tar [-z|-j|-J] [tv] [-f ????tar???] ==?? -$ tar [-z|-j|-J] [xv] [-f ????tar???] [-C ??] ==????? --z ?????zip?? --j ?????bzip2?? --J ?????xz?? --c ????????????? --t ??????????????????????? --x ?????????????????? --v ???????/?????????????????????????????? --f : filename????????????? --C ?? ?? ?????????????? +$ tar [-z|-j|-J] [cv] [-f ½tarļ] filename... ==ѹ +$ tar [-z|-j|-J] [tv] [-f еtarļ] ==鿴 +$ tar [-z|-j|-J] [xv] [-f еtarļ] [-C Ŀ¼] ==ѹ +-z ʹzip +-j ʹbzip2 +-J ʹxz +-c ½ļ +-t 鿴ļЩļ +-x ѹĹܣ +-v ѹ/ѹĹУʾڴļ +-f : filenameҪļ +-C Ŀ¼ ضĿ¼ѹ ``` -???????????? +õķʽ£ -?????? ?? tar -jcv -f filename.tar.bz2 ??????????????????? -?? ??       ?? tar -jtv -f filename.tar.bz2 -?????     ??tar -jxv -f filename.tar.bz2 -C ?????????? +ѹ tar -jcv -f filename.tar.bz2 ҪѹļĿ¼ +         tar -jtv -f filename.tar.bz2 +ѹ     tar -jxv -f filename.tar.bz2 -C ҪѹĿ¼ # BASH -??????? shell ?????????????Bash ???? shell ?????? +ͨ shell ںṩBash shell һ֡ -## Bash???? +## Bash -**1. ???????** +**1. ʷ** -??????????????????????????????????????? \~/.bash_history ????????????????????????? +¼ʹùε¼ִеʱŵڴУ \~/.bash_history ļм¼ǰһε¼ִй -**2. ????????????** +**2. ļȫ** -??????tab +ݼtab -**3. ????????** +**3. ** -???? lm ?? ls -al ??????? + lm ls -al ı **4. shell scripts** -**5. ????** +**5. ͨ** -???? ls -l /usr/bin/X\* ?? /usr/bin ?????????? X ?????????? + ls -l /usr/bin/X\* г /usr/bin X ͷļ -## ???????? +## -?????????????????? = ??????????????????????? \$ ????????? \${} ????????????????? echo ??? +һֱֵʹ = ԱȡҪڱǰ \$ Ҳ \${} ʽʹ echo  ```bash $ var=abc @@ -743,24 +743,24 @@ $ echo $var $ echo ${var} ``` -????????????????????????????????????????????????????????????????????var="lang is \$LANG"????var???? lang is zh_TW.UTF-8??????????????????????????????????????? var='lang is \$LANG'???? var ???? lang is \$LANG?? +пոҪʹ˫Żߵš˫ڵַԱԭԣvar="lang is \$LANG"varֵΪ lang is zh_TW.UTF-8ڵַַ var='lang is \$LANG' var ֵΪ lang is \$LANG -??????? \`???\` ???? \$(???) ????????????????????????????? version=\$(uname -r)???? version ???? 3.10.0-229.el7.x86_64?? +ʹ \`ָ\` \$(ָ) ķʽִָнֵ version=\$(uname -r) version ֵΪ 3.10.0-229.el7.x86_64 -??????? export ??????????????????????????????????????????????????????????????? Bash ?????????? Bash?? +ʹ export Զתɻӳʹãνӳɵǰ Bash Bash -Bash ??????????????????????????????????????????????????????????????????????????????????????????? declare ???? +Bash ıΪ֡עûиĬַ͡ʹ declare  ```html $ declare [-aixr] variable --a ?? ????????????? --i ?? ????????????? --x ?? ????????????? --r ?? ?????readonly???? +-a Ϊ +-i Ϊ +-x Ϊ +-r Ϊreadonly ``` -??? [ ] ??????????????? +ʹ [ ] в ```bash $ array[1]=a @@ -768,53 +768,53 @@ $ array[2]=b $ echo ${array[1]} ``` -## ?????????? +## ָ˳ -1. ??????????????????????? /bin/ls ???? ./ls ?? -2. ?????????????????? -3. ?? Bash ????????????? -4. ?? \$PATH ???????????????????????????????????? +1. ԾԻ·ִָ /bin/ls ./ls +2. ɱҵִָУ +3. Bash ڽִָУ +4. \$PATH ָ·˳ҵһִָС -## ??????????? +## ض -??????????????????????????????????????????? +ضʹļ׼롢׼ͱ׼ -1. ??????? (stdin)      ??????? 0 ????? < ?? << ?? -2. ?????? (stdout)    ??????? 1 ????? > ?? >> ?? -3. ??????????(stderr)??????? 2 ????? 2> ?? 2>> ?? +1. ׼ (stdin)      Ϊ 0 ʹ < << +2. ׼ (stdout)    Ϊ 1 ʹ > >> +3. ׼(stderr)Ϊ 2 ʹ 2> 2>> -???????????????????????????????????????????????????????? +УһͷıʾԸǵķʽض򣬶ͷıʾ׷ӵķʽض -??????????????????????????????????? /dev/null ??????????????? +ԽҪı׼Լ׼ض /dev/null ൱ӽ䡣 -??????????????????????????????????????????????????????????????????????? 2>&1 ??????????????????????????? +Ҫ׼Լ׼ͬʱضһļҪijתΪһ 2>&1 ʾ׼תΪ׼ ```bash $ find /home -name .bashrc > list 2>&1 ``` -## ??????? +## ָ -?????????????????????????????????????????????????????????????????????????????????????????????????????????? | ?????????????? +ǽһı׼Ϊһı׼룬ҪĴ֮ܵõҪĸʽʱͿʹùߡ֮ʹ | ָ ```bash $ ls -al /etc | less ``` -### 1. ??????cut +### 1. ȡָcut -???????????????? +ȡһһеؽС -cut ???????????????????????? +cut ݽз֣ȡҪIJ֡ ```html $ cut --d ??????? --f ?????? -d ???????? -f n ????? n ?????? --c ????????????????? +-d ָ +-f -d ָʹ -f n ȡ n +-c ַΪλȡ ``` -????1??last ???????????????????????????????? +1last ʾĵߵϢҪʾû ```html $ last @@ -825,7 +825,7 @@ root pts/1 192.168.201.254 Thu Feb 5 22:37 - 23:53 (01:16) $ last | cut -d ' ' -f 1 ``` -????2???? export ????????????? 12 ?????????????????? +2 export ѶϢȡõ 12 ַԺַ ```html $ export @@ -833,28 +833,28 @@ declare -x HISTCONTROL="ignoredups" declare -x HISTSIZE="1000" declare -x HOME="/home/dmtsai" declare -x HOSTNAME="study.centos.vbird" -.....(???????)..... +.....(ʡ)..... $ export | cut -c 12 ``` -### 2. ????????sort??uniq +### 2. sortuniq -**sort** ???????? +**sort** ```html $ sort [-fbMnrtuk] [file or stdin] --f ??????? --b ?????????????? --M ????????????????????? JAN, DEC --n ????????? --r ?????????? --u ?????? unique ????????????????? --t ?????????????tab --k ?????????????? +-f ԴСд +-b ǰĿո +-M ·ݵ JAN, DEC +-n ʹ +-r +-u ൱ unique ظֻһ +-t ָĬΪtab +-k ָ ``` -??????/etc/passwd ???????? : ????????????????????? +/etc/passwd : ָģԵ ```html $ cat /etc/passwd | sort -t ':' -k 3 @@ -864,15 +864,15 @@ alex:x:1001:1002::/home/alex:/bin/bash arod:x:1002:1003::/home/arod:/bin/bash ``` -**uniq** ????????????????????? +**uniq** Խظֻȡһ ```html $ uniq [-ic] --i ??????? --c ???????? +-i ԴСд +-c м ``` -?????????????????????? +ȡÿ˵ĵ¼ܴ ```html $ last | cut -d ' ' -f 1 | sort | uniq -c @@ -884,88 +884,88 @@ $ last | cut -d ' ' -f 1 | sort | uniq -c 1 wtmp ``` -### 3. ???????????tee +### 3. ˫ضtee -???????????????????????????? **tee** ??????????????????????????????????????????????? tee ????????????????????????????? +ضὫضļУ **tee** ܹܣܱĻϵҲ˵ʹ tee ָһͬʱ͵ļĻϡ ```html $ tee [-a] file ``` -### 4. ?????????tr??col??expand??join??paste +### 4. ַתָtrcolexpandjoinpaste - **tr** ??????????????????????????????I?? + **tr** ɾһеַ߶ַ滻 ```html $ tr [-ds] SET1 ... --d ?? ??????? SET1 ???????? +-d ɾ SET1 ַ ``` -???????? last ???????????????????? + last ϢСдתΪд ```html $ last | tr '[a-z]' '[A-Z]' ``` - **col** ?? tab ????????????? + **col** tab ַתΪոַ ```html $ col [-xb] --x ?? ?? tab ????????????? +-x tab תɶԵȵĿո ``` -**expand** ?? tab ??????????????????? 8 ???? +**expand** tab תһĿոĬ 8 ```html $ expand [-t] file --t ??tab ?????????? +-t tab תΪո ``` -**join** ??????????????????????? +**join** ͬݵһкϲһ ```html $ join [-ti12] file1 file2 --t ???????????????? --i ???????????? --1 ???????????????????? --2 ???????????????????? +-t ָĬΪո +-i ԴСдIJ +-1 һļõıȽֶ +-2 ڶļõıȽֶ ``` -**paste** ???????????????? +**paste** ֱӽճһ ```html $ paste [-d] file1 file2 --d ????????????? tab +-d ָĬΪ tab ``` -### 5. ???????split +### 5. ָsplit -**split** ???????????????????? +**split** һļֳɶļ ```html $ split [-bl] file PREFIX --b ????????????????????????? b, k, m ?? --l ?????????????????? -- PREFIX ?????????????????? +-b ԴСзɼӵλ b, k, m +-l з +- PREFIX ļǰ ``` -# ?????????????????????? +# ʾļʽ ## grep -??????????????????????????? +ʹʾʽƥȡ ```html -$ grep [-acinv] [--color=auto] ???????? filename --a ?? ?? binary ????? text ?????????????? --c ?? ??????????? --i ?? ????? --n ?? ????? --v ?? ????????????????? ???????? ?????????? ---color=auto ?????????????????? +$ grep [-acinv] [--color=auto] Ѱַ filename +-a binary ļ text ļķʽѰ +-c ҵ +-i ԴСд +-n к +-v ѡ༴ʾû Ѱַ ݵһ +--color=auto ҵĹؼּɫʾ ``` -??????????? the ??????????????????????????? --color=auto ???????????????? Linux ?????????? the ??????? +Ѻ the ַȡעĬϻ --color=auto ѡ Linux ɫʾ the ַ ```html $ grep -n 'the' regular_express.txt @@ -976,19 +976,19 @@ $ grep -n 'the' regular_express.txt 18:google is the best tools for search keyword ``` -??? { ?? } ??????? shell ???????????????????????????????????????^ +Ϊ { } ķ shell ģ˱Ҫʹʹתַת塣 ```html $ grep -n 'go\{2,5\}g' regular_express.txt ``` -?????????????? [???????](https://github.com/00000H/notes/blob/master/notes/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.md). +ʽο [ʽ](https://github.com/00000H/notes/blob/master/notes/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.md). ## printf -????????????? +ڸʽ -????????????????? printf ????????????? $( ) ????? +ڹܵڸ printf ʱҪʹ $( ) ʽ ```html $ printf '%10s %5i %5i %5i %8.2f \n' $(cat printf.txt) @@ -1000,12 +1000,12 @@ $ printf '%10s %5i %5i %5i %8.2f \n' $(cat printf.txt) ## awk ```html -$ awk '???????? 1{???? 1} ???????? 2{???? 2} ...' filename +$ awk ' 1{ 1} 2{ 2} ...' filename ``` -awk ??????????????????????????????????????\$n??n ??????? 1 ?????\$0 ???????? +awk ÿδһУСλֶΣÿֶεʽΪ\$nn Ϊֶκţ 1 ʼ\$0 ʾһС -???? 1???????????????????? ip + 1ȡ¼ûû ip ```html $ last -n 5 @@ -1018,15 +1018,15 @@ dmtsai tty1 Fri May 29 11:55 - 12:11 (00:15) $ last -n 5 | awk '{print $1 "\t" $3} ``` -awk ?????? +awk -| ???????? | ???????? | +| | | | -- | -- | -| NF | ?????????????? | -| NR | ??????????????????? | -| FS | ??????????????????? | +| NF | ÿһӵеֶ | +| NR | Ŀǰǵڼ | +| FS | ĿǰķַָĬǿո | -???? 2???????????????????????????????? + 2ڴкţʾÿһжֶ ```html $ last -n 5 | awk '{print $1 "\t lines: " NR "\t columns: " NF}' @@ -1037,9 +1037,9 @@ dmtsai lines: 4 columns: 10 dmtsai lines: 5 columns: 9 ``` -??????????????????????????? ==?? +ʹôڵ߼еʹ == -???? 3??/etc/passwd ????????????? UID???? UID ?? 10 ??????????? + 3/etc/passwd ļֶΪ UID UID С 10 ݽд ```text cat /etc/passwd | awk 'BEGIN {FS=":"} $3 < 10 {print $1 "\t " $3}' @@ -1048,24 +1048,24 @@ bin 1 daemon 2 ``` -# vim ?????? +# vim ģʽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/341c632a-1fc1-4068-9b9f-bf7ef68ebb4c.jpg) -????????????????????????????????????? +ָģʽ£뿪ߴ洢ļ -| ???? | ???? | +| | | | -- | -- | -| :w | ?????| -| :w! | ?????????????????????????????????????????????????? | -| :q | ??| -| :q! | ???????????| -| :wq | ????????| -| :wq!| ???????????| +| :w | д| +| :w! | ļΪֻʱǿд̡ܲд룬ûԸļȨй | +| :q | 뿪| +| :q! | ǿ뿪| +| :wq | д̺뿪| +| :wq!| ǿд̺뿪| -# ?????? +# ο -- ???. ?? ?? ?? Linux ? ?? ?? ?? ?? ? ?? ?? ??[J]. 2009. -- [Linux ??????????????](https://www.ibm.com/developerworks/cn/linux/l-cn-rpmdpkg/index.html) +- . Linux ˽ ƪ [J]. 2009. +- [Linux ƽ̨ϵ](https://www.ibm.com/developerworks/cn/linux/l-cn-rpmdpkg/index.html) diff --git a/notes/MySQL.md b/notes/MySQL.md index 13500542..4a86f910 100644 --- a/notes/MySQL.md +++ b/notes/MySQL.md @@ -1,226 +1,226 @@ -* [?????](#?????) +* [洢](#洢) * [1. InnoDB](#1-innodb) * [2. MyISAM](#2-myisam) - * [3. InnoDB ?? MyISAM ????](#3-innodb-??-myisam-????) -* [????????](#????????) - * [1. ????](#1-????) - * [2. ??????](#2-??????) - * [3. ?????](#3-?????) - * [4. ????????](#4-????????) -* [????](#????) - * [1. ????????](#1-????????) - * [1.1 B-Tree ????](#11-b-tree-????) - * [1.2 ???????](#12-???????) - * [1.3. ????????????R-Tree??](#13-????????????r-tree??) - * [1.4 ???????](#14-???????) - * [2. ?????????](#2-?????????) - * [3. ???????](#3-???????) - * [3.1 ????????](#31-????????) - * [3.2 ??????](#32-??????) - * [3.3 ????????](#33-????????) - * [3.4 ?????????](#34-?????????) - * [3.5 ???????](#35-???????) - * [3.6 ????????](#36-????????) - * [4. B-Tree ?? B+Tree ???](#4-b-tree-??-b+tree-???) + * [3. InnoDB MyISAM ıȽ](#3-innodb--myisam-ıȽ) +* [](#) + * [1. ](#1-) + * [2. ](#2-) + * [3. ַ](#3-ַ) + * [4. ʱ](#4-ʱ) +* [](#) + * [1. ](#1-) + * [1.1 B-Tree ](#11-b-tree-) + * [1.2 ϣ](#12-ϣ) + * [1.3. ռݣR-Tree](#13-ռr-tree) + * [1.4 ȫ](#14-ȫ) + * [2. ŵ](#2-ŵ) + * [3. Ż](#3-Ż) + * [3.1 ](#31-) + * [3.2 ǰ׺](#32-ǰ׺) + * [3.3 ](#33-) + * [3.4 е˳](#34-е˳) + * [3.5 ۴](#35-۴) + * [3.6 ](#36-) + * [4. B-Tree B+Tree ԭ](#4-b-tree--b+tree-ԭ) * [4. 1 B-Tree](#4-1-b-tree) * [4.2 B+Tree](#42-b+tree) - * [4.3 ?????????????? B+Tree](#43-??????????????-b+tree) - * [4.4 ?????? B-Tree ?? B+Tree](#44-??????-b-tree-??-b+tree) -* [??????????](#??????????) + * [4.3 ˳ָ B+Tree](#43-˳ָ-b+tree) + * [4.4 Ϊʲôʹ B-Tree B+Tree](#44-Ϊʲôʹ-b-tree--b+tree) +* [ѯŻ](#ѯŻ) * [1. Explain](#1-explain) - * [2. ??????????](#2-??????????) - * [3. ??????????](#3-??????????) - * [4. ????? DELETE ?? INSERT ???](#4-?????-delete-??-insert-???) -* [???????](#???????) -* [?????????????](#?????????????) - * [1. ???????](#1-???????) - * [2. ??????](#2-??????) -* [??????](#??????) + * [2. ٷص](#2-ٷص) + * [3. ٷص](#3-ٷص) + * [4. ִ DELETE INSERT ](#4-ִ-delete--insert-) +* [ֱֿ](#ֱֿ) +* [תƺ͹ϻָ](#תƺ͹ϻָ) + * [1. ת](#1-ת) + * [2. ϻָ](#2-ϻָ) +* [ο](#ο) -# ????? +# 洢 ## 1. InnoDB -InnoDB ?? MySQL ?????????????????????? InnoDB ????????????????????????????? +InnoDB MySQL Ĭ棬ֻҪ InnoDB ֵ֧ʱſʹ洢档 -???? MVCC ????????????????????????????????????????????????? + MVCC ָ֧߲ʵĸ׼ĸ뼶Ĭϼǿظ -?????????????????????????????????????????????? +ǻھ۴ģIJѯкܸߵ -??????????????????????????????????????????????????????????? hash ?????????????????????????????????????????????????????????? +ڲ˺ܶŻӴ̶ȡʱõĿԤԶܹԶڴд hash ԼٶӦϣԼܹٲIJ뻺ȡ -??????????????????????????? +ͨһЩƺ͹֧ȱݡ ## 2. MyISAM -MyISAM ??????????????????????????????????????GIS??????? MyISAM ???????????????????????????????????? +MyISAM ṩ˴ԣȫѹռ亯GISȡ MyISAM ֧мұ޷ȫָ -????????????????????????? +ֻܶűС -?????????????????????????????????????????????????????????????????????????????????????????? +ֹԶִм޸ǺָԼָͬܵһЩݶʧ޸Ƿdzġ -???????????????????? +԰̬߾̬С -???????? DELAY_KEY_WRITE ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ָ DELAY_KEY_WRITE ѡÿ޸ִʱ޸ĵд̣ǻдڴеļֻ߹رձʱŻὫӦдַ̡ʽԼдܣݿʱ𻵣Ҫִ޸ -??????????????????????????????????????????????????????? MyISAM ????? +ڴԺ󣬲ٽ޸IJôıʺϲ MyISAM ѹ -????????????????????????????????????????????????????? MyISAM?? +ֻݣ߱ȽС޸ȻԼʹ MyISAM -MyISAM ?????????????????????????????????????? +MyISAM Ƽ򵥣Խܸʽ洢ijЩܺܺá -## 3. InnoDB ?? MyISAM ???? +## 3. InnoDB MyISAM ıȽ -**????** +**** -InnoDB ?????????? +InnoDB ͵ġ -**????** +**** -InnoDB ????????????? +InnoDB ֧ȱݡ -**???????** +**ָ** -MyISAM ??????????????? InnoDB ?????????????????????? +MyISAM 𻵵ĸʱ InnoDB ߺܶ࣬һָٶҲ -**????** +**** -MyISAM ??????????? InnoDB ??????????? +MyISAM ֱֻ֧ InnoDB ֧м -**????????** +**** -MyISAM ???????????????????????? +MyISAM ֧ȫռ -# ???????? +# -## 1. ???? +## 1. -TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT ?????? 8, 16, 24, 64 ???????????????????? +TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT ֱʹ 8, 16, 24, 64 λ洢ռ䣬һԽСԽá -INT(11) ?????????????????????????????????????????????????????? +INT(11) еֻǹ涨˽ʾַĸڴ洢ͼ˵ûġ -## 2. ?????? +## 2. -FLOAT ?? DOUBLE ??????????DECIMAL ?????????????CPU ????????????????????? DECIMAl ??????????? DECIMAL ????????????????????????? +FLOAT DOUBLE ΪͣDECIMAL Ϊ߾С͡CPU ԭָ֧㣬Dz֧ DECIMAl ͵ļ㣬 DECIMAL ļȸҪߵĴۡ -FLOAT??DOUBLE ?? DECIMAL ??????????????? DECIMAL(18, 9) ?????? 18 ??? 9 ??????????? 9 ?????????? +FLOATDOUBLE DECIMAL ָп DECIMAL(18, 9) ʾܹ 18 λȡ 9 λ洢С֣ʣ 9 λ洢֡ -## 3. ????? +## 3. ַ -????? CHAR ?? VARCHAR ????????????????????????????? +Ҫ CHAR VARCHAR ͣһǶģһDZ䳤ġ -VARCHAR ????????????????????????????????????????????? UPDATE ???????????????????????????????????????????????????????MyISAM ???????????ě???? InnoDB ???????????????????? +VARCHAR ֱ䳤ܹʡռ䣬ΪֻҪ洢Ҫݡִ UPDATE ʱܻʹбñԭһҳɵĴСʱҪִжIJMyISAM ὫвɲͬƬδ洢 InnoDB ҪҳʹзŽҳڡ -VARCHAR ??????????????? CHAR ??????? +VARCHAR ᱣַĩβĿո񣬶 CHAR ɾ -## 4. ???????? +## 4. ʱ -MySQL ?????????????????????????DATATIME ?? TIMESTAMP?? +MySQL ṩƵʱͣDATATIME TIMESTAMP **DATATIME** -???????? 1001 ?? 9999 ????????????????????? 8 ??????? +ܹ 1001 굽 9999 ںʱ䣬Ϊ룬ʹ 8 ֽڵĴ洢ռ䡣 -??????????? +ʱ޹ء -?????????MySQL ???????????????????????? DATATIME ??????2008-01016 22:37:08???????? ANSI ???????????????????????? +Ĭ£MySQL һֿġĸʽʾ DATATIME ֵ硰2008-01016 22:37:08 ANSI ׼ںʱʾ **TIMESTAMP** -?? UNIX ?????????????? 1970 ?? 1 ?? 1 ????????????????????????????????? 4 ????????????? 1970 ?? ?? 2038 ?? + UNIX ʱͬ 1970 1 1 ҹʱ䣩ʹ 4 ֽڣֻܱʾ 1970 2038 ꡣ -?????????? +ʱйء -MySQL ???? FROM_UNIXTIME() ?????? Unxi ??????????????????? UNIX_TIMESTAMP() ?????????????? Unix ?????? +MySQL ṩ FROM_UNIXTIME() Unxi ʱתΪڣṩ UNIX_TIMESTAMP() תΪ Unix ʱ -??????????????????????? TIMESTAMP ?????????????????????? +Ĭ£ʱûָ TIMESTAMP еֵὫֵΪǰʱ䡣 -????????? TIMESTAMP????????? DATETIME ????????? +Ӧþʹ TIMESTAMPΪ DATETIME ռЧʸߡ -# ???? +# -???????????????????????????????????????????????????????????????????? +ڴ洢ʵֵģڷʵֵģԲͬ洢вͬͺʵ֡ -??????????????????????????????????? +ܹ׽ѯ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ڷdzСı󲿷¼򵥵ȫɨȽЧе͵ıͷdzЧǶش͵ıʹĴ۽֮£ҪõһֱֳּҪѯһݣһ¼һ¼ƥ䣬ʹ÷ -## 1. ???????? +## 1. -### 1.1 B-Tree ???? +### 1.1 B-Tree -B-Tree ?????????? MySQL ?????????????????? +B-Tree Ǵ MySQL 洢Ĭ͡ -???????????????????k????????????????????????????????? +ΪҪȫɨ裬ֻҪɣ˲ٶȿܶࡣ -?????????????????????????????????????B-Tree ??????????????????????????????????????????????????????????? +ָΪУйͬɼB-Tree ȫֵֵΧͼǰ׺ңмǰ׺ֻǰ׺ҡ -????????????????????????????? +ڲңͷ顣 -??????????????????????????????????????? +ǰе˳вң޷ʹ -### 1.2 ??????? +### 1.2 ϣ -???????????????????????? +ڹϣʵ֣ŵDzҷdz졣 -?? MySQL ????? Memory ?????????????????? + MySQL ֻ Memory ʽֹ֧ϣ -InnoDB ?????????????????????????????????????????????????????????????? B-Tree ???????????????????????????????? B-Tree ?????????????????????????????????? +InnoDB һĹܽСӦϣijֵʹõķdzƵʱ B-Tree ֮ٴһϣ B-Tree йϣһЩŵ㣬ٵĹϣҡ -???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ƣϣֻϣֵָ룬洢ֵֶԲʹеֵȡСڴееٶȺܿ죬Դ󲿷һӰ첢ԣ޷ڷֻ֧־ȷң޷ڲֲҺͷΧңϣͻܶ࣬ٶȻú -### 1.3. ????????????R-Tree?? +### 1.3. ռݣR-Tree -MyISAM ????????????????????????????????? +MyISAM 洢ֿ֧ռڵݴ洢 -??????????????????????????????????????????????????????????? +ռάݣЧʹάϲѯ -### 1.4 ??????? +### 1.4 ȫ -MyISAM ?????????????????????????????????????????????????????? +MyISAM 洢֧ȫڲıеĹؼʣֱӱȽеֵ -??? MATCH AGAINST????????????? WHERE?? +ʹ MATCH AGAINSTͨ WHERE -## 2. ????????? +## 2. ŵ -- ???????????????????????????? +- ˷Ҫɨ -- ?????????????????????????????? +- ʹʱ -- ????? I/O ?????? I/O?? +- I/O Ϊ˳ I/O -## 3. ??????? +## 3. Ż -### 3.1 ???????? +### 3.1 -??????????????????????????????????????????????????????????????? +ڽвѯʱвDZʽһ֣ҲǺIJ޷ʹ -?????????????????? actor_id ???????? +IJѯʹ actor_id е ```sql SELECT actor_id FROM sakila.actor WHERE actor_id + 1 = 5; ``` -### 3.2 ?????? +### 3.2 ǰ׺ -???? BLOB??TEXT ?? VARCHAR ???????????????????????????????????????? + BLOBTEXT VARCHAR ͵Уʹǰ׺ֻʼIJַ -???????????????????? **?????????** ????????????????????????????????????????????????????????? 1 ?????????????????????????????? +ǰ׺ȵѡȡҪ **ѡ** ȷظֵͼ¼ıֵѡԽߣѯЧҲԽߡֵΪ 1 ʱÿ¼ΨһӦ -### 3.3 ???????? +### 3.3 -????????????????????????????????????????????????????????????????????????????? actor_id ?? file_id ??????????????? +ҪʹöΪвѯʱʹöʹöܸáУð actor_id file_id Ϊ ```sql SELECT file_id, actor_ id FROM sakila.film_actor WhERE actor_id = 1 OR film_id = 1; ``` -### 3.4 ????????? +### 3.4 е˳ -??????????????????????????????????????? customer_id ???????? staff_id ???????????? customer_id ??????????????? +ѡǿзǰ棬ʾĽ customer_id ѡԱ staff_id ߣð customer_id зڶǰ档 ```sql SELECT COUNT(DISTINCT staff_id)/COUNT(*) AS staff_id_selectivity, @@ -235,107 +235,107 @@ customer_id_selectivity: 0.0373 COUNT(*): 16049 ``` -### 3.5 ??????? +### 3.5 ۴ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b9e9ae8c-e216-4c01-b267-a50dbeb98fa4.jpg) -????????????????????????????????????????? +۴һͣһݴ洢ʽ -??????????????????????????????????InnoDB ??????????????????? B-Tree ???????? +۴ءʾкڵļֵܵش洢һInnoDB ľ۴д B-Tree ҶҳС -???????????????????????????????????????????????????????? +Ϊ޷дͬĵطһֻһ۴ -**???** +**ŵ** -1. ??????????????????????? I/O ?????? -2. ???????????? B-Tree ??????????????? +1. ԰ݱһ𣬼 I/O +2. Ϊݱ B-Tree Уݷʸ졣 -**???** +**ȱ** -1. ?????????????????? I/O ?????????????????????????????????????????????????? -2. ??????????????????????????????????????????? -3. ????????????????????????????????????????? -4. ???????????????????????????????????????????????????????????????????? -5. ????????k????????????????????????????????????????????????????????? +1. ۴޶ I/O ܼӦõܣȫڴ棬ûҪþ۴ +2. ٶڲ˳򣬰˳ġ +3. ²ۺܸߣΪÿµжƶµλá +4. 뵽ijҳУ洢ὫҳѳҳɸУҳѻᵼ±ռøĴ̿ռ䡣 +5. бȽϡ裬ҳѵݴ洢ʱ۴ܵȫɨٶȱ -### 3.6 ???????? +### 3.6 -?????????????????????????? +Ҫѯֶεֵ -## 4. B-Tree ?? B+Tree ??? +## 4. B-Tree B+Tree ԭ ### 4. 1 B-Tree ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5ed71283-a070-4b21-85ae-f2cbfd6ba6e1.jpg) -??????? B-Tree??????????????????????????? [key, data]??key ?????????data ????????? key ???????? +Ϊ B-Treeȶһݼ¼ΪһԪ [key, data]key Ϊ¼ļdata Ϊݼ¼ key ݡ -B-Tree ??????????????????????? +B-Tree ݽṹ -- ?????????????????????????? B-Tree ??????? -- ???????? key ????????????? -- ?????????????????? key ????? keyi ?? keyi+1?????? null?????????????????? key ???? keyi ???? keyi+1?? +- ҶڵͬȣҲ˵ B-Tree ƽģ +- һڵе key ҷǵݼУ +- ijָ key ֱ keyi keyi+1ҲΪ nullָָڵ key keyi С keyi+1 -?? B-Tree ?? key ??????????????????????????????????????????????????? data????????????????????????????????????????????? null ???????????????????????? + B-Tree а key ݵ㷨dzֱۣȴӸڵжֲңҵ򷵻ضӦڵ dataӦָָĽڵݹвңֱҵڵҵ null ָ룬ǰ߲ҳɹ߲ʧܡ -???????????????????????? B-Tree ????????????????????????????????????????????????????????? B-Tree ????? +ڲɾµݼ¼ƻ B-Tree ʣڲɾʱҪһѡϲתƵȲԱ B-Tree ʡ ### 4.2 B+Tree ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/63cd5b50-d6d8-4df6-8912-ef4a1dd5ba13.jpg) -?? B-Tree ????B+Tree ?????????? + B-Tree ȣB+Tree ²ͬ㣺 -- ??????????????? 2d ?????? 2d+1?? -- ????? data???? key??????????? +- ÿڵָΪ 2d 2d+1 +- ڽڵ㲻洢 dataֻ洢 keyҶӽڵ㲻洢ָ롣 -### 4.3 ?????????????? B+Tree +### 4.3 ˳ָ B+Tree ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1ee5f0a5-b8df-43b9-95ab-c516c54ec797.jpg) -????????????????????????? B+Tree ????????? B+Tree ???????????????????????????????????????????????????????????????????????? +һݿϵͳļϵͳʹõ B+Tree ṹھ B+Tree ϽŻҶӽڵ˳ָ룬ŻĿΪʵܡ -### 4.4 ?????? B-Tree ?? B+Tree +### 4.4 Ϊʲôʹ B-Tree B+Tree -?????????????????????????????????????????????????????? B-/+Tree ??????????? +ݽṹҲʵļϵͳݿϵͳձ B-/+Tree Ϊṹ -????????????????????????????????????????????????????????????????????????????????????????????? 4k??????????????????????????? +ҳǼ洢߼飬Ӳϵͳʹ̴洢ָΪĴСȵĿ飬ÿ洢ΪһҳϵͳУҳôСͨΪ 4kʹҳΪλݡ -??????????????????????????????????????????????????????????????????????????????? I/O????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? I/O ????????????B-Tree ????????????? h-1 ?? I/O?????????????????????? O(h)=O(logdN)????????????????? d ??????????????????? 100????? h ?????????????? 3??????????????????h ??????????????????????????????????????????????????????????????? B-Tree ???? +һ˵Ҳܴ󣬲ȫ洢ڴУļʽ洢ĴϡΪ˼ٴ I/OϸȡÿζԤǼѧľֲԭһݱõʱ丽Ҳͨϱʹáݿϵͳ˴ԤԭһڵĴСΪһҳÿڵֻҪһ I/O Ϳȫ롣B-Tree һμҪ h-1 I/Oڵ㳣פڴ棩ӶΪ O(h)=O(logdN)һʵӦУ d Ƿdz֣ͨ 100 h dzСͨ 3ֽṹh ҪĶࡣ߼ϺܽĽڵ㣨ӣϿܺԶ޷þֲԣЧԱ B-Tree ܶࡣ -B+Tree ????????????????????????? d ??????? B+Tree ????????? data ????????????????????????????? +B+Tree ʺԭڽڵ d йء B+Tree ڽڵȥ data ˿ӵиijȣӵиõܡ -# ?????????? +# ѯŻ ## 1. Explain -???????? SQL ??????????????????????? + SQL 䣬бȽҪֶУ -- select_type : ?????????????????????????? +- select_type : ѯͣм򵥲ѯϲѯӲѯ -- key : ???????? +- key : ʹõ -- rows : ???????? +- rows : ɨ -## 2. ?????????? +## 2. ٷص -???????????????????????????????????????????????????????? +ѯҪΪ˹ݣ˷ʹ֮⣬ҲʹС -??????? SELECT * ???????????????????? +òҪʹ SELECT * 䣬ҪҪѡѯС -## 3. ?????????? +## 3. ٷص -?????? LIMIT ????????????????? +ʹ LIMIT ȡҪЩС -????????????????????????????????????????????????????????????????????????????k???????????????ݔ??????????? Explain ???????????? rows ???????????????? +Խȫɨ衣䣬Ҫȫɨ裬ʹֻҪɨ輸м¼ɣʹ Explain ͨ۲ rows ֲֶ졣 ```sql SELECT * FROM sakila.film_actor WHERE film_id = 1; ``` -## 4. ????? DELETE ?? INSERT ??? +## 4. ִ DELETE INSERT -????????????????????????????????????????????????????????????????????????????? +һִеĻһסܶݡռ־ľϵͳԴܶСĵҪIJѯ ```sql DELEFT FROM messages WHERE create < DATE_SUB(NOW(), INTERVAL 3 MONTH); @@ -348,74 +348,74 @@ do { } while rows_affected > 0 ``` -# ??????? +# ֱֿ -**1. ????????????** +**1. ֱIJͬ** -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ֱǽһűֳɶСЩСӵвͬıǽһűݷΪ飬ЩԴ洢ͬһϣҲԴ洢ڲͬĴϣַʽ±Ȼֻһ -**2. ????????????** +**2. ʹ÷ֱֿԭ** -?????????????????????????????????????????????????????????????????????????????? +ʱҵķչݿеıԽԽ࣬ұеҲԽԽôдĿҲ -**3. ?????** +**3. ֱз** -???????????????????????????????????????????????????????? payDB?????????? userDB ????????????????????????????????? +ģ顢ϵг̶Ȼֳ𵽲ͬĿϡ磬ǻὨƷݿ payDBûݿ userDB ȣֱ洢ĿƷйصıûйصı -**4. ????** +**4. ˮƽз** -???????????????????????????????????? id ?????????????????? +ѱеݰijֹ洢ṹͬıУ簴 id ɢֵԱȽл֣ -**5. ???????????????** +**5. ֱзˮƽзֵѡ** -???????????????????????????????????????????????????? +ݿеı̫࣬Ŀҵ߼ôֱзѡ -???????????????????????????????????????? +ݿı࣬ǵܴӦѡˮƽз֡ -**6. ??????????** +**6. ˮƽзֵʵַʽ** -????????? merge ???? +򵥵ʹ merge 洢档 -**7. ???????????????** +**7. ֱֿڵ** -(1) ???????? +(1) -???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ִзֱֿ֮ݴ洢˲ͬĿϣݿѡݿⱾķֲʽȥִ񣬽ܴۣ߰ӦóȥЭƣγɳ߼ϵֻɱ̷ĸ -(2) ????????????? +(2) -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ִ˷ֱֿ֮ԱὫԭ߼ԺǿݻֵͬıͬĿϡʱӲܵƣ޷λڲֿͬıҲ޷ӷֱȲͬıԭֻҪһβѯܹɵҵҪжβɡ -# ????????????? +# תƺ͹ϻָ -???????????????????????????????????????????????????????????????????????????????????????????????? +תҲлֹʱл⣬ʹΪ⡣ϻָ˼Ǵӹлָұ֤ݵȷԡ -## 1. ??????? +## 1. ת -**1.1 ??????????????** +**1.1 лɫ** -??????????????????????????-????????????????????????? +һ̨Ϊ⣬һ-ƽṹеͱɫ -**1.2 ???? IP ????? IP ??** +**1.2 IP ַ IP й** -? MySQL ???????????? IP ??????? MySQL ???????????? IP ??????????? MySQL ????????? +Ϊ MySQL ʵָһ߼ IP ַ MySQL ʵʧЧʱԽ IP ַתƵһ̨ MySQL ϡ -**1.3 ??????????** +**1.3 м** -??????????????????????????????????? +ͨ·ʹõķϡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/fabd5fa0-b75e-48d0-9e2c-31471945ceb9.jpg) -**1.4 ???????????????** +**1.4 Ӧдת** -????????????????????????????????????? +תϵӦпܵӦñ̫׾ -## 2. ?????? +## 2. ϻָ -# ?????? +# ο -- ?????? MySQL -- [MySQL ????????????????????? ](http://blog.codinglabs.org/articles/theory-of-mysql-index.html) -- [MySQL ???????????? ](http://www.runoob.com/w3cnote/mysql-index.html) -- [20+ ?? MySQL ??????????????? ](https://www.jfox.info/20-tiao-mysql-xing-nen-you-hua-de-zui-jia-jing-yan.html) +- MySQL +- [MySQL ݽṹ㷨ԭ ](http://blog.codinglabs.org/articles/theory-of-mysql-index.html) +- [MySQL Żȫ ](http://www.runoob.com/w3cnote/mysql-index.html) +- [20+ MySQL ŻѾ ](https://www.jfox.info/20-tiao-mysql-xing-nen-you-hua-de-zui-jia-jing-yan.html) diff --git a/notes/SQL 语法.md b/notes/SQL 语法.md index f95e4cce..b128542b 100644 --- a/notes/SQL 语法.md +++ b/notes/SQL 语法.md @@ -1,52 +1,52 @@ -* [????](#????) -* [???](#???) -* [????](#????) -* [????](#????) -* [????](#????) -* [???????](#???????) -* [????](#????) - * [???????](#???????) - * [??????????](#??????????) - * [???????](#???????) - * [????](#????) -* [????](#????) -* [????](#????) -* [????](#????) - * [??????](#??????) - * [??????](#??????) - * [???????](#???????) - * [??????](#??????) -* [?????](#?????) -* [????](#????) -* [????](#????) -* [???](#???) -* [??????](#??????) -* [????](#????) -* [???](#???) -* [?????](#?????) -* [??](#??) -* [??????](#??????) -* [??????](#??????) -* [?????](#?????) -* [??????](#??????) +* [](#) +* [ѯ](#ѯ) +* [](#) +* [](#) +* [ͨ](#ͨ) +* [ֶ](#ֶ) +* [](#) + * [ı](#ı) + * [ںʱ䴦](#ںʱ䴦) + * [ֵ](#ֵ) + * [](#) +* [](#) +* [Ӳѯ](#Ӳѯ) +* [](#) + * [](#) + * [](#) + * [Ȼ](#Ȼ) + * [](#) +* [ϲѯ](#ϲѯ) +* [](#) +* [](#) +* [ɾ](#ɾ) +* [](#) +* [޸ı](#޸ı) +* [ͼ](#ͼ) +* [洢](#洢) +* [α](#α) +* [](#) +* [](#) +* [ַ](#ַ) +* [Ȩ޹](#Ȩ޹) -# ???? +# -????????????????ě??????????????????????????????????????????? +ģʽδ洢洢ʲôԼηֽϢݿͱģʽ -??????????????????????????????????????????????????????????????????? +ֵ޸ģҲãʹѾɾֵе -SQL??Structured Query Language)????? SQL ?? ANSI ???????????????? ANSI SQL?????? DBMS ??????????????? PL/SQL??Transact-SQL ??? +SQLStructured Query Language)׼ SQL ANSI ׼ίԱӶΪ ANSI SQL DBMS Լʵ֣ PL/SQLTransact-SQL ȡ -# ??? +# ѯ -SQL ???????????????????????????????????????????????? DBMS ??????? +SQL 䲻ִСдݿֵǷھ DBMS Լá **DISTINCT** -?????????????????????????????????????????????????????? +ֵֻͬһΡУҲ˵еֵͬͬ ```sql SELECT DISTINCT col1, col2 @@ -55,9 +55,9 @@ FROM mytable; **LIMIT** -??????????????????????????????????????????????? 0 ???????????????????????????? +ƷصһΪʼУ 0 ʼڶΪص -????? 5 ?? SQL?? +ǰ 5 е SQL ```sql SELECT * @@ -71,7 +71,7 @@ FROM mytable LIMIT 0, 5; ``` -????? 3 \~ 5 ?? +ص 3 \~ 5 У ```sql SELECT * @@ -79,22 +79,22 @@ FROM mytable LIMIT 2, 3; ``` -**???** +**ע** ```sql -# ??? +# ע SELECT * -FROM mytable -- ??? -/* ???1 - ???2 */ +FROM mytable -- ע +/* ע1 + ע2 */ ``` -# ???? +# -**ASC**?????????? -**DESC**?????? +**ASC**Ĭϣ +**DESC** -???????????????? +԰н ```sql SELECT * @@ -102,9 +102,9 @@ FROM mytable ORDER BY col1 DESC, col2 ASC; ``` -# ???? +# -????????????????????????????????????????????????????????????????????????????????????? +ӦòҲԹݣDzڷ˽й˵ݷdz󣬵ͨ紫˺ܶݣӶ˷ ```sql SELECT * @@ -112,155 +112,155 @@ FROM mytable WHERE col IS NULL; ``` -??????? WHERE ???????????? +±ʾ WHERE ӾõIJ -| ?????? | ??? | +| | ˵ | | ------------ | ------------ | -| = < > | ???? ?? ???? | -| <> != | ?????? | -| <= !> | ????? | -| >= !< | ??????? | -| BETWEEN | ?????????? | -| IS NULL | ?NULL? | +| = < > | С | +| <> != | | +| <= !> | Сڵ | +| >= !< | ڵ | +| BETWEEN | ֵ֮ | +| IS NULL | ΪNULLֵ | -????????NULL ?? 0 ???????????????? +Ӧע⵽NULL 0 ַͬ -**AND OR** ??????????????????????????? AND?????????????????p????? AND ?? OR ????????? () ????????????? +**AND OR** Ӷȴ AND˵һ˱ʽ漰 AND OR ʱӦʹ () ȼ -**IN** ??????????????????????????????? SELECT ???????????????????????? +**IN** ƥһֵҲԽһ SELECT Ӿ䣬ӶƥӲѯõһֵ -**NOT** ???????????????????? +**NOT** ڷһ -# ???? +# ͨ -???????????????????????????????? +ͨҲڹУֻıֶΡ -- **%** ??? >=0 ????????????????? \*?? +- **%** ƥ >=0 ַ \* -- **\_** ??? ==1 ????????????????? \.?? +- **\_** ƥ ==1 ַ \. -- **[ ]** ??????????????????????? ^ ?????????? +- **[ ]** ƥ伯ڵַַ ^ Զз -??? Like ????????????? +ʹ Like ͨƥ䡣 ```sql SELECT * FROM mytable -WHERE col LIKE '[^AB]%' -- ????AB???????????? +WHERE col LIKE '[^AB]%' -- ABͷı ``` -????????????????????????????????? +Ҫͨͨλڿͷƥdz -# ??????? +# ֶ -????????????????????????????????????????????????????????????????????????????????????????????????? +ݿݵת͸ʽĹȿͻϿö࣬ת͸ʽٵĻԼͨ -???????????????? **AS** ???????????????????????????????????? +ֶͨҪʹ **AS** ȡʱֶΪʽ ```sql SELECT col1*col2 AS alias FROM mytable ``` -**Concat()** ???????????????????????????????????????????????????????????????????? **TRIM()** ???????????? +**Concat()** ֶΡݿʹÿոһֵΪпӵĽһЩҪĿոʹ **TRIM()** ȥβո ```sql SELECT Concat(TRIM(col1), ' (', TRIM(col2), ')') FROM mytable ``` -# ???? +# -???? DBMS ??????????????????????????? + DBMS ĺDzͬģ˲ֲ -## ??????? +## ı -| ???? | ??? | +| | ˵ | | ------------ | ------------ | -| LEFT() RIGHT() | ????????????? | -| LOWER() UPPER() | ????????? | -| LTRIM() RTIM() | ??????????????? | -| LENGTH() | ???? | -| SUNDEX() | ????????? | +| LEFT() RIGHT() | ߻ұߵַ | +| LOWER() UPPER() | תΪСдߴд | +| LTRIM() RTIM() | ȥ߻ұߵĿո | +| LENGTH() | | +| SUNDEX() | תΪֵ | -????**SOUNDEX()** ???????????????????????????????????????????????????????????????????? +У**SOUNDEX()** ǽһַתΪʾĸģʽ㷨ǸݷĸȽϡ ```sql SELECT * FROM mytable WHERE SOUNDEX(col1) = SOUNDEX('apple') ``` -## ?????????? +## ںʱ䴦 -????????YYYY-MM-DD +ڸʽYYYY-MM-DD -???????HH:MM:SS +ʱʽHH:MM:SS -|?? ?? | ? ??| +| | ˵ | | --- | --- | -| AddDate() | ??????????????????| -| AddTime() | ?????????????????| -| CurDate() | ?????????? | -| CurTime() | ????????? | -|Date() |???????????????????| -|DateDiff() |???????????????| -|Date_Add() |?????????????????| -|Date_Format() |??????????????????????| -|Day()| ????????????????????| -|DayOfWeek() |?????????????????????????| -|Hour() |????????????????| -|Minute() |?????????????????| -|Month() |?????????????????| -|Now() |??????????????| -|Second() |???????????????| -|Time() |????????????????????| -|Year() |??????????????????| +| AddDate() | һڣ졢ܵȣ| +| AddTime() | һʱ䣨ʱֵȣ| +| CurDate() | صǰ | +| CurTime() | صǰʱ | +|Date() |ʱڲ| +|DateDiff() |֮| +|Date_Add() |߶㺯| +|Date_Format() |һʽڻʱ䴮| +|Day()| һڵ| +|DayOfWeek() |һڣضӦڼ| +|Hour() |һʱСʱ| +|Minute() |һʱķӲ| +|Month() |һڵ·ݲ| +|Now() |صǰںʱ| +|Second() |һʱ벿| +|Time() |һʱʱ䲿| +|Year() |һڵݲ| ```sql mysql> SELECT NOW(); -> '2017-06-28 14:01:52' ``` -## ??????? +## ֵ -| ???? | ??? | +| | ˵ | | --- | --- | -| SIN() | ???? | -|COS() | ???? | -| TAN() | ???? | -| ABS() | ????? | -| SQRT() | ?????| -| MOD() | ????| -| EXP() | ???| -| PI() | ?????| -|RAND() | ?????| +| SIN() | | +|COS() | | +| TAN() | | +| ABS() | ֵ | +| SQRT() | ƽ| +| MOD() | | +| EXP() | ָ| +| PI() | Բ| +|RAND() | | -## ???? +## -|?? ?? |? ??| +| |˵ | | --- | --- | -|AVG() |???????????| -|COUNT()| ???????????| -|MAX()| ???????????| -|MIN()| ??????????| -|SUM() |???????????| +|AVG() |ijеƽֵ| +|COUNT()| ijе| +|MAX()| ijеֵ| +|MIN()| ijеСֵ| +|SUM() |ijֵ֮| -AVG() ????? NULL ?? +AVG() NULL С -DISTINCT ?????????????????? +DISTINCT ؼֵֻֻܲͬ ```sql SELECT AVG(DISTINCT col1) AS avg_col FROM mytable ``` -# ???? +# -?????????????????????????? +ǰͬݷͬһС -??????????????????????????????????????????????? +Զÿʹûܺдÿƽֵȡ -?? col ???????????? + col 򲢷ݣ ```sql SELECT col, COUNT(*) AS num @@ -268,7 +268,7 @@ FROM mytable GROUP BY col; ``` -WHERE ??????HAVING ???????????????????????? +WHERE УHAVING ˷飬йӦˣ ```sql SELECT col, COUNT(*) AS num @@ -278,7 +278,7 @@ GROUP BY col HAVING COUNT(*) >= 2; ``` -GROUP BY ?????????????????? ORDER BY ?????????????????????? +GROUP BY ΪֶΣ ORDER BY ҲԾۼֶ ```sql SELECT col, COUNT(*) AS num @@ -287,18 +287,18 @@ GROUP BY col ORDER BY num; ``` -?????? +涨 -1. GROUP BY ???????? WHERE ??????ORDER BY ??????? -2. ????????????????SELECT ??????????????? GROUP BY ????????? -3. NULL ??????????? -4. ????? SQL ??????? GROUP BY ????????????????? +1. GROUP BY Ӿ WHERE Ӿ֮ORDER BY Ӿ֮ǰ +2. ˻ܼ֮⣬SELECT еÿһж GROUP BY Ӿи +3. NULL лᵥΪһ飻 +4. SQL ʵֲ֧ GROUP BY опɱ䳤ȵ͡ -# ???? +# Ӳѯ -????????????????? +ӲѯֻܷһС -???????????????? WHRER ????????????? +ԽӲѯĽΪ WHRER Ĺ ``` SELECT * @@ -307,7 +307,7 @@ WHERE col1 IN (SELECT col2 FROM mytable2); ``` -?????????????????????????????????????????????????????????? +ԼͻĶӲѯԼÿͻִһΣ ```sql SELECT cust_name, (SELECT COUNT(*) @@ -318,17 +318,17 @@ FROM Customers ORDER BY cust_name; ``` -# ???? +# -??????????????????? JOIN ???????????????????? ON?? +Ӷʹ JOIN ؼ֣ʹ ON -????????I?????????????????????????? +ӿ滻ӲѯұӲѯЧһ졣 -?????? AS ?????????????????????????????????????????? SQL ??????????????? + AS ֶκͱȡȡΪ˼ SQL Լͬ -## ?????? +## -??????????????????? INNER JOIN ?????? +ֳƵֵӣʹ INNER JOIN ؼ֡ ``` select a, b, c @@ -336,7 +336,7 @@ from A inner join B on A.key = B.key ``` -??????????? INNER JOIN????????????????? WHERE ???????????????????????????????????? +Բȷʹ INNER JOINʹͨѯ WHERE нҪӵõֵ ``` select a, b, c @@ -344,15 +344,15 @@ from A, B where A.key = B.key ``` -???????????????????????????? +û·صѿ -## ?????? +## -?????????????????????????????????????????? +ӿԿӵһֻ֣ӵıѡ -??????????????????????????????????????? Jim ???????????????????????? +һԱԱԱţҪҳ Jim ͬһŵԱ -**?????** +**Ӳѯ汾** ``` select name @@ -363,7 +363,7 @@ where department = ( where name = "Jim"); ``` -**??????** +**Ӱ汾** ``` select name @@ -372,24 +372,24 @@ where e1.department = e2.department and e1.name = "Jim"; ``` -?????????????????? +һӲѯЧʸߡ -## ??????? +## Ȼ -???????????????????????????????????????????????? +Ȼǰֵͬͨģͬпж -????????????????????????????????????????????????????????????????????????????? +ӺȻӵṩӵУȻԶͬУȻӡ ``` select * from employee natural join department; ``` -## ?????? +## -????????????????????????????????????????????????????????????????????????????? +ӱûйЩСΪӣԼȫӣӾDZС -????????????????????????????????????? +й˿͵ĶϢûжϢĹ˿͡ ``` select Customers.cust_id, Orders.order_num @@ -397,7 +397,7 @@ select Customers.cust_id, Orders.order_num on Customers.cust_id = Orders.curt_id ``` -?????????????????????????????? +Ҫͳƹ˿͵Ķʹþۼ ``` select Customers.cust_id, @@ -407,13 +407,13 @@ on Customers.cust_id = Orders.curt_id group by Customers.cust_id ``` -# ????? +# ϲѯ -??? **UNION** ??????????????????????????????????????????????????? +ʹ **UNION** ѯÿѯͬСʽ߾ۼ -?????????????????????????????? UNION ALL ?? +ĬϻȥͬУҪͬУʹ UNION ALL -????????? ORDER BY ??????????????????? +ֻܰһ ORDER BY Ӿ䣬ұλ ```sql SELECT col @@ -425,16 +425,16 @@ FROM mytable WHERE col =2; ``` -# ???? +# -**???????** +**ͨ** ```sql INSERT INTO mytable(col1, col2) VALUES(val1, val2); ``` -**?????????????????** +**** ```sql INSERT INTO mytable1(col1, col2) @@ -442,14 +442,14 @@ SELECT col1, col2 FROM mytable2; ``` -**?????????????????????** +**һݸƵһ±** ```sql CREATE TABLE newtable AS SELECT * FROM mytable; ``` -# ???? +# ```sql UPDATE mytable @@ -457,18 +457,18 @@ SET col = val WHERE id = 1; ``` -# ??? +# ɾ ```sql DELETE FROM mytable WHERE id = 1; ``` -**TRUNCATE TABLE** ?????????????????????? +**TRUNCATE TABLE** ձҲɾС -????????????????????? WHERE ????????????????????????????????? SELECT ????????????????????? +ʹøºɾʱһҪ WHERE Ӿ䣬Ȼűݶƻ SELECT вԣֹɾ -# ?????? +# ```sql CREATE TABLE mytable ( @@ -479,38 +479,38 @@ CREATE TABLE mytable ( PRIMARY KEY (`id`)); ``` -# ???? +# ޸ı -**?????** +**** ```sql ALTER TABLE mytable ADD col CHAR(20); ``` -**?????** +**ɾ** ```sql ALTER TABLE mytable DROP COLUMN col; ``` -**?????** +**ɾ** ```sql DROP TABLE mytable; ``` -# ??? +# ͼ -????????????????????????????????????????????????????????????????????????? +ͼıݣҲͲܶͼIJͶͨIJһ -??????????????? +ͼºô -1. ?????? SQL ?????????y??????? -2. ??????????????????? -3. ??????????????????????????????????? -4. ???????????????? +1. 򻯸ӵ SQL 縴ӵ᣻ +2. ֻʹʵʱһݣ +3. ֻͨûͼȨޣ֤ݵİȫԣ +4. ݸʽͱʾ ```sql CREATE VIEW myview AS @@ -519,25 +519,25 @@ FROM mytable WHERE col5 = val; ``` -# ????? +# 洢 -?????????????????? SQL ???????????? +洢̿ԿǶһϵ SQL -**??????????** +**ʹô洢̵ĺô** -1. ??????????????????????????????????? -2. ?????????? -3. ????????????????????????? +1. ʵַװ˴洢У򵥣Ҳ֤˰ȫԣ +2. Ըô룻 +3. Ԥȱ룬˾кܸߵܡ -**?????????** +**洢** -?????????????????????????????????????????? ; ??????????????????????????????????????????????????????????????? +д洢ҪԶָΪ ; Ϊ洢Ҳ˷ֺţ˻ⲿַֺŵǽ﷨ -???? in??out ?? inout ????????? + inout inout ֲ -???????????????? select into ??? +ֵҪ select into 䡣 -????????????????????????????????? +ÿֻܸһֵּ֧ϵIJ ```sql delimiter // @@ -558,18 +558,18 @@ call myprocedure(@ret); select @ret; ``` -# ?? +# α -?????????????????????????????????????? +ڴ洢ʹαԶһƶ -????????????????????????????????????????????????????? +αҪڽʽӦãûҪݼен޸ġ -**????????????s** +**ʹαĸ裺** -1. ????????????????????????????? -2. ???? -3. ???????? -4. ????? +1. α꣬ûʵʼݣ +2. αꣻ +3. ȡݣ +4. رαꣻ ```sql delimiter // @@ -579,7 +579,7 @@ create procedure myprocedure(out ret int) declare mycursor cursor for select col1 from mytable; - # ?????????continue handler???? sqlstate '02000' ??????????????????? set done = 1 + # һcontinue handler sqlstate '02000' ʱִ set done = 1 declare continue handler for sqlstate '02000' set done = 1; open mycursor; @@ -594,43 +594,43 @@ create procedure myprocedure(out ret int) delimiter ; ``` -# ?????? +# -??????????????????????????????????DELETE??INSERT??UPDATE +ijִʱԶִУDELETEINSERTUPDATE -???????????????????????????????????????????? BEFORE ??????????????? AFTER ??????BEFORE ?????????????????? +ִָ֮ǰ֮ԶִУ֮ǰִʹ BEFORE ؼִ֣֮ʹ AFTER ؼ֡BEFORE ֤; -INSERT ???????????????? NEW ??????? +INSERT һΪ NEW ```sql CREATE TRIGGER mytrigger AFTER INSERT ON mytable FOR EACH ROW SELECT NEW.col; ``` -DELETE ???????????????? OLD ??????????????????? +DELETE һΪ OLD ֻġ -UPDATE ???????????????? NEW ???????? OLD ??????????? NEW ????????????? OLD ???????? +UPDATE һΪ NEW һΪ OLD NEW ǿԱ޸ĵأ OLD ֻġ -???????????????????????????????????????????? +ʹôƸ٣޸ļ¼һűС -MySQL ?????????????????? CALL ??? ?????????????????? +MySQL ڴʹ CALL ҲDzܵô洢̡ -# ?????? +# -**????????** +**** -1. ????transaction?????? SQL ??? -2. ?????rollback?????????? SQL ???????? -3. ????commit???????? SQL ????????????? -4. ??????savepoint????????????????????????placeholder????????????????????????????????????????? +1. transactionָһ SQL 䣻 +2. ˣrollbackָָ SQL Ḷ́ +3. ύcommitָδ洢 SQL дݿ +4. 㣨savepointָõʱռλplaceholderԶˣͬ -??????? SELECT ??????? SELECT ???????????????? CRETE ?? DROP ??? +ܻ SELECT 䣬 SELECT Ҳû壻Ҳܻ CRETE DROP 䡣 -MySQL ???????????????????????????????????????????????? START TRANSACTION ??????????????????? COMMIT ?? ROLLBACK ??????????????????????????????? +MySQL ύĬʽύҲÿִһͻύһΡ START TRANSACTION ʱرʽύ COMMIT ROLLBACK ִкԶرգ»ָʽύ -??????? autocommit ? 0 ????????????????? autocommit ??????? 1 ???????autocommit ??????????????????????????????? +ͨ autocommit Ϊ 0 ȡԶύֱ autocommit Ϊ 1 Żύautocommit ÿӶԷġ -???????????????ROLLBACK ?????? START TRANSACTION ????????????????????????? ROLLBACK ?????????????????????????? +ûñ㣬ROLLBACK ˵ START TRANSACTION 䴦˱㣬 ROLLBACK ָñ㣬˵ñ㡣 ```sql START TRANSACTION @@ -642,15 +642,15 @@ ROLLBACK TO delete1 COMMIT ``` -# ????? +# ַ -**????????** +**** -1. ??????????????????? -2. ?????????????????????????? -3. ??????????????????????????? +1. ַΪĸͷŵļϣ +2. ΪijַԱڲʾ +3. УַָαȽϣҪͷ顣 -?????????????????????????????????? +˸ַָУ⣬ҲԸָ ```sql CREATE TABLE mytable @@ -658,7 +658,7 @@ CREATE TABLE mytable DEFAULT CHARACTER SET hebrew COLLATE hebrew_general_ci; ``` -???????????????????? +򡢷ʱָУԣ ```sql SELECT * @@ -666,67 +666,67 @@ FROM mytable ORDER BY col COLLATE latin1_general_ci; ``` -# ?????? +# Ȩ޹ -MySQL ?????????????? mysql ?????????? +MySQL ˻Ϣ mysql ݿС ```sql USE mysql; SELECT user FROM user; ``` -**???????** +**˻** ```sql CREATE USER myuser IDENTIFIED BY 'mypassword'; ``` -??????????????????? +´˻ûκȨޡ -**????????** +**޸˻** ```sql RENAME myuser TO newuser; ``` -**??????** +**ɾ˻** ```sql DROP USER myuser; ``` -**?????** +**鿴Ȩ** ```sql SHOW GRANTS FOR myuser; ``` ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c73aa08e-a987-43c9-92be-adea4a884c25.png) -????? username@host ????????username@% ????????????????? +˻ username@host ʽ壬username@% ʹõĬ -**???????** +**Ȩ** ```sql GRANT SELECT, INSERT ON mydatabase.* TO myuser; ``` -**??????** +**ɾȨ** ```sql REVOKE SELECT, INSERT ON mydatabase.* FROM myuser; ``` -GRANT ?? REVOKE ?????????????????????? +GRANT REVOKE ڼϿƷȨޣ -- ??????????????? GRANT ALL?? REVOKE ALL?? -- ???????????? ON database.\*?? -- ????????? ON database.table?? -- ??????? -- ?????????? +- ʹ GRANT ALL REVOKE ALL +- ݿ⣬ʹ ON database.\* +- ضıʹ ON database.table +- ضУ +- ضĴ洢̡ -**????????** +**** -??????? Password() ???? +ʹ Password() ```sql SET PASSWROD FOR myuser = Password('newpassword'); diff --git a/notes/剑指 offer 题解.md b/notes/剑指 offer 题解.md index 5108480c..51793e1b 100644 --- a/notes/剑指 offer 题解.md +++ b/notes/剑指 offer 题解.md @@ -1,89 +1,89 @@ -* [????? ??????????????](#?????-??????????????) - * [2. ??? Singleton](#2-???-singleton) - * [3. ???????????????](#3-???????????????) - * [4. ????????????](#4-????????????) - * [5. ?I???](#5-?I???) - * [6. ????????????](#6-????????????) - * [7. ?????????](#7-?????????) - * [8. ????????????????](#8-????????????????) - * [9. ?????????????](#9-?????????????) - * [10.1 ??????????](#101-??????????) - * [10.2 ?????](#102-?????) - * [10.3 ????????](#103-????????) - * [10.4 ??????](#104-??????) - * [11. ??????????????](#11-??????????????) - * [12. ????????](#12-????????) - * [13. ????????????](#13-????????????) - * [14. ??????](#14-??????) - * [15. ???????? 1 ?????](#15-????????-1-?????) -* [?????? ???????????](#??????-???????????) - * [16. ???????????](#16-???????????) - * [18. ????????????????](#18-????????????????) - * [19. ??????????](#19-??????????) - * [20. ?????????????](#20-?????????????) - * [21. ????????????????????????](#21-????????????????????????) - * [22. ?????????? k ?????](#22-??????????-k-?????) - * [23. ?????????????](#23-?????????????) - * [24. ???????](#24-???????) - * [25. ????????????????](#25-????????????????) - * [26. ???????](#26-???????) -* [?????? ???????????](#??????-???????????) - * [27. ???????????](#27-???????????) - * [28.1 ?????????](#281-?????????) - * [28.2 ????????](#282-????????) - * [29. ??????????](#29-??????????) - * [30. ???? min ???????](#30-????-min-???????) - * [31. ??????????????](#31-??????????????) - * [32.1 ????????????????](#321-????????????????) - * [32.3 ???????????????](#323--???????????????) - * [32.3 ??????????????????](#323-??????????????????) - * [33. ??????????????????????](#33-??????????????????????) - * [34. ????????????????](#34-????????????????) - * [35. ????????????](#35-????????????) - * [36. ???????????????????](#36-???????????????????) - * [37. ??????????](#37-??????????) - * [38. ???????????](#38-???????????) -* [?????? ???????????](#??????-???????????) - * [39. ???????????????????????](#39-???????????????????????) - * [40. ???? K ????](#40-????-k-????) - * [41.1 ????????????](#411-????????????) - * [14.2 ????????????????????](#142-????????????????????) - * [42. ???????????????](#42-???????????????) - * [43. ?? 1 ?? n ?????? 1 ????????](#43-??-1-??-n-??????-1-????????) - * [45. ???????????????](#45-???????????????) - * [49. ????](#49-????) - * [50. ??????????????????](#50-??????????????????) - * [51. ???????????](#51-???????????) - * [52. ????????????????????](#52-????????????????????) -* [?????? ?????????????](#??????-?????????????) - * [53 ??????????????????????](#53-??????????????????????) - * [54. ????????????? k ?????](#54-?????????????-k-?????) - * [55 ???????????](#55-???????????) - * [56. ??????????????????](#56-??????????????????) - * [57.1 ??? S ??????????](#571-???-s-??????????) - * [57.2 ??? S ??????????????](#572-???-s-??????????????) - * [58.1 ????????????](#581-????????????) - * [58.2 ??????????](#582-??????????) - * [59. ?????????????](#59-?????????????) - * [61. ????????](#61-????????) - * [62. ?????????????](#62-?????????????) - * [63. ????????????](#63-????????????) - * [64. ?? 1+2+3+...+n](#64-??-1+2+3++n) - * [65. ?????????????](#65-?????????????) - * [66. ???????????](#66-???????????) -* [?????? ???????????](#??????-???????????) - * [67. ????????????????](#67-????????????????) - * [68. ??????????????????????](#68-??????????????????????) +* [ڶ ҪĻ֪ʶ](#ڶ-ҪĻ֪ʶ) + * [2. ʵ Singleton](#2-ʵ-singleton) + * [3. ظ](#3-ظ) + * [4. άеIJ](#4-άеIJ) + * [5. 滻ո](#5-滻ո) + * [6. βͷӡ](#6-βͷӡ) + * [7. ؽ](#7-ؽ) + * [8. һ](#8-һ) + * [9. ջʵֶ](#9-ջʵֶ) + * [10.1 쳲](#101-쳲) + * [10.2 ̨](#102-̨) + * [10.3 ̨̬](#103-̨̬) + * [10.4 θ](#104-θ) + * [11. תС](#11-תС) + * [12. е·](#12-е·) + * [13. ˵˶Χ](#13-˵˶Χ) + * [14. ](#14-) + * [15. 1 ĸ](#15--1-ĸ) +* [ Ĵ](#-Ĵ) + * [16. ֵη](#16-ֵη) + * [18. ɾظĽ](#18-ɾظĽ) + * [19. ʽƥ](#19-ʽƥ) + * [20. ʾֵַ](#20-ʾֵַ) + * [21. ˳ʹλżǰ](#21-˳ʹλżǰ) + * [22. е k ](#22-е-k-) + * [23. лڽ](#23-лڽ) + * [24. ת](#24-ת) + * [25. ϲ](#25-ϲ) + * [26. ӽṹ](#26-ӽṹ) +* [ ˼·](#-˼·) + * [27. ľ](#27-ľ) + * [28.1 ԳƵĶ](#281-ԳƵĶ) + * [28.2 ƽ](#282-ƽ) + * [29. ˳ʱӡ](#29-˳ʱӡ) + * [30. min ջ](#30--min-ջ) + * [31. ջѹ롢](#31-ջѹ뵯) + * [32.1 ´ӡ](#321-´ӡ) + * [32.3 Ѷӡɶ](#323--Ѷӡɶ) + * [32.3 ֮˳ӡ](#323-֮˳ӡ) + * [33. ĺ](#33-ĺ) + * [34. кΪijһֵ·](#34-кΪijһֵ·) + * [35. ĸ](#35-ĸ) + * [36. ˫](#36-˫) + * [37. л](#37-л) + * [38. ַ](#38-ַ) +* [ ŻʱͿռЧ](#-ŻʱͿռЧ) + * [39. гִһ](#39-гִһ) + * [40. С K ](#40-С-k-) + * [41.1 еλ](#411-еλ) + * [14.2 ַеһظַ](#142-ַеһظַ) + * [42. ](#42-) + * [43. 1 n 1 ֵĴ](#43--1--n--1-ֵĴ) + * [45. ųС](#45-ųС) + * [49. ](#49-) + * [50. һֻһεַλ](#50-һֻһεַλ) + * [51. е](#51-е) + * [52. ĵһ](#52-ĵһ) +* [ еĸ](#-еĸ) + * [53 гֵĴ](#53-гֵĴ) + * [54. ĵ k ](#54-ĵ-k-) + * [55 ](#55-) + * [56. ֻһε](#56-ֻһε) + * [57.1 Ϊ S ](#571-Ϊ-s-) + * [57.2 Ϊ S ](#572-Ϊ-s-) + * [58.1 ת˳](#581-ת˳) + * [58.2 תַ](#582-תַ) + * [59. ڵֵ](#59-ڵֵ) + * [61. ˿˳](#61-˿˳) + * [62. ԲȦʣµ](#62-ԲȦʣµ) + * [63. Ʊ](#63-Ʊ) + * [64. 1+2+3+...+n](#64--1+2+3++n) + * [65. üӼ˳ӷ](#65-üӼ˳ӷ) + * [66. ˻](#66-˻) +* [ ԰](#-԰) + * [67. ַת](#67-ַת) + * [68. ڵ͹](#68-ڵ͹) -# ????? ?????????????? +# ڶ ҪĻ֪ʶ -## 2. ??? Singleton +## 2. ʵ Singleton -**???????** +**ʵ** -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? if(uniqueInstance == null) ??????????????????? uniqueInstance ?????????? +ʵУ˽о̬ӳٻʵĺôǣûõ࣬ôͲᴴ˽о̬ӶԼԴʵڶ̻߳DzȫģΪܹ߳ͬʱ if(uniqueInstance == null) ڵ飬ôͻʵ uniqueInstance ˽о̬ ```java public class Singleton { @@ -99,9 +99,9 @@ public class Singleton { } ``` -**???????????????????** +**̲߳ȫĽһ** -?????? getUniqueInstance() ????????????????????????????????????????????? uniqueInstance ???????????????????????????????????????????????????????????????????????? +ֻҪ getUniqueInstance() ø÷һֻһ̷߳ʣӶ˶ uniqueInstance жʵ⡣һһֻһ߳̽룬ϻһ˷ѡ ```java public static synchronized Singleton getUniqueInstance() { @@ -111,17 +111,17 @@ public static synchronized Singleton getUniqueInstance() { return uniqueInstance; } ``` -**????????????????????** +**̲߳ȫĽ** -???????????????????????????? +ӳʵֱʵ ```java private static Singleton uniqueInstance = new Singleton(); ``` -**????????????????????** +**̲߳ȫĽ** -????????????????????????? getUniqueInstance() ????????????????????????? uniqueInstance = new Singleton(); ???????????????????????????????? uniqueInstance ??????????????????????????????????? +ǵһֱӶ getUniqueInstance() мʵֻҪ uniqueInstance = new Singleton(); ɡʹж uniqueInstance ǷѾʵûʵҪ ```java public class Singleton { @@ -141,15 +141,15 @@ public class Singleton { } ``` -## 3. ??????????????? +## 3. ظ -**???????** +**Ŀ** -?????????? n ???????????????????? 0 ?? n-1 ?????? ?????????????????????????????????????????????????????????????????????????????????????????? ????????????? 7 ?????? {2, 3, 1, 0, 2, 5, 3}???????????????????????????? 2?? +һΪ n ֶ 0 n-1 ķΧڡ ijЩظģ֪мظġҲ֪ÿظΡҳһظ֡ 磬볤Ϊ 7 {2, 3, 1, 0, 2, 5, 3}ôӦǵһظ 2 -**?????** +**˼·** -????????????? [0, n-1] ???????????????? i ????????? i ??????? +Ԫ [0, n-1] Χڵ⣬ԽֵΪ i Ԫطŵ i λϡ ```java public boolean duplicate(int numbers[], int length, int[] duplication) { @@ -172,11 +172,11 @@ private void swap(int[] numbers, int i, int j) { } ``` -## 4. ???????????? +## 4. άеIJ -**???????** +**Ŀ** -???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +һάУÿһжմҵ˳ÿһжմϵµ˳һһάһжǷи ```java public boolean Find(int target, int [][] array) { @@ -192,21 +192,21 @@ public boolean Find(int target, int [][] array) { } ``` -## 5. ?I??? +## 5. 滻ո -**???????** +**Ŀ** -?????????????????????????????I??%20??????????????? We Are Happy. ????I?????????? We%20Are%20Happy?? +ʵһһַеĿո滻ɡ%20磬ַΪ We Are Happy. 򾭹滻ַ֮Ϊ We%20Are%20Happy -**??????** +**ĿҪ** -?? O(1) ??????????? + O(1) Ŀռ临Ӷ⡣ ```java public String replaceSpace(StringBuffer str) { int n = str.length(); for (int i = 0; i < n; i++) { - if (str.charAt(i) == ' ') str.append(" "); // ????????? + if (str.charAt(i) == ' ') str.append(" "); // β } int idxOfOriginal = n - 1; @@ -225,9 +225,9 @@ public String replaceSpace(StringBuffer str) { } ``` -## 6. ???????????? +## 6. βͷӡ -????????????? Collections.reverse(). +Ȼ Collections.reverse(). ```java public ArrayList printListFromTailToHead(ListNode listNode) { @@ -241,7 +241,7 @@ public ArrayList printListFromTailToHead(ListNode listNode) { } ``` -??? +ݹ ```java public ArrayList printListFromTailToHead(ListNode listNode) { @@ -254,11 +254,11 @@ public ArrayList printListFromTailToHead(ListNode listNode) { } ``` -?????????????????????????????????????????????????? +ʹÿ⺯Ҳʹõݹĵʵ֣ͷ巨Ϊԡ ```java public ArrayList printListFromTailToHead(ListNode listNode) { - ListNode head = new ListNode(-1); // ???? + ListNode head = new ListNode(-1); // ͷ ListNode cur = listNode; while (cur != null) { ListNode next = cur.next; @@ -276,11 +276,11 @@ public ArrayList printListFromTailToHead(ListNode listNode) { } ``` -## 7. ????????? +## 7. ؽ -**???????** +**Ŀ** -?????????????????????????????????????????????? +ݶǰĽؽö ```java public TreeNode reConstructBinaryTree(int[] pre, int[] in) { @@ -300,11 +300,11 @@ private TreeNode reConstructBinaryTree(int[] pre, int preL, int preR, int[] in, } ``` -## 8. ???????????????? +## 8. һ -**???????** +**Ŀ** -????????????????????????????????????????????????????????????????????????????????????????????? +һеһ㣬ҳ˳һ㲢ҷءע⣬еĽ㲻ӽ㣬ͬʱָ򸸽ָ롣 ```java public TreeLinkNode GetNext(TreeLinkNode pNode) { @@ -325,7 +325,7 @@ public TreeLinkNode GetNext(TreeLinkNode pNode) { } ``` -## 9. ????????????? +## 9. ջʵֶ ```java Stack stack1 = new Stack(); @@ -345,7 +345,7 @@ public int pop() { } ``` -## 10.1 ?????????? +## 10.1 쳲 ```java private int[] fib = new int[40]; @@ -363,7 +363,7 @@ public int Fibonacci(int n) { } ``` -## 10.2 ????? +## 10.2 ̨ ```java public int JumpFloor(int target) { @@ -378,7 +378,7 @@ public int JumpFloor(int target) { } ``` -## 10.3 ???????? +## 10.3 ̨̬ ```java public int JumpFloorII(int target) { @@ -393,11 +393,11 @@ public int JumpFloorII(int target) { } ``` -## 10.4 ?????? +## 10.4 θ -**???????** +**Ŀ** -????????? 2\*1 ????????????????????????????????? n ?? 2\*1 ??????????????????? 2\*n ???????????????????? +ǿ 2\*1 СκŻȥǸľΡ n 2\*1 Сصظһ 2\*n ĴΣܹжַ ```java public int RectCover(int target) { @@ -407,11 +407,11 @@ public int RectCover(int target) { ``` -## 11. ?????????????? +## 11. תС -**???????** +**Ŀ** -???????????????????????????????????????????????? ?????????????????????????????????????????????? ???????? {3, 4, 5, 1, 2} ? {1, 2, 3, 4, 5} ????????????????????? 1?? NOTE???????????????????? 0?????????? 0?????? 0?? +һʼɸԪذᵽĩβdz֮Ϊת һǵݼһתתСԪء {3, 4, 5, 1, 2} Ϊ {1, 2, 3, 4, 5} һתСֵΪ 1 NOTEԪض 0СΪ 0뷵 0 ```java public int minNumberInRotateArray(int[] array) { @@ -423,11 +423,11 @@ public int minNumberInRotateArray(int[] array) { } ``` -## 12. ???????? +## 12. е· -**???????** +**Ŀ** -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???? a b c e s f c s a d e e ???????????????? "bcced" ??????????????????? "abcb" ????????????????????? b ???????????????????????????????????????? +һжһǷһijַַ··ԴӾеһӿʼÿһھңϣƶһӡһ·˾еijһӣ·ٽøӡ a b c e s f c s a d e e аһַ "bcced" ·Ǿв "abcb" ·Ϊַĵһַ b ռ˾еĵһеڶ֮·ٴνøӡ ```java private int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; @@ -464,11 +464,11 @@ private boolean backtracking(char[][] m, int rows, int cols, char[] str, boolean ``` -## 13. ???????????? +## 13. ˵˶Χ -**???????** +**Ŀ** -????????? m ?? n ??????????????????? 0, 0 ??????????????????????????????????????????????????????????????????????????? k ?????? ????? k ? 18 ??????????????????35, 37??????? 3+5+3+7 = 18??????????????????35, 38??????? 3+5+3+8 = 19??????????????????????????? +һ m к n еķһ˴ 0, 0 ĸӿʼƶÿһֻңϣĸƶһ񣬵Dzܽλ֮ʹ k ĸӡ 磬 k Ϊ 18 ʱܹ뷽35, 37Ϊ 3+5+3+7 = 18ǣܽ뷽35, 38Ϊ 3+5+3+8 = 19ʸûܹﵽٸӣ ```java private int cnt = 0; @@ -510,15 +510,15 @@ private void initDigitSum(int rows, int cols) { } ``` -## 14. ?????? +## 14. -**???????** +**Ŀ** -???????????????????????????????? +һӼɶΣʹÿεijȳ˻ -**?????** +**˼·** -????????????? 3 ????????????????????? 1 ???????????????????????????ܨ???? 3 ?????????????????? 1 ????????????????????????????? 2 ??????? +ܶüΪ 3 ӣҲгΪ 1 ӳ֣ˣʹѾкóΪ 3 óһ볤Ϊ 1 ϣгγΪ 2 ӡ ```java int maxProductAfterCuttin(int length) { @@ -532,7 +532,7 @@ int maxProductAfterCuttin(int length) { } ``` -## 15. ???????? 1 ????? +## 15. 1 ĸ ```java public int NumberOf1(int n) { @@ -540,7 +540,7 @@ public int NumberOf1(int n) { } ``` -n&(n-1) ??????????? n ???????????????????????????????? 10110100????? 1 ??? 10110011???????????????? 10110000?? +n&(n-1) λȥ n λʾ͵һλڶƱʾ 10110100ȥ 1 õ 10110011õ 10110000 ```java public int NumberOf1(int n) { @@ -554,9 +554,9 @@ public int NumberOf1(int n) { ``` -# ?????? ??????????? +# Ĵ -## 16. ??????????? +## 16. ֵη ```java public double Power(double base, int exponent) { @@ -573,7 +573,7 @@ public double Power(double base, int exponent) { } ``` -## 18. ???????????????? +## 18. ɾظĽ ```java public ListNode deleteDuplication(ListNode pHead) { @@ -592,11 +592,11 @@ public ListNode deleteDuplication(ListNode pHead) { } ``` -## 19. ?????????? +## 19. ʽƥ -**???????** +**Ŀ** -?????????????????????? '.' ?? '\*' ?????????????????? '.' ????????????????? '\*' ???????????????????????????? 0 ???? ?????????????????????????????????????????????? "aaa" ???? "a.a" ?? "ab\*ac\*a" ????????? "aa.a" ?? "ab\*a" ??????? +ʵһƥ '.' '\*' ʽģʽеַ '.' ʾһַ '\*' ʾǰַԳΣ 0 Σ ڱУƥַַָƥģʽ磬ַ "aaa" ģʽ "a.a" "ab\*ac\*a" ƥ䣬 "aa.a" "ab\*a" ƥ ```java public boolean match(char[] str, char[] pattern) { @@ -619,11 +619,11 @@ public boolean match(char[] str, char[] pattern) { } ``` -## 20. ????????????? +## 20. ʾֵַ -**???????** +**Ŀ** -????????????????????????????????????????????????????????? "+100","5e2","-123","3.1416" ?? "-1E-16" ?????????? ???? "12e","1a3.14","1.2.3","+-5" ?? "12e+4.3" ??????? +ʵһжַǷʾֵС磬ַ "+100","5e2","-123","3.1416" "-1E-16" ʾֵ "12e","1a3.14","1.2.3","+-5" "12e+4.3" ǡ ```java public boolean isNumeric(char[] str) { @@ -632,10 +632,10 @@ public boolean isNumeric(char[] str) { } ``` -## 21. ???????????????????????? +## 21. ˳ʹλżǰ -????? : O(n2) -????? : O(1) +ʱ临Ӷ : O(n2) +ռ临Ӷ : O(1) ```java public void reOrderArray(int[] array) { @@ -655,8 +655,8 @@ public void reOrderArray(int[] array) { } ``` -????? : O(n) -????? : O(n) +ʱ临Ӷ : O(n) +ռ临Ӷ : O(n) ```java public void reOrderArray(int[] array) { @@ -671,7 +671,7 @@ public void reOrderArray(int[] array) { } ``` -## 22. ?????????? k ????? +## 22. е k ```java public ListNode FindKthToTail(ListNode head, int k) { @@ -690,7 +690,7 @@ public ListNode FindKthToTail(ListNode head, int k) { -## 23. ????????????? +## 23. лڽ ```java public ListNode EntryNodeOfLoop(ListNode pHead) { @@ -712,7 +712,7 @@ public ListNode EntryNodeOfLoop(ListNode pHead) { } ``` -## 24. ??????? +## 24. ת ```java public ListNode ReverseList(ListNode head) { @@ -727,7 +727,7 @@ public ListNode ReverseList(ListNode head) { } ``` -## 25. ???????????????? +## 25. ϲ ```java public ListNode Merge(ListNode list1, ListNode list2) { @@ -749,7 +749,7 @@ public ListNode Merge(ListNode list1, ListNode list2) { } ``` -## 26. ??????? +## 26. ӽṹ ```java public boolean HasSubtree(TreeNode root1, TreeNode root2) { @@ -766,9 +766,9 @@ private boolean isSubtree(TreeNode root1, TreeNode root2) { } ``` -# ?????? ??????????? +# ˼· -## 27. ??????????? +## 27. ľ ```java public void Mirror(TreeNode root) { @@ -781,7 +781,7 @@ public void Mirror(TreeNode root) { } ``` -## 28.1 ????????? +## 28.1 ԳƵĶ ```java boolean isSymmetrical(TreeNode pRoot) { @@ -797,7 +797,7 @@ boolean isSymmetrical(TreeNode t1, TreeNode t2) { } ``` -## 28.2 ???????? +## 28.2 ƽ ```java private boolean isBalanced = true; @@ -816,7 +816,7 @@ private int height(TreeNode root) { } ``` -## 29. ?????????? +## 29. ˳ʱӡ ```java public ArrayList printMatrix(int[][] matrix) { @@ -833,7 +833,7 @@ public ArrayList printMatrix(int[][] matrix) { } ``` -## 30. ???? min ??????? +## 30. min ջ ```java private Stack stack = new Stack<>(); @@ -861,7 +861,7 @@ public int min() { } ``` -## 31. ?????????????? +## 31. ջѹ롢 ```java public boolean IsPopOrder(int[] pushA, int[] popA) { @@ -878,7 +878,7 @@ public boolean IsPopOrder(int[] pushA, int[] popA) { } ``` -## 32.1 ???????????????? +## 32.1 ´ӡ ```java public ArrayList PrintFromTopToBottom(TreeNode root) { @@ -899,7 +899,7 @@ public ArrayList PrintFromTopToBottom(TreeNode root) { } ``` -## 32.3 ??????????????? +## 32.3 Ѷӡɶ ```java ArrayList> Print(TreeNode pRoot) { @@ -922,7 +922,7 @@ ArrayList> Print(TreeNode pRoot) { } ``` -## 32.3 ?????????????????? +## 32.3 ֮˳ӡ ```java public ArrayList> Print(TreeNode pRoot) { @@ -953,7 +953,7 @@ public ArrayList> Print(TreeNode pRoot) { ``` -## 33. ?????????????????????? +## 33. ĺ ```java public boolean VerifySquenceOfBST(int[] sequence) { @@ -976,7 +976,7 @@ private boolean verify(int[] sequence, int start, int end) { } ``` -## 34. ???????????????? +## 34. кΪijһֵ· ```java private ArrayList> ret = new ArrayList<>(); @@ -1000,21 +1000,21 @@ private void dfs(TreeNode node, int target, int curSum, ArrayList path) } ``` -## 35. ???????????? +## 35. ĸ -**???????** +**Ŀ** -??????????????????????????????????????????????????????????????????????????????????????????????????? head?????????????????????????????????????????????????????? +һÿڵнڵֵԼָ룬һָһڵ㣬һָָһڵ㣩ؽΪƺ headע⣬벻ҪزеĽڵãֱӷؿգ -??????????????????????????? +һÿڵĺ븴ƵĽڵ㡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f8b12555-967b-423d-a84e-bc9eff104b8b.jpg) -??????????????? random ?????????? +ڶԸƽڵ random ӽиֵ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7b877a2a-8fd1-40d8-a34c-c445827300b8.jpg) -???????????? +֡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b2b6253c-c701-4b30-aff4-bc3c713542a7.jpg) @@ -1022,7 +1022,7 @@ private void dfs(TreeNode node, int target, int curSum, ArrayList path) ```java public RandomListNode Clone(RandomListNode pHead) { if (pHead == null) return null; - // ???????? + // ½ڵ RandomListNode cur = pHead; while (cur != null) { RandomListNode node = new RandomListNode(cur.label); @@ -1030,7 +1030,7 @@ public RandomListNode Clone(RandomListNode pHead) { cur.next = node; cur = node.next; } - // ???? random ???? + // random cur = pHead; while (cur != null) { RandomListNode clone = cur.next; @@ -1039,7 +1039,7 @@ public RandomListNode Clone(RandomListNode pHead) { } cur = clone.next; } - // ??? + // RandomListNode pCloneHead = pHead.next; cur = pHead; while (cur.next != null) { @@ -1051,11 +1051,11 @@ public RandomListNode Clone(RandomListNode pHead) { } ``` -## 36. ??????????????????? +## 36. ˫ -**???????** +**Ŀ** -???????????????????????????????????????????????????????????????????????????????????? +һööתһ˫ҪܴκµĽ㣬ֻܵнָָ ```java private TreeNode pre = null; @@ -1076,7 +1076,7 @@ private void inOrder(TreeNode node) { } ``` -## 37. ?????????? +## 37. л ```java private String serizeString = ""; @@ -1109,11 +1109,11 @@ private TreeNode Deserialize() { } ``` -## 38. ??????????? +## 38. ַ -**???????** +**Ŀ** -???????????? , ?????????????????????????????????????????????? abc, ??????????? a, b, c ????????????????????? abc, acb, bac, bca, cab ?? cba?? +һַ , ֵӡַַСַ abc, ӡַ a, b, c гַ abc, acb, bac, bca, cab cba ```java private ArrayList ret = new ArrayList<>(); @@ -1133,7 +1133,7 @@ private void backtracking(char[] chars, boolean[] used, String s) { } for (int i = 0; i < chars.length; i++) { if (used[i]) continue; - if (i != 0 && chars[i] == chars[i - 1] && !used[i - 1]) continue; // ???????? + if (i != 0 && chars[i] == chars[i - 1] && !used[i - 1]) continue; // ֤ظ used[i] = true; backtracking(chars, used, s + chars[i]); used[i] = false; @@ -1141,9 +1141,9 @@ private void backtracking(char[] chars, boolean[] used, String s) { } ``` -# ?????? ??????????? +# ŻʱͿռЧ -## 39. ??????????????????????? +## 39. гִһ ```java public int MoreThanHalfNum_Solution(int[] array) { @@ -1165,12 +1165,12 @@ public int MoreThanHalfNum_Solution(int[] array) { ``` -## 40. ???? K ???? +## 40. С K -??????? k ??????? +СΪ k Сѡ -??????O(nlgk) -??????O(k) +ʱ临ӶȣO(nlgk) +ռ临ӶȣO(k) ```java public ArrayList GetLeastNumbers_Solution(int[] input, int k) { @@ -1187,10 +1187,10 @@ public ArrayList GetLeastNumbers_Solution(int[] input, int k) { } ``` -?????????? +ÿѡ -??????O(n) -??????O(1) +ʱ临ӶȣO(n) +ռ临ӶȣO(1) ```java public ArrayList GetLeastNumbers_Solution(int[] input, int k) { @@ -1245,22 +1245,22 @@ private boolean less(int v, int w) { } ``` -## 41.1 ???????????? +## 41.1 еλ -**???????** +**Ŀ** -???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +εõһеλжֵôλֵ֮λмֵжżֵôλֵ֮мƽֵ ```java -private PriorityQueue maxHeap = new PriorityQueue<>((o1, o2) -> o2-o1); // ????????? -private PriorityQueue minHeep = new PriorityQueue<>(); // ???????????????????????????????? +private PriorityQueue maxHeap = new PriorityQueue<>((o1, o2) -> o2-o1); // ʵ߲ +private PriorityQueue minHeep = new PriorityQueue<>(); // ʵұ߲֣ұ߲Ԫش߲ private int cnt = 0; public void Insert(Integer num) { - // ?????????????????????? + // Ҫ֤Ѵƽ״̬ if(cnt % 2 == 0) { - // ??????????????????????????????????????????????????????????????? + // Ϊż²뵽СѣȾɸѡܱ֤еԪضССеԪ maxHeap.add(num); minHeep.add(maxHeap.poll()); } else { @@ -1279,11 +1279,11 @@ public Double GetMedian() { } ``` -## 14.2 ???????????????????? +## 14.2 ַеһظַ -**???????** +**Ŀ** -????????????????????????????????????????????????????????????????????? "go" ????????????????????? "g"??????????????????????????google" ????????????????????? "l"?? +ʵһҳַеһֻһεַ磬ַֻǰַ "go" ʱһֻһεַ "g"Ӹַжǰַgoogle" ʱһֻһεַ "l" ```java //Insert one char from stringstream @@ -1306,7 +1306,7 @@ public char FirstAppearingOnce() { ``` -## 42. ??????????????? +## 42. ```java public int FindGreatestSumOfSubArray(int[] array) { @@ -1322,9 +1322,9 @@ public int FindGreatestSumOfSubArray(int[] array) { } ``` -## 43. ?? 1 ?? n ?????? 1 ???????? +## 43. 1 n 1 ֵĴ -???????[Leetcode : 233. Number of Digit One](https://leetcode.com/problems/number-of-digit-one/discuss/64381/4+-lines-O(log-n)-C++JavaPython) +ο[Leetcode : 233. Number of Digit One](https://leetcode.com/problems/number-of-digit-one/discuss/64381/4+-lines-O(log-n)-C++JavaPython) ```java public int NumberOf1Between1AndN_Solution(int n) { @@ -1337,11 +1337,11 @@ public int NumberOf1Between1AndN_Solution(int n) { } ``` -## 45. ??????????????? +## 45. ųС -**???????** +**Ŀ** -??????????????????????????????????????????????????????????????????????????????????????????? {3??32??321}??????????????????????????????? 321323?? +һ飬ƴųһӡƴӳСһ {332321}ӡųɵСΪ 321323 ```java public String PrintMinNumber(int[] numbers) { @@ -1355,11 +1355,11 @@ public String PrintMinNumber(int[] numbers) { } ``` -## 49. ???? +## 49. -**???????** +**Ŀ** -??????????? 2??3 ?? 5 ??????????????Ugly Number???????? 6??8 ??????????? 14 ?????????????????? 7?? ?????????? 1 ????????????????????????????? N ???????? +ֻ 23 5 Ugly Number 68 dz 14 ǣΪ 7 ϰǰ 1 ǵһ󰴴С˳ĵ N ```java public int GetUglyNumber_Solution(int index) { @@ -1380,7 +1380,7 @@ public int GetUglyNumber_Solution(int index) { } ``` -## 50. ?????????????????? +## 50. һֻһεַλ ```java public int FirstNotRepeatingChar(String str) { @@ -1391,7 +1391,7 @@ public int FirstNotRepeatingChar(String str) { } ``` -## 51. ??????????? +## 51. е ```java private long cnt = 0; @@ -1418,7 +1418,7 @@ private void merge(int[] a, int start, int mid, int end) { else if (a[i] < a[j]) tmp[k] = a[i++]; else { tmp[k] = a[j++]; - this.cnt += mid - i + 1; // a[i] > a[j] ????? a[i...mid] ?????? a[j] + this.cnt += mid - i + 1; // a[i] > a[j] ˵ a[i...mid] a[j] } k++; } @@ -1429,7 +1429,7 @@ private void merge(int[] a, int start, int mid, int end) { } ``` -## 52. ???????????????????? +## 52. ĵһ ```java public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) { @@ -1444,9 +1444,9 @@ public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) { } ``` -# ?????? ????????????? +# еĸ -## 53 ?????????????????????? +## 53 гֵĴ @@ -1464,7 +1464,7 @@ public int GetNumberOfK(int[] array, int k) { } ``` -## 54. ????????????? k ????? +## 54. ĵ k ```java TreeNode ret; @@ -1485,7 +1485,7 @@ private void inorder(TreeNode root, int k) { } ``` -## 55 ??????????? +## 55 ```java public int TreeDepth(TreeNode root) { @@ -1494,25 +1494,25 @@ public int TreeDepth(TreeNode root) { } ``` -## 56. ?????????????????? +## 56. ֻһε -**???????** +**Ŀ** -????????????????????????????????????????????????????????????? +һ֮⣬ֶΣҳ -**?????** +**˼·** -????????????????????????????????????? +ȵԪλʾϱضһλڲͬ -?????????????????????????????????????????????????? +ԪõĽΪظԪĽ -diff &= -diff ????? diff ?????? 0 ????????????????????????????????????????????????????????????????????????????? +diff &= -diff õ diff Ҳ಻Ϊ 0 λҲDzظԪλʾҲ಻ͬһλһλͿԽԪֿ ```java public void FindNumsAppearOnce(int[] array, int num1[], int num2[]) { int diff = 0; for (int num : array) diff ^= num; - // ???????? + // õһλ diff &= -diff; for (int num : array) { if ((num & diff) == 0) num1[0] ^= num; @@ -1521,11 +1521,11 @@ public void FindNumsAppearOnce(int[] array, int num1[], int num2[]) { } ``` -## 57.1 ??? S ?????????? +## 57.1 Ϊ S -**???????** +**Ŀ** -???????????????????????????? S??????????????????????????????????? S????????????????? S???????????????????? +һһ Sвǵǵĺ Sжֵĺ͵ Sij˻Сġ ```java public ArrayList FindNumbersWithSum(int[] array, int sum) { @@ -1540,11 +1540,11 @@ public ArrayList FindNumbersWithSum(int[] array, int sum) { } ``` -## 57.2 ??? S ?????????????? +## 57.2 Ϊ S -**???????** +**Ŀ** -??? 100 ???????????? 18, 19, 20, 21, 22 +Ϊ 100 18, 19, 20, 21, 22 ```java public ArrayList> FindContinuousSequence(int sum) { @@ -1575,13 +1575,13 @@ public ArrayList> FindContinuousSequence(int sum) { } ``` -## 58.1 ???????????? +## 58.1 ת˳ -**???????** +**Ŀ** -????"I am a student." +룺"I am a student." -?????"student. a am I" +"student. a am I" ```java public String ReverseSentence(String str) { @@ -1611,11 +1611,11 @@ private void reverse(char[] c, int start, int end) { } ``` -## 58.2 ?????????? +## 58.2 תַ -**???????** +**Ŀ** -???????????????????? S???????????????? K ?????????????????????? S=??abcXYZdef??, ????????????? 3 ???????????XYZdefabc???? +һַ Sѭ K λ磬ַ S=abcXYZdef, Ҫѭ 3 λĽXYZdefabc ```java public String LeftRotateString(String str, int n) { @@ -1638,11 +1638,11 @@ private void reverse(char[] c, int i, int j) { } ``` -## 59. ????????????? +## 59. ڵֵ -**???????** +**Ŀ** -??????????????????????????????????????????????????????????????? {2, 3, 4, 2, 6, 2, 5, 1} ???????????? 3???????????? 6 ???????????????????????? {4, 4, 6, 6, 6, 5}?? +һͻڵĴСҳлֵֵ磬 {2, 3, 4, 2, 6, 2, 5, 1} ڵĴС 3ôһ 6 ڣǵֱֵΪ {4, 4, 6, 6, 6, 5} ```java public ArrayList maxInWindows(int[] num, int size) { @@ -1660,11 +1660,11 @@ public ArrayList maxInWindows(int[] num, int size) { } ``` -## 61. ???????? +## 61. ˿˳ -**???????** +**Ŀ** -???????????????????????? 0???????????????? +ƣдСΪӣСΪ 0жǷ˳ӡ ```java public boolean isContinuous(int[] numbers) { @@ -1682,15 +1682,15 @@ public boolean isContinuous(int[] numbers) { } ``` -## 62. ????????????? +## 62. ԲȦʣµ -**???????** +**Ŀ** -????????????????????? , ????????????? m, ????? 0 ??????????????????? m-1 ????????????????? , ??????????????????????????? , ???????????? , ????????????????? , ???? 0...m-1 ???? .... ??????? .... ???????????????? , ??????????? +СΧһȦȻ , ָһ m, ñΪ 0 Сѿʼÿκ m-1 ǸСҪг׸ , ȻƷѡ , ҲٻصȦ , һСѿʼ , 0...m-1 .... ȥ .... ֱʣһС , Բñݡ -**?????** +**˼·** -???? +Լɪ ```java public int LastRemaining_Solution(int n, int m) { @@ -1700,11 +1700,11 @@ public int LastRemaining_Solution(int n, int m) { } ``` -## 63. ???????????? +## 63. Ʊ -**???????** +**Ŀ** -??????????????????????????????????????????? +һһǰ档 ```java public int maxProfit(int[] prices) { @@ -1720,11 +1720,11 @@ public int maxProfit(int[] prices) { } ``` -## 64. ?? 1+2+3+...+n +## 64. 1+2+3+...+n -**???????** +**Ŀ** -?? 1+2+3+...+n???????????????for??while??if??else??switch??case ????????????????A?B:C?? + 1+2+3+...+nҪʹó˳forwhileifelseswitchcase ȹؼּж䣨A?B:C ```java public int Sum_Solution(int n) { @@ -1733,9 +1733,9 @@ public int Sum_Solution(int n) { } ``` -## 65. ????????????? +## 65. üӼ˳ӷ -a ^ b ????????????????????????(a & b) << 1 ????????????????????? (a & b) << 1 ?????????? 0??????????????????? 0 ?????????????????? 0?????????? +a ^ b ʾûпǽλĺͣ(a & b) << 1 ǽλݹֹԭ (a & b) << 1 ұ߻һ 0ôݹ飬λұߵ 0 ࣬λΪ 0ݹֹ ```java public int Add(int num1, int num2) { @@ -1744,11 +1744,11 @@ public int Add(int num1, int num2) { } ``` -## 66. ??????????? +## 66. ˻ -**???????** +**Ŀ** -??????????? A[0, 1,..., n-1], ??????????? B[0, 1,..., n-1], ???? B ????? B[i]=A[0]\*A[1]\*...\*A[i-1]\*A[i+1]\*...\*A[n-1]????????????? +һ A[0, 1,..., n-1], 빹һ B[0, 1,..., n-1], B еԪ B[i]=A[0]\*A[1]\*...\*A[i-1]\*A[i+1]\*...\*A[n-1]ʹó ```java public int[] multiply(int[] A) { @@ -1773,9 +1773,9 @@ public int[] multiply(int[] A) { } ``` -# ?????? ??????????? +# ԰ -## 67. ???????????????? +## 67. ַת ```java public int StrToInt(String str) { @@ -1792,9 +1792,9 @@ public int StrToInt(String str) { } ``` -## 68. ?????????????????????? +## 68. ڵ͹ -???????????????????????????? +Ƕ͹⣺ ```java public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { diff --git a/notes/程序员的职业素养.md b/notes/程序员的职业素养.md index efadf001..b0a7761d 100644 --- a/notes/程序员的职业素养.md +++ b/notes/程序员的职业素养.md @@ -1,129 +1,129 @@ -* [??????](#??????) -* [???](#???) -* [??????](#??????) -* [???](#???) -* [???](#???) -* [???](#???) -* [????????????](#????????????) -* [???](#???) +* [](#) +* [ǰ](#ǰ) +* [רҵ](#רҵ) +* [˵](#˵) +* [˵](#˵) +* [](#) +* [](#) +* [ϰ](#ϰ) -# ?????? +# -???? Bob ???????????????????????????????????????????????????????????????? + Bob 壬Сԭģʽʵ֮Աְҵ -???????????????????????????????????????????????????????????????????? +ڽ⣬ҪIJⱾǽķʽԼ˼ȣְҵ -# ??? +# ǰ -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ڹٺܴIJѹӼԱΣվ棬Žˡսߡɻյºɻڸ߿ձը˵ԱѾܶȥֹη䣬Dz˵ģǾûд绰ȥ̨¶˴ηΣԡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/bccb799f-56e2-4356-95f0-a9ea05b0de2a.jpg) -# ?????? +# רҵ -?????I?????????????????????????? +רҵ岻ζͽҲζΡ -???????????????????????????????????????????????????????????????????????????????? bug ????????????????????????????????????????????? +Լһ绰·ϼϵͳʱΪڵս¹ܣûжϵͳвԣһ bug ij֡Ϊһֲεı֣ҲDzרҵı֡ -???????????? bug???????????????? bug ???? + bugDzζ㲻Ϊ bug -????? QA ????? bug ??????????? QA ???? bug??????????????????? bug??????????? +˰ QA ɲ bug Ļ QA bugԼȴȥ bugDzԵġ -???????????????????????????????????????????????????????? +Ϊ˱֤ȷԣҪдһЩʱеĵԪԣҲϵȥǡ -?????????????? - ???????????????????????????????????????????? +Ŀĸԭ - ޸ġΪԼ޸ģҪȥ޸ -???????????????????????????????????????????????????????? +ԪԿóԱȥ޸Ĵ룬Ϊȷ޸ĵǷȷ -??????????????????? +רҵݣ -1. ????? -2. ??????????????? -3. ???????????????????????????? -4. ????????????????????????????????????????????????????? -5. ??????????????????????????? -6. ????? / ?????????? -7. ????????????????????????? +1. ѧϰ +2. ÿϰһЩ򵥵ıĿ +3. Ǻӱ˴ѧණ +4. ͨԼ˼룬ԼIJԼ˼ϵIJ졣 +5. ˽ҵҼ鿴 +6. / ͻһ +7. ǫѷΪ֪ԼҲܷ -# ??? +# ˵ -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +һЩҪ󣬲ֻ˵ԿᱻǽҪԼֲ֪ҪᵼصĺͲչȥһҪ˵ -??????????????????????????????????????????? +˵ʱҪϸϸ˵Ƿط -???????????????????????????????????????? +ҪԲʱЭһЩ˫ܽܵķ -"?????????????" ?????????????????????????????????"?????????"?????????????????????????????????????? ipone ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Bob ???????????????????????????????????????????????????????????????????????????? +"пдô" ƪģһοһΪ"ɫ"ܹù˿ͿƷϢ̵ϢԼŻȹܵ ipone Ӧãʼ̵ľŵ˵ֻҪдӲӦþˣǿͻҪκһܣܱʼʱ˵ҪࡣʱĽͻIJϣԼıòдһѺõĴ룬ԼȷʵЩģʽȸ߼ǷdzԵġΪʵʵĿΪͻأдԼдĴ롣Bob Ϊƪĵ߲Ǵ˴οЩĸˣΪûжЩҪ˵ -# ??? +# ˵ -??????????????????s????????????????????????? +ŵ裺ͷϴӦж -??????????? +ȱŵĴ -1. ???????????????????? -2. ??????????????????????? -3. ??????????????????????? +1. ҪҪꣻ +2. ϣϣ +3. ǣǰꡣ -????????????????????????????????????????????????????????????????????????????...??...?? +ȱŵ˲صԼϣҲȷ˵һĽֹڡijŵġһ...֮ǰ... -?????????????????????????????? +Լ޷ȫյ£Ҫ׳ŵ -???????????????????????????????????????? +Լ޷ŵȥ˶Ԥڡ -??????????????????????????????????????????????????????????????????????????????????????????????????????????????? +Կ˵ԼԳŵе˼ɣ˲ҪԿΪһֳŵȷԼܴﵽŵݶŵôصĺ -# ??? +# -????????????????????????????????????????? +ҪͨһҪԸü߱ġ֪͡ -?????????????????????????????????????????????????????????????????????????????????????????????? +״̬õʱñд롣״̬÷Ϊƣͣ簾ҹǣΪԼܰʱӦµٽб̡ -?????????????????????????????????????????????????????????????????????????????????? +״̬ĸоܺãÿܶ£״̬ʵһdzڤ룬˼ά½Ҫ״̬ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f0321ed1-fa93-460e-951b-4239fef819f3.jpg) -????????????????????????????????????????????????????????????????? +״̬صǸͨԱָܹ˽Աֹܹ״̬ -???????????????????????????????????????????????????????????????????????????????????????????? +ԼûʱԱǸѡ񣬽ԱʱԺлѧ仯Ҳ˵˼άʽı䣬ͻƵǰ˼ά״̬ -?????????????????????????????????????????????????????? +ʱֻռһԴ˻ᵼЧ½ⲻǾԡ -?????????????????????????????????????????????????? +һЩԵݣƻС˵ȣԼĴҲõ -?????????????????????????????????????? +ʱҲܱ󣬲ܹ͵Եʱ䡣 -?????????????????????????????????????????????????????????????????????????????????????????????????????? +һ޷ȫٶỵֻ̄ͨάֽȡʤҪֺԼĽ࣬ƣ͵ʱʵϢ -????????????????????????????????????????????????? +ȺֹԤ׼ԤͱԤʱ㡣 -???????????????????????????????????????????????????????????????????????? +ԼԤĽȸϲϽֹʱ䣬ҪӦڽֹʱȥɣΪ֪ԼԤDzܵġ -?????????????????????????????????????^ +ɵı׼ͨһԶղ塣 -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ҲԼôڰ˵ʱҪԼִܲ٣Ӧһ˲Ҫرʱ䡣ҲҪѧ˵İΪԼܵģͨ˵İܸõĽ⡣ -# ???????????? +# -TDD ????????????????????????????????????????????????????????????????????????????????????????????????????????????????? bug???? bug ???????????????? +TDD ں̣ܶһӾͿһγ򣬶ڿԿٵõڿرҪһ߳Աļ飬һܹ緢 bug bug Խ緢޸ĵشҲԽ͡ -TDD ?????? +TDD -1. ??????? -2. ?????????????????????????????? -3. ??????????????????????????????????????? +1. дԣ +2. һԪʧܵʱ򣬲ҪдԴ룻 +3. ƷǡͨǰʧܵĵԪԼɣҪд -TDD ??? +TDD ŵ -1. ?????????????????????? -2. ??????????????????????????????????????????????????????????? -3. ??????????????????????????????????????????????????????????? +1. ȷԣȷǰϵͳǷȷ +2. ģԱϢȥ޸ĻҵĴ룬ΪͨԪԿ֪Ƿ޸ĵȷ +3. ĵԪǵײϸڵĵͨĶԪܹ֪зʽ -# ??? +# ϰ -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +һƺõʽʽģʵϰ߲ѵϤʽӶ졣̿һױ̹ûͼ̵Ķϰ߲Ϊ˽⣬ΪѾ֪˽ϰҪĶ;ߡ -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʹ˶Ŀڱ򣬿һдԪԣһдͨԪԡ磬һʵ㷨дԵ˿Ժ׵ٶȺڴ棬ͬʩѹ diff --git a/notes/算法.md b/notes/算法.md index a69d2ddb..7da2d004 100644 --- a/notes/算法.md +++ b/notes/算法.md @@ -1,100 +1,98 @@ -* [????](#????) - * [?](#?) - * [1. ???????](#1-???????) - * [2. ???????](#2-???????) - * [????](#????) - * [??????](#??????) - * [1. ???????](#1-???????) - * [2. ??????](#2-??????) +* [](#) + * [ջ](#ջ) + * [1. ʵ](#1-ʵ) + * [2. ʵ](#2-ʵ) + * [](#) + * [㷨](#㷨) + * [1. ת](#1-ת) + * [2. ѧģ](#2-ѧģ) * [3. ThreeSum](#3-threesum) - * [4. ???????](#4-???????) - * [5. ???????](#5-???????) + * [4. ʵ](#4-ʵ) + * [5. ע](#5-ע) * [union-find](#union-find) - * [1. quick-find ??](#1-quick-find-??) - * [2. quick-union ??](#2-quick-union-??) - * [3. ??? quick-union ??](#3-???-quick-union-??) - * [4. ????????? quick-union ??](#4-?????????-quick-union-??) - * [5. ???? union-find ??????](#5-????-union-find-??????) -* [????? ????](#?????-????) - * [??????????](#??????????) - * [1. ???](#1-???) - * [2. ???????](#2-???????) - * [3. ????????](#3-????????) - * [4. ??????????????????](#4-??????????????????) - * [5. ???????](#5-???????) - * [?????](#?????) - * [1. ?????](#1-?????) - * [2. ???????????](#2-???????????) - * [3. ???????????](#3-???????????) - * [????????](#????????) - * [1. ??????](#1-??????) - * [2. ??](#2-??) - * [3. ???????](#3-???????) - * [4. ?????](#4-?????) - * [4.1 ????????????](#41-????????????) - * [4.2 ?????](#42-?????) - * [4.3 ??????](#43-??????) - * [???????](#???????) - * [1. ??](#1-??) - * [2. ????????](#2-????????) - * [3. ???????](#3-???????) - * [4. ?????????](#4-?????????) - * [5. ??????](#5-??????) - * [6. ????](#6-????) - * [???](#???) - * [1. ??????????](#1-??????????) - * [2. Java ???????????](#2-java-???????????) - * [3. ???????????????](#3-???????????????) -* [?????? ????](#??????-????) - * [?????](#?????) - * [1. API](#1-api) - * [2. ????????](#2-????????) - * [3. ???????????](#3-???????????) - * [4. ???????????????](#4-???????????????) - * [5. ????????????](#5-????????????) - * [?????????](#?????????) + * [1. quick-find 㷨](#1-quick-find-㷨) + * [2. quick-union 㷨](#2-quick-union-㷨) + * [3. Ȩ quick-union 㷨](#3-Ȩ-quick-union-㷨) + * [4. ·ѹļȨ quick-union 㷨](#4-·ѹļȨ-quick-union-㷨) + * [5. union-find 㷨ıȽ](#5--union-find-㷨ıȽ) +* [ڶ ](#ڶ-) + * [㷨](#㷨) + * [1. Լ](#1-Լ) + * [2. ѡ](#2-ѡ) + * [3. ](#3-) + * [4. ѡͲıȽ](#4-ѡͲıȽ) + * [5. ϣ](#5-ϣ) + * [鲢](#鲢) + * [1. 鲢](#1-鲢) + * [2. Զ¹鲢](#2-Զ¹鲢) + * [3. ԵϹ鲢](#3-ԵϹ鲢) + * [](#) + * [1. 㷨](#1-㷨) + * [2. з](#2-з) + * [3. ܷ](#3-ܷ) + * [4. 㷨Ľ](#4-㷨Ľ) + * [4.1 л](#41-л) + * [4.2 ȡ](#42-ȡ) + * [4.3 з](#43-з) + * [ȶ](#ȶ) + * [1. ](#1-) + * [2. ϸ³](#2-ϸ³) + * [3. Ԫ](#3-Ԫ) + * [4. ɾԪ](#4-ɾԪ) + * [5. ](#5-) + * [6. ](#6-) + * [Ӧ](#Ӧ) + * [1. 㷨ıȽ](#1-㷨ıȽ) + * [2. Java 㷨ʵ](#2-java-㷨ʵ) + * [3. зֵĿѡ㷨](#3-зֵĿѡ㷨) +* [ ](#-) + * [ű](#ű) + * [1. ű](#1-ű) + * [2. ű](#2-ű) + * [3. ֲʵű](#3-ֲʵű) + * [](#) * [1. get()](#1-get) * [2. put()](#2-put) - * [3. ????](#3-????) + * [3. ](#3-) * [4. floor()](#4-floor) * [5. rank()](#5-rank) * [6. min()](#6-min) * [7. deleteMin()](#7-deletemin) * [8. delete()](#8-delete) * [9. keys()](#9-keys) - * [10. ???????](#10-???????) - * [????????](#????????) - * [2-3 ??????](#2-3-??????) - * [1. ???????](#1-???????) - * [2. ????](#2-????) - * [???????????](#???????????) - * [1. ?????](#1-?????) - * [2. ?????](#2-?????) - * [3. ??????](#3-??????) - * [4. ????](#4-????) - * [5. ???????](#5-???????) - * [6. ????](#6-????) - * [???](#???) - * [?????](#?????) - * [???????????????](#???????????????) - * [????????????????](#????????????????) - * [????](#????) - * [????](#????) - * [???](#???) - * [?????????](#?????????) - * [???](#???) - * [??????????????](#??????????????) - * [Java ?????????](#java-?????????) - * [????????](#????????) - * [??????????](#??????????) + * [10. ܷ](#10-ܷ) + * [ƽ](#ƽ) + * [2-3 ](#2-3-) + * [1. ](#1-) + * [2. ](#2-) + * [ڶ](#ڶ) + * [1. ת](#1-ת) + * [2. ת](#2-ת) + * [3. ɫת](#3-ɫת) + * [4. ](#4-) + * [5. ɾС](#5-ɾС) + * [6. ](#6-) + * [ɢб](#ɢб) + * [ɢк](#ɢк) + * [ɢб](#ɢб) + * [̽ⷨɢб](#̽ⷨɢб) + * [](#) + * [](#) + * [ɾ](#ɾ) + * [С](#С) + * [Ӧ](#Ӧ) + * [ַűʵֵıȽ](#ַűʵֵıȽ) + * [Java ķűʵ](#java-ķűʵ) + * [](#) + * [ϡ˷](#ϡ˷) -# ???? +# -## ? +## ջ -### 1. ??????? +### 1. ʵ ```java @@ -117,7 +115,7 @@ public class ResizeArrayStack implements Iterable { return item; } - // ????????????????????????? + // Сʹջ private void resize(int size) { Item[] tmp = (Item[]) new Object[size]; for (int i = 0; i < N; i++) { @@ -136,7 +134,7 @@ public class ResizeArrayStack implements Iterable { @Override public Iterator iterator() { - // ????????????????????? + // Ҫĵ return new ReverseArrayIterator(); } @@ -156,15 +154,15 @@ public class ResizeArrayStack implements Iterable { } ``` -????????????????Java ?????????????????????????????????? +ʵʹ˷ͣJava ֱӴ飬ֻʹת ```java Item[] arr = (Item[]) new Object[N]; ``` -### 2. ??????? +### 2. ʵ -????????????????????????????????????????????????????? next ?????????????????????????????????????????????????????????? +Ҫʹͷ巨ʵ֣Ϊͷ巨ѹջԪĿͷ next ָָǰһѹջԪأڵԪʹͿǰһѹջԪسΪջԪء ```java public class Stack { @@ -202,11 +200,11 @@ public class Stack { } ``` -## ???? +## -??????????????????????? first ?? last ??????????????????? +Ƕеʵ֣Ҫά first last ڵָ룬ֱָ׺Ͷβ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? next ?????????????????????????? first ???????????? +Ҫĸָָͷڵ㣬ĸָָβڵ㡣ΪвҪöԪصһԪسΪףҪ׻ȡһԪأͷڵ next ָָһԪأöָ first ָĿͷ ```java public class Queue { @@ -226,7 +224,7 @@ public class Queue { return N; } - // ????? + // public void enqueue(Item item){ Node newNode = new Node(); newNode.item = item; @@ -241,7 +239,7 @@ public class Queue { N++; } - // ?????? + // public Item dequeue(){ Node node = first; first = first.next; @@ -251,41 +249,41 @@ public class Queue { } ``` -## ?????? +## 㷨 -### 1. ??????? +### 1. ת -??????????????????????????????????????????????? +ָתΪԺӶںͼʾĸֱۡ -T(N)=aN3 ???? lg(T(N))=3lgN+lga +T(N)=aN3 תΪ lg(T(N))=3lgN+lga ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5510045a-8f32-487f-a756-463e51a6dab0.png) -### 2. ?????? +### 2. ѧģ -**????** +**** -??? \~f(N) ????????????? N ????????? f(N) ?????????? 1 ????? , ???? N3/6-N2/2+N/3 \~ N3/6?? +ʹ \~f(N) ʾ N f(N) Ľ 1 ĺ , N3/6-N2/2+N/3 \~ N3/6 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ca3a793e-06e5-4ff3-b28e-a9c20540d164.png) -**??????????** +**** -???????????????????????????????????????????????? N3 ????????? Java ??????????????????????????? +㷨ʵָ뿪һ㷨Ϊ N3 Ƿ Java ʵ֣Ƿض޹ء ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1ea4dc9a-c4dd-46b5-bb11-49f98d57ded1.png) -**?????** +**ѭ** -?????????????????????????????????????????????????? +ִƵָ˳ִеʱ䣬ЩָΪѭ -**??????** +**ɱģ** -????????????????????????????????????????????? +ʹóɱģ㷨ķʴһֳɱģ͡ ### 3. ThreeSum -ThreeSum ???????????????????????????? 0 ???????? +ThreeSum ͳһԪĺΪ 0 ```java public class ThreeSum { @@ -306,13 +304,13 @@ public class ThreeSum { } ``` -??????????? if (a[i] + a[j] + a[k] == 0) ????????????? N3/6-N2/2+N/3?????????????????? \~N3/6????????????? N3?? +óѭΪ if (a[i] + a[j] + a[k] == 0) 䣬ִܹеĴΪ N3/6-N2/2+N/3ĽִдΪ \~N3/6Ϊ N3 -**???** +**Ľ** -??????????????????????????????????????????????????????????????????????????????????? 0?? +ͨ򣬶ԪͣöֲҷǷڸú͵෴ڣ˵ԪĺΪ 0 -????????? ThreeSum ????????????????? N2logN?? +÷Խ ThreeSum 㷨Ϊ N2logN ```java public class ThreeSumFast { @@ -323,7 +321,7 @@ public class ThreeSumFast { for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { for (int k = j + 1; k < N; k++) { - // rank() ???????????????????????????????????????? -1????????????????????? j???????????????????? + // rank() Ԫе±꣬Ԫزڣ᷵ -1Ӧע± jͲظͳˡ if (BinarySearch.rank(-a[i] - a[j], a) > j) { cnt++; } @@ -335,41 +333,41 @@ public class ThreeSumFast { } ``` -### 4. ??????? +### 4. ʵ -??? T(N) \~ aNblgN????? T(2N)/T(N) \~ 2b?????????????????? ThreeSum ???????????? \~N3/6????????????????????????? + T(N) \~ aNblgNô T(2N)/T(N) \~ 2bڱ ThreeSum 㷨ʱΪ \~N3/6бʵõ½ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6f5ed46f-86d7-4852-a34f-c1cf1b6343a0.png) -??? T(2N)/T(N)\~23??????? b ? 3?? +ɼ T(2N)/T(N)\~23Ҳ b Ϊ 3 -### 5. ??????? +### 5. ע -**????** +**** -??????????????????????????????????????????????? +ʱͼijϵܴôƵĽǴġ -**????** +**** -??????????????y?????????????????????????????????????????? +ϵͳʹû漼֯ڴ棬ڵԪػȷʲڵԪؿܶࡣ -**?????????????????** +**µܵı֤** -??????????????????????????????????????????????????????????? +ں˷ӦѡɲеµʮҪġ -**???????** +**㷨** -????????????????????????????? +ͨ룬ȥ㷨 -**???????** +**̯** -???????????????????????????????????????????????????? N ???????? push() ???????????????????? N+4+8+16+...+2N=5N-4??N ????????????????????????????????????????????????????????????????????????????????????????? +вܳɱԲɱ̯һջ N push() ҪԪΪ N+4+8+16+...+2N=5N-4N дԪأĶǵСʱиҪķ̯ÿβƽΪ ## union-find -**????** +**** -???????????????????????????????????????????????????? +ڽ̬ͨ⣬̬ܶ㣬жǷӡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5d387d02-6f96-44d6-b5d0-4538349f868e.png) @@ -377,11 +375,11 @@ public class ThreeSumFast { ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a9b91b7d-65d7-4aa3-8ef6-21876b05ad16.png) -**?????????** +**ݽṹ** ```java public class UF { - // ??? id ?????????????????? + // ʹ id ͨϢ private int[] id; public UF(int N) { @@ -397,11 +395,11 @@ public class UF { } ``` -### 1. quick-find ?? +### 1. quick-find 㷨 -??????????????????????? id ????? +֤ͬһͨд id ֵȡ -????????????????????????? id ????????????????????????????? union ???????????????????????????????????????? id ???????????????? id ??? +ַԿȡһ id ֵжǷͨ union IJȴܸߣҪһͨенڵ id ֵ޸Ϊһڵ id ֵ ```java public int find(int p) { @@ -418,9 +416,9 @@ public class UF { } ``` -### 2. quick-union ?? +### 2. quick-union 㷨 -?? union ????????? id ????????????? id ?????????? id ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? id ??????????????? id??? + union ʱֻ id ֵָһ id ֱֵ id 洢ͨ͹һõνṹڵҪָԼڽвһڵͨʱҪһֱϲֱڵ㣬ʹøڵ id ֵΪͨ idֵ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9192dc0a-a7cd-4030-8df6-e388600644cf.jpg) @@ -438,22 +436,22 @@ public class UF { } ``` -????????????????? union ?????????? find ?????????????????????????????????????????? +ַԿٽ union find ߳ȣĸ߶ΪĿ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d206d090-d911-4263-a1fe-d6f63f5d1776.png) -### 3. ??? quick-union ?? +### 3. Ȩ quick-union 㷨 -????? quick-union ??????????????????? quick-union ?? union ?????????????????????????? +Ϊ˽ quick-union ܸͨߵ⣬Ȩ quick-union union ʱýСӽϴ档 -?????????????? quick-union ??????????????????? lgN?? +о֤Ȩ quick-union 㷨಻ lgN ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8d6af5ac-74eb-4e07-99aa-654b9f21f1d3.jpg) ```java public class WeightedQuickUnionUF { private int[] id; - // ?????????????? + // ڵϢ private int[] sz; public WeightedQuickUnionUF(int N) { @@ -489,25 +487,25 @@ public class WeightedQuickUnionUF { } ``` -### 4. ????????? quick-union ?? +### 4. ·ѹļȨ quick-union 㷨 -?????????????????????????????????? find ??????????????? +ڼڵͬʱֱӵڵ㣬ֻҪ find һѭɡ -### 5. ???? union-find ?????? +### 5. union-find 㷨ıȽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e5baeb38-0ec9-4ad7-8374-1cdb0dba74a6.jpg) -# ????? ???? +# ڶ -## ?????????? +## 㷨 -### 1. ??? +### 1. Լ -???????????????? Java ?? Comparable ?????????? compareTo() ?????? +ԪҪʵ Java Comparable ӿڣýӿ compareTo() -?????????????????????????????????????? +о㷨ijɱģʱDZȽϺͽĴ -?????????? less() ?? exch() ??????????????????????????????????????? +ʹø less() exch() бȽϺͽIJʹôĿɶԺͿֲԸá ```java private static boolean less(Comparable v, Comparable w){ @@ -521,9 +519,9 @@ private void exch(Comparable[] a, int i, int j){ } ``` -### 2. ??????? +### 2. ѡ -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ҵеСԪأȻĵһԪؽλáȻٴʣµԪҵСԪأĵڶԪؽλáϽIJֱ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/222768a7-914f-4d64-b874-d98f3b926fb6.jpg) @@ -542,11 +540,11 @@ public class Selection { } ``` -?????????? \~N2/2 ???? \~N ????????????????????????????????????????????????????????????????????????????? +ѡҪ \~N2/2 αȽϺ \~N νʱ޹أصʹһѾҲҪôıȽϺͽ -### 3. ???????? +### 3. -?????????????????????????????????????????????????????????????????????????????????????????????? +һԪز뵽Уʹò֮ҲġҲÿԪأÿβ֮󲿵ġ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/065c3bbb-3ea0-4dbf-8f26-01d0e0ba7db7.png) @@ -563,21 +561,21 @@ public class Insertion { } ``` -????????????????????????????????????????????????????????????????????????????????? \~N2/4 ?????? \~N2/4 ?????????????????? \~N2/2 ?????? \~N2/2 ????????????????????????????????????????? N-1 ???? 0 ??????????????????????????????? +ĸӶȡijʼ˳Ѿˣôܿ졣ƽ²Ҫ \~N2/4 ȽԼ \~N2/4 νҪ \~N2/2 ȽԼ \~N2/2 νģõҪ N-1 αȽϺ 0 νõѾˡ -???????????????????????????????????? +ڲСģرЧ -### 4. ?????????????????? +### 4. ѡͲıȽ -?????????????????????????????????????????????????????????????????????????????????? +ظ飬ѡʱƽģ֮һСij -### 5. ??????? +### 5. ϣ -???????????????????????????????????????????????????????????????????????????????? +ڴģ飬ΪֻܽڵԪأҪԪشһƵһˣҪܶβ -????????????????????????????????????????????????????????????????????????????????? +ϣij־Ϊ˸Ľ־ԣͨڵԪأʹԪظƵȷλϡ -??????????????????? h ??????????????? h ??????????????????????????????????? h??????? h=1?????????????????????????? +ϣʹòԼ h н h ܴôԪؾܺܿƵԶĵطͨϼС h h=1Ϳʹġ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8320bad6-3f91-4a15-8e3d-68e8f39649b5.png) @@ -601,15 +599,15 @@ public class Shell { } ``` -??????????????????????????????????? 1, 4, 13, 40, ... ??????????????????????????? N ??????????????????????????????????????????????????????????? +ϣʱﲻƽʹõ 1, 4, 13, 40, ... ϣҪıȽϴᳬ N ɱڵеijȡܵĸ߼㷨ֻϣҡ -## ????? +## 鲢 -???????????????????????????????????????????? +鲢˼ǽֱֳ֣Ȼ鲢 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/dcf265ad-fe35-424d-b4b7-d149cdf239f4.png) -### 1. ????? +### 1. 鲢 ```java public class MergeSort { @@ -619,20 +617,20 @@ public class MergeSort { int i = lo, j = mid + 1; for (int k = lo; k <= hi; k++) { - aux[k] = a[k]; // ?????????????????? + aux[k] = a[k]; // ݸƵ } for (int k = lo; k <= hi; k++) { if (i > mid) a[k] = aux[j++]; else if (j > hi) a[k] = aux[i++]; - else if (aux[i].compareTo(a[j]) < 0) a[k] = aux[i++]; // ???????????????????? + else if (aux[i].compareTo(a[j]) < 0) a[k] = aux[i++]; // Ƚһ֤ȶ else a[k] = aux[j++]; } } } ``` -### 2. ??????????? +### 2. Զ¹鲢 ```java public static void sort(Comparable[] a) { @@ -653,13 +651,13 @@ public class MergeSort { ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c7665f73-c52f-4ce4-aed3-592bbd76265b.png) -????????????????????????? O(NlgN)?? +׿㷨ʱ临ӶΪ O(NlgN) -????????????????????????????????????????????q????????????? +ΪСĵݹƵʹòС齫øߵܡ -### 3. ??????????? +### 3. ԵϹ鲢 -????????????????????????? +ȹ鲢Щ΢飬ȻɶԹ鲢õ顣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c7b9b4c8-83d1-4eb0-8408-ea6576a9ed90.png) @@ -675,11 +673,11 @@ public class MergeSort { } ``` -## ???????? +## -### 1. ?????? +### 1. 㷨 -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +鲢Ϊֱ򣬲鲢ʹ򣻿ͨһзԪؽΪ飬СڵзԪأڵзԪأҲͽˡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/61b4832d-71f3-413c-84b6-237e219b9fdc.png) @@ -699,9 +697,9 @@ public class QuickSort { } ``` -### 2. ?? +### 2. з -? a[lo] ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? j ?????????????????????????????????????????? a[lo] ??????????????????? a[j] ???????? j ???? +ȡ a[lo] ΪзԪأȻɨֱҵһڵԪأٴҶɨҵһСڵԪأԪأϼ̣ͿԱָ֤ԪضзԪأָ j ҲԪضСзԪءָʱзԪ a[lo] ҲԪ a[j] Ȼ󷵻 j ɡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e198c201-f386-4491-8ad6-f7e433bf992d.png) @@ -720,29 +718,29 @@ public class QuickSort { } ``` -### 3. ??????? +### 3. ܷ -??????????????????????????????????????????????? +ԭ򣬲Ҫ飬ǵݹҪջ -?????????????????????????????????????????????????????????????????????????? CN=2CN/2+N???????????? O(NlgN)?? +õÿζܽ԰֣ݹôٵġ±ȽϴΪ CN=2CN/2+NҲǸӶΪ O(NlgN) -???????????????????????????????????????????????????????????????? N2/2????????????????????????????????????????????????? +£һδСԪз֣ڶδӵڶСԪз֣㡣ҪȽ N2/2Ϊ˷ֹʼģڽпʱҪ顣 -### 4. ????? +### 4. 㷨Ľ -#### 4.1 ???????????? +#### 4.1 л -??????????????????????????????????????????????????????????????????????????????????????? +ΪСҲԼС飬ȿܸãСпл -#### 4.2 ????? +#### 4.2 ȡ -?????????????????????????????????????????????????????????????? 3 ??????????????????????????????? +õÿζȡλΪзԪأǼλĴۺܸߡǷȡ 3 ԪزСеԪΪзԪصЧá -#### 4.3 ?????? +#### 4.3 з -??????????????????????????????????????????????????????????? +дظԪص飬ԽзΪֱ֣ӦСڡںʹзԪء -??????????????????????????????????????????????????????????? +зֻֿɲͬʱ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9d2226dc-c4a3-40ec-9b3e-a46bf86af499.png) @@ -764,15 +762,15 @@ public class Quick3Way { } ``` -## ??????? +## ȶ -???????????????????????? +ȶҪڴԪء -### 1. ?? +### 1. -?????????????????????????????????????? +壺һŶÿڵ㶼ڵӽڵ㡣 -??????????????????????????????????????????????????????????????????? k ???????????? k/2????????????????????? 2k ?? 2k+1??????????????????????? 0 ??????????????????????????? +ѿʾΪһȫȫ׾ʹ洢Сλ k ĽڵĸڵλΪ k/2ӽڵλ÷ֱΪ 2k 2k+1DzʹΪ 0 λãΪ˸ڵĹϵ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a9b6c1db-0f4a-4e91-8ac8-6b19bd106b51.png) @@ -805,9 +803,9 @@ public class MaxPQ { } ``` -### 2. ???????? +### 2. ϸ³ -????????????????????????????????????????????????????????????????????????????????????????????????????? +ڶУһڵȸڵôҪڵ㡣󻹿ܱµĸڵҪϵؽбȽϺͽֲΪϸ ```java private void swim(int k) { @@ -818,7 +816,7 @@ private void swim(int k) { } ``` -????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ƵأһڵӽڵСҲҪϵ±ȽϺͽֲΪ³һڵӽڵ㣬Ӧӽڵôڵн ```java private void sink(int k) { @@ -832,9 +830,9 @@ private void sink(int k) { } ``` -### 3. ??????? +### 3. Ԫ -??????????????????????????????? +ԪطŵĩβȻϸʵλá ```java public void insert(Key v) { @@ -843,9 +841,9 @@ public void insert(Key v) { } ``` -### 4. ????????? +### 4. ɾԪ -?????B???????????????????????????????????????????????????????????? +鶥ɾԪأһԪطŵˣԪ³ʵλá ```java public Key delMax() { @@ -857,13 +855,13 @@ public Key delMax() { } ``` -### 5. ?????? +### 5. -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ڶѿԺ׵õԪزɾϵؽֲԵõһݼСԪغ͵ǰһԪؽλãҲɾôͿԵõһβͷĵݼУһС˺ʹö򣬲Ҷԭ򣬲ռöռ䡣 -??????????????????????????????????n?????????????????????????????????????????????????????????????????????????? +Ҫ׶Σһ׶ǰ齨һѣڶ׶ǽԪغ͵ǰѵһԪأҽ³άֶѵ״̬ -???????n????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +齨ֱӵķǴұ飬ȻϸһЧķǴ³һڵڵ㶼ѾǶô³ʹڵΪڵĶҶӽڵ㲻Ҫ³˿ԺҶӽڵԪأֻҪһԪؼɡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a2670745-a7b1-497b-90a4-dbddc4e2006d.jpg) @@ -880,33 +878,33 @@ public static void sort(Comparable[] a){ } ``` -### 6. ???? +### 6. -????????? lgN???????????????????????????????? lgN?? +һѵĸ߶Ϊ lgNڶвԪغɾԪصĸӶȶΪ lgN -???????????????? N ????????????????????????? NlgN?? +ڶҪ N ڵ³˸ӶΪ NlgN -?????????????????????????????? +ʱһԭûöĿռ䡣 -???????????????????????????????????????????????????????????????? +ִϵͳʹöΪ޷û棬ҲԪغٺڵԪؽбȽϡ -## ??? +## Ӧ -### 1. ?????????? +### 1. 㷨ıȽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/be53c00b-2534-4dc6-ad03-c55995c47db9.jpg) -????????????????????????????????????????????????????????????????????????????????????????????????????? \~cNlgN??????? c ???????????????????????????????????????????????????????????????????????????????????????????????????????? +ʱͨ㷨ѭָ٣û棬Ϊ˳طݡʱΪ \~cNlgN c Զ㷨ҪСʹз֮ʵӦпֵܳijЩֲܹﵽԼ𣬶㷨ȻҪԶʱ䡣 -### 2. Java ??????????? +### 2. Java 㷨ʵ -Java ??????????????? java.util.Arrays.sort()?????????????????????????????????????????????????? +Java ϵͳеҪ򷽷Ϊ java.util.Arrays.sort()ԭʼʹзֵĿ򣬶ʹù鲢 -### 3. ??????????????? +### 3. зֵĿѡ㷨 -????????? partition() ????????????? a[lo] ?? a[hi] ?????????????????? j ??? a[lo..j-1] ????? a[j]???? a[j+1..hi] ??????? a[j]???????? j=k??a[j] ????? k ?????? + partition() Ὣ a[lo] a[hi] 򲢷һ j ʹ a[lo..j-1] Сڵ a[j] a[j+1..hi] ڵ a[j]ô j=ka[j] ǵ k -??????????????????????????????????????????????? (N+N/2+N/4+..)?????????? k ???????????????? 2N?? +㷨ԼģΪÿý֣ôȽϵܴΪ (N+N/2+N/4+..)ֱҵ k ԪأȻС 2N ```java public static Comparable select(Comparable[] a, int k) { @@ -921,37 +919,33 @@ Java ????? } ``` -# ?????? ???? +# -???????????????????????????????????????????????????????? +ʹ־ʵָЧķűɢб -## ????? +## ű -### 1. API +### 1. ű ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b69d7184-ab62-4957-ba29-fb4fa25f9b65.jpg) -??????????? null ???????????????????????????? put(key, null) ??? delete(key) ???????????? - -### 2. ???????? +### 2. ű ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ba6ae411-82da-4d86-a434-6776d1731e8e.jpg) -???????????????? Comparable ???? +űļҪʵ Comparable ӿڡ -??????????????????????????????????????????????? +ҵijɱģͣıȽϴڲбȽʱʹķʴ -### 3. ??????????? +### 3. ֲʵű -?????????????????? N ??????????? \~N2/2 ???? +ʹһƽ飬һ洢һ洢ֵ -### 4. ??????????????? +Ҫһ Key ͵ Comparable һ Value ͵ Object 顣 -????????????????????????? +rank() Ҫڱʱܹ֪üλãڱʱҲ֪ںδ¼ -?????????? Key ????? Comparable ???????????? Value ????? Object ??????? - -rank() ?????????????????????????????????????????????????????????????????????????? +ӶȣֲҪ lgN+1 αȽϣʹöֲʵֵķűIJҲҪʱǶġDzҪƶԪأԼġ ```java public class BinarySearchST, Value> { @@ -1010,17 +1004,14 @@ public class BinarySearchST, Value> { } ``` -### 5. ???????????? -??????????????????? lgN+1 ??????????????????????????????????????????????????????????????????????????????????????? +## -## ????????? - -????????????????????????????????????????????????????????????????????????????????????BST???????????????????????????????????????????????????????????????????????? +壺ΪһӣһӵĽڵ㣬ÿӶָһӶBSTһŶÿڵļеڵļСڵļ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/25226bb2-92cc-40cb-9e7f-c44e79fbb64a.jpg) -??????????????????????????????????????????????????? +IJҲÿεһ룬Ͷֲơ ```java public class BST, Value> { @@ -1030,7 +1021,7 @@ public class BST, Value> { private Key key; private Value val; private Node left, right; - // ????????????????????? + // ԸýڵΪнڵ private int N; public Node(Key key, Value val, int N) { @@ -1053,7 +1044,7 @@ public class BST, Value> { ### 1. get() -????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ǿյģδУҵļ͸ڵļȣУݹвңҵļСвңϴвҡ ```java public Value get(Key key) { @@ -1070,7 +1061,7 @@ private Value get(Node x, Key key) { ### 2. put() -????????????????????????????????????????????????????????????????????? +ļУҪһ½ڵ㣬ҸϲڵʹøýڵȷӵС ```java public void put(Key key, Value val) { @@ -1087,17 +1078,17 @@ private Node put(Node x, Key key, Value val) { } ``` -### 3. ???? +### 3. -???????????????????????????????????????????????????????????????????????????????????????????????????????? lgN????????????????????? N?? +㷨ʱȡ״״ȡڼȺ˳õȫƽģÿӺ͸ڵľ붼Ϊ lgN£ĸ߶Ϊ N ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/73a3983d-dd18-4373-897e-64b706a7e370.jpg) -???????????????????????????? +ӶȣҺͲΪ ### 4. floor() -??? key ??????? key?????????? key ????????????????????????? key ????????? key????????????????????????? key ????????? key ???????????????????????????????????? key ????????? + key Сڸڵ keyôСڵ key ڵһУ key ڸڵ keyֻеڵдСڵ key Ľڵ㣬Сڵ key ڵУڵСڵ key ڵ㡣 ```java public Key floor(Key key) { @@ -1145,7 +1136,7 @@ private Node min(Node x) { ### 7. deleteMin() -???????????????????????????????? +ָСڵָСڵ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6e2cb20a-8d2a-46fe-9ac7-68a2126b7bd5.jpg) @@ -1163,7 +1154,7 @@ public Node deleteMin(Node x) { ### 8. delete() -???????????????????????????????????????????????????????????????????????????I???? +ɾĽڵֻôֻҪָڵָΨһɣСڵ滻ýڵ㡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b488282d-bfe0-464f-9e91-1f5b83a975bd.jpg) @@ -1191,7 +1182,7 @@ private Node delete(Node x, Key key) { ### 9. keys() -??????????????????????????????????? +öĽΪеص㡣 ```java public Iterable keys(Key lo, Key hi) { @@ -1209,42 +1200,42 @@ private void keys(Node x, Queue queue, Key lo, Key hi) { } ``` -### 10. ??????? +### 10. ܷ -???????????????????????????????????????????????????????? +ӶȣвҪʱ䶼ĸ߶ȳȡ -## ???????? +## ƽ -### 2-3 ?????? +### 2-3 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2548f2ec-7b00-4ec7-b286-20fc3022e084.jpg) -??????????? 2-3 ????????????????????????????????????? +һƽ 2-3 пӵڵľӦͬġ -#### 1. ??????? +#### 1. -?????????????????? 4- ??????????? 4- ??????? 3 ?? 2- ?????????? 2- ?????????????????????????????????? 4- ?????????????????????????????? 4- ??? +֮һʱ 4- ڵʱҪ 4- ڵѳ 3 2- ڵ㣬м 2- ڵƵϲڵУƲʱ 4- ڵһֱзƣֱʱ 4- ڵ㡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/912174d8-0786-4222-b7ef-a611d36e5db9.jpg) -#### 2. ???? +#### 2. -2-3 ???????????????????????????????????????????????????????????????????????????????????????????????????? +2-3 ı任ǾֲģصĽڵ֮ⲻ޸Ļ߼֣Щֲ任ӰȫԺƽԡ -2-3 ?????????????????????????????????????????????????????????????????????? logN ???????? 10 ??????? 2-3 ????????????????? 30 ???????????????????????????? +2-3 IJҺͲӶȺͲ˳޹أ²ҺͲʵĽڵȻ logN 10 ڸڵ 2-3 ֻҪ 30 ڵܽIJҺͲ -### ??????????? +### ڶ -2-3 ???????????? 2- ???? 3- ???????????????????? 3- ????????????????????????????????????????????????????? 3- ???????????????????? +2-3 Ҫõ 2- ڵ 3- ڵ㣬ʹúʵ 3- ڵ㡣ָһڵɫΪɫôڵϲڵʾһ 3- ڵ㣬ɫͨӡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7080a928-06ba-4e10-9792-b8dd190dc8e2.jpg) -?????????????????? +ʣ -1. ??????????????? -2. ??????????????????????????????????????????????? +1. ӶΪӣ +2. ɫƽ⣬ӵڵ·ϵĺͬ -??????????????????????? +ʱԽӻƽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/62077f5d-a06d-4129-9b43-78715b82cb03.png) @@ -1276,9 +1267,9 @@ public class RedBlackBST, Value> { } ``` -#### 1. ????? +#### 1. ת -?????????????????????????????????????????????????????????????????? +ΪϷĺӶΪӣΪӣôҪת ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/33a4e822-2dd0-481e-ac89-7f6161034402.jpg) @@ -1297,9 +1288,9 @@ public Node rotateLeft(Node h) { } ``` -#### 2. ????? +#### 2. ת -???????????????????????????????????????????????????????? +תΪתӣ֮IJ̽֡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/dfd078b2-aa4f-4c50-8319-232922d822b8.jpg) @@ -1317,9 +1308,9 @@ public Node rotateRight(Node h) { } ``` -#### 3. ?????? +#### 3. ɫת -??? 4- ???????????????????????????????????????? 4- ?????????????????????????????????????????????????? 2-3 ???????????????????????? +һ 4- ڵںбΪһڵӽڵ㶼Ǻɫġ 4- ڵҪӽڵɫɺ֮⣬ͬʱҪڵɫɺڱ죬 2-3 ĽǶȿǽмڵƵϲڵ㡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/de7c5a31-55f5-4e9d-92ec-4ed5b2ec3828.jpg) @@ -1333,13 +1324,13 @@ void flipColors(Node h){ } ``` -#### 4. ???? +#### 4. -???????? +㷨 -- ???????????????????????????????????????? -- ?????????????????????????????????????????????? -- ??????????????????????????????? +- ӽڵǺɫĶӽڵǺɫģת +- ӽڵǺɫӽڵҲǺɫģת +- ӽڵΪɫģɫת ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/40639782-5df2-4e96-a4f3-f9dd664d0ca1.jpg) @@ -1365,55 +1356,55 @@ private Node put(Node x, Key key, Value val) { } ``` -???????????????? BST ??????????????????????????????????????????? +Կò BST IJƣֻתɫ任ɡ -???????????????????????????????????????????????????????flipColors() ?????????????????????????????????????????????????????? 1. +ڵһΪɫΪڵûϲڵ㣬Ҳûϲڵָڵ㡣flipColors() пܻʹøڵɫΪɫÿڵɺɫɺɫʱĺӸ߶ȼ 1. -#### 5. ??????? +#### 5. ɾС -???????????? 2- ????????????????????????????????????????????????????????? 2- ??????? 2- ???????? 3- ?????? 4- ??????????????????????????????? key?????????????????? key???????????? 2- ???????????????????? key ??????????????????????????? 2- ????????????????????????????????????? +Сһ 2- ڵУôɾüһӣƻƽԣҪȷС 2- ڵС 2- ڵת 3- ڵ 4- ڵַһϲڵһ keyֵܽڵһ keyϲڵ 2- ڵ㣬ôû취ϲڵ key ˣҪ֤ɾ·ϵнڵ㶼 2- ڵ㡣ɾĹУ֤֮һ -1. ?????????????????? 2- ??????? -2. ?????????????????? 2- ??????????????? 2- ??????????????? key ?????? -3. ???????????????????????????? 2- ???????????????????????????????????????? 4- ??? +1. ǰڵӽڵ㲻 2- ڵ㣬ɣ +2. ǰڵӽڵ 2- ڵֵܽڵ㲻 2- ڵ㣬ֵܽڵһ key +3. ǰڵӽڵֵܽڵ㶼 2- ڵ㣬ӽڵ㡢ڵеСֵܽڵϲΪһ 4- ڵ㡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b001fa64-307c-49af-b4b2-2043fc26154e.png) -?????????????????? 3- ?????? 4- ????????????????????????????????? 4- ??? +õһС 3- ڵ 4- ڵ㣬ֱӴɾȻٴͷֽʱ 4- ڵ㡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/70b66757-755c-4e17-a7b7-5ce808023643.png) -#### 6. ???? +#### 6. -????? N ??????????????? 2lgN?????????????????????? 2-3 ???????????????????????? 3- ?????????? 2- ??? +һŴСΪ N ĺĸ߶Ȳᳬ 2lgNӦ 2-3 йߵ·ڵȫ 3- ڵ඼ 2- ڵ㡣 -???????????????????????????????????? +IJҪʱ䶼Ƕġ -## ??? +## ɢб -?????????????????????????????????????????????????????????????????????????????????????????????????? +ɢб飬԰ɢбɢֵֵɢбͷԪһ٣ڳʱʵֲҺͲķű -?????????????????????????????????????????????????? +޷ͨɢֵ֪ĴСϵɢб޷ʵԲ -### ????? +### ɢк -?????????? M ???????????????????????? [0, M-1] ?????????????????????? hash ??? +һСΪ M ɢбɢкܹתΪ [0, M-1] ڵΪ hash ֵ -?????????????????????????????????????? hash ??? +ɢбгͻĴڣҲͬļͬ hash ֵ -?????????????????????????? +ɢкӦ -1. ???????????????????? hash ??? -2. ?????????????????????????? hash ???????????????? hash ???????????? -3. ????????????? hash ????????????? [0, M-1] ?????????????????????????????????? +1. һԣȵļӦȵ hash ֵ +2. ЧԣӦ㣬бҪĻ԰ hash ֵڵ hash ʱֱӷء +3. ԣм hash ֵӦȵطֲ [0, M-1] ֮䣬ҪֱӰ쵽ɢбܡ -?????????????????????? [0, M-1] ???????????????? k?????? k%M ???????? [0, M-1] ???? hash ?????? M ????????????????????????????????????????????? M ? 10k??????????????? k ?? +Խɢе [0, M-1] ֮䣬һ k k%M ȿɵõһ [0, M-1] ֮ hash ֵע M һ޷üϢ M Ϊ 10kôֻüĺ k λ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ԽתʽȻóڸԽʾɶʽȻʹöʽֵг -???????????????????????????? hash ??????????????????????? hash ????????????????????????????? R ???????????????????????????????? +жಿϵļÿֶҪ hash ֵϲʱҪÿ hash ֵͬҪĵλԽü R ƵÿֶвͬȨֵ -?????????????????????? +磬ַɢкʵ ```java int hash = 0; @@ -1421,21 +1412,21 @@ for(int i = 0; i < s.length(); i++) hash = (R * hash + s.charAt(i)) % M; ``` -??????????????????????????????? +ٱ磬ӵжԱԶĹϣ ```java int hash = (((day * R + month) % M) * R + year) % M; ``` -R ????????????????? 31?? +R ֵǺҪͨȡ 31 -Java ?? hashCode() ????? hash ????????????????????????????????? hashCode() ????????????????????????????????????? 32 ?????????????? 31 ???????????????????????????????????????? +Java е hashCode() ʵ hash Ĭʹöڴֵַʹ hashCode() ʱӦϳʹáΪڴַ 32 λֻҪ 31 λķǸӦηλ֮ʹó ```java int hash = (x.hashCode() & 0x7fffffff) % M; ``` -??? Java ????? HashMap ????????????????????????? Key ????? hashCode() ??????????????????????? M ??????Java ? hashCode() ??????????????????? 32 ??????Java ?? String??Integer ?????? hashCode() ??????????????????????????????????? hashCode()?? +ʹ Java Դ HashMap ԴĹϣʵʱֻҪȥʵ Key ͵ hashCode() ɣҲͲҪ M ĴСȡJava 涨 hashCode() ܹȷֲе 32 λJava е StringInteger ȶ hashCode() ʵһ㡣չʾԶʵ hashCode() ```java public class Transaction{ @@ -1453,17 +1444,17 @@ public class Transaction{ } ``` -### ??????????????? +### ɢб -???????????????? hash ????????????????????????????????????????????? Key ??????????????????????????? +ʹ洢 hash ֵͬļӶͻʱҪȲ Key ڵȻ˳ҡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/540133af-aaaf-4208-8f7f-33cb89ac9621.png) -???? N ??????M ?????? (N>M)?????????????????????????????????????????????? N/M???????????????????????????????? \~N/M?? + N M (N>M)ϣܹԵÿĴС N/MδеIJҺͲҪıȽϴΪ \~N/M -### ???????????????? +### ̽ⷨɢб -???????????????????????????????????????????????????????????????????????? M ????????????? N??M>N)?? +̽ⷨʹÿλͻͻʱǰ̽һλ洢ͻļʹ߳̽ⷨĴС M Ӧڼĸ NM>N) ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2b3410f1-9559-4dd1-bc3d-e3e572247be2.png) @@ -1494,7 +1485,7 @@ public class LinearProbingHashST { } ``` -#### ???? +#### ```java public Value get(Key key) { @@ -1507,7 +1498,7 @@ public Value get(Key key) { } ``` -#### ???? +#### ```java public void put(Key key, Value val) { @@ -1525,9 +1516,9 @@ public void put(Key key, Value val) { } ``` -#### ??? +#### ɾ -?????????????????????????????????????? +ɾӦҲڵļֵ²ɢбС ```java public void delete(Key key) { @@ -1553,15 +1544,15 @@ public void delete(Key key) { } ``` -#### ????????? +#### С -?????????????????????????????????????????????????????????????????????????? +̽ⷨijɱȡĿijȣĿҲо۴ء۴غܳʱڲҺͲʱҲҪкܶ̽⡣ -?? = N/M???? ?? ????????????????????? ?? ?? 1/2 ?????????????? 1.5 ?? 2.5 ??? + = N/M Ϊʡ֤ С 1/2 ʱ̽Ԥƴֻ 1.5 2.5 ֮䡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/0ddebc5c-7c24-46b1-98db-4fa5e54db16b.png) -???????????????????????????????? ?? ?? [1/4, 1/2] ??? +Ϊ˱֤ɢбܣӦĴСʹ [1/4, 1/2] ֮䡣 ```java private void resize() { @@ -1582,29 +1573,29 @@ private void resize(int cap) { } ``` -????????????????Y????????????????????????????????????????????????????????????????????????????????A????????????????????? 1?????????????????????????????????????????????????? +Ȼÿµ鶼Ҫ°ÿֵԲ뵽ɢбǴ̯ĽǶҪĴȴǺСġͼԿÿ鳤ȼӱۼƽֵ 1ΪÿҪ¼ɢֵƽֵ½ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/01658047-0d86-4a7a-a8ca-7ea20fa1fdde.png) -## ??? +## Ӧ -### ?????????????? +### ַűʵֵıȽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9ee83c8c-1165-476c-85a6-e6e434e5307a.jpg) -???????????????????????????????????? +ӦȿɢбҪԲʱʹú -### Java ????????? +### Java ķűʵ -Java ?? java.util.TreeMap ?? java.util.HashMap ?????????????????????????????????? +Java java.util.TreeMap java.util.HashMap ֱǻںɢбķűʵ֡ -### ???????? +### -???????????????????????????????????????????????????????????????????????????? +˷űҲʹãֻмûֵü洢һϵеļȻжһǷڼС -### ?????????? +### ϡ˷ -?????????p?? N ?????????????????????????????????????????? 0 ??????????????????????????? 0 ???????? +漰 N γ˷Ϊϡʱʹ÷ű洢еķ 0 ֵʹó˷ֻҪЩ 0 Ԫؽмɡ ```java import java.util.HashMap; diff --git a/notes/编写可读代码的艺术.md b/notes/编写可读代码的艺术.md index 81b55de5..5f14f947 100644 --- a/notes/编写可读代码的艺术.md +++ b/notes/编写可读代码的艺术.md @@ -1,92 +1,92 @@ -* [?? 1 ?? ???????????](#??-1-??-???????????) -* [?? 2 ?? ?????????????](#??-2-??-?????????????) -* [?? 3 ?? ??????????????](#??-3-??-??????????????) -* [?? 4 ?? ??????????](#??-4-??-??????????) -* [?? 5 ?? ?????](#??-5-??-?????) -* [?? 6 ?? ??????](#??-6-??-??????) -* [?? 7 ?? ??????????????](#??-7-??-??????????????) -* [?? 8 ?? ????????](#??-8-??-????????) -* [?? 9 ?? ??????????](#??-9-??-??????????) -* [?? 10 ?? ???????](#??-10-??-???????) -* [?? 11 ?? ???????????](#??-11-??-???????????) -* [?? 12 ?? ????????????????](#??-12-??-????????????????) -* [?? 13 ?? ?????????](#??-13-??-?????????) +* [ 1 ɶԵҪ](#-1--ɶԵҪ) +* [ 2 ֱ뺬](#-2--ֱ뺬) +* [ 3 ֲܴ](#-3--ֲܴ) +* [ 4 õĴ](#-4--õĴ) +* [ 5 дע](#-5--дע) +* [ 6 αдע](#-6--αдע) +* [ 7 ߿Ŀɶ](#-7--߿Ŀɶ) +* [ 8 ֳʽ](#-8--ֳʽ) +* [ 9 ɶ](#-9--ɶ) +* [ 10 ȡ](#-10--ȡ) +* [ 11 һֻһ](#-11--һֻһ) +* [ 12 ȻԱ](#-12--ȻԱ) +* [ 13 ٴ](#-13--ٴ) -# ?? 1 ?? ??????????? +# 1 ɶԵҪ -???????????????????????????????????????????????????????????????????????????????????????? +кܴһʱĶ룬ҪĶԼĴ룬ҪĶ˵Ĵ롣ˣɶõĴܹ߱Чʡ -?????????????????????????????????????????????????????????????????????? +ɶõĴôܹãΪԱԸȥ޸ⲿִ룬Ҳ޸ġ -???????????????????????????????????????????? +ֻںΪЧʲſԷɶԣɶǵһλ -# ?? 2 ?? ????????????? +# 2 ֱ뺬 -??????????????? +һЩȽбĵʣ -| ???? | ????????? | +| | | | --- | --- | -| send | deliver??dispatch??announce??distribute??route | -| find | search??extract??locate??recover | -| start| launch??create??begin??open| -|make|create??set up??build??generate??compose??add??new| +| send | deliverdispatchannouncedistributeroute | +| find | searchextractlocaterecover | +| start| launchcreatebeginopen| +|make|createset upbuildgeneratecomposeaddnew| -??? i??j??k ???????????????????????????????????????????????????? user_i??member_i???????????????????????????????????????????????? +ʹ ijk ΪѭǺõģΪӸбֻã user_imember_iΪѭԽ࣬Խ⣬бĵֿɶԻ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ΪݴʵϢָбҲ䳤̵ֳ׼ǣԽԽֻڶʹһЩ֡ -# ?? 3 ?? ?????????????? +# 3 ֲܴ -???????????????????????????ܦ???????????????????????^ +Ҫ˼һ±˻кν᲻ԭĺ塣 -?? min??max ??????????? + minmax ʾΧ -?? first??last ?????????????????begin??end ??????????????????? end ?????????? + firstlast ʾʿռİΧbeginend ʾʿռųΧ end β ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/26772ecc-a3e3-4ab7-a46f-8b4656990c27.jpg) -???????????????? is??can??should??has ?????? +ص iscanshouldhas ǰ׺ -# ?? 4 ?? ?????????? +# 4 õĴ -???????????? +ʵĿк -????????????? +עͣ ``` -int a = 1; // ??? -int b = 11; // ??? -int c = 111; // ??? +int a = 1; // ע +int b = 11; // ע +int c = 111; // ע ``` -?????????????????? html ?????????????????????? html ????????? +˳⣬ html ıĸֵӦúͱ html е˳һ£ -????????????????????? +صĴ밴֯һ -# ?? 5 ?? ????? +# 5 дע -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? getter ?? setter ?????????????????????????????? +ĶȻע⵽עͣעû̫ãôͻ˷ѴĶʱ䡣ЩֱӿĴ벻ҪдעͣرDzҪΪÿעͣЩ򵥵 getter setter ΪЩдעͷôɶԸ -??????????????????????????????????????????????? +Ϊע;֣ȡֶдע͡ -?????????????????????????????????????????????????????? +ע¼õǰ취˼̣Ӷö߸롣 -????????????????????? +עһЩ -?? TODO ???????? + TODO ǣ -| ??? | ?? | +| | ÷ | |---|---| -|TODO| ???? | -|FIXME| ????? | -|HACH| ?????????? | -|XXX| ?????????????????? | +|TODO| | +|FIXME| ޸ | +|HACH| ֲڵĽ | +|XXX| ΣգҪ | -# ?? 6 ?? ?????? +# 6 αдע -???????????? +ˣ ``` // The first String is student's name @@ -99,7 +99,7 @@ Map scoreMap = new HashMap<>(); Map scoreMap = new HashMap<>(); ``` -????????????????? +Ӳ˵ ``` //... @@ -109,7 +109,7 @@ int add(int x, int y) { } ``` -???????????????????????????????? +ںܸӵĺжÿ֣ ``` int a = 1; @@ -117,37 +117,37 @@ int b = 2; int num = add(\* x = *\ a, \* y = *\ b); ``` -??????????????????????????????????????????????? +ʹרҵ̸ϵĽͣģʽ˵롣 -# ?? 7 ?? ?????????????? +# 7 ߿Ŀɶ -?????????????????????????????????????????????????? +ʽУDZҲdzһȷ ``` if(len < 10) if(10 > len) ``` -if / else ??????????????????????? ????????? ???????????????? +if / else 䣬ȴ߼ ߼ ؼ߼ۣ߼ ``` if(a == b) { - // ????? + // ߼ } else{ - // ????? + // ߼ } ``` -??????????????????? ? : ?????????????????????????????? if / else?? +ֻ߼򵥵ʹ ? : ĿʹգӦòֳ if / else -do / while ????????????????????????????????????????????? while ????? +do / while ں棬ˣһһЩԻĵطʹ while 档 -????????? goto ?????? goto ?????????????????????? goto ???????????????????????? goto?? +ֻһ goto Ŀ꣬ô goto һܽܣǹڸӵ goto ôɶرӦñʹ goto -?????????????? return ????????????????????? +Ƕ׵ѭУһЩ return ܼǶ׵IJ -# ?? 8 ?? ???????? +# 8 ֳʽ -????????????????????????????????????????? +ʽĿɶԺܲһЩͱӶֱʽ ``` if line.split(':')[0].strip() == "root": @@ -159,7 +159,7 @@ if username == "root": ... ``` -????????????????????? +ʹĦһЩ߼ʽ ``` if(!a && !b) { @@ -172,9 +172,9 @@ if(a || b) { } ``` -# ?? 9 ?? ?????????? +# 9 ɶ -????????????????????????? break ???? return ????????????????????? +ȥѭͨ break return Լٿʹá ``` boolean done = false; @@ -195,9 +195,9 @@ while(/* condition */) { } ``` -????????????????????????????????????????????? +СԽСԽ׶λʹõĵط -JavaScript ????????????????????????? submit_form ???????????submitted ??????????????????????????????? submitted ??????????????????? submitted ???????????????????????????????? +JavaScript ñհС´ submit_form Ǻsubmitted ƺᱻύΡһʵ submitted ȫֱڶʵְ submitted ŵУӶΧ ``` submitted = false; @@ -218,16 +218,16 @@ var submit_form = (function() { } submitted = true; } -}()); // () ????????????????????? +}()); // () ʹִ ``` -JavaScript ??????? var ????????????????????????????????????????????????????? var ???????????? +JavaScript û var ıȫֱȫֱԻӦ var -????????????????????????????? +λӦʹõλ -**???????** +**ʵ** -?????????????????????????? +һҳıֶΣ ``` @@ -236,7 +236,7 @@ JavaScript ??????? var ????????????????????????????????????????????????????? var ``` -?????????????????????????????????? input ???????????????? +Ҫһַŵһյ input ֶУʼʵ£ ``` var setFirstEmptyInput = function(new_alue) { @@ -256,11 +256,11 @@ var setFirstEmptyInput = function(new_alue) { } ``` -????????????????? +ʵ⣺ -- found ????????? -- elem ????????? -- ?????? for ??????? while ????? +- found ȥ +- elem +- for ѭ while ѭ ``` var setFirstEmptyInput = function(new_value) { @@ -277,13 +277,13 @@ var setFirstEmptyInput = function(new_value) { }; ``` -# ?? 10 ?? ??????? +# 10 ȡ -???????????????????????????????????????????? +ѧǰѴֳСٰЩĽŻһ -?????????????????????????????????????????????????????????????????????????? +Ӧȷһĸ߲Ŀ꣬ȻڲֱΪĿ깤Ĵ룬ȡŵĺС -?????????? +ԵĴ룺 ``` int findClostElement(int[] arr) { @@ -304,7 +304,7 @@ int findClostElement(int[] arr) { } ``` -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ϴѭҪ룬ⲿֲڴ߲Ŀ߲꣬ĿѰСֵ˿԰ⲿִȡĺСҲһĺôУԵвԡԿҵ޸ġ ``` public int findClostElement(int[] arr) { @@ -321,22 +321,22 @@ public int findClostElement(int[] arr) { } ``` -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ǺȡԽԽãȡ࣬ĶʱҪȥֻڵǰҪȥ˽ijһϸڶܹʱȡӺǺõġ -?????????????????????? +ȡҲڼСࡣ -# ?? 11 ?? ??????????? +# 11 һֻһ -?????????????????????????????????? +ֻһµĴ֪Ҫ£ -????????????????????????????????????????????????????????????? +̣г񣻰ÿֵͬĺ߲ͬĶ䡣 -# ?? 12 ?? ???????????????? +# 12 ȻԱ -???????????????????????????????????????????????????????????? +Ȼд߼Ҳα룬Ȼд룬߼ -# ?? 13 ?? ????????? +# 13 ٴ -???????????????????????????????????????????????????? +Ҫƣ̻кܶ仯Ƶݵõġ -???????????? +ñ׼ʵ֡ diff --git a/notes/计算机操作系统.md b/notes/计算机操作系统.md index ebbc03c5..8656dac4 100644 --- a/notes/计算机操作系统.md +++ b/notes/计算机操作系统.md @@ -1,248 +1,248 @@ -* [????? ????](#?????-????) - * [??????????????](#??????????????) - * [1. ????](#1-????) - * [2. ????](#2-????) - * [3. ????](#3-????) - * [4. ??](#4-??) - * [??????](#??????) - * [?????](#?????) - * [1. ????](#1-????) - * [2. ??](#2-??) - * [3. ????](#3-????) - * [??????????](#??????????) - * [1. ?????](#1-?????) - * [2. ????](#2-????) -* [????? ???????](#?????-???????) - * [?????????](#?????????) - * [1. ????](#1-????) - * [2. ???](#2-???) - * [3. ????](#3-????) - * [??????????](#??????????) - * [??????](#??????) - * [1. ?????????????](#1-?????????????) - * [1.1 ?????????FCFS??](#11-?????????fcfs??) - * [1.2 ??????????SJF??](#12-??????????sjf??) - * [1.3 ??????????????SRTN??](#13-??????????????srtn??) - * [2. ????????????](#2-????????????) - * [2.1 ?????????](#21-?????????) - * [2.2 ???????](#22-???????) - * [2.3 ??????????](#23-??????????) - * [2.4 ?????????](#24-?????????) - * [3. ?????????](#3-?????????) - * [???????](#???????) - * [1. ?????](#1-?????) - * [2. ???????](#2-???????) - * [3. ?????](#3-?????) - * [4. ???](#4-???) - * [???????](#???????) - * [1. ???](#1-???) - * [2. ?????](#2-?????) - * [3. ???????](#3-???????) - * [4. ???](#4-???) - * [5. ???????](#5-???????) - * [6. ?????](#6-?????) - * [???????????](#???????????) - * [1. ????-??????](#1-????-??????) - * [2. ????????????](#2-????????????) -* [?????? ????](#??????-????) - * [??????????](#??????????) - * [???????????](#???????????) - * [1. ???????](#1-???????) - * [2. ???????](#2-???????) - * [2.1 ???????????](#21-???????????) - * [2.2 ???????????????](#22-???????????????) - * [2.3 ??????????????](#23-??????????????) - * [2.4 ????????](#24-????????) - * [3. ????????](#3-????????) - * [3.1 ?????](#31-?????) - * [3.2 ???????????????](#32-???????????????) - * [3.3 ??????????????](#33-??????????????) - * [4. ????????????????](#4-????????????????) - * [4.1 ?????????](#41-?????????) - * [4.2 ???????](#42-???????) -* [?????? ???????](#??????-???????) - * [???????](#???????) - * [???????](#???????) - * [1. ???](#1-???) - * [2. ???](#2-???) - * [3. ????](#3-????) - * [4. ???????????](#4-???????????) - * [????????](#????????) - * [1. ????Optimal??](#1-????optimal??) - * [2. ????????FIFO??](#2-????????fifo??) - * [3. ??????????LRU??Least Recently Used??](#3-??????????lru??least-recently-used??) - * [4. ????Clock??](#4-????clock??) -* [?????? ?????](#??????-?????) - * [?????????](#?????????) - * [1. ?????????FCFS??First Come First Serverd??](#1-?????????fcfs??first-come-first-serverd??) - * [2. ??????????????SSTF??Shortest Seek Time First??](#2-??????????????sstf??shortest-seek-time-first??) - * [3. ???????SCAN??](#3-???????scan??) - * [4. ??????????CSCAN??](#4-??????????cscan??) -* [??????](#??????) +* [һ ](#һ-) + * [ϵͳ](#ϵͳ) + * [1. ](#1-) + * [2. ](#2-) + * [3. ](#3-) + * [4. 첽](#4-첽) + * [ϵͳ](#ϵͳ) + * [жϷ](#жϷ) + * [1. ж](#1-ж) + * [2. 쳣](#2-쳣) + * [3. ](#3-) + * [ں˺΢ں](#ں˺΢ں) + * [1. ں](#1-ں) + * [2. ΢ں](#2-΢ں) +* [ڶ ̹](#ڶ-̹) + * [߳](#߳) + * [1. ](#1-) + * [2. ߳](#2-߳) + * [3. ](#3-) + * [״̬л](#״̬л) + * [㷨](#㷨) + * [1. ϵͳеĵ](#1-ϵͳеĵ) + * [1.1 ȷFCFS](#11-ȷfcfs) + * [1.2 ҵȣSJF](#12-ҵsjf) + * [1.3 ʣʱȣSRTN](#13-ʣʱsrtn) + * [2. ʽϵͳеĵ](#2-ʽϵͳеĵ) + * [2.1 Ȩ](#21-Ȩ) + * [2.2 ʱƬת](#22-ʱƬת) + * [2.3 ༶](#23-༶) + * [2.4 ̽](#24-̽) + * [3. ʵʱϵͳеĵ](#3-ʵʱϵͳеĵ) + * [ͬ](#ͬ) + * [1. ٽ](#1-ٽ) + * [2. ͬ뻥](#2-ͬ뻥) + * [3. ź](#3-ź) + * [4. ܳ](#4-ܳ) + * [ͨ](#ͨ) + * [1. ܵ](#1-ܵ) + * [2. ź](#2-ź) + * [3. Ϣ](#3-Ϣ) + * [4. ź](#4-ź) + * [5. ڴ](#5-ڴ) + * [6. ׽](#6-׽) + * [ͬ](#ͬ) + * [1. -д](#1--д) + * [2. ѧҽ](#2-ѧҽ) +* [ ](#-) + * [](#) + * [Ĵ](#Ĵ) + * [1. ](#1-) + * [2. Ԥ](#2-Ԥ) + * [2.1 ƻ](#21-ƻ) + * [2.2 ƻ뱣](#22-ƻ뱣) + * [2.3 ƻռ](#23-ƻռ) + * [2.4 ƻ·ȴ](#24-ƻ·ȴ) + * [3. ](#3-) + * [3.1 ȫ״̬](#31-ȫ״̬) + * [3.2 Դм㷨](#32-Դм㷨) + * [3.3 Դм㷨](#33-Դм㷨) + * [4. ָ](#4-ָ) + * [4.1 㷨](#41-㷨) + * [4.2 ָ](#42-ָ) +* [ 洢](#-洢) + * [ڴ](#ڴ) + * [ҳֶ](#ҳֶ) + * [1. ҳ](#1-ҳ) + * [2. ֶ](#2-ֶ) + * [3. ҳʽ](#3-ҳʽ) + * [4. ҳֶ](#4-ҳֶ) + * [ҳû㷨](#ҳû㷨) + * [1. ѣOptimal](#1-optimal) + * [2. ȽȳFIFO](#2-Ƚȳfifo) + * [3. δʹãLRULeast Recently Used](#3-δʹlruleast-recently-used) + * [4. ʱӣClock](#4-ʱclock) +* [ 豸](#-豸) + * [̵㷨](#̵㷨) + * [1. ȷFCFSFirst Come First Serverd](#1-ȷfcfsfirst-come-first-serverd) + * [2. ѰʱȣSSTFShortest Seek Time First](#2-Ѱʱsstfshortest-seek-time-first) + * [3. ɨ㷨SCAN](#3-ɨ㷨scan) + * [4. ѭɨ㷨CSCAN](#4-ѭɨ㷨cscan) +* [ο](#ο) -# ????? ???? +# һ -## ?????????????? +## ϵͳ -### 1. ???? +### 1. -????????????????????????????????????????????????????????????? +ָһʱͬʱж򣬶ָͬһʱжָ -???????????????????????????????? +ҪӲ֧֣ˮ߻߶ദ -????????????????????????????????????? +ϵͳ̺̣ͨ߳ʹóܹС -### 2. ???? +### 2. -???????????????????????????????????? +ָϵͳеԴԹĽ̹ͬʹá -????????????????????????? +ֹʽ⹲ͬʱ -???????????????????????????????????????????????????????????????????????????????????????????????? +⹲ԴΪٽԴӡȣͬһʱֻһ̷ʣִҪͬʵֶٽԴķʡ -### 3. ???? +### 3. -??????????????????????????????^????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +⼼һʵתΪ߼ʵ塣Ҫ⼼ʱָüͿշָüͬһϲִʹʱָüÿռдÿִֻһСʱƬлͺжд -### 4. ?? +### 4. 첽 -??????????????????????????????????????????????????????? +첽ָ̲һִϣͣͣԲ֪ٶǰƽ -## ?????? +## ϵͳ -????????????????????????????????????????????????????????????????????????? +һû̬ҪõϵͳһЩܣҪʹϵͳôӶںˣɲϵͳΪɡ -?????????????????????????????????????????????????????????? +ϵͳĹ豸ļ̹ͨš洢ȡ -## ????? +## жϷ -### 1. ???? +### 1. ж -?? CPU ???????????????????? I/O ???????????????/????????????????????????????????????/??????????????????????????? + CPU ִָ¼ I/O жϣʾ豸/Ѿɣܹһ/󡣴⻹ʱжϡ̨жϵȡ -### 2. ?? +### 2. 쳣 -?? CPU ?????????????????????????????????????????? + CPU ִָڲ¼Ƿ롢ַԽ硢ȡ -### 3. ???? +### 3. -???????????????????? +ûʹϵͳá -## ?????????? +## ں˺΢ں -### 1. ????? +### 1. ں -????????????????????????????????????????????????s??????????????????? +ںǽϵͳΪһܽϵŵںˣڸģ鹲Ϣкܸߵܡ -### 2. ???? +### 2. ΢ں -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ڲϵͳϸӣ˽һֲϵͳƳںˣӶں˵ĸԡƳIJָݷֲԭ򻮷ֳɷ໥ҪƵû̬ͺ̬֮лһʧ -# ????? ??????? +# ڶ ̹ -## ????????? +## ߳ -### 1. ???? +### 1. -?????????????????????????????? +DzϵͳԴĻλ -???????? (Process Control Block, PCB) ???????????????????????????????????????????????????? PCB ??????? +̿ƿ (Process Control Block, PCB) ̵ĻϢ״̬νĴ̺ͳָ̣ PCB IJ -### 2. ??? +### 2. ߳ -???????????????????????????????????????????????????????????????????????????? +һ߳пж̣߳ǶȵĻλͬһеĶ֮߳ԲִУǹԴ -### 3. ???? +### 3. -?? ???????????????????????????????????????????????????????????????????? + ӵԴԴĻλ̲߳ӵԴ߳̿Է̵Դ -?? ????????????????????????????????????????????????????????????????????????????????????????????????????????? + ȣ߳ǶȵĻλͬһУ̵߳ллһڵ߳лһе߳ʱл -?? ?????????????????????????????????????????????????????I/O ?????????????????????????????????????????????????????????????????p???????? CPU ????????p???????? CPU ????????????????????b????????????????????????????????????????????????????????????????????????????????????????????????????????????????? + ϵͳڴʱϵͳҪΪ֮Դڴռ䡢I/O 豸ȣ˲ϵͳĿԶڴ߳ʱĿƵأڽнлʱ漰ǰִн CPU ı漰µȽ CPU á߳лʱֻ豣ĴݣС⣬ͬһڵĶ̵̹߳ĵַռ䣬ˣЩ֮߳ͬͨŷdzʵ֣ϵͳĸԤ -?? ????????????? (IPC) ??????????????????????????????????????????????????????/????????????????????????????? + ͨŷ棺̼ͨ (IPC) ҪͬͻֶεĸԱ֤ݵһԣֱ̼߳ͨӶ/дݶΣȫֱͨš -??????QQ ?? ???????????????????????????????????????? http ????????????????????????????????????????????????????????????????? http ??????????????????????????????????? +QQ ̣к̣ܶ߳ http ̡߳¼Ӧ̡߳Ⱦ̵߳ȵȣ̵߳IJִʹеһӴӶ http ʱӦû¼ -## ?????????? +## ״̬л ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1706ce58-a081-4fed-9b36-c3c0d7e22b3a.jpg) -????????????????????????????????????????????????????? CPU????? CPU ???????????????????????? +״̬ȱҪԴӶ״̬תǸԴ CPUȱ CPU ý̴̬תΪ̬ -??????????????????????????????????????????????????????????????????? CPU ????????????????????????????????????? CPU ?????????????????????????????????? +ֻо̬̬໥תĶǵת״̬Ľͨ㷨Ӷ CPU ʱ䣬תΪ״̬״̬Ḷ̌ڷ CPU ʱƬ֮ͻתΪ״̬ȴһεȡ -## ?????? +## 㷨 -????????????????????????? +ҪԲͬ۵㷨 -### 1. ????????????? +### 1. ϵͳеĵ -#### 1.1 ?????????FCFS?? +#### 1.1 ȷFCFS -first-come first-serverd?? +first-come first-serverd -??????????????????????? +Ƚеҵ -?????????????????????????????????????????????????????????????????????????????????????????????????????? +ڳҵڶҵΪҵһֱȴǰijҵִϲִУҵҪִкܳʱ䣬˶ҵȴʱ -#### 1.2 ??????????SJF?? +#### 1.2 ҵȣSJF -shortest job first?? +shortest job first -??????????????????????? +ȹʱ̵ҵ -???????????????????????????????????????????????????????????????????????????? +ҵпܻһֱȴҵִϵ״̬һֱжҵôҵԶòȡ -#### 1.3 ??????????????SRTN?? +#### 1.3 ʣʱȣSRTN -shortest remaining time next?? +shortest remaining time next -### 2. ???????????? +### 2. ʽϵͳеĵ -#### 2.1 ????????? +#### 2.1 Ȩ -??????????????????????????????????????????????????????????????????????????? +˿ֶȨ֮⣬԰ӦΪȨֵȷʽӦȵ㷨 -????? = (?????? + ?????????) / ????????? = ?????? / ????????? +Ӧ = (ȴʱ + Ҫʱ) / Ҫʱ = Ӧʱ / Ҫʱ -??????????????????? SJF ????????????????????????????????????????????????????? +ֵ㷨ҪΪ˽ SJF гҵܻ⣬ΪŵȴʱӦҲԽԽߡ -#### 2.2 ??????? +#### 2.2 ʱƬת -????????????? FCFS ????????????????????????? CPU ?????????????????????????????????????????????????????????????????????????????????????????????????????????????? CPU ?????????????? +о̰ FCFS ԭųһУÿεʱ CPU ׽̣ýִ̿һʱƬʱƬʱɼʱʱжϣȳֹͣý̵ִУеĩβͬʱ CPU ׵Ľ̡ -????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʱƬת㷨ЧʺʱƬкܴϵΪÿνлҪ̵Ϣ½̵ϢʱƬ̫̣л̫ƵڽлϾͻỨʱ䡣 -#### 2.3 ?????????? +#### 2.3 ༶ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/042cf928-3c8e-4815-ae9c-f2780202c68f.png) -?? ??????????????????????????M?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? + öУΪи費ͬȼһеȼߣڶд֮еȨ͡㷨нִʱƬĴСҲͬȨԽߵĶУΪÿ涨ִʱƬԽС -?? ??????????????????????????????????????? FCFS ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????? + һ½̽ڴȽһеĩβ FCFS ԭŶӵȴȡֵýִʱڸʱƬɣ׼ϵͳһʱƬʱδɣȳ㽫ýתһеĶβ -?? ????? i -1 ?????????????????? i ????????????? + ǰ i -1 оʱŻȵ i еĽС -???????????????????????????? +ŵ㣺ʵʱԺãҲʺжҵͳҵ -#### 2.4 ????????? +#### 2.4 ̽ -### 3. ????????? +### 3. ʵʱϵͳеĵ -???????????????????????????????????? +ʵʱϵͳҪһһȷʱڵõӦ -????????????????????????????????????????????????????? +ΪӲʵʱʵʱǰ߱ԵĽֹʱ䣬߿һijʱ -## ??????? +## ͬ -### 1. ????? +### 1. ٽ -????????????????????????????? +ٽԴзʵǶδΪٽ -?????????????????????????????????????????????? +Ϊ˻ٽԴÿڽٽ֮ǰҪȽм顣 ```html // entry section @@ -250,44 +250,44 @@ shortest remaining time next?? // exit section ``` -### 2. ??????? +### 2. ͬ뻥 -?????????????????????????????????????????????????????????????? +ָ̰ͬһ˳ִУָͬһʱֻһܽٽ -???????????????????????????????????????????????????? +ͬڶٽʵĻϣͨʵʵġ -### 3. ????? +### 3. ź -**???????Samaphore??**???????????????????????? down ?? up ???????????????? P ?? V ?????? +**źSamaphore**һͱԶִ down up Ҳdz P V -- **down** : ???????????? 0 ????? - 1 ?????????????????? 0???????????????????????? 0?? -- **up**???????????? + 1 ????????????????????????????? down ?????? +- **down** : ź 0 ִ - 1 ź 0˯ߣȴź 0 +- **up**źִ + 1 һ˯ߵḶ̌ý down -down ?? up ???????????????????????????????????????????????????????? +down up ҪƳԭɷָִͨЩʱжϡ -???????????????? 0 ???? 1???????????**????????Mutex??**??0 ?????????????????1 ?????????????? +źȡֵֻΪ 0 1ôͳΪ**Mutex**0 ʾٽѾ1 ʾٽ ```c typedef int samaphore; samaphore mutex = 1; void P1() { down(mutex); - // ????? + // ٽ up(mutex); } void P2() { down(mutex); - // ????? + // ٽ up(mutex); } ``` -**?????????????????-??????????** +**ʹźʵ-** -???????????? mutex ?????????????????empty ??????????????????full ??????????????????? +ʹһ mutex ٽԴзʣempty ¼ջfull ¼ -???????????? down ??????????????????????????????????????????????????????????????????? down ???????????????????????????????????????? down(empty) ?????????? empty = 0?????????????????????????????????????????????????????????????????????? up(empty) ?????????????????????????????????? +ע⣬ִ down ûٽȶٽȻִ down ߶ٽִ down(empty) empty = 0ʱ˯ߡߴʱܽٽΪ߶ٽˣҲ޷ִ up(empty) ôߺ߾ͻһֱȴȥ ```c #define N 100 @@ -319,11 +319,11 @@ void consumer() { } ``` -### 4. ??? +### 4. ܳ -??????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʹźʵֵҪͻ˴ܶƣ̰ܳѿƵĴ׳Ҳʹÿͻ˴øס -c ?????????????????????????????? Pascal ????????????????????????????? insert() ?? remove() ??????????????????????????????????????????-?????????? +c Բֹ֧̣ܳʾʹ Pascal ̡ܳʾеĹܳṩ insert() remove() ͻ˴ͨ-⡣ ```html monitor ProducerConsumer @@ -342,11 +342,11 @@ monitor ProducerConsumer end monitor; ``` -??????????????????????????????????????????????????????????????????????????????????????????????????????????? +ܳһҪԣһʱֻһʹù̡ܳ޷ִеʱһֱռụ̀ܳ뽫Զʹù̡ܳ -????????? **????????** ????????????**wait()** ?? **signal()** ??????????????????????????? wait() ??????????????????????????????????????????signal() ?????????????????????? +ܳ **** ԼصIJ**wait()** **signal()** ʵִͬ wait() ᵼµýѹܳóһ̳Сsignal() ڻѱĽ̡ -**?????????????-??????????** +**ʹùܳʵ-** ```html monitor ProducerConsumer @@ -390,55 +390,55 @@ begin end; ``` -## ??????? +## ͨ -?????????????????????????????????????????????????????????????????????????????????????? +ͨſԿDz̼ͬ߳ͨţͬһ̵߳ͨŷʽҪʹźͬơ -### 1. ??? +### 1. ܵ -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ܵǵġȽȳġ޽ṹġ̶Сֽһ̵ı׼һ̵ı׼һдڹܵβдݣڹܵ׶˶ݡݶ󽫴ӹܵߣ̶ٶЩݡ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ܵṩ˼򵥵ƻƣͼչܵʱдܵǰ̽һֱͬأܵѾʱͼд̴ܵӹ֮ܵǰд̽һֱ -Linux ??????????????????? +Linux йܵͨļʵ֡ -?????????? +֣ܵ -?? ????????????????????????????????????????????????????????????????????? + ͨܵƣһְֻ֧˫ͨŷʽֻܵ䣻ֻڸӽ֮ʹã -?? ???????????????????????????? + ܵȥһƣ֧˫䣻 -?? ????????????????????????????????????????????? + ܵȥڶƣڲؽ֮ͨš -### 2. ????? +### 2. ź -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +źһƶ̶ԹԴķʡΪһƣֹijڷʹԴʱҲʸԴˣҪΪ̼Լͬһڲֶ֮ͬ߳ͬΡ -### 3. ??????? +### 3. Ϣ -??????????????????????????????????????????????????????????? +Ϣп˷źŴϢ١ֻܵܳ޸ʽֽԼС޵ȱ㡣 -### 4. ??? +### 4. ź -????????????????????????????????????????????????? +źһֱȽϸӵͨŷʽ֪ͨսij¼Ѿ -### 5. ??????? +### 5. ڴ -????????????????????????????????????????????????????????????????????????????????????????? IPC ???????????????????????????????????????????????????????????????????????????????????????????????? +ڴӳһܱʵڴ棬ιڴһ̴̶Էʡڴ IPC ʽ̼ͨŷʽЧʵͶרƵġͨŻƣźʹãʵֽ̼ͬͨš -### 6. ????? +### 6. ׽ -????????????????????????????????????????????????????????????????? +׽Ҳһֽ̼ͨŻƣͨŻƲͬǣڲͬĽͨš -## ??????????? +## ͬ -?????????????????????????????? +ߺǰѾ۹ -### 1. ????-?????? +### 1. -д -???????????????????????????????????????????????????????? +ͬʱݽжDzдԼддͬʱ -?????????? count ????????????????????????????????????? count_mutex ????? count ??????????????? data_mutex ????????????????? +һͱ count ¼ڶݽжĽһ count_mutex ڶ count һ data_mutex ڶԶдݼ ```c typedef int semaphore; @@ -450,7 +450,7 @@ void reader() { while(TRUE) { down(count_mutex); count++; - if(count == 1) down(data_mutex); // ??????????????????????????????????? + if(count == 1) down(data_mutex); // һҪݽмֹд̷ up(count_mutex); read(); down(count_mutex); @@ -469,13 +469,13 @@ void writer() { } ``` -### 2. ???????????? +### 2. ѧҽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a9077f06-7584-4f2b-8c20-3a8e46928820.jpg) -??????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ѧΧһԲܣÿѧǰŷѧҵֽԷԼ˼һѧҳԷʱҪһһߵĿӡ -??????????????????????????????????????????????????????????????????????????????? +һִĽⷨǵÿѧͬʱֱߵĿӣô޷ֱߵĿӣ ```c #define N 5 @@ -496,7 +496,7 @@ void philosopher(int i) { } ``` -??????????????????????????????????????????????????????????????????????????????????????????????????? +Ϊ˷ֹķԼһƣֻͬʱߵĿӣһӵǶδ ```c semaphore mutex = 1; @@ -517,223 +517,223 @@ void philosopher(int i) { } ``` -# ?????? ???? +# -## ?????????? +## ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c037c901-7eae-4e31-a1e4-9d41329e5c3e.png) -1. ???? -2. ???????????????????????????????????????????????????? -3. ??????? -4. ????? +1. +2. 뱣֣һԴʱѻõԴֲš +3. ռ +4. ·ȴ -## ??????????? +## Ĵ -### 1. ??????? +### 1. -???????????????????????????? +ͷɳװû⡣ -????????????? +ֲԲȡ -### 2. ??????? +### 2. Ԥ -???????????????????????? +ڳ֮ǰԤ -#### 2.1 ??????????? +#### 2.1 ƻ -????????????????????????????????????????????????????????????????????????? +ѻӡɸͬʱΨһӡĽǴӡػ̡ -#### 2.2 ??????????????? +#### 2.2 ƻ뱣 -??????????????????????????????????????????? +һʵַʽǹ涨нڿʼִǰҪȫԴ -#### 2.3 ?????????????? +#### 2.3 ƻռ -#### 2.4 ???????? +#### 2.4 ƻ·ȴ -???????????????????????????????????? +Դͳһţֻܰ˳Դ -### 3. ???????? +### 3. -?????????????????????? +ڳʱⷢ -#### 3.1 ????? +#### 3.1 ȫ״̬ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ed523051-608f-4c3f-b343-383e2d194470.png) -? a ?????? has ????????????????????? max ??????????????????free ??????????????????????? a ????????????? B ???????????????????????????? B????? free ??? 4??????????????????? C ?? A???????????????????????????? a ?????????????? +ͼ a ĵڶ has ʾӵеԴ max ʾܹҪԴfree ʾпʹõԴͼ a ʼ B ӵԴнͷ Bʱ free Ϊ 4ͬķʽ C Aʹн̶ܳɹУ˿Գͼ a ʾ״̬ʱȫġ -?????????????????????????????????????????????????????????????????????????????????????????????????????? +壺ûҼʹнͻȻԴҲȻijֵȴܹʹÿһϣƸ״̬ǰȫġ -#### 3.2 ??????????????? +#### 3.2 Դм㷨 -??????????????????????????????????????????????????????????????????????????????????????????????????? +һСмңһȺͻֱŵһĴȣ㷨Ҫж϶Ƿ벻ȫ״̬ǣ;ܾ󣻷Է䡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d160ec2e-cfe2-4640-bda7-62f53e58b8c0.png) -??? c ????????????????????????????????????? c ?????? +ͼ c Ϊȫ״̬㷨ܾ֮ǰ󣬴Ӷͼ c е״̬ -#### 3.3 ?????????????? +#### 3.3 Դм㷨 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/62e0dd4f-44c3-43ee-bb6e-fedb9e068519.png) -????????????????????????????????????????????????????????????????????????? E??P ??? A ???????????????????????????????????????????????????????????????????? A=(1020)????? 4 ??????????? 1/0/2/0?? +ͼ̣ĸԴߵͼʾѾԴұߵͼʾҪԴұߵ EP Լ A ֱʾԴѷԴԼԴעΪǾֵ A=(1020)ʾ 4 Դֱʣ 1/0/2/0 -????????????????????? +һ״̬Ƿȫ㷨£ -?? ????????????????????????????? A??????????????????????????????????????????????? + ұߵľǷһСڵ AУôϵͳᷢ״̬Dzȫġ -?? ?????????????????????????????????????????????? A ?? + ҵһУý̱ΪֹѷԴӵ A С -?? ??????????????????????????????????????????? + ظֱн̶Ϊֹ״̬ʱȫġ -### 4. ???????????????? +### 4. ָ -?????????????????????????????????????????????? +ͼ֯ǵ⵽ʱȡʩлָ -#### 4.1 ????????? +#### 4.1 㷨 -?????????????????????????????????????????????????????????????????????????????????????????????????????? +Ļ˼ǣһԴܹ㣬ôִУͷӵеԴȻĽִС ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e1eda3d5-5ec8-4708-8e25-1a04c5e11f48.png) -?????????????????????????????????????????? +ͼУĸԴÿݴĺ£ -E ????????????? -A ?????????????? -C ???????????????????????????????????????????????????? -R ??????????????????????? +E Դ +A Դʣ +C ÿӵеԴÿһжһӵԴ +R ÿԴ -???? P1 ?? P2 ????????????????????????? P3 ??????? P3 ????????? P3 ??????????? A = (2 2 2 0)??P1 ????????????? P1 ???????? A = (4 2 2 2) ??P2 ?????????????????????????????????? + P1 P2 Դò㣬ֻн P3 ԣ P3 ִУ֮ͷ P3 ӵеԴʱ A = (2 2 2 0)P1 ִУִкͷ P1 ӵеԴ A = (4 2 2 2) P2 ҲִСн̶˳ִУû -?????????? +㷨ܽ£ -?????????????????????????????????????????????????????????????????????? +ÿʼʱǣִйпܱǡ㷨ʱκûбǵĽ̶̡ -?? ?????????????? Pi??????????????????? A?? -?? ????????????????????????? C ?????? i ????????? A ??????????????? ??? -?? ??????????????????????????? + ѰһûбǵĽ PiԴСڵ A + ҵһ̣ô C ĵ i ӵ A УǸỵ́ת ١ + ûһ̣㷨ֹ -#### 4.2 ??????? +#### 4.2 ָ -?? ?????????? -?? ??????? + ռָ + ɱ -# ?????? ??????? +# 洢 -## ??????? +## ڴ -??????????????????????????????????????????? ??????????????????????????????????????????????????????????????????? +ÿӵԼĵַռ䣬ַռ䱻ָɶ飬ÿһΪһ ҳЩҳӳ䵽ڴ棬Ҫӳ䵽ڴ棬ҲҪҳڴС -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????g????????????? +õһڴеĵַռʱӲִбҪӳ䡣õһֲڴеĵַռʱɲϵͳȱʧIJװڴ沢ִʧָܵ -## ??????? +## ҳֶ -### 1. ??? +### 1. ҳ -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ûĵַռ䱻Ϊɹ̶С򣬳ΪҳӦأڴռֳɸ飬ҳͿĴСȡɽûһҳڴһУʵɢ䣬һҳά֮ӳϵ -### 2. ??? +### 2. ֶ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/22de0538-7c6e-4365-bd3b-8ce3c5900216.png) -????????????????????????????????? 4 ????????????????????????????????????????????????????????????? +ͼΪһڱнĶ 4 Ƕ̬ģʹ÷ҳϵͳһάַռ䣬̬صᵼ¸ij֡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e0900bb2-220a-43b7-9aa9-1d5cd55ff56e.png) -??????????????????????????????????????????????????????????????? +ֶεǰÿֳɶΣһιһĵַռ䡣ÿεijȿԲͬԶ̬ı䡣 -???????????????????? +ÿζҪԱ֡ -### 3. ???? +### 3. ҳʽ -????????????????????????????????????????????????????????????????????????????????????????? +÷ֶη͹洢ĵַռ䰴߼λֳɻĶΣÿһԼĶٰÿηֳɹ̶Сҳ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????? +÷ҳ͹ʵ档ֳҳСȵĴ洢飬װҵκһҳڴĵǰҳеġֿɰʵֹͱ -### 4. ??????????? +### 4. ҳֶ -?? ???????????????????????????????????????????????? + ԳԱ͸ԣҳ͸ǷֶҪԱʾÿΡ -?? ?????????????????????????????????? + ַռάȣҳһάַռ䣬ֶǶάġ -?? ??????????????????????????????? + СǷԸı䣺ҳĴСɱ䣬εĴСԶ̬ı䡣 -?? ??????????????????????????????????????????????????????????????????????????????????????????????????????? + ֵԭ򣺷ҳҪʵڴ棬Ӷøĵַռ䣻ֶҪΪʹݿԱΪ߼϶ĵַռ䲢ڹͱ -## ???????? +## ҳû㷨 -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ڳйУҪʵҳ治ڴҪǵڴ棬ڴ޿пռʱϵͳڴеһҳ浽̶ԻУҽҪҳڴСҳû㷨ҪĿʹҳûƵͣҲ˵ȱҳͣ -### 1. ????Optimal?? +### 1. ѣOptimal -?????????????????????????????????????????????????????? +ѡıҳ潫ʱڲٱʣͨԱ֤͵ȱҳʡ -???????????????????????????????????????????? +һϵ㷨Ϊ޷֪һҳ೤ʱᱻٷʵ -????????????????????????????????????????????????? +һϵͳΪij̷飬ҳУ -7??0??1??2??0??3??0??4??2??3??0??3??2??1??2??0??1??7??0??1 +70120304230321201701 -?????????????? 7,0,1 ?????????????????????????? 2 ????????????????? 7 ???????????? 7 ??????????????? +ʱȽ 7,0,1 ҳװڴ档Ҫҳ 2 ʱȱҳжϣὫҳ 7 Ϊҳ 7 ٴαʵʱ -### 2. ????????FIFO?? +### 2. ȽȳFIFO -??????????????????????? +ѡ񻻳ҳȽҳ档 -???????????????????????????????????????????? +㷨ὫЩʵҳҲӶʹȱҳߡ -### 3. ??????????LRU??Least Recently Used?? +### 3. δʹãLRULeast Recently Used -???????????????????????????????????????????????????LRU ??????????????I???? +Ȼ޷֪Ҫʹõҳǿ֪ȥʹҳLRU δʹõҳ滻 -???????????????????՛????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ջʵָ㷨ջд洢ҳҳš̷һҳʱҳҳŴջƳѹջʵҳҳջδʹõҳҳջס -4??7??0??7??1??0??1??2??1??2??6 +47071012126 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/eb859228-c0f2-4bce-910d-d9f76929352b.png) -### 4. ????Clock?? +### 4. ʱӣClock -Clock ??????????????????????????????R????????????????? 1?? +Clock ҳû㷨Ҫõһλһҳ汻ʱΪΪ 1 -????????????????????????????????????????????????n?????????????????????????? 0?????????I???????????????????? 0??????????????????????????? +ȣڴеҳӳһѭУȱҳжϷʱ鵱ǰָָҳķλλΪ 0ͽҳ滻򽫸ҳķλΪ 0ҳڶεĻᣬƶָ顣 -# ?????? ????? +# 豸 -## ????????? +## ̵㷨 -??????????????????????????????????????????????????????????????????????????????????????? +ͬʱʴʱҪд̵ƶԴ̵ķʡ̵ȵҪĿʹ̵ƽѰʱ١ -### 1. ?????????FCFS??First Come First Serverd?? +### 1. ȷFCFSFirst Come First Serverd -????????????????????????????????????????????????????????????????????????????????????????? +ݽʴ̵Ⱥеȡŵǹƽͼ򵥣ȱҲԣΪδѰκŻʹƽѰʱܽϳ -### 2. ??????????????SSTF??Shortest Seek Time First?? +### 2. ѰʱȣSSTFShortest Seek Time First -???????????????????????????????????????????????????????????????????????? FCFS ??? +ҪʵĴŵ뵱ǰͷڴŵȽеȡ㷨ܱ֤ƽѰʱ̣DZ FCFS úܶࡣ -### 3. ???????SCAN?? +### 3. ɨ㷨SCAN -SSTF ?????????????????????????????????????????????????????????????????????????????????????????????????????? +SSTF ֽм󡣿½ʵĴŵͷڴŵľDZһڵȴĽĽôȴĽ̻һֱȴȥ -SCAN ???? SSTF ???????????????????????????????????????????????????????????????????????????????????????????????????????????????? +SCAN 㷨 SSTF 㷨֮Ͽ˴ͷƶҪʵĴŵڴͷǰƶϲܹõȡΪƶôһʵĴŵһõȡ -??????????????????????????????????????????????????????????????????????????????????? SCAN ?????????????? +һͷƶʱƵıƶΪƶĹڵݵУֳ SCAN 㷨Ϊݵ㷨 -### 4. ??????????CSCAN?? +### 4. ѭɨ㷨CSCAN -CSCAN ?? SCAN ?????????????????????????????????? +CSCAN SCAN ˸ĶҪͷʼһƶ -# ?????? +# ο - Tanenbaum A S, Bos H. Modern operating systems[M]. Prentice Hall Press, 2014. -- ?????, ?????, ????. ???????????[M]. ???????????????????, 2001. +- , ܷ, С. ϵͳ[M]. ӿƼѧ, 2001. -- Bryant, R. E., & O??Hallaron, D. R. (2004). ?????????????. +- Bryant, R. E., & OHallaron, D. R. (2004). ϵͳ. -- [???????????????](http://wdxtub.com/interview/index.html) +- [Сˢʼ](http://wdxtub.com/interview/index.html) -- [??????????????](http://blog.csdn.net/yufaw/article/details/7409596) +- [̼ļͨŷʽ](http://blog.csdn.net/yufaw/article/details/7409596) diff --git a/notes/计算机网络.md b/notes/计算机网络.md index a202625f..b4581956 100644 --- a/notes/计算机网络.md +++ b/notes/计算机网络.md @@ -1,840 +1,840 @@ -* [????? ????](#?????-????) - * [?????????](#?????????) +* [һ ](#һ-) + * [](#) * [ISP](#isp) - * [???????????](#???????????) - * [?????????????](#?????????????) - * [????????????](#????????????) - * [1. ??????](#1-??????) - * [2. ???????](#2-???????) - * [3. ?????](#3-?????) - * [???](#???) - * [1. ???????](#1-???????) - * [2. ???????](#2-???????) - * [3. ???????](#3-???????) - * [4. ??????](#4-??????) - * [??????????????*](#??????????????) - * [1. ?????](#1-?????) - * [2. ?????](#2-?????) - * [3. ???????????????????](#3-???????????????????) - * [4. TCP/IP ?????](#4-tcpip-?????) -* [????? ?????](#?????-?????) - * [?????](#?????) - * [???????](#???????) - * [?????????](#?????????) - * [1. ????????????](#1-????????????) - * [2. ?????????](#2-?????????) - * [3. ???????](#3-???????) - * [4. ??????](#4-??????) -* [?????? ????????](#??????-????????) - * [????????????](#????????????) - * [1. ??????](#1-??????) - * [2. ???????](#2-???????) - * [3. ?????](#3-?????) - * [??????? -PPP ??](#???????--ppp-??) - * [????????????](#????????????) - * [????? - CSMA/CD ??*](#?????---csmacd-??) - * [??????](#??????) - * [MAC ??](#mac-??) - * [?????????](#?????????) -* [?????? ?????*](#??????-?????) - * [?????? IP ????](#??????-ip-????) - * [IP ????????](#ip-????????) - * [IP ??????](#ip-??????) - * [1. ????? IP ???](#1-?????-ip-???) - * [2. ????????](#2-????????) - * [3. ??????? CIDR???????????](#3-???????-cidr???????????) - * [IP ????? MAC ???](#ip-?????-mac-???) - * [????????? ARP](#?????????-arp) - * [???????](#???????) - * [??????????????????](#??????????????????) - * [???????????????](#???????????????) - * [???????](#???????) - * [1. ????????? RIP](#1-?????????-rip) - * [2. ????????? OSPF](#2-?????????-ospf) - * [3. ???????? BGP](#3-????????-bgp) - * [???????????? ICMP](#????????????-icmp) - * [??????????? PING](#???????????-ping) - * [IP ??](#ip-??) - * [????????? VPN](#?????????-vpn) - * [????????? NAT](#?????????-nat) -* [?????? ?????*](#??????-?????) - * [UDP ?? TCP ?????](#udp-??-tcp-?????) - * [UDP ??????](#udp-??????) - * [TCP ??????](#tcp-??????) - * [TCP ??????????](#tcp-??????????) - * [TCP ???????](#tcp-???????) - * [TCP ????????](#tcp-????????) - * [TCP ???????](#tcp-???????) - * [TCP ????????](#tcp-????????) - * [TCP ???????](#tcp-???????) - * [??????????????](#??????????????) - * [??????????](#??????????) -* [?????? ???*](#??????-???) - * [?????? DNS](#??????-dns) - * [1. ???](#1-???) - * [2. ????????](#2-????????) - * [????????? FTP](#?????????-ftp) - * [???????? TELNET](#????????-telnet) - * [????? WWW](#?????-www) - * [?????????](#?????????) + * [](#) + * [֮ͨŷʽ](#֮ͨŷʽ) + * [·齻](#·齻) + * [1. ·](#1-·) + * [2. Ľ](#2-Ľ) + * [3. 齻](#3-齻) + * [ʱ](#ʱ) + * [1. ʱ](#1-ʱ) + * [2. ʱ](#2-ʱ) + * [3. ʱ](#3-ʱ) + * [4. Ŷʱ](#4-Ŷʱ) + * [ϵṹ*](#ϵṹ) + * [1. ߲Э](#1-߲Э) + * [2. Э](#2-Э) + * [3. ڸ֮Ĵݹ](#3-ڸ֮Ĵݹ) + * [4. TCP/IP ϵṹ](#4-tcpip-ϵṹ) +* [ڶ ](#ڶ-) + * [ͨŷʽ](#ͨŷʽ) + * [ͨ](#ͨ) + * [ŵü](#ŵü) + * [1. Ƶָáʱָ](#1-Ƶָʱָ) + * [2. ͳʱָ](#2-ͳʱָ) + * [3. ָ](#3-ָ) + * [4. ָ](#4-ָ) +* [ ·](#-·) + * [](#) + * [1. װ֡](#1-װ֡) + * [2. ͸](#2-͸) + * [3. ](#3-) + * [Եŵ -PPP Э](#Եŵ--ppp-Э) + * [](#) + * [㲥ŵ - CSMA/CD Э*](#㲥ŵ---csmacd-Э) + * [](#) + * [MAC ](#mac-) + * [](#) +* [ *](#-) + * [Э IP ](#Э-ip-) + * [IP ݱʽ](#ip-ݱʽ) + * [IP ַַ](#ip-ַַ) + * [1. IP ַ](#1--ip-ַ) + * [2. ](#2-) + * [3. ޷ַ CIDRɳ](#3-޷ַ-cidrɳ) + * [IP ַ MAC ַ](#ip-ַ-mac-ַ) + * [ַЭ ARP](#ַЭ-arp) + * [·Ľṹ](#·Ľṹ) + * [·](#·) + * [·ת](#·ת) + * [·ѡЭ](#·ѡЭ) + * [1. ڲЭ RIP](#1-ڲЭ-rip) + * [2. ڲЭ OSPF](#2-ڲЭ-ospf) + * [3. ⲿЭ BGP](#3-ⲿЭ-bgp) + * [ʿƱЭ ICMP](#ʿƱЭ-icmp) + * [̽ PING](#̽-ping) + * [IP ಥ](#ip-ಥ) + * [ר VPN](#ר-vpn) + * [ַת NAT](#ַת-nat) +* [ *](#-) + * [UDP TCP ص](#udp--tcp-ص) + * [UDP ײʽ](#udp-ײʽ) + * [TCP ײʽ](#tcp-ײʽ) + * [TCP ](#tcp-) + * [TCP Ĵλ](#tcp-Ĵλ) + * [TCP ](#tcp-) + * [TCP ɿ](#tcp-ɿ) + * [TCP ](#tcp-) + * [TCP ӵ](#tcp-ӵ) + * [ʼӵ](#ʼӵ) + * [شָ](#شָ) +* [ Ӧò*](#-Ӧò) + * [ϵͳ DNS](#ϵͳ-dns) + * [1. νṹ](#1-νṹ) + * [2. ](#2-) + * [ļЭ FTP](#ļЭ-ftp) + * [ԶնЭ TELNET](#ԶնЭ-telnet) + * [ά WWW](#ά-www) + * [ʼЭ](#ʼЭ) * [POP3](#pop3) * [IMAP](#imap) * [SMTP](#smtp) - * [????????????? DHCP](#?????????????-dhcp) - * [?????? P2P](#??????-p2p) - * [Web ??????????](#web-??????????) - * [??????](#??????) -* [??????](#??????) + * [̬Э DHCP](#̬Э-dhcp) + * [Ե㴫 P2P](#Ե㴫-p2p) + * [Web ҳ](#web-ҳ) + * [ö˿](#ö˿) +* [ο](#ο) -# ????? ???? +# һ -## ????????? +## -????????????????????????????????????????????????????????????????????? +ǰѶֲͬ˻硣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/04ff7ae6-7bee-4cf8-82f8-dfe2ba1f3616.jpg) ## ISP -?????????????? ISP ??????????????????????? IP ????????????????????????????????????????? ISP ????????????????????????? +ṩ ISP Դӻ IP ַͬʱӵͨ·Լ·豸˻ ISP һķþͿԽ뻥 -????????????????? ISP ????ISP ???????????????????? ISP?????? ISP ????? ISP?? +ĿǰĻһֶ ISP ṹISP ݸĴСΪ ISP ISP ͱ ISP -???????????? IXP ?????????????????????????????????????????? + IXP ֱþת顣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/17d807ef-03bf-4824-a97c-ea5fb58ec61d.jpg) -## ??????????? +## -1. ?????????????????????????????????????????????? -2. ????????????????????????????????????????????????????????? +1. Ե֣ڻϵûֱʹã +2. IJ֣ɴЩ·ɡΪԵֵṩ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/005d83c2-e64a-41f0-bbdd-51c71d494a18.jpg) -## ????????????? +## ֮ͨŷʽ -**1. ??? - ????????C/S??** +**1. ͻ - C/S** -???????????????????????????????? +ͻǷ󷽣Ƿṩ -**2. ????P2P??** +**2. ԵȣP2P** -????????????????? +ֿͻͷ -## ???????????? +## ·齻 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b97958dd-3e43-45f7-97f5-3ec20f3f8b88.jpg) -### 1. ?????? +### 1. · -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? 10%?? +·ڵ绰ͨϵͳûҪ֮ͨǰҪһרõ·ͨŹʼռø·ͨŵĹвһֱʹô·˵··ʺܵͣ 10% -### 2. ??????? +### 2. Ľ -?????????????????????????????????????????????????????????????????????????????????????????????????? +Ľʾͨϵͳʾֽյһݱ֮ȴ洢ȻĿĵѡԵذѱתһĿĵأ̾Ǵ洢ת̡ -### 3. ????? +### 3. 齻 -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +齻Ҳʹ˴洢תתǷDZġݳΪһģһĿܺܳҪȽз֣ܴĴСÿзֵǰײ֮ͳΪ˷飬ײĿĵַԴַȿϢ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6f4af159-8b03-4246-8d0e-222db65bb83c.jpg) -??????????????????????????????????????????????????????????????? +洢תһ·ϴͶķ飬ҲǷ齻Ҫռö˵˵·Դ -?????????????????????????????????????????? +ڱĽڷȱĸС洢תٶҲ͸졣 -## ??? +## ʱ -????? = ??????? + ??????? + ??????? + ?????? +ʱ = ʱ + ʱ + ʱ + Ŷʱ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ceee91c2-da26-4169-94c3-e4608b46b9ac.png) -### 1. ??????? +### 1. ʱ -????????????????????????????? +·֡Ҫʱ䡣 ![](http://latex.codecogs.com/gif.latex?\\\\delay=\frac{l(bit)}{v(bit/s)}) -???? l ??????????????v ???????????? + l ʾ֡ijȣv ʾʡ -### 2. ??????? +### 2. ʱ -?????????????????????????????????????????????????? +ŲŵдһľҪѵʱ䣬ŲٶȽӽ١ ![](http://latex.codecogs.com/gif.latex?\\\\delay=\frac{l(m)}{v(m/s)}) -???? l ???????????v ??????????????????????? + l ʾŵȣv ʾŲŵϵĴʡ -### 3. ??????? +### 3. ʱ -????????????????????????????????????????????????????????????????? +·յʱдҪʱ䣬ײӷȡݲֵȡ -### 4. ?????? +### 4. Ŷʱ -??????????????????????????????????????????????y?????????? +·кŶӵȴʱ䣬ȡ統ǰͨ -## ??????????????* +## ϵṹ* ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1005dc9d-9049-4b06-9524-6171e56ebd8c.png) -### 1. ????? +### 1. ߲Э -??? a ???????????????????????? +ͼ a ʾбʾͻỰ;£ -1. ??????????????????????????????????????????????????????? -2. ?????????????????????????????? +1. ʾ㣺Ϣ﷨ԼǵĹܽܡת롢ѹѹ +2. Ự㣺ͬϵû֮佨Ự -### 2. ????? +### 2. Э -1. ???????????????????????????? HTTP??DNS ?????????????? +1. Ӧò㣺ΪضӦóṩݴ HTTPDNS ȡݵλΪġ -2. ???????????????????????????????????????????????????????????????????????????????????????????????? TCP????????????????????????????????????????????????? UDP??????????????????????????????????????????????? +2. 㣺ṩǽ̼ͨݴӦòЭܶ࣬ͨõЭͿֲ֧ӦòЭ顣Э飺Э TCPṩӡɿݴݵλΪĶΣûݱЭ UDPṩӡŬݴݵλΪûݱ -3. ????????????????????????????????????????????????????????????????????????????????????????????????????? +3. 㣺Ϊ֮ṩ񣬶ЭΪеĽṩıĶλûݱװɷд䡣 -4. ??????????????????????????????????????????????????????????????????????????????????????????????????? +4. ·㣺ԵĻ֮䣬֮кܶ··ЭΪڽ֮ṩ·㴫ķװ֡ -5. ?????????????????????????????????????????????????????????^???????????????????????????????????????????????????????????????? +5. 㣺ǵڴýϴݱָĴý塣ǾδýֶͨεIJ죬ʹϵ·оЩ졣 -### 3. ??????????????????? +### 3. ڸ֮Ĵݹ -??????????????????????????????????????????????????????????????????? +µĹУҪ²ЭҪײβϵĹвϲײβ -?????????????????????????????????????????????????????????? +·ֻЭ飬Ϊ·λУҪΪ̻Ӧóṩ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f7d5da89-2d75-4d8f-85e7-6b608865dc00.jpg) -### 4. TCP/IP ????? +### 4. TCP/IP ϵṹ -??????????????????????????????????????????? +ֻIJ㣬൱Э·ϲΪӿڲ㡣 -????? TCP/IP ????????????? OSI ??????????????????? IP ???????????? +ڵ TCP/IP ϵṹϸѭ OSI ֲӦòֱܻʹ IP ӿڲ㡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/3e2200b3-1c18-4853-ae42-7788e8e1f939.png) -TCP/IP ???????????????????????IP ?????????????????????? +TCP/IP Эһɳ©״мСߴIP Эռþصĵλ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9ecaebee-670e-4cb2-9cdb-3029c00f33bd.png) -# ????? ????? +# ڶ -## ????? +## ͨŷʽ -1. ???????????????????? -2. ?????????????????????? -3. ????????????????????? +1. ͨţֳΪͨţ +2. ˫ͨţֳΪ˫ͨţ +3. ˫ͬʱͨţֳΪȫ˫ͨš -## ??????? +## ͨ -?????????????????????????????????????????????????????????????? +ģźźţźɢźšͨưźתΪģźš ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d2c55c84-aa1f-43c1-bd97-457bcb7816b3.png) -## ????????? +## ŵü -### 1. ???????????? +### 1. Ƶָáʱָ -??????????????????????????????????????????????????????????????????????????????????? +ƵָõûͬʱռòͬƵʴԴʱָõûڲͬʱռͬƵʴԴ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʹַʽͨţͨŵĹûһֱռһŵԴڼݵͻʣûҪһֱռŵԴóûʹãַʽŵʶߡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/543d47a1-f0dd-414f-b23c-0c142c814854.png) -### 2. ????????? +### 2. ͳʱָ -?????????????????????????????????????????????????????????????????????????? +ǶʱָõһָĽ̶ÿûʱָ֡еλãֻҪݾͼʱָ֡Ȼ͡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/29058e09-bb72-4040-a73d-4c497895e9ce.jpg) -### 3. ??????? +### 3. ָ -??????????????????????????????????????????????????????????? +ƵָáڹƵʺܸߣϰòƵʾʹõĹز ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/78534153-88d1-4f83-a6e0-59064dbdc43a.png) -### 4. ?????? +### 4. ָ -??????????? m bit ??????????????????????????????????????? $\vec{S}$ ?? $\vec{T}$ ?? +Ϊÿû m bit ƬеƬƬ $\vec{S}$ $\vec{T}$ ![](http://latex.codecogs.com/gif.latex?\\\\\vec{S}\cdot\vec{T}=0) -??????? m=8??????? $\vec{S}$ ? 00011011?????????????????????? 1 ??????????????????? 0 ??????????????? 11100100?? +Ϊ˷㣬ȡ m=8Ƭ $\vec{S}$ Ϊ 00011011ӵиƬûͱ 1 ʱͷ͸Ƭͱ 0 ʱͷ͸Ƭķ 11100100 -???????? 00011011 ???? (-1 -1 -1 +1 +1 -1 +1 +1)???????? +ڼʱ 00011011 (-1 -1 -1 +1 +1 -1 +1 +1)Եõ ![](http://latex.codecogs.com/gif.latex?\\\\\frac{1}{m}\vec{S}\cdot\vec{S}=1) ![](http://latex.codecogs.com/gif.latex?\\\\\frac{1}{m}\vec{S}\cdot\vec{S'}=-1) -???? $\vec{S'}$ ? $\vec{S}$ ????? + $\vec{S'}$ Ϊ $\vec{S}$ ķ롣 -?????????????????????????????????? $\vec{S}$ ????????????????????????????? 0 ????????????????????????? 1 ??????????????? 1?????? -1 ??????????????? 0?? +ʽ֪նʹƬ $\vec{S}$ ԽյݽڻʱΪ 0 û͵ݣΪ 1 û͵ı 1Ϊ -1 û͵ı 0 -???????????????????? m ???? +ָ÷͵Ϊԭȵ m ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/0042edad-8e3b-4279-bd93-6906fcd1b640.jpg) -# ?????? ???????? +# · -## ???????????? +## -### 1. ?????? +### 1. װ֡ -?????????????????????????????????????????????? +㴫ķײβڱ֡Ŀʼͽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ea5ed9b2-6d9f-48fb-b890-0288caf9088a.jpg) -### 2. ??????? +### 2. ͸ -???????????????????????????????????? +͸ʾһʵʴڵ￴񲻴һ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +֡ײβ֡ݲֺкײβͬݣô֡ĿʼͽλþͻᱻжҪгײβͬǰתַҪþתַôתַǰټӸתַڽն˽д֮Իԭԭʼݡ͸תַûѲתַĴڡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/44e1d90e-3fe6-4dd6-8dce-6daab12e7663.jpg) -### 3. ????? +### 3. -?????????????????????????CRC???????????? +Ŀǰ·㷺ʹѭ飨CRCز -## ??????? -PPP ?? +## Եŵ -PPP Э -??????????????????????? ISP ?????????????????PPP ??????????????? ISP ??????????????????????? +ûͨҪӵij ISP ֮ܽ뵽PPP Эû ISP ͨʱʹõ·Э顣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8b5bd2c8-8425-4a8b-89db-235c95800de9.jpg) -?? PPP ?????F ?????????????A ?? C ?????????^FCS ????? CRC ??????????????????????? 1500?? + PPP ֡УF ֶΪ֡ĶA C ʱû塣FCS ʹ CRC ļСϢֶεijȲ 1500 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a5fa89e7-54b9-4e2f-8c48-a35712d7b2f5.jpg) -## ???????????? +## ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8b15e36f-69b4-46b6-a07c-7234ac7c7927.jpg) -## ????? - CSMA/CD ??* +## 㲥ŵ - CSMA/CD Э* -????????????????????????????????????? +ڹ㲥ŵϣͬһʱֻһ̨ݡ -CSMA/CD ???????????????? / ?????? +CSMA/CD ʾز / ײ⡣ -**??????**???????????????????????????????????????????? -**???????**?????????????????????????????????????????????????????????? -**??????**??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +****˵磬Զķʽӵϡ +**ز**ÿվ벻ͣؼŵڷǰŵʹãͱȴ +**ײ**ڷУŵվڷݣͱʾײȻÿһվڷ֮ǰѾŵΪУڵŲĴʱӵĴڣпܻᷢײ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f9ed4da5-0032-41e6-991a-36d995ec28fd.png) -?????????????? ??????????????????? 2?? ?????????????????????? 2?? ? **??????**?????????????????????????????????????????????? +Ƕ˵˵ĴʱΪ ӣȷ͵վྭ 2 Ϳ֪Ƿײ 2 Ϊ ****ֻо֮ûм⵽ײܿ϶ηͲᷢײ -??????????????????????????????????????????????? **????????????????** ?????????????????????? {0, 1, .., (2k-1)} ??????????????????? r?????? r ?????????????????????? +ײʱվҪֹͣͣȴһʱٷ͡ʱ **ض϶ָ˱㷨** ȷɢ {0, 1, .., (2k-1)} ȡһ rȻȡ r Ϊشȴʱ䡣 -## ?????? +## -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ӱϿʹüľһǼʹõģʵߵĹ߼һϵͳһͳ̫С ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/897a4f4e-2683-44e1-a26a-c0d0234dc576.jpg) ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/40c3f8e5-3a20-45b6-a60c-77b9b952e104.jpg) -## MAC ?? +## MAC -MAC ????? 6 ????48 ????????????????????????????????????????????? MAC ?????????????????????????????????????????????????????? +MAC ַ 6 ֽڣ48 λĵַΨһʾһ̨ӵжж MAC ַʼDZձ -MAC ??????????????????????????????????? 46-1500 ???????????????FCS ???????????????? CRC ????????????????????????????? FCS ???????????????????????? +MAC ֶ֡ϲʹʲôЭ飻ֶγ 46-1500 ֮䣬̫СҪ䣻FCS Ϊ֡Уʹõ CRC 鷽ǰǰֻͬΪ˼ FCS ʱģ֮ᶪ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/50d38e84-238f-4081-8876-14ef6d7938b5.jpg) -## ????????? +## -???????????????????????????????????????????????????????????????????????????? (A1, A2, A3, A4) ??????????????????A1 ???????? A2??A3??A4 ????????????????????? +Խλ޹ص߼飬ֻͬһеijԱŻյ㲥Ϣͼ (A1, A2, A3, A4) һA1 ͵Ĺ㲥ᱻ A2A3A4 յվղ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a74b70ac-323a-4b31-b4d5-90569b8a944b.png) -# ?????? ?????* +# * -## ?????? IP ???? +## Э IP -??????????????????????????????????????????????????????????????????????????????????????????????? +ΪĺģӦ㾡ܼ򵥡ֻṩġӵġŬݱ -??? IP ?????????????????????????????????????????????????????????? +ʹ IP Э飬԰칹ʹ㿴һͳһ硣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/fe3d224c-8ffd-40f9-85b1-86ffe1393f6c.jpg) -?? IP ?????????????????? + IP ЭʹõĻЭ飺 -1. ????????? ARP??Address Resolution Protocol?? -2. ???????????? ICMP??Internet Control Message Protocol?? -3. ??????????? IGMP??Internet Group Management Protocol?? +1. ַЭ ARPAddress Resolution Protocol +2. ʿƱЭ ICMPInternet Control Message Protocol +3. Э IGMPInternet Group Management Protocol ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/163cf8b4-5f30-46c9-af00-316a71b3c890.jpg) -## IP ???????? +## IP ݱʽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8681db55-0873-434b-aa98-83d07e8392ae.jpg) -**?** : ?? 4??IPv4???? 6??IPv6????????? +**汾** : 4IPv4 6IPv6ֵ -**???????** : ? 4 ?????????? 15???? 1 ??????? 1 ?? 32 ???????????? 4 ?????????????????? 20 ???????????? 5??????????????????? 4 ?????????????????????????????? +**ײ** : ռ 4 λֵΪ 15ֵΪ 1 ʾ 1 32 λֵijȣҲ 4 ֽڡΪײ̶Ϊ 20 ֽڣ˸ֵСΪ 5ѡֵijȲ 4 ֽڵβ䲿䡣 -**???????** : ??????????????????????????? +**ַ** : øõķһ²á -**?????** : ??????????????????????? +**ܳ** : ײȺݲֳȡ -**???** : ?????????????????????????????????????????????????????????????? +**ʶ** : ݱȹӶƬ£ͬݱIJͬƬͬıʶ -**????** : ????????????????????????????????? 8 ???? +**Ƭƫ** : ͱʶһڷƬƬƫƵĵλΪ 8 ֽڡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/45c86855-9b18-4cf4-a9a7-f8b6eb78d133.png) -**???????** ??TTL????????????????????????????????????????????????????????????? TTL ? 0 ????????????? +**ʱ** TTLĴΪ˷ֹ޷ݱڻв϶Ȧӡ·Ϊλ TTL Ϊ 0 ʱͶݱ -**??**??????????????????????????????????? ICMP??TCP??UDP ??? +**Э**ָЯӦϽĸЭд ICMPTCPUDP ȡ -**????????**??????????????????????????????????????????????????????????????????????????? +**ײ**Ϊݱÿһ·Ҫ¼ͣ˼ͲݲֿԼټĹ -## IP ?????? +## IP ַַ -IP ?????????????????????????? +IP ַıַʽʷ׶Σ -1. ????? IP ????? -2. ?????????? -3. ????????? +1. IP ַ +2. Ļ֣ +3. ɳ -### 1. ????? IP ??? +### 1. IP ַ -??????????????????????????????????????????????????????????? +ɣźţвͬвͬųȣǹ̶ġ -IP ??? ::= {< ????? >, < ?????? >} +IP ַ ::= {< >, < >} ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2ddd6132-60be-4a72-9daa-3d9756191f4a.png) -### 2. ???????? +### 2. -?????????????????????????????????????? IP ???????????? IP ?????????????I?????????????? +ֶͨһΪţ IP ַΪ IP ַע⣬ⲿ翴Ĵڡ -IP ??? ::= {< ????? >, < ?????? >, < ?????? >} +IP ַ ::= {< >, < >, < >} -????????????????????????????? B ?????????????????? 255.255.0.0????? B ???????????????????????????????? 11111111 11111111 11000000 000000??????? 255.255.192.0?? +Ҫʹ롣һ B ַĬΪ 255.255.0.0 B ַռأôΪ 11111111 11111111 11000000 000000Ҳ 255.255.192.0 -### 3. ??????? CIDR??????????? +### 3. ޷ַ CIDRɳ -CIDR ???????? A ??B ??? C ????????????????????????????????????????? IP ???????????????????????????????? +CIDR ˴ͳ A ࡢB C ַԼĸʹǰ׺ IP ַб룬ǰ׺ijȿԸҪ仯 -IP ??? ::= {< ???????? >, < ?????? >} +IP ַ ::= {< ǰ׺ >, < >} -CIDR ??????????? IP ?????????????????????????????? 128.14.35.7/20 ???? 20 ????????? +CIDR ļǷϲ IP ַǰ׺ȵķ 128.14.35.7/20 ʾǰ 20 λΪǰ׺ -CIDR ??????????????????????????????????? 1 ????????????????? +CIDR ĵַԼΪ룬 1 Ϊǰ׺ijȡ -??? CIDR ????????????????? CIDR ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +һ CIDR ַкַܶһ CIDR ʾͿԱʾԭĺܶ磬·ɱֻҪһ·ɾͿԴԭĶ·ɣ·ɱͨʹǰ׺·ɱķʽΪ·ɾۺϣҲΪɳ -???????????????????????????????????????????????????????????????????????????????? +·ɱÿĿɡǰ׺͡һַɣڲʱܻõֹһƥӦǰ׺ƥ䡣 -## IP ????? MAC ??? +## IP ַ MAC ַ -??????????????????????????????????????????????????????????IP ?????????????????????????? MAC ????????????????? +ʵ֮ͨţ·ʵ־ÿ·֮ͨšͨŹУIP ݱԴַĿĵַʼղ䣬 MAC ַ·ĸıı䡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/86b71296-0d1e-4a63-bcd9-54955b6b781b.jpg) -## ????????? ARP +## ַЭ ARP -????? IP ?????? MAC ????? +ʵ IP ַõ MAC ַ -?????????????? ARP ??????????????????? IP ??? ?? MAC ??????????????????????????????? ARP ????????? IP ????????????? ARP ????????? MAC ????? +ÿһ ARP ٻ棬ӳһ IP ַ MAC ַӳ䲻ڸñУͨ㲥ķʽ ARP 飬ƥ IP ַᷢ ARP Ӧ֪ MAC ַ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8bc6fc2c-d198-4759-b06c-18d94d851e97.png) -## ??????? +## ·Ľṹ -???????????????????????????????? +·ԻΪ󲿷֣·ѡͷת -??????????????????????????????????????????????????? +תɣṹһ˿ںһ˿ڡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/3a676c54-b559-4466-9b21-eb10f1e25879.jpg) -???????????????????????????????? +ṹĽʵַʽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7f82fd18-7f16-4125-ada6-bb6b795b4fda.png) -## ?????????????????? +## · -- ????????????????????????? MAC ????????? MAC ????????????????????????????????? IP ????? MAC ???????? +- ·㣬ʶ MAC ַ MAC ַת·֡ѧά IP ַ MAC ַӳ䡣 -- ???????????????? IP ????????? IP ????????????????????????????????? +- ·λ㣬ʶ IP ַ IP ַת顣ά·ɱ·ɱѡ·ߡ -## ??????????????? +## ·ת -1. ???????????????????????? IP ??? D?????????????? N????????????????????? IP ??????????????????????????????? -2. ?? N ???????????????????????????????????????????? -3. ??????????????? D ??????????????????????????????????????????????? -4. ??????????????? N ????????????????????????????????????????? -5. ???????????????????????????????????????????????????? -6. ?????????????? +1. ݱײȡĿ IP ַ DõĿַ N·ɱŶ IP ַ·ɱĿ +2. N ·ֱijֱַӽ +3. ·ɱĿĵַΪ D ض·ɣݱ͸ָһ· +4. ·ɱе N ·ɣݱ͸·ɱָһ· +5. ·ɱһĬ·ɣݱ͸·ɱָĬ· +6. ת ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8d211911-0e62-4190-ab00-d8610adec4a0.jpg) -## ??????? +## ·ѡЭ -????????????????Y??????????????????????????????????????????????? +ʹõ·ѡЭ鶼Ӧģͨ˱仯Ӧؽе -?????????????????????????? AS????? AS ????????????? AS ??????????? +ԻΪСϵͳ ASһ AS ʹһֺͱ AS ͬ·ѡЭ顣 -???????????l????????? +԰·ѡЭ黮Ϊࣺ -1. ????????? IGP??Interior Gateway Protocol?? ????????????????? RIP ?? OSPF?? -2. ???????? EGP??External Gateway Protocol?? ????????????????? BGP?? +1. ڲЭ IGPInterior Gateway Protocol ϵͳڲʹã RIP OSPF +2. ⲿЭ EGPExternal Gateway Protocol ϵͳ֮ʹã BGP ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e0be6970-5b0e-44a2-bc71-df4d61c42b8f.jpg) -### 1. ????????? RIP +### 1. ڲЭ RIP -RIP ??????????????????????????????????????????????????????????? 1?????????? 15?????? 15 ???????? +RIP һֲַʽĻھ·ѡЭ顣ֱָ·Ϊ 1Ϊ 15 15 ʾɴ -RIP ????????????????????????????????????????????????????????????????????????????????????????????????????????????? +RIP ̶ʱ·Լ·ɱɴν֮·ջ֪ﱾϵͳκһ̾һ·ַ -???????????? +㷨 -1. ????? X ???????????????? RIP ????????????????????????????????????????? X???????????????? 1?? -2. ??????? RIP ?????????????????????????s - - ?????????????????????? N?????????????????? - - ???????????????????? X??????????????I???????????????????????????????? d ?????????????????????????????? Net2, 5, P??????? Net2, 4, X??????????????????????? -3. ?? 3 ????????????????????????????????????????????????????????? 16?? +1. ԵַΪ X · RIP ģ޸ıеĿһֶеĵַΪ Xеľֶμ 1 +2. ޸ĺ RIP еÿһĿ²裺 + - ԭ·ɱûĿ NѸĿӵ·ɱУ + - һ·ַ XյĿ滻ԭ·ɱеĿյĿеľ d С·ɱеľ룬и£ԭʼ·ɱΪ Net2, 5, P±Ϊ Net2, 4, X£ʲôҲ +3. 3 ӻûյ·ĸ·ɱѸ·ΪɴѾΪ 16 -RIP ????????????????? RIP ????????????? 15????????????????????????????????????????????????????????????????????????? +RIP Эʵּ򵥣С RIP ʹõΪ 15ĹģҵֹʱҪȽϳʱܽϢ͵· -### 2. ????????? OSPF +### 2. ڲЭ OSPF -????????????? OSPF????????? RIP ????????????????? +· OSPFΪ˿˷ RIP ȱġ -?????? OSPF ??????????????????????????????????????????????? Dijkstra ???????????? SPF?? +űʾ OSPF ijһҳ̿ƣǹģ·Ϊʹ Dijkstra ·㷨 SPF -OSPF ??????????? +OSPF ص㣺 -1. ???????????????????????????????????????? -2. ?????????????????????????????????????????????????????????????????????????????????????????? -3. ????????????????????????????? +1. ϵͳе·ϢַǺ鷺 +2. ͵Ϣ··״̬·״̬Щ·Լ·Ķ÷á롢ʱӡʾ +3. ֻе·״̬仯ʱ·ŻᷢϢ -??????????????????????????????????????????? RIP??OSPF ???????????????? +·ȫ˽ṹͼһµġ RIPOSPF ĸ¹ĺܿ졣 -### 3. ???????? BGP +### 3. ⲿЭ BGP -AS ????????????????????????????????????? AS ??????????????????????????????????????? AS ??????????????????????????? AS ??????????? AS ?????? +AS ֮·ѡѣҪǻģܴ󡣲Ҹ AS ڲʹòͬ·ѡЭ飬޷׼ȷ·Ķ AS ֮·ѡ뿼йصIJԣЩ AS Ը AS -BGP ?????????????????????????????????????????????? +BGP ֻѰһȽϺõ·ɣ·ɡ··ѡЭ顣 -??? AS ?????????? BGP ???????????????????? BGP ?????????? TCP ????????????????? +ÿ AS BGP ˣͨ BGP ֮佨 TCP ·Ϣ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/eb6271de-22c9-4f4b-8b31-eab1f560efac.png) -## ???????????? ICMP +## ʿƱЭ ICMP -ICMP ????????????? IP ???????????????????????????? IP ??????????????????? +ICMP Ϊ˸Чת IP ݱ߽ɹĻᡣװ IP ݱУDzڸ߲Э顣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9b5e0fa0-9274-4219-a3a9-84fbb509c735.jpg) -ICMP ???????????????????? +ICMP ķΪ汨ĺѯʱġ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6e11b122-95ce-4869-bf7d-3b0d7591707e.jpg) -## ??????????? PING +## ̽ PING -PING ?? ICMP ???????????????????????????????????????? +PING ICMP һҪӦãҪ̨֮ͨԡ -PING ?????? +PING Ḷ́ -1. PING ??????????????????????????? MAC ??????????????????????????? MAC ??????????????? ARP ???? -2. PING ???????????????????????????????????????????????????????????????? MAC ????????? MAC ???????????? +1. PING ͬһεĿ MAC ַȻֱӽ޷ҵ MAC ַҪһ ARP +2. PING ͬεͷ͸תͬҪ͸ҲҪͨص MAC ַ MAC ַת -## IP ?? +## IP ಥ -????????????????????????`?????????????????????? +һԶͨУಥҪ鸴ƶݣӶԼԴ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c77b6a18-dfac-42a2-ac89-7e99481275dc.jpg) -## ????????? VPN +## ר VPN -???? IP ???????????????????????? IP ??????????????????????????????????????????????????????????????????????????????????????????????????????? IP ?????????????? + IP ַĽȱһ뵽 IP ַԶСڱӵеһҪе뵽ⲿĻУڵļʹýڱЧ IP ַרõַ -???????????? +רõַ飺 1. 10.0.0.0 \~ 10.255.255.255 2. 172.16.0.0 \~ 172.31.255.255 3. 192.168.0.0 \~ 192.168.255.255 -VPN ?????????????????????????????????????^???????????????????????????????????????????????????????????????????????????????????? +VPN ʹùõĻΪר֮ͨ塣רָڵֻ뱾ڵͨţָǡʵϲǣоõĻ -????????? A ?? B ????????????????????????? A ?????? X ???????????? B ?????? Y ????IP ????????????? 10.1.0.1????????? 10.2.0.3????????????????????????????? R1??R1 ??????????????????????????????????????????????? R1 ??????? 125.1.2.3????????????? R2 ??????? 194.4.5.6?????? R2 ??????????????????????????????????????????????? 10.2.0.3????????? Y?? +ͼУ A B ͨŲ A X Ҫһ B Y ͨţIP ݱԴַ 10.1.0.1Ŀĵַ 10.2.0.3ݱȷ͵뻥· R1R1 ڲݽмܣȻ¼ݱײԴַ· R1 ȫַ 125.1.2.3Ŀĵַ· R2 ȫַ 194.4.5.6· R2 յݱݲֽнܣָԭݱʱĿĵַΪ 10.2.0.3ͽ Y ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/bf4ed077-d481-4db7-9e7a-85d841a5a8c3.jpg) -## ????????? NAT +## ַת NAT -???????????????????? IP ????????????????????????????????? NAT ???????? IP ??????? IP?? +רڲʹñ IP ַͻϵͨʱʹ NAT IP תΪȫ IP -???????NAT ?????? IP ????? IP ?????????????????? n ????? IP ???????????????????????? n ?????????????????????????????? IP ????????????? NAT ???????????????????????????????????????????????????????????? IP ???????????? NAT ?????????????????? NAPT?? +ǰNAT IP ȫ IP һһӦַʽӵ n ȫ IP ַרֻͬʱ n ̨뻥Ϊ˸Чȫ IP ַڳõ NAT תĶ˿ںҲˣʹöרڲһȫ IP ַʹö˿ںŵ NAT Ҳַ˿ת NAPT ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/0f31bc7a-d60b-48a6-8e3f-597708369e52.png) -# ?????? ?????* +# * -????????????????????????????????????????????????????????? +ֻѷ鷢͵ĿͨŵIJеĽ̡ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ṩӦý̼߼ͨš߲ûĺϸڣʹӦó򿴼ĺʵ֮һ˵˵߼ͨŵ -## UDP ?? TCP ????? +## UDP TCP ص -?????????? UDP??User Datagram Protocol?? -????????? TCP??Transmission Control Protocol?? +ûݰЭ UDPUser Datagram Protocol +Э TCPTransmission Control Protocol -UDP ???????????????????????????????????????????????????????????????????????????? UDP ??????? +UDP ӵģܽûӵƣģӦóıIJϲҲֻ֣ UDP ײ -TCP ????????????????????????????????????????????????????????????????????????????????????????????????????????????? +TCP ӵģṩɿƣӵƣṩȫ˫ͨţֽӦò㴫ıĿֽֽ֯ɴСȵݿ飩 -## UDP ?????? +## UDP ײʽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/bd6c05f3-02ee-4c8a-b374-40c87154a898.jpg) -????????? 8 ????????????????????????????????12 ???????????????????????????? +ײֶֻ 8 ֽڣԴ˿ڡĿĶ˿ڡȡ͡12 ֽڵαײΪ˼Ͷʱӵġ -## TCP ?????? +## TCP ײʽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/21a00b02-c0a6-4bcd-9af0-5ec6bb66e34c.jpg) -**???** ?????????????????????????? 301????????????????? 301????????????????? 100 ?????????????????????? 401?? +**** ڶֽбţΪ 301ʾһֽڵıΪ 301ЯݳΪ 100 ֽڣôһĶεӦΪ 401 -**????** ????????????????????????????? B ?????? A ???????????????????? 501?????????????? 200 ??????? B ?????????????????? 701??B ????? A ????????????????? 701?? +**ȷϺ** յһĶεš B ȷյ A һĶΣΪ 501ЯݳΪ 200 ֽڣ B һĶεΪ 701B ͸ A ȷϱĶȷϺžΪ 701 -**???????** ????????????????????????????????????????????????????? +**ƫ** ָݲ־뱨Ķʼƫʵָײijȡ -**??? ACK** ???? ACK=1 ????????????????????TCP ?????????????????????????????? ACK ?? 1?? +**ȷ ACK** ACK=1 ʱȷϺֶЧЧTCP 涨ӽд͵ıĶζ ACK 1 -**??? SYN** ????????????????????????? SYN=1??ACK=0 ???????????????????????????????????????????????? SYN=1??ACK=1?? +**ͬ SYN** ӽʱͬš SYN=1ACK=0 ʱʾһĶΡԷͬ⽨ӣӦ SYN=1ACK=1 -**??? FIN** ??????????????????? FIN=1 ???????????????????????????????????????????????? +**ֹ FIN** ͷһӣ FIN=1 ʱʾ˱ĶεķͷѷϣҪͷӡ -**????** ?????????????????????????????????????????????????????????????????????????????????? +**** ֵΪշ÷ͷ䷢ʹڵݡ֮ҪƣΪշݻռ޵ġ -## TCP ?????????? +## TCP ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/086871db-5871-460f-97b7-126cd738bb0e.jpg) -???? A ???????B ?????????? + A ΪͻˣB Ϊˡ -1. ???? B ???? LISTEN???????????????????????????? -2. A ?? B ???????????????SYN=1??ACK=0???????????????? x?? -3. B ??????????????????????????????? A ???????????????SYN=1??ACK=1??????? x+1??????????????????? y?? -4. A ??? B ????????????????? B ????????????? y+1?????? x+1?? -5. B ??? A ??????????????? +1. B LISTEN״̬ȴͻ +2. A B ĶΣSYN=1ACK=0ѡһʼ x +3. B յĶΣͬ⽨ӣ A ȷϱĶΣSYN=1ACK=1ȷϺΪ x+1ͬʱҲѡһʼ y +4. A յ B ȷϱĶκ󣬻Ҫ B ȷϣȷϺΪ y+1Ϊ x+1 +5. B յ A ȷϺӽ -## TCP ??????? +## TCP Ĵλ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/78f65456-666b-4044-b4ee-f7692dbbc0d3.jpg) -??????????????????????????????????????????????????? ACK????? ACK ????????????? 1?? +źȷϺţΪźȷϺŵĹȽϼ򵥡Ҳ ACKΪ ACK ӽ֮Ϊ 1 -1. A ???????????????FIN=1?? -2. B ?????????????? TCP ???????????B ???? A ??????????? A ?????? B ????????? -3. ?? B ?????????????????????????????????FIN=1?? -4. A ????????????????????? +1. A ͷűĶΣFIN=1 +2. B յ֮󷢳ȷϣʱ TCP ڰر״̬B A ݵ A B ݣ +3. B ҪҪʱͷĶΣFIN=1 +4. A յ󷢳ȷϣʱͷš **TIME_WAIT** -?????????????????? FIN ???????????????????????????? CLOSED ?????????????????????????????????????????????? +ͻ˽յ˵ FIN ĺ״̬ʱֱӽ CLOSED ״̬Ҫȴһʱʱõʱ䡣ôɣ -1. ?????????????????????????? B ???? A ?????????????????????????????????????????A ???????????????????????????????? -2. ???????????????????????????????????????????????????????????????????? +1. ȷһȷϱĶܹ B ûյ A ȷϱĶΣôͻ·ͷĶΣA ȴһʱΪ˴ķ +2. ӹ̿ܡʧЧĶΡΪ˷ֱֹĶγڱ֮⣬Ҫȴһʱ䡣 -## TCP ???????? +## TCP ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/223fc26e-2fd6-484c-bcb7-443cac134f15.jpg) -???????????????????????????????????????????????????????????????? TCP ??????????????????????????????????????????????????????????????????? +ǻһ֣ʱֽͷͽշһڣշͨ TCP ĶеĴֶθ߷ͷԼĴڴСͷֵϢԼĴڴС -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʹڵֽڶͣմڵֽڶաʹ󲿵ֽѾͲյȷϣôͽʹһһ룬ֱ󲿵һֽڲѷͲȷϵ״̬մڵĻƣմֽѾȷϲһմڡ -???????????????????????????????????????????????????????????? {31, 32, 34, 35}?????? {31, 32} ??????? {34, 35} ??????????????? 32 ????????????????????????????????????????????????????????????? +մֻԴһ򵽴ֽڽȷϣմѾյֽΪ {31, 32, 34, 35} {31, 32} 򵽴 {34, 35} Ͳǣֻֽ 32 ȷϡͷõһֽڵȷ֮󣬾ֽ֪֮ǰֽڶѾա -## TCP ??????? +## TCP ɿ -TCP ??????????????????????????????????????????????????????????????????????? +TCP ʹóʱشʵֿɿ䣺һѾ͵ıĶڳʱʱûյȷϣôشĶΡ -???????????????????????????????????????? RTT??????????????? RTTs ????????? +һĶδӷ͵յȷʱΪʱ RTTȨƽʱ RTTs £ ![](http://latex.codecogs.com/gif.latex?\\\\RTTs=(1-a)*(RTTs)+a*RTT) -??????????????? RTO ???????? RRTs??TCP ????????????????? +֪ʱʱ RTO ӦԴ RRTsTCP ʹõijʱʱ£ ![](http://latex.codecogs.com/gif.latex?\\\\RTO=RTTs+4*RTT_d) -???? RTTd ??????????? RRT ?? RRTs ??? + RTTd Ϊƫµ RRT RRTs йء -## TCP ???????? +## TCP -?????????????????????????????????????????????? +Ϊ˿Ʒͷʣ֤շüա -???????????????????????????????????????????????????????????????????L???????????? 0??????????????????? +շ͵ȷϱеĴֶοƷͷڴСӶӰ췢ͷķʡ罫ֶΪ 0ͷܷݡ -## TCP ??????? +## TCP ӵ -??????????????????q??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ӵ齫ᶪʧʱͷشӶӵ̶ȸߡ˵ӵʱӦƷͷʡһƺ񣬵dz㲻ͬΪýշüܣӵΪ˽ӵ̶ȡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a69af9bb-b5ad-4896-862d-697e5ee4feb1.png) -TCP ?????????????????????????????????????????????????????????????????????????????????cwnd???????????????????????????????????????????????????????????????????????????????????????????? +TCP Ҫͨ㷨ӵƣʼӵ⡢شָͷҪάһӵڣcwnd״̬עӵ뷢ͷڵӵֻһ״̬ʵʾͷܷͶݵǷͷڡ -???????????????????s +Ϊ˱ۣ¼裺 -1. ?????????????????????????????????? -2. ??? TCP ????????????????????????????????? +1. շ㹻Ľջ棬˲ᷢƣ +2. Ȼ TCP Ĵڻֽڣ贰ڵĴСλΪĶΡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/346244ff-98c1-4f12-9a87-d0832e8c04cf.jpg) -### ?????????????? +### ʼӵ -???????????????????? cwnd=1????????????? 1 ????????????????? cwnd ?????????????????????????????2??4??8 ... +͵ִʼ cwnd=1ͷֻܷ 1 ĶΣյȷϺ󣬽 cwnd ӱ֮ͷܹ͵ıĶΪ248 ... -???????????????? cwnd ????????????? cwnd ????????????????????????????????????????????????????????????????????????? ssthresh???? cwnd >= ssthresh ??????????????????????? cwnd ?? 1?? +ע⵽ʼÿִζ cwnd ӱ cwnd ٶȷdz죬Ӷʹ÷ͷ͵ٶٶȹ죬ӵĿҲ͸ߡһʼ ssthresh cwnd >= ssthresh ʱӵ⣬ÿִֻ cwnd 1 -????????????????? ssthresh = cwnd / 2??????????????????? +˳ʱ ssthresh = cwnd / 2Ȼִʼ -### ?????????? +### شָ -???????????????????????????????????????????????????????? M1 ?? M2???????? M4?????????? M2 ?????? +ڽշҪÿνյĶζӦ÷ͶյĶεȷϣѾյ M1 M2ʱյ M4ӦͶ M2 ȷϡ -??????????????????????????????????????????????????????????? M2 ???? M3 ????????????????????????????????? +ڷͷյظȷϣôȷһĶζʧյ M2 M3 ʧʱִпششһĶΡ -??????????????????????????????????????????????????? ssthresh = cwnd / 2 ??cwnd = ssthresh????????????????????? +£ֻǶʧĶΣӵִпָ ssthresh = cwnd / 2 cwnd = ssthreshע⵽ʱֱӽӵ⡣ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b18d679b-c8e2-4564-88ee-7600090e46da.jpg) -# ?????? ???* +# Ӧò* -## ?????? DNS +## ϵͳ DNS -????????????? IP ????? +Ϊ IP ַ -????????????? +Ƴɷֲʽϵͳ -### 1. ??? +### 1. νṹ -?????????????????????????????????????????????????????????????????????????????????????????????? +һɶιɣϲ㵽²ֱΪԼļԻһ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c2117f61-1177-4768-bf33-cf4f950d911c.png) ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a4b162e5-db2a-4a27-b213-1fe481c5a06a.png) -???????????????????????? +ԷΪࣺ -**(1) ????????????**???????????????? +**(1) ** -**(2) ??????????????**???????????????? +**(2) ** -**(3) ?????????????**????????????????? +**(3) Ȩ**ڵ -??????????????????????????????????? b ???? abc.com ??????????????abc.com ?? y.abc.com +ĸͬһлֶͼ b abc.com лabc.com y.abc.com ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/fc0c6b2d-68c7-4de8-aaaa-97355a4f0472.jpg) -?????????????????????????? +˾ҪȨ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8b335d94-c1ca-42e1-ad48-bb179d28a4f1.jpg) -**(4) ??????????????**???????????????????????????????????????? +**(4) **ҲΪĬøٻ档 -### 2. ???????? +### 2. -??????????????????????????????????????????????????????????????????????????????????????? +򱾵Ĺ̲õݹ飬ʹõݹ͵ַʽ -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ķʽ£һ֮󣬽صȻ󱾵ݹطʽ£ֱӷصģǼǰĽŻ᷵ء ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6bc61bb8-3b1c-4dc8-ac25-cef925ace0eb.jpg) -## ????????? FTP +## ļЭ FTP -FTP ?????????? TCP????????????????????? TCP ????????????????????????????????????????????????????????????????????????????????????????????? 21???????????????? 20?? +FTP ʹ TCPҪе TCP ӣӺӡỰڼһֱִ򿪣ݴ֮͹رաʹö˿ں 21ʹö˿ں 20 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/58633775-8584-4a01-ad3f-eee4d9a466e1.jpg) -## ???????? TELNET +## ԶնЭ TELNET -TELNET ????????????????????????????????????????? +TELNET ڵ¼ԶϣԶϵҲ᷵ء -TELNET ??????????????????????????????????????????????^ +TELNET ӦͲϵͳIJ죬粻ͬϵͳϵͳĻз塣 -## ????? WWW +## ά WWW -?? HTTP ???? + HTTP ʼǡ -## ????????? +## ʼЭ -???????????????????????????????????????????????????????????????@?? SMTP??????@?? POP3 ?? IMAP?? +һʼϵͳɣûʼԼʼЭͶȡЭ顣зЭ鳣 SMTPȡЭ鳣 POP3 IMAP ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/de1e46d2-748f-4da3-a29e-7de7bc840366.jpg) ### POP3 -POP3 ?????????????????????????????????????????? +POP3 صֻҪûӷ϶ȡʼͰѸʼɾ ### IMAP -IMAP ??????????????????????????????????????????????????????????????????????IMAP ?????????????????????????????????????????IMAP ???????????????????? +IMAP Эпͻ˺ͷϵʼͬȥֶɾʼôϵʼҲᱻɾIMAP ûʱȥʷϵʼIMAP ЭҲִ֧ԶļС ### SMTP -SMTP ?????? ASCII ????????????????? MIME ?????????????????MIME ????????????? SMTP???????????????????????????? ASCII ????????? +SMTP ֻܷ ASCII 룬ʼ MIME ԷͶļMIME ûиĶȡ SMTPʼĽṹ˷ ASCII ı ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ed5522bb-3a60-481c-8654-43e7195a48fe.png) -## ????????????? DHCP +## ̬Э DHCP -DHCP ???????????????????????????????????? IP ?????????? +DHCP ṩ˼弴õʽûҪȥֶ IP ַϢ -DHCP ?????????????? IP ?????????????????????????? IP ????????????????? IP ????? +DHCP õݲ IP ַ롢Ĭ· IP ַ IP ַ -??????????????? IP ??????????????? DHCP ??????????????????? 1???? 255.255.255.255:67???????????? 0???? 0.0.0.0:68????DHCP ??????????????????????? IP ???????????????????? DHCP ??????????????? +ʽ£Ҫ IP ַ㲥 DHCP ֱģĿĵַΪȫ 1 255.255.255.255:67ԴַΪȫ 0 0.0.0.0:68DHCP յֱ֮ IP ַȡһַ DHCP ṩĸ -## ?????? P2P +## Ե㴫 P2P -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ijļַжԵȼϳΪһļݵԪΪļ飬ĴСǹ̶ġһµĶԵȷijһʼûļ飬ܹԵȷ𽥵صһЩļ飬ͬʱҲΪĶԵȷϴһЩļ顣 -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ÿһʩΪ׷һԵȷʱ׷ǼǣԵ֪ͨ׷ںСκʱ˳ij -????????????????????????????????????????????????????????????????????????????????????????????????????????????Y??????????????? +һµĶԵȷʱ׷ӺѡɸԵȷ¶ԵȷЩԵȷӣЩԵȷΪڶԵȷպͷļ鶼ڶԵȷнС -?????????????????????????????????????????????????????????????????????????????????????????????????????? +һԵȷҪܶļʱͨʹϡȵIJȡļ飬ҲһļڶԵȷи٣ôļ顣 -???????????????????????????????????????????????????????????????????? +ܶԵȷͬһԵȷļʱöԵȷѡ䷢ļĶԵȷ -P2P ?????????????????????????????????????????? DHT??????????????????? IP ?????? +P2P һֲʽϵͳκʱжԵȷ˳ʹ÷ֲʽɢб DHTԲҺеԴ IP ַӳ䡣 -## Web ?????????? +## Web ҳ -1. ?? DNS ?????????? DNS ??????????????????? +1. DNS DNS ѯ -2. ??????? HTTP ???????????? TCP ????? +2. ʼ HTTP ỰҪȽ TCP ӡ -3. ????????????????HTTP ?????????? TCP ??HTTP ???????????? 80??????????????????? 80 ?????????????????????????????????????????????????? TCP ???????????????????? +3. ĴУHTTP ıװ TCP СHTTP ʹö˿ں 80Ϊ 80 ˿ڡӽ֮󣬷һ˿ںŸضĿͻˣ֮ TCP 䶼Ķ˿ںš -4. ????????????????TCP ?????????? IP ??????IP ???????????????????? +4. ĴУTCP Ķλᱻװ IP УIP 龭·ѡ󵽴Ŀĵء -5. ??????IP ?????????? MAC ???IP ????????? MAC ????????? ARP?? +5. ·㣬IP ᱻװ MAC ֡УIP ַ MAC ַҪʹ ARP -6. ???????? HTTP ??????????????? +6. ͻ˷ HTTP ģȡҳ档 -7. ?????????? HTTP ?????????????????????? +7. HTTP Ӧģͻ˴Ӷȡҳ档 -8. ????????????????????????????????????? +8. õҳ֮󣬽Ⱦûչʾҳ档 -## ?????? +## ö˿ -| ????? | ???? | ??????? | +| ӦòЭ | ˿ں | Э | | -- | -- | -- | | DNS | 53 | UDP | -| FTP | ???????? 21?????????? 20 | TCP | +| FTP | 21 20 | TCP | | TELNET | 23 | TCP | | DHCP | 67 68 | UDP | | HTTP | 80 | TCP | @@ -842,7 +842,7 @@ P2P ?????????????? | POP3 | 110 | TCP | | IMAP | 143 | TCP | -# ?????? +# ο -- ????????? ????? -- ??????????????? +- ߰ +- Զ¼ diff --git a/notes/设计模式.md b/notes/设计模式.md index 27aae434..c4e44538 100644 --- a/notes/设计模式.md +++ b/notes/设计模式.md @@ -1,74 +1,74 @@ -* [?? 1 ?? ?????????](#??-1-??-?????????) -* [?? 2 ?? ???????](#??-2-??-???????) -* [?? 3 ?? ?????](#??-3-??-?????) -* [?? 4 ?? ??????](#??-4-??-??????) - * [4.1 ?????](#41-?????) - * [4.2 ??????????](#42-??????????) - * [4.3 ???????](#43-???????) -* [?? 5 ?? ??????](#??-5-??-??????) -* [?? 6 ?? ??????](#??-6-??-??????) -* [?? 7 ?? ???????????????](#??-7-??-???????????????) - * [7.1 ????????](#71-????????) - * [7.2 ?????](#72-?????) -* [?? 8 ?? ??????](#??-8-??-??????) -* [?? 9 ?? ?????????????](#??-9-??-?????????????) - * [9.1 ????????](#91-????????) - * [9.2 Java ??????????](#92-java-??????????) - * [9.3 ?????](#93-?????) -* [?? 10 ?? ????](#??-10-??-????) -* [?? 11 ?? ?????? // TODO](#??-11-??-??????--todo) -* [?? 12 ?? ??????](#??-12-??-??????) +* [ 1 ģʽ](#-1--ģʽ) +* [ 2 ۲ģʽ](#-2--۲ģʽ) +* [ 3 װģʽ](#-3--װģʽ) +* [ 4 ģʽ](#-4--ģʽ) + * [4.1 򵥹](#41-򵥹) + * [4.2 ģʽ](#42-ģʽ) + * [4.3 󹤳ģʽ](#43-󹤳ģʽ) +* [ 5 ģʽ](#-5--ģʽ) +* [ 6 ģʽ](#-6--ģʽ) +* [ 7 ģʽģʽ](#-7--ģʽģʽ) + * [7.1 ģʽ](#71-ģʽ) + * [7.2 ģʽ](#72-ģʽ) +* [ 8 ģ巽ģʽ](#-8--ģ巽ģʽ) +* [ 9 ģʽ](#-9--ģʽ) + * [9.1 ģʽ](#91-ģʽ) + * [9.2 Java õĵ](#92-java-õĵ) + * [9.3 ģʽ](#93-ģʽ) +* [ 10 ״̬ģʽ](#-10--״̬ģʽ) +* [ 11 ģʽ // TODO](#-11--ģʽ--todo) +* [ 12 ģʽ](#-12--ģʽ) * [12.1 MVC](#121-mvc) - * [12.1.1 ??? MVC](#1211-???-mvc) - * [12.1.2 Web ?? MVC](#1212-web-??-mvc) -* [?? 13 ?? ?????????](#??-13-??-?????????) -* [?? 14 ?? ?????? // TODO](#??-14-??-??????--todo) + * [12.1.1 ͳ MVC](#1211-ͳ-mvc) + * [12.1.2 Web е MVC](#1212-web-е-mvc) +* [ 13 ģʽദ](#-13--ģʽദ) +* [ 14 ʣµģʽ // TODO](#-14--ʣµģʽ--todo) -# ?? 1 ?? ????????? +# 1 ģʽ -**1. ?????????** +**1. ģʽ** -??????????????????????????????????????????????????`?? +ģʽǴ룬ǽķѧϰеģʽ鸴á -???????????????????????????????????????????????????? +ӵģʽʻ㣬ڹͨʱøٵĴʻۣҲҪ˽ײϸڡ -**2. ????????** +**2. ** -?????????????????????????????? +ƲͬѼӵвͬĽͷзʽ -**3. ????????** +**3. ʵַ** -???????????????????????????????????????????????????????????????????????????? +ʹü̳еĽ£ַ޷ãѼӵͬķзʽظĴ롣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/144d28a0-1dc5-4aba-8961-ced5bc88428a.jpg) -**4. ??????** +**4. ԭ** -**????**????????????????????????? +**װ仯**仯ѼӽкͷеΪʽ -**??????????????????????** ????????????????????????????????????????????????????????????????????????????????????????????????????????? +**Խӿڱ̣ʵֱ** Ϊ࣬Ǿijࡣеķʵֲڸ࣬ڸࡣʱԶ̬ıָ͡ -??????????????????????????????????????????????????????????????????? +һԭ򣬽кͷеΪʵֲֶͬĽкͷе࣬ȥʵ־Ľкͷзʽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1c8ccf5c-7ecd-4b8a-b160-3f72a510ce26.png) -**?????????????** ???????? has-a ???????????????????????????????????????????????????????????????????????????????????????????????????? +**ϣü̳** Ҳ has-a ϵͨϣʱ̬ıʵֻ֣Ҫͨı丸ָĸ༴ɡ̳оͲЩ̳ϵڴʱѾȷ -???????????? Duck ??????? FlyBehavior ?? QuackBehavior ??performQuack() ?? performFly() ?????????????????????????????????? Duck ??????????????????? FlyBehavior ?? QuackBehavior ????????????????????????? +һԭ Duck FlyBehavior QuackBehavior ࣬performQuack() performFly() ίиȥַͨʽһ Duck ԸҪȥʵ FlyBehavior QuackBehavior 󣬲ҲԶ̬ؽиı䡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/29574e6f-295c-444e-83c7-b162e8a73a83.jpg) -**5. ????????** +**5. ͼ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e13833c8-e215-462e-855c-1d362bb8d4a0.jpg) -**6. ??????** +**6. ģʽ** -**??????** ??????????????????????????????????????I????????????????????????????? +**ģʽ** 㷨壬ֱװ֮Ի滻ģʽ㷨ı仯ʹ㷨Ŀͻ -**7. ??????** +**7. ʵִ** ```java public abstract class Duck { @@ -164,42 +164,42 @@ public class MiniDuckSimulator { } } ``` -???? +ִн ```html QuackBehavior.Quack FlyBehavior.FlyWithWings FlyBehavior.FlyNoWay ``` -# ?? 2 ?? ??????? +# 2 ۲ģʽ -**1. ??????** +**1. ģʽ** -???????????????????????????????????????????????????????????????????????????Subject??????????????????????????Observer??????????? +˶֮һԶһı״̬ʱ߶֪ܵͨԶ¡⣨SubjectDZ۲Ķ󣬶ߣObserverΪ۲ߡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/26cb5e7e-6fa3-44ad-854e-fe24d1a5278c.jpg) -**2. ?????** +**2. ģʽͼ** -?????????????????????????????????????????????????????????????????????????? +оעƳ۲ߣ֪ͨעߵĹܣͨάһŹ۲бʵЩġ -??????????????????????????????????????????????????????????????????????????????? +۲ӵһãΪעᡢƳݶ⵱УͨӦܡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5c558190-fccd-4b5e-98ed-1896653fc97f.jpg) -**3. ????????** +**3. ** -?????????????????????????????????????????????????????????????????????? +ݲϢıʱݣжڽӡ -**4. ??????????** +**4. ͼ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/760a5d63-d96d-4dd9-bf9a-c3d126b2f401.jpg) -**5. ??????** +**5. ԭ** -**?????????????????????????** ???????????????????????????????????????????????????????????????????????????????????????????????????????? +**Ϊ֮ƶŬ** ֮ϣȻԽDz̫˴˵ϸڡϵ֮以̶ȺܵͣϵͳеԣܹӦԱ仯 -**6. ??????** +**6. ʵִ** ```java public interface Subject { @@ -297,7 +297,7 @@ public class WeatherStation { } } ``` -???? +ִн ```html CurrentConditionsDisplay.update:0.0 0.0 0.0 StatisticsDisplay.update:0.0 0.0 0.0 @@ -305,39 +305,39 @@ CurrentConditionsDisplay.update:1.0 1.0 1.0 StatisticsDisplay.update:1.0 1.0 1.0 ``` -# ?? 3 ?? ????? +# 3 װģʽ -**1. ????????** +**1. ** -????????????????????????????????????????????????????????????????????? +ƲͬϣÿϿԶ̬µIJϣţ̡һϵļ۸ -**2. ??????** +**2. ģʽ** -?????????????????????????????????????????????????????????? +̬ؽθӵϡչϣװṩ˱ȼ̳иеԵ -????? DarkRoast ???? Mocha ??????Mocha ??????? Whip ??????????????????????????????? cost() ????????????????? cost() ??????????????????? cost() ???????????????? DarkRoast ????? Mocha??????????? Mocha ???? DarkRoast?????????? Whip ?????? Whip ???? Mocha???????? cost() ????????????????????????? +ͼ DarkRoast Mocha Mocha ֱ Whip Ƕ̳ͬ࣬ cost() cost() ʵֵڲ cost() ˣҪ DarkRoast MochaôֻҪ Mocha DarkRoastҪ Whip Whip Mocha cost() ֶܰļ۸񶼰ȥ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/41a4cb30-f393-4b3b-abe4-9941ccf8fa1f.jpg) -**3. ?????** +**3. ģʽͼ** -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +װߺ;̳ͣоķʵֲҪ󣬶װӵһͶװװ߻߾νװΣǰװڱװεĶ֮⣬Ӷ̬չװߵĹܡװߵķһԼģĹܣȻñװߵķʵ֣ӶҲ˱װߵĹܡԿӦװβεͲ㣬ΪֻоֱʵֶҪίиȥ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/3dc454fb-efd4-4eb8-afde-785b2182caeb.jpg) -**4. ???????????????** +**4. ͼ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9c997ac5-c8a7-44fe-bf45-2c10eb773e53.jpg) -**5. ??????** +**5. ԭ** -**??????????????????????**??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +**Ӧöչţ޸Ĺرա**Ҳ¹ʱҪ޸Ĵ롣ڱиԭڣµIJϣҪȥ޸ϵĴ롣۲ģʽҲԭ򡣲඼ʵԭӦѸԭӦпܸıĵط -**6. Java I/O ?????????** +**6. Java I/O еװģʽ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2a40042a-03c8-4556-ad1f-72d89f8c555c.jpg) -**7. ???????** +**7. ʵ** ```java public interface Beverage { @@ -402,32 +402,32 @@ public class StartbuzzCoffee { } ``` -??? + ```html 3.0 ``` -# ?? 4 ?? ?????? +# 4 ģʽ -## 4.1 ????? +## 4.1 򵥹 -**1. ????????** +**1. ** -????? Pizza?????????????????????????????? Pizza ???? +вͬ Pizzaݲͬòͬʵһ Pizza -**2. ????** +**2. ** -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +򵥹ģʽһֱϰߡʵһĶʱʵҪݾʹĸࡣ£ʵIJŵУùӦĸʵѿͻ;ʵֽͻҪ֪ЩԼʵĸࡣΪͻжʹü򵥹ôеĿͻ඼Ҫ֪ϸڣһ෢ı䣬࣬ôеĿͻ඼Ҫı䡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c470eb9b-fb05-45c5-8bb7-1057dc3c16de.jpg) -**3. ??????????** +**3. ͼ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/dc3e704c-7c57-42b8-93ea-ddd068665964.jpg) -**4. ???????** +**4. ʵ** ```java public interface Pizza { @@ -473,33 +473,33 @@ public class PizzaStore { } ``` -????? +н ```java CheesePizza ``` -## 4.2 ?????????? +## 4.2 ģʽ -**1. ????????** +**1. ** -????????? Pizza ?????????????????????????????????????????????????????????? cheese ????? Pizza ?????????????????? Pizza ??????? +ÿ Pizza ȻͬǶԼķζҪ֡磬һͻŦԼ cheese Pizza ֥Ӹͬ Pizza Dzͬġ -**2. ??????** +**2. ģʽ** -????????????????????????????????????????????????????????????????????????????? +һĽӿڣҪʵһʵƳٵࡣ -**3. ?????** +**3. ģʽͼ** -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ڼ򵥹Уһ࣬ڹУ󡣹ÿ඼ԼһƷࡣΪÿഴļ򵥹ǰѼ򵥹дĴŵԼĿΪ಻DzƷ࣬ȫô ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/903093ec-acc8-4f9b-bf2c-b990b9a5390c.jpg) -**4. ??????????** +**4. ͼ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/664f8901-5dc7-4644-a072-dad88cc5133a.jpg) -**5. ???????** +**5. ʵ** ```java public interface Pizza { @@ -588,36 +588,36 @@ public class PizzaTestDrive { } ``` -????? +н ```html NYStyleCheesePizza is making.. ChicagoStyleCheesePizza is making.. ``` -## 4.3 ??????? +## 4.3 󹤳ģʽ -**1. ??????** +**1. ԭ** -**???????????**??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? PizzaStore ???????????????????????????? Pizza ?????????????????? Pizza ???????????????????? Pizza ???????????? +**ԭ**Ҫ󣬲ҪࡣԽӿڱ̣ʵֱ̣ԭ˵ˣø߲ײңܸ߲ײ߶Ӧڳ磬ͼ PizzaStore ڸ߲ڵײ Pizza ľࡣ Pizza ij࣬ôͿԲù Pizza ľʵϸڡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ddf72ca9-c0be-49d7-ab81-57a99a974c8e.jpg) -**2. ??????** +**2. ģʽ** -?????????????????????????????????????????????????? +ṩһӿڣڴػļ壬Ҫȷָࡣ -**3. ?????** +**3. ģʽͼ** -??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????AbstractFactory ?? CreateProductA ?? CreateProductB ????????????????????????????????????????????????????????????????????^?????????????????????????? Client ?????Client ???? AbstractFactory ?????????????????????????????????????????????????????????Client ????????????????????????????????????????????????????? Cilent ????? AbstractFactory ????????????????? +󹤳ģʽǶ壬ҲǺܶһ󣬲ЩصģҲ˵һ𴴽ģʽֻڴһͳ󹤳ģʽкܴͬң󹤳ģʽҲõ˹ģʽһͼ󲿣AbstractFactory е CreateProductA CreateProductB ʵ֣ڴһϹģʽĶ塣ڴļһ Client ֣Client Ҫͨ AbstractFactory ͬʱкܴԣClient ҪЭ񡣴Ӹ߲󹤳ʹϣ Cilent AbstractFactory ģʽʹ˼̳С ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d301774f-e0d2-41f3-95f4-bfe39859b52e.jpg) -**4. ??????????** +**4. ͼ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8785dabd-1285-4bd0-b3aa-b05cc060a24a.jpg) -**5. ???????** +**5. ʵ** ```java public interface Dough { @@ -719,28 +719,28 @@ public class NYPizzaStoreTestDrive { } ``` -????? +н ```html ThickCrustDough MarinaraSauce ``` -# ?? 5 ?? ?????? +# 5 ģʽ -**1. ??????** +**1. ģʽ** -??????????????????????????????????? +ȷһֻһʵṩһȫַʵ㡣 -**2. ?????** +**2. ģʽͼ** -???????? Java ????????????????????????????????????????????????????????????????????????????????????????????????????? +ģʽ Java ʵһ˽йһ˽о̬Լһо̬ú˽бʹͨúȡĶָΨһ˽о̬ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/59aff6c1-8bc5-48e4-9e9c-082baeb2f274.jpg) -**3. ???????** +**3. ʵ** -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? if(uniqueInstance == null) ??????????????????? uniqueInstance ?????????? +ʵУ˽о̬ӳٻʵĺôǣûõ࣬ôͲᴴ˽о̬ӶԼԴʵڶ̻߳DzȫģΪܹ߳ͬʱ if(uniqueInstance == null) ڵ飬ôͻʵ uniqueInstance ˽о̬ ```java public class Singleton { @@ -759,9 +759,9 @@ public class Singleton { } ``` -**4. ???????????????????** +**4. ̲߳ȫĽһ** -?????? getUniqueInstance() ????????????????????????????????????????????? uniqueInstance ???????????????????????????????????????????????????????????????????????? +ֻҪ getUniqueInstance() ø÷һֻһ̷߳ʣӶ˶ uniqueInstance жʵ⡣һһֻһ߳̽룬ϻһ˷ѡ ```java public static synchronized Singleton getUniqueInstance() { @@ -772,17 +772,17 @@ public class Singleton { } ``` -**5. ????????????????????** +**5. ̲߳ȫĽ** -???????????????????????????? +ӳʵֱʵ ```java private static Singleton uniqueInstance = new Singleton(); ``` -**6. ????????????????????** +**6. ̲߳ȫĽ** -????????????????????????? getUniqueInstance() ????????????????????????? uniqueInstance = new Singleton(); ???????????????????????????????? uniqueInstance ??????????????????????????????????? +ǵһֱӶ getUniqueInstance() мʵֻҪ uniqueInstance = new Singleton(); ɡʹж uniqueInstance ǷѾʵûʵҪ ```java public class Singleton { @@ -805,31 +805,31 @@ public class Singleton { } ``` -# ?? 6 ?? ?????? +# 6 ģʽ -**1. ????????** +**1. ** -???????????????????????????????????????????????????????????????????????????????? +һңкܶఴťÿťԷһһҵӦзdzļҵ磬ҲӼҵ硣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7b8f0d8e-a4fa-4c9d-b9a0-3e6a11cb3e33.jpg) ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c3ca36b2-8459-4cf1-98b0-cc95a0e94f20.jpg) -**2. ??????** +**2. ģʽ** -?????????????????????????????????????????? +װɶԱʹòͬ -**3. ?????** +**3. ģʽͼ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1e09d75f-6268-4425-acf8-8ecd1b4a0ef3.jpg) -**4. ??????????** +**4. ͼ** -Invoker ?????????????????????????????????????? execute() ??????Receiver ???????????????????????ConcreteCommand ?????????? Receiver ??????????????? Receiver ??????????????? LightOnCommand ????? excute ??????? Light ??????????Light ??????????? on() ??????????????Invoker ???? Client ??????????????????????? Invoker ?????????????????? Client ?????????????????? +Invoker ңҵ execute() Receiver ǵƣִߡConcreteCommand һ Receiver ִίи Receiver Ҳ LightOnCommand excute ίи Light Light ͨ on() ɲInvoker Client ΪĴ Invoker ɵģҪ Client Щ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5ef94f62-98ce-464d-a646-842d9c72c8b8.jpg) -**5. ???????** +**5. ʵ** ```java public interface Command { @@ -864,7 +864,7 @@ public class LightOnCommand implements Command{ ``` ```java /** - * ??????? + * ң */ public class SimpleRemoteControl { Command slot; @@ -895,39 +895,39 @@ public class RemoteLoader { } ``` -??? + ```html Light is on! ``` -# ?? 7 ?? ??????????????? +# 7 ģʽģʽ -## 7.1 ???????? +## 7.1 ģʽ -**1. ??????** +**1. ģʽ** -?????????????????????????????????????????????????????????????? +һĽӿڣתΪͻһӿڡԭݵԺ޼䡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8e8ba824-7a9e-4934-a212-e6a41dcc1602.jpg) -**2. ?????** +**2. ģʽͼ** -??????????????????????????????????????????????????????????????????????Adapter??????????????????Adaptee??????????????????????????????????????????Adapter ??????? Target ?? Adaptee ?????????????? Adaptee ??????????????? Adapter ????????????? Target ?????????? Client ?????????????? Target ??????????? +ģʽʵ֣һǶʽһ෽ʽʽͨϵķࣨAdapterӵһĶAdapteeӶӦĴίиĶ෽ʽõؼ̳УAdapter Կ Target Adaptee ͣȰ Adaptee Ȼʵһ Adapter ٰ Target ͵ģ Client Ϳ԰󵱳 Target Ķ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/253bd869-ea48-4092-9aed-6906ccb2f3b0.jpg) ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a797959a-0ed5-475b-8d97-df157c672019.jpg) -**3. ????????** +**3. ** -??????Duck????????Turkey????Duck ?? quack() ???????? Turkey ??? gobble() ?????????????? Turkey ??? Duck ?? quack() ?????? +ѼӣDuck𼦣TurkeyDuck quack() Turkey ֻ gobble() ҲҪ Turkey Ҳ Duck quack() -**4. ??????????** +**4. ͼ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1a511c76-bb6b-40ab-b8aa-39eeb619d673.jpg) -**5. ???????** +**5. ʵ** ```java public interface Duck { @@ -984,69 +984,69 @@ public class DuckTestDrive { } ``` -????? +н ```html gobble! fly! ``` -## 7.2 ????? +## 7.2 ģʽ -**1. ??????** +**1. ģʽ** -?????????????????????????????????????????????????????????????????? +ṩһͳһĽӿڣϵͳеһȺӿڡ۶һ߲ӿڣϵͳʹá -**2. ?????** +**2. ģʽͼ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/78f2314e-2643-41df-8f3d-b7e28294094b.jpg) -**3. ????????** +**3. ** -???????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ͥӰԺڶҪйۿӰʱҪԺܶвҪЩʹüͥӰԺֻṩһ򻯵ĽӿڣṩһӰĽӿڶþڶ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/106f5585-b2e7-4718-be5d-3b322d1ef42a.jpg) -**4. ??????????** +**4. ͼ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/25387681-89f8-4365-a2fa-83b86449ee84.jpg) -**5. ??????** +**5. ԭ** -**?????????**???????????????????????????????????????????????????????????? +**֪ʶԭ**̸ֻҲӦʹÿͻҪĶӦ١ -# ?? 8 ?? ?????? +# 8 ģ巽ģʽ -**1. ??????** +**1. ģʽ** -?????????????????????????????????????????? +һжһ㷨ĹǼܣһЩӳٵС -??????????????????????????????????????????????? +ģ巽ʹڲı㷨ṹ£¶㷨еijЩ衣 -**2. ?????** +**2. ģʽͼ** -???? templateMethod() ?????????????????? primitiveOperation1() ?? primitiveOperation2() ??????????? primitiveOperation1() ?? primitiveOperation2() ??????????????? +ģ巽 templateMethod() 㷨ĹǼܣȷ primitiveOperation1() primitiveOperation2() ִе˳򣬶 primitiveOperation1() primitiveOperation2() ȥʵ֡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ed62f400-192c-4185-899b-187958201f0c.jpg) -**3. ????????** +**3. ** -?????????????????????????????????? +忧Ⱥͳ趼Ƶ̣ijЩе㲻һ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d8f873fc-00bc-41ee-a87c-c1b4c0172844.png) -**4. ??????????** +**4. ͼ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/aa20c123-b6b5-432a-83d3-45dc39172192.jpg) -**5. ??????** +**5. ԭ** -**?????????**???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +**ԭ**ã绰ǣǻã绰㡣һԭԷֹܣֹ߲ײײ߲ԭģ巽Ϊֻи࣬಻øࡣ -**6. ????** +**6. ** -?????hock????????????????????????????????????????????????????????????? templteMethod() ??????????????????????????????????????? +ӣhockijЩڲͬʵппޣȶһʲôķӵģ巽 templteMethod() УҪ͸ĬʵֲԼʵ֡ -**7. ???????** +**7. ʵ** ```java public abstract class CaffeineBeverage { @@ -1109,7 +1109,7 @@ public class CaffeineBeverageTestDrive { } ``` -????? +н ```html boilWater @@ -1123,21 +1123,21 @@ pourInCup Tea.addCondiments ``` -# ?? 9 ?? ????????????? +# 9 ģʽ -## 9.1 ???????? +## 9.1 ģʽ -**1. ??????** +**1. ģʽ** -????????????????????????????????????????????????????? +ṩһ˳һۺ϶еĸԪصķֲ¶ڲıʾ -**2. ?????** +**2. ģʽͼ** -????????????????????????????????????????????????????????????????????????????????????????????????? +ͻӵһۺ϶͵󣬵Ǿۺ϶ɵġֻҪƶIJͿþۺ϶ܹ˳ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/439deca7-fed0-4c89-87e5-7088d10f1fdb.jpg) -**3. ???????** +**3. ʵ** ```java public class Aggregate { @@ -1195,7 +1195,7 @@ public class Client { } } ``` -????? +н ```html 0 1 @@ -1209,13 +1209,13 @@ public class Client { 9 ``` -## 9.2 Java ?????????? +## 9.2 Java õĵ -**1. ?????** +**1. ʵֽӿ** -Java ????????? Iterator ????????? Java ??????????????????? Iterable ????????????? iterator() ??????????? Iterator ??????? Java ??????????????????????????? foreach ???????????????????????? +Java Ѿ Iterator ӿڣʹ Java ʵʱҪþۺ϶ʵ Iterable ӿڣýӿһ iterator() ᷵һ Iterator ʹ Java õĵʵ֣ͻʹ foreach ѭۺ϶еÿԪء -**2. ???????** +**2. ʵ** ```java import java.util.Iterator; @@ -1271,25 +1271,25 @@ public class Client { } ``` -## 9.3 ????? +## 9.3 ģʽ -**1. ??????** +**1. ԭ** -???????????????????????? +һӦֻһıԭ -**2. ??????** +**2. ģʽ** -??????????????????????????? / ?????????? +ϳνṹ֡ / ֡νṹ -?????????????????????????????????????? +ÿͻһµķʽԼϡ -**3. ?????** +**3. ģʽͼ** -????????Composite???????????????Component??????????????????????????????????????????????????????????????????????? +ϣCompositeӵһComponent϶λνṹм䣬Լ󣬲ľ͡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f99c019e-7e91-4c2e-b94d-b031c402dcb5.jpg) -**4. ???????** +**4. ʵ** ```java public abstract class Component { @@ -1316,7 +1316,7 @@ public class Leaf extends Component { @Override public void addChild(Component component) { - throw new UnsupportedOperationException(); // ???????????????????? , ?????????????????????????? + throw new UnsupportedOperationException(); // ͸Իȡһְԭ , ͲÿҶӽڵ㻹Ͻڵ } @Override @@ -1378,7 +1378,7 @@ public class Client { } } ``` -????? +н ```html Composite:root @@ -1390,56 +1390,56 @@ Composite:root --left:3 ``` -# ?? 10 ?? ???? +# 10 ״̬ģʽ -**1. ??????** +**1. ģʽ** -????????????????????????????????????????????????????? +ڲ״̬ıʱıΪ޸ࡣ -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +״̬ģʽͼͲģʽһҶܹ̬ıΪ״̬ģʽͨ״̬״̬תıͻϵ״̬󣬶ģʽͨͻľıϵIJԶ磬״̬ģʽ£ͻί״̬һô״̬пܷ״̬תƣʹÿͻӵе״̬ı䡣״̬˿ͻ״̬ת״̬ͨıͻϵ״̬ʵֵġ -**2. ?????** +**2. ģʽͼ** ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c28fd93a-0d55-4a19-810f-72652feee00d.jpg) -**3. ????????** +**3. ** -???????????????????????????????????????????????????????????????????????? +ǹۻж״̬ÿ״̬ۻвͬΪ״̬ԷתƣʹۻΪҲı䡣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f7d880c9-740a-4a16-ac6d-be502281b4b2.jpg) -**4. ?????????** +**4. ֱӽ** -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ǹÿ棬жϵǰ״̬ݲͬ״̬вͬĴҷͬ״̬תơֽеʵϸڶŵͻ࣬״̬ʱҪȥ޸ĿͻĴ롣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/62ebbb63-8fd7-4488-a866-76a9dc911662.png) -**5. ???????????????** +**5. ʹ״̬ģʽĽ** -???????????????????????????????????????????????????????????????????????????????? +״̬תƱƵ״̬棬ͻÿֻҪίи״̬༴ɣҪ֪ǰʲô״̬Լ״̬ʱνתƵġ -**6. ???????** +**6. ʵ** ```java public interface State { /** - * ???25 ??? + * Ͷ25 Ǯ */ void insertQuarter(); /** - * ???25 ??? + * ˻25 Ǯ */ void ejectQuarter(); /** - * ??????? + * ת */ void turnCrank(); /** - * ??????? + * ǹ */ void dispense(); } @@ -1667,7 +1667,7 @@ public class GumballMachineTestDrive { } } ``` -????? +н ```html You insert a quarter You turned... @@ -1696,34 +1696,34 @@ You turned, but there are no gumballs No gumball dispensed ``` -# ?? 11 ?? ?????? // TODO +# 11 ģʽ // TODO -# ?? 12 ?? ?????? +# 12 ģʽ ## 12.1 MVC -### 12.1.1 ??? MVC +### 12.1.1 ͳ MVC -??????????????????????????????????????????????? +ͼʹģʽģʹ˹۲ģʽʹ˲ģʽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4f67611d-492f-4958-9fa0-4948010e345f.jpg) -### 12.1.2 Web ?? MVC +### 12.1.2 Web е MVC -???????????????? +ģʽʹù۲ģʽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1dd56e61-2970-4d27-97c2-6e81cee86978.jpg) -# ?? 13 ?? ????????? +# 13 ģʽദ -?????? **??** ??????? **????** ????? **???????**?? +壺ij **龳** £ij **** ij **** -????????????????????????????????????????????????????????????????????????????????????? +ʹģʽܵ´뱻ȹ̻Ӧ򵥵ĽɹҪģʽĵطʹ -?????????????????????????????????????????????????????????? +ģʽõĽһ⡣ҪΪ˾治ҪʹЩ -?????? +ģʽࣺ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/524a237c-ffd7-426f-99c2-929a6bf4c847.jpg) -# ?? 14 ?? ?????? // TODO +# 14 ʣµģʽ // TODO diff --git a/notes/重构.md b/notes/重构.md index 53a6b7b2..a7d15340 100644 --- a/notes/重构.md +++ b/notes/重构.md @@ -1,300 +1,300 @@ -* [????? ?????????](#?????-?????????) -* [????? ??????](#?????-??????) -* [?????? ????????](#??????-????????) - * [1. Duplicated Code?????????](#1-duplicated-code?????????) - * [2. Long Method????????????](#2-long-method????????????) - * [3. Large Class?????????](#3-large-class?????????) - * [4. Long Parameter List?????????????](#4-long-parameter-list?????????????) - * [5. Divergent Change?????????](#5-divergent-change?????????) - * [6. Shotgun Surgery??????????](#6-shotgun-surgery??????????) - * [7. Feature Envy?????????](#7-feature-envy?????????) - * [8. Data Clumps???????????](#8-data-clumps???????????) - * [9. Primitive Obsession??????????????](#9-primitive-obsession??????????????) - * [10. Switch Statements??switch ???????](#10-switch-statements??switch-???????) - * [11. Parallel Inheritance Hierarchies???????????](#11-parallel-inheritance-hierarchies???????????) - * [12. Lazy Class????????](#12-lazy-class????????) - * [13. Speculative Generality?????????????](#13-speculative-generality?????????????) - * [14. Temporary Field????????????????](#14-temporary-field????????????????) - * [15. Message Chains?????????????????](#15-message-chains?????????????????) - * [16. Middle Man???????](#16-middle-man???????) - * [17. Inappropriate Intimacy??????????](#17-inappropriate-intimacy??????????) - * [18. Alernative Classes with Different Interfaces?????????????](#18-alernative-classes-with-different-interfaces?????????????) - * [19. Incomplete Library Class?????????????](#19-incomplete-library-class?????????????) - * [20. Data Class?????????????](#20-data-class?????????????) - * [21. Refused Bequest???????????????](#21-refused-bequest???????????????) - * [22. Comments???????????](#22-comments???????????) -* [?????? ???????????](#??????-???????????) -* [?????? ?????](#??????-?????) -* [?????? ???????????](#??????-???????????) - * [1. Extract Method????????????](#1-extract-method????????????) - * [2. Inline Method????????????](#2-inline-method????????????) - * [3. Inline Temp???????????????](#3-inline-temp???????????????) - * [4. Replace Temp with Query??????????????????](#4-replace-temp-with-query??????????????????) - * [5. Introduce Explaining Variable??????????????](#5-introduce-explaining-variable??????????????) - * [6. Split Temporary Variable??????????????](#6-split-temporary-variable??????????????) - * [7. Remove Assigments to Parameters????????????????](#7-remove-assigments-to-parameters????????????????) - * [8. Replace Method with Method Object????????????????????](#8-replace-method-with-method-object????????????????????) - * [9. Subsititute Algorithn???I????](#9-subsititute-algorithn???I????) -* [?????? ???????????????](#??????-???????????????) - * [1. Move Method???????????](#1-move-method???????????) - * [2. Move Field?????????](#2-move-field?????????) - * [3. Extract Class????????](#3-extract-class????????) - * [4. Inline Class??????????????](#4-inline-class??????????????) - * [5. Hide Delegate???????????????](#5-hide-delegate???????????????) - * [6. Remove Middle Man??????????](#6-remove-middle-man??????????) - * [7. Introduce Foreign Method??????????????](#7-introduce-foreign-method??????????????) - * [8. Introduce Local Extension?????????????](#8-introduce-local-extension?????????????) -* [????? ???????????](#?????-???????????) - * [1. Self Encapsulate Field?????????](#1-self-encapsulate-field?????????) - * [2. Replace Data Value with Object?????????????????](#2-replace-data-value-with-object?????????????????) - * [3. Change Value to Reference??????????????????](#3-change-value-to-reference??????????????????) - * [4. Change Reference to value??????????????????](#4-change-reference-to-value??????????????????) - * [5. Replace Array with Object?????????????](#5-replace-array-with-object?????????????) - * [6. Duplicate Observed Data????????????????????](#6-duplicate-observed-data????????????????????) - * [7. Change Unidirectional Association to Bidirectional??????????????????????](#7-change-unidirectional-association-to-bidirectional??????????????????????) - * [8. Change Bidirectional Association to Unidirectional??????????????????????](#8-change-bidirectional-association-to-unidirectional??????????????????????) - * [9. Replace Magic Number with Symbolic Constant???????????????????](#9-replace-magic-number-with-symbolic-constant???????????????????) - * [10. Encapsulate Field????????](#10-encapsulate-field????????) - * [11. Encapsulate Collection??????????](#11-encapsulate-collection??????????) - * [12. Replace Record with Data Class??????????????????](#12-replace-record-with-data-class??????????????????) - * [13. Replace Type Code with Class???????????????](#13-replace-type-code-with-class???????????????) - * [14. Replace Type Code with Subcalsses?????????????????](#14-replace-type-code-with-subcalsses?????????????????) - * [15. Replace Type Code with State/Strategy ???? State/Strategy ?????????](#15-replace-type-code-with-statestrategy-????-statestrategy-?????????) - * [16. Replace Subclass with Fields??????????????](#16-replace-subclass-with-fields??????????????) -* [????? ??????????](#?????-??????????) - * [1. Decompose Conditional???????????????](#1-decompose-conditional???????????????) - * [2. Consolidate Conditional Expression???????????????](#2-consolidate-conditional-expression???????????????) - * [3. Consolidate Duplicate Conditional Fragments ?????????????????](#3-consolidate-duplicate-conditional-fragments-?????????????????) - * [4. Remove Control Flag????????????](#4-remove-control-flag????????????) - * [5. Replace Nested Conditional with Guard Clauses ?????????????????????????](#5-replace-nested-conditional-with-guard-clauses-?????????????????????????) - * [6. Replace Conditional with Polymorphism ???????????????????](#6-replace-conditional-with-polymorphism-???????????????????) - * [7. Introduce Null Object??????Null????](#7-introduce-null-object??????null????) - * [8. Introduce Assertion??????????](#8-introduce-assertion??????????) -* [????? ?????????](#?????-?????????) - * [1. Rename Method????????????](#1-rename-method????????????) - * [2. Add Parameter??????????](#2-add-parameter??????????) - * [3. Remove Parameter???????????](#3-remove-parameter???????????) - * [4. Separate Query from Modifier???????????????????????](#4-separate-query-from-modifier???????????????????????) - * [5. Parameterize Method??????????????](#5-parameterize-method??????????????) - * [6. Replace Parameter with Explicit Methods????????????????????](#6-replace-parameter-with-explicit-methods????????????????????) - * [7. Preserve Whole Object???????????????](#7-preserve-whole-object???????????????) - * [8. Replace Parameter with Methods????????????????](#8-replace-parameter-with-methods????????????????) - * [9. Introduce Parameter Object?????????????](#9-introduce-parameter-object?????????????) - * [10. Remove Setting Method??????????????](#10-remove-setting-method??????????????) - * [11. Hide Method???????????](#11-hide-method???????????) - * [12. Replace Constructor with Factory Method ??????????????????????](#12-replace-constructor-with-factory-method-??????????????????????) - * [13. Encapsulate Downcast?????????????](#13-encapsulate-downcast?????????????) - * [14. Replace Error Code with Exception ???????????????](#14-replace-error-code-with-exception-???????????????) - * [15. Replace Exception with Test??????????????](#15-replace-exception-with-test??????????????) -* [?????? ??????????](#??????-??????????) - * [1. Pull Up Field??????????](#1-pull-up-field??????????) - * [2. Pull Up Method???????????](#2-pull-up-method???????????) - * [3. Pull Up Constructor Body?????????????????](#3-pull-up-constructor-body?????????????????) - * [4. Push Down Method???????????](#4-push-down-method???????????) - * [5. Push Down Field??????????](#5-push-down-field??????????) - * [6. Extract Subclass??????????](#6-extract-subclass??????????) - * [7. Extract Superclass??????????](#7-extract-superclass??????????) - * [8. Extract Interface??????????](#8-extract-interface??????????) - * [9. Collapse Hierarchy?????????????](#9-collapse-hierarchy?????????????) - * [10. Form Template Method????????????](#10-form-template-method????????????) - * [11. Replace Inheritance with Delegation ?????????????](#11-replace-inheritance-with-delegation-?????????????) - * [12. Replace Delegation with Inheritance ????????????](#12-replace-delegation-with-inheritance-????????????) +* [һ һ](#һ-һ) +* [ڶ عԭ](#ڶ-عԭ) +* [ Ļζ](#-Ļζ) + * [1. Duplicated Codeظ룩](#1-duplicated-codeظ) + * [2. Long Method](#2-long-method) + * [3. Large Classࣩ](#3-large-class) + * [4. Long Parameter ListIJУ](#4-long-parameter-listIJ) + * [5. Divergent Changeɢʽ仯](#5-divergent-changeɢʽ仯) + * [6. Shotgun Surgeryɢʽ޸ģ](#6-shotgun-surgeryɢʽ޸) + * [7. Feature Envyᣩ](#7-feature-envy) + * [8. Data Clumpsţ](#8-data-clumps) + * [9. Primitive Obsessionƫִ](#9-primitive-obsessionƫִ) + * [10. Switch Statementsswitch ](#10-switch-statementsswitch-) + * [11. Parallel Inheritance Hierarchiesƽм̳ϵ](#11-parallel-inheritance-hierarchiesƽм̳ϵ) + * [12. Lazy Classࣩ](#12-lazy-class) + * [13. Speculative Generality̸δԣ](#13-speculative-generality̸δ) + * [14. Temporary FieldԻʱֶΣ](#14-temporary-fieldԻʱֶ) + * [15. Message ChainsϵϢ](#15-message-chainsϵϢ) + * [16. Middle Manмˣ](#16-middle-manм) + * [17. Inappropriate Intimacyǹϵ](#17-inappropriate-intimacyǹϵ) + * [18. Alernative Classes with Different Interfacesࣩͬ](#18-alernative-classes-with-different-interfacesͬ) + * [19. Incomplete Library Class⣩](#19-incomplete-library-class) + * [20. Data Classɵࣩ](#20-data-classɵ) + * [21. Refused Bequestܾ](#21-refused-bequestܾ) + * [22. Commentsעͣ](#22-commentsע) +* [ ϵ](#-ϵ) +* [ عб](#-عб) +* [ ֯](#-֯) + * [1. Extract Method](#1-extract-method) + * [2. Inline Method](#2-inline-method) + * [3. Inline Tempʱ](#3-inline-tempʱ) + * [4. Replace Temp with QueryԲѯȡʱ](#4-replace-temp-with-queryԲѯȡʱ) + * [5. Introduce Explaining Variableͱ](#5-introduce-explaining-variableͱ) + * [6. Split Temporary Variableֽʱ](#6-split-temporary-variableֽʱ) + * [7. Remove Assigments to ParametersƳԲĸֵ](#7-remove-assigments-to-parametersƳԲĸֵ) + * [8. Replace Method with Method ObjectԺȡ](#8-replace-method-with-method-objectԺȡ) + * [9. Subsititute Algorithn滻㷨](#9-subsititute-algorithn滻㷨) +* [ ڶ֮](#-ڶ֮) + * [1. Move Methodƺ](#1-move-methodƺ) + * [2. Move FieldֶΣ](#2-move-fieldֶ) + * [3. Extract Classࣩ](#3-extract-class) + * [4. Inline Class](#4-inline-class) + * [5. Hide Delegateءίйϵ](#5-hide-delegateءίйϵ) + * [6. Remove Middle ManƳмˣ](#6-remove-middle-manƳм) + * [7. Introduce Foreign MethodӺ](#7-introduce-foreign-methodӺ) + * [8. Introduce Local Extension뱾չ](#8-introduce-local-extension뱾չ) +* [ڰ ֯](#ڰ-֯) + * [1. Self Encapsulate FieldԷװֶΣ](#1-self-encapsulate-fieldԷװֶ) + * [2. Replace Data Value with ObjectԶȡֵ](#2-replace-data-value-with-objectԶȡֵ) + * [3. Change Value to Referenceֵijö](#3-change-value-to-referenceֵijö) + * [4. Change Reference to valueöΪֵ](#4-change-reference-to-valueöΪֵ) + * [5. Replace Array with ObjectԶȡ飩](#5-replace-array-with-objectԶȡ) + * [6. Duplicate Observed Dataֵݡ](#6-duplicate-observed-dataֵݡ) + * [7. Change Unidirectional Association to BidirectionalΪ˫](#7-change-unidirectional-association-to-bidirectionalΪ˫) + * [8. Change Bidirectional Association to Unidirectional˫Ϊ](#8-change-bidirectional-association-to-unidirectional˫Ϊ) + * [9. Replace Magic Number with Symbolic Constant泣ȡħ](#9-replace-magic-number-with-symbolic-constant泣ȡħ) + * [10. Encapsulate FieldװֶΣ](#10-encapsulate-fieldװֶ) + * [11. Encapsulate Collectionװϣ](#11-encapsulate-collectionװ) + * [12. Replace Record with Data Classȡ¼](#12-replace-record-with-data-classȡ¼) + * [13. Replace Type Code with Classȡ룩](#13-replace-type-code-with-classȡ) + * [14. Replace Type Code with Subcalssesȡ룩](#14-replace-type-code-with-subcalssesȡ) + * [15. Replace Type Code with State/Strategy State/Strategy ȡ룩](#15-replace-type-code-with-statestrategy--statestrategy-ȡ) + * [16. Replace Subclass with Fieldsֶȡࣩ](#16-replace-subclass-with-fieldsֶȡ) +* [ھ ʽ](#ھ-ʽ) + * [1. Decompose Conditionalֽʽ](#1-decompose-conditionalֽʽ) + * [2. Consolidate Conditional Expressionϲʽ](#2-consolidate-conditional-expressionϲʽ) + * [3. Consolidate Duplicate Conditional Fragments ϲظƬΣ](#3-consolidate-duplicate-conditional-fragments-ϲظƬ) + * [4. Remove Control FlagƳƱǣ](#4-remove-control-flagƳƱ) + * [5. Replace Nested Conditional with Guard Clauses ȡǶʽ](#5-replace-nested-conditional-with-guard-clauses-ȡǶʽ) + * [6. Replace Conditional with Polymorphism Զ̬ȡʽ](#6-replace-conditional-with-polymorphism-Զ̬ȡʽ) + * [7. Introduce Null ObjectNull](#7-introduce-null-objectnull) + * [8. Introduce Assertionԣ](#8-introduce-assertion) +* [ʮ 򻯺](#ʮ-򻯺) + * [1. Rename Method](#1-rename-method) + * [2. Add ParameterӲ](#2-add-parameterӲ) + * [3. Remove ParameterƳ](#3-remove-parameterƳ) + * [4. Separate Query from Modifierѯ޸ĺ룩](#4-separate-query-from-modifierѯ޸ĺ) + * [5. Parameterize MethodЯ](#5-parameterize-methodЯ) + * [6. Replace Parameter with Explicit Methodsȷȡ](#6-replace-parameter-with-explicit-methodsȷȡ) + * [7. Preserve Whole Objectֶ](#7-preserve-whole-objectֶ) + * [8. Replace Parameter with MethodsԺȡ](#8-replace-parameter-with-methodsԺȡ) + * [9. Introduce Parameter Object](#9-introduce-parameter-object) + * [10. Remove Setting MethodƳֵ](#10-remove-setting-methodƳֵ) + * [11. Hide Methodغ](#11-hide-methodغ) + * [12. Replace Constructor with Factory Method Թȡ캯](#12-replace-constructor-with-factory-method-Թȡ캯) + * [13. Encapsulate Downcastװתͣ](#13-encapsulate-downcastװת) + * [14. Replace Error Code with Exception 쳣ȡ룩](#14-replace-error-code-with-exception-쳣ȡ) + * [15. Replace Exception with TestԲȡ쳣](#15-replace-exception-with-testԲȡ쳣) +* [ʮһ ϵ](#ʮһ-ϵ) + * [1. Pull Up Fieldֶƣ](#1-pull-up-fieldֶ) + * [2. Pull Up Methodƣ](#2-pull-up-method) + * [3. Pull Up Constructor Body캯ƣ](#3-pull-up-constructor-body캯) + * [4. Push Down Methodƣ](#4-push-down-method) + * [5. Push Down Fieldֶƣ](#5-push-down-fieldֶ) + * [6. Extract Subclassࣩ](#6-extract-subclass) + * [7. Extract Superclassࣩ](#7-extract-superclass) + * [8. Extract Interfaceӿڣ](#8-extract-interfaceӿ) + * [9. Collapse Hierarchy۵̳ϵ](#9-collapse-hierarchy۵̳ϵ) + * [10. Form Template Methodģ庯](#10-form-template-methodģ庯) + * [11. Replace Inheritance with Delegation ίȡ̳У](#11-replace-inheritance-with-delegation-ίȡ̳) + * [12. Replace Delegation with Inheritance Լ̳ȡίУ](#12-replace-delegation-with-inheritance-Լ̳ȡί) -# ????? ????????? +# һ һ -????????????????????????????????????????????????????????????????????? +㷢ԼҪΪһԣṹʹ޷ܷشĿģǾع -??????????????????????????????????????????? +عǰҪȹÿɿIJԻȷȫع -????????????????????????????????????????????? +ع΢СIJ޸ij´󣬺ױԷ -**????????** +**** -?????????????????????Movie??Rental ?? Customer??Rental ????????? Movie ????????? +ӰƬӦó򣬰ࣺMovieRental CustomerRental ޵ Movie Լ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a758c8b2-0ac7-438f-90c2-3923ffad6328.png) -???????????????????????? Customer ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʼʵǰеļƷѴ붼 Customer Уڱ仯ʱҪⲿִиġпܷı仯УһļƷѷʽı䣻µĵӰ𡣿ǵƷѴܴڶദһıʱҪмƷѴ޸ġ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9e5e3cc6-3107-4051-b584-8ff077638fe6.png) -???????? Movie ????????????????????? Movie ?????????????????????????? +Ǽ̳ Movie Ķ̬һ Movie ᶯ̬ı䣬ַС ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2a502516-5d34-4eef-8f39-916298a60035.png) -???? Price ???????????????????????? Movie ???? Price ?????????????????????????????? Price ?????????? Movie ??????????????????????????????????????????? + Price ӦϢͨϵķʽ Movie м Price ÿļƷѷʽװڲͬ Price У Movie ҲԶ̬ıַʽԺܺõӦᵽı仯 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c02a83b8-a6b9-4d00-a509-6f0516beaf5e.png) -????????????????? +عʱͼͼ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/95f4559c-3d2a-4176-b365-4fbc46c76cf1.png) ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/293b9326-02fc-4ad8-8c79-b4a7b5ba60d3.png) -# ????? ?????? +# ڶ عԭ -?????????????????????????????????????????????????????????????????????????????? +عǶڲṹһֵĿڲıɹ۲Ϊǰ£ԣ޸ijɱ -??????????????????????????????????????? bug??????????? +عĺôĽƣʹ⣻ҵ bug߱ٶȡ -??????????????????????????????????????????????????????????????????????????????? +η򣺵һijʱֻȥڶʱȥƵ£Ӧع -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ӲعѧеĺܶͨһӲӲ¼ֵ߼ֿͼʵ֣仯װ߼عΪʵλòӲԼڲҪʱƳӲ㡣 -???????????????????????????????????????? Java ???? @deprecation ?????????????????????????????????????????????????? +޸ĽӿڣԱɽӿڣþɽӿȥ½ӿڣʹ Java ṩ @deprecation ɽӿڱΪáбҪҪӿڣҲҪ緢ӿڡ -???????????????????????????????????????????????????????????????????????????????????????????? +дڻʱӦдعһеİ취ǣװһȻԸдعľ -???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +޷ԤƣΪкܶ仯ʼܶǽȥعԼƣعһ򵥵Ͻ޸ĸģ仯ʱһķʽȥӦԱ仯õơ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +Ϊ⣬عܻᵼܼ͡ڱдʱöܹעֻŻ׶ٿ⡣ӦֻעؼܣΪֻһСֵĴǹؼ롣 -# ?????? ???????? +# Ļζ -## 1. Duplicated Code????????? +## 1. Duplicated Codeظ룩 -?????????????????????????????? Extract Method ???????????? +ͬһͬʽ Extract Method ȡظ룻 -???????????????????????????????? Extract Method?????????????????? Pull Up Method ?????? +Ϊֵܵຬͬıʽʹ Extract MethodȻȡĺ Pull Up Method 볬ࡣ -???????????????? Extract Method ????????????????????????? Form Template Method ??????????????? +ֻDzͬ Extract Method ƲֺͲ첿֣Ȼʹ Form Template Method ģ巽ģʽ -???????????????????????????????? Extract Class ????????????????????????????? +صظ룬ʹ Extract Class ظȡһС -## 2. Long Method???????????? +## 2. Long Method -?????????????????????????????????????? +Ӳļֵѡ -?????????????????????????????????????????????????????????????? +ֽ⺯ԭ򣺵Ҫע˵һδʱҪⲿִдһĺС -Extract Method ??????????????????????????????????? Replace Temp with Query ?????????????Introduce Parameter Object ?? Preserve Whole Object ????????????????????? +Extract Method Ѻܶʱ Replace Temp with Query ʱIntroduce Parameter Object Preserve Whole Object ԽIJбøࡣ -?????????????????????????????? +ѭҲҪȡµĺС -## 3. Large Class????????? +## 3. Large Classࣩ -??????????????????????? Extract Class ?? Extract Subclass?? +˹飬Ҫʹ Extract Class Extract Subclass -???????????????????????????? Extract Interface ????????????????????? +ȷͻʹǣȻ Extract Interface Ϊÿһʹ÷ʽȡһӿڡ -## 4. Long Parameter List????????????? +## 4. Long Parameter ListIJУ -## 5. Divergent Change????????? +## 5. Divergent Changeɢʽ仯 -????????????????? +һֱܵ仯Ӱ죻 -??????????????? Extract Class ????????????????? +ijԭı仯ʹ Extract Class һС -## 6. Shotgun Surgery?????????? +## 6. Shotgun Surgeryɢʽ޸ģ -???????????????? +һ仯޸ģ -??? Move Method ?? Move Field ?????????????????????????? +ʹ Move Method Move Field Ҫ޸ĵشŵͬһС -## 7. Feature Envy????????? +## 7. Feature Envyᣩ -????????????????????????????????????????????????????????????? +һijȤڶԼȤͨǹݡ -??? Move Method ??????????????????????????? Feature Envy?????? Extract Method ?????????????? +ʹ Move Method ƵȥĵطԶ඼ Feature Envy Extract Method ȡ -## 8. Data Clumps??????????? +## 8. Data Clumpsţ -????????????????????????????????????????????????????????? Extract Class ???????????? +Щݾһֶ֣ͬΡຯͬIJʹ Extract Class Ƿһ -## 9. Primitive Obsession?????????????? +## 9. Primitive Obsessionƫִ -???????????????????????????? Replace Data Value with Object ????????I????? +ʹʹû͸ãʹ Replace Data Value with Object ֵ滻Ϊ -## 10. Switch Statements??switch ??????? +## 10. Switch Statementsswitch -## 11. Parallel Inheritance Hierarchies??????????? +## 11. Parallel Inheritance Hierarchiesƽм̳ϵ -??????????????????????????????????????????????? +ÿΪijһ࣬ҲΪһӦһࡣ -????????????????????????????????????????????????????????????????????????? +ֽһЩظԣظԵһԣһ̳ϵʵһ̳ϵʵ -## 12. Lazy Class???????? +## 12. Lazy Classࣩ -????????????????????????????????? +һû㹻ĹӦʧ -## 13. Speculative Generality????????????? +## 13. Speculative Generality̸δԣ -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +Щδܷı仯ϵͳάԤδܷĸıܿܺʼ෴ˣDZҪͲҪô -## 14. Temporary Field???????????????? +## 14. Temporary FieldԻʱֶΣ -???????????????????k?????????????????????????????????????????????????? +ijֶνΪijض裬Ĵ벻⣬ΪͨΪʱҪֶΡ -?????????????????????????? Extract Class ????????????????? +ֶκضĴʹ Extract Class һС -## 15. Message Chains????????????????? +## 15. Message ChainsϵϢ -??????????????????????????????????????????????...????????????????????????????????????????????????????? +һһȻһȻ...Ϣַʽζſͻ뽫Ĺϵϡ -???????????????????????????????? +úúίһ -## 16. Middle Man??????? +## 16. Middle Manмˣ -???????????????????????????????????????????????????????????????????? Remove Middle Man????????????????? +м˸ίиIJһйĺίи࣬ǾǹίУӦ Remove Middle Manֱ븺Ķ򽻵 -## 17. Inappropriate Intimacy?????????? +## 17. Inappropriate Intimacyǹϵ -??????????????????????????????? private ???? +ܣ̫ʱȥֱ̽˴˵ private ɷ֡ -## 18. Alernative Classes with Different Interfaces????????????? +## 18. Alernative Classes with Different Interfacesࣩͬ -## 19. Incomplete Library Class????????????? +## 19. Incomplete Library Class⣩ -?????????????????????????????????????????????????????????????????????????????????????????????? Introduce Foreign Method???????????????????????? Introduce Local Extension?? +߲Ƴ⣬ҪһЩ޸ʱʹַֻ޸һʹ Introduce Foreign MethodҪһѶΪʹ Introduce Local Extension -## 20. Data Class????????????? +## 20. Data Classɵࣩ -?????????????? +ֻӵһЩֶΡ -????????????????????????????? Data Class ?? +ҳֶʹõĵطȻӦIJƵ Data Class С -## 21. Refused Bequest??????????????? +## 21. Refused Bequestܾ -?????????????????????????????????????? +̳гкݣֻҪһ֡ -??????????????????????????????????? Push Down Method ?? Push Down Field ???????????? +Ϊ½һֵ࣬Ҫĺʹ Push Down Method Push Down Field ƸǸֵܡ -## 22. Comments??????????? +## 22. Commentsעͣ -??? Extract Method ?????????????????????????????????????????? +ʹ Extract Method Ҫע͵IJ֣ȻúͺΪ -# ?????? ??????????? +# ϵ -Java ??????? Junit ?????????? +Java ʹ Junit еԪԡ -????????????????????????????????????????????????? +ԪԵĶķܲԿͻĽǶȱ֤С -???????????????????????? +ӦвԿܳı߽ -# ?????? ????? +# عб -??????????????? +СǰƵԡ -# ?????? ??????????? +# ֯ -## 1. Extract Method???????????? +## 1. Extract Method -????????????????????????????????????????????? +δŽһУúƽ͸ú; -## 2. Inline Method???????????? +## 2. Inline Method -????????????????????????????? +һıͬ׶ -?????????????????????????????? +ںõ뺯壬ȻƳú -## 3. Inline Temp??????????????? +## 3. Inline Tempʱ -???????????????????????????????????????????????? +һʱֻ򵥱ʽֵһΣعַ -?????????????????I????????????????????? +жԸñ滻ΪֵǸʽ ```java double basePrice = anOrder.basePrice(); @@ -305,9 +305,9 @@ return basePrice > 1000; return anOrder.basePrice() > 1000; ``` -## 4. Replace Temp with Query?????????????????? +## 4. Replace Temp with QueryԲѯȡʱ -??????????????????????????????????????????????????????????????????????????????I?????????????Replace Temp with Query ?????? Extract Method ????????????????k??????????????????????????? +ʱijһʽʽһУжʱõ滻ΪºĵáReplace Temp with Query Extract Method ֮ǰزٵһ裬Ϊֲʹ ```java double basePrice = quantity * itemPrice; @@ -329,9 +329,9 @@ double basePrice(){ } ``` -## 5. Introduce Explaining Variable?????????????? +## 5. Introduce Explaining Variableͱ -?????????????????????????????????????????????????????????????????? +ӱʽһ֣ĽŽһʱԴ˱ͱʽ; ```java if((platform.toUpperCase().indexOf("MAC") > -1) && @@ -351,15 +351,15 @@ if(isMacOS && isIEBrower && wasInitialized() && wasResized) { } ``` -## 6. Split Temporary Variable?????????????? +## 6. Split Temporary Variableֽʱ -?????????????????????????????????????????????????????????? +ijʱֵһΣȲѭҲռ -???????????????????????????????????????????????????????? +ÿθֵһӦʱÿʱֻеһΡ -## 7. Remove Assigments to Parameters???????????????? +## 7. Remove Assigments to ParametersƳԲĸֵ -?????????????????????????? +һʱȡԸòĸֵ ```java int discount (int inputVal, int quentity, int yearToDate){ @@ -372,39 +372,39 @@ int discount (int inputVal, int quentity, int yearToDate){ if (inputVal > 50) result -= 2; ``` -## 8. Replace Method with Method Object???????????????????? +## 8. Replace Method with Method ObjectԺȡ -?????????????????? Extract Method ?????????????????????????ڨ????? +һͺ Extract Method ʱڰ˾ֲʹúѽиò -?????????????????????????????????????????????????????????????????????????????????????????????? +ŽһУһֲͳ˶ڵֶΡȻͬһнͺֽΪСͺ -## 9. Subsititute Algorithn???I???? +## 9. Subsititute Algorithn滻㷨 -# ?????? ??????????????? +# ڶ֮ -## 1. Move Method??????????? +## 1. Move Methodƺ -?????????????????????????????????????????????? +еijһиཻú߻߱ߵá -??????????????????????? +ƵһС -## 2. Move Field????????? +## 2. Move FieldֶΣ -????????????????????????????????????????????????????????????????????????? +еijֶαһõõָȡֵֵӦѸֶƵһС -## 3. Extract Class???????? +## 3. Extract Classࣩ -??????????????????????????? +ijӦ¡ -??????????????????????????????????????? +Ӧһ࣬صֶκͺӾƵࡣ -## 4. Inline Class?????????????? +## 4. Inline Class -?? Extract Class ???? + Extract Class ෴ -## 5. Hide Delegate??????????????? +## 5. Hide Delegateءίйϵ -???????????????????????? +ĺίйϵ ```java class Person{ @@ -424,13 +424,13 @@ class Department{ } ``` -?????????????????????????????? Department ?????????????????? Department ???????? +ͻϣ֪ij˵ľ˭ Department ͶԿͻ¶ Department Ĺԭ ```java Person manager = john.getDepartment().getManager(); ``` -???? Peron ??????????????????????????? +ͨΪ Peron һίйϵ ```java public Person getManager(){ @@ -438,61 +438,61 @@ public Person getManager(){ } ``` -## 6. Remove Middle Man?????????? +## 6. Remove Middle ManƳмˣ -?? Hide Delegate ?????????????????????????????????????? + Hide Delegate ෴ҪƳίкÿͻֱӵίࡣ -Hide Delegate ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +Hide Delegate кܴôĴǣÿͻҪʹʱͱڷһ򵥵ίкίеԽԽ࣬ȫһмˡ -## 7. Introduce Foreign Method?????????????? +## 7. Introduce Foreign MethodӺ -???????????????????????????????????????? +ҪΪṩһ޷޸ࡣ -?????????????????????????????????????????????????????????????????????????? +ڿͻнһԵһʽһʵÿͻϷʵ -## 8. Introduce Local Extension????????????? +## 8. Introduce Local Extension뱾չ -?? Introduce Foreign Method ???????????? Introduce Local Extension ?????????????????????????????????????????????????????????????????????????? + Introduce Foreign Method Ŀһ Introduce Local Extension ͨµʵַ֡ʽ߰װ̳࣬ͨʵ֣װͨʵ֡ -# ????? ??????????? +# ڰ ֯ -## 1. Self Encapsulate Field????????? +## 1. Self Encapsulate FieldԷװֶΣ -????????/?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +Ϊֶνȡֵ/ֵЩֶΡֻеʳһֶΣнֶηʸΪһֵʹַʽֱӷֶεķʽˡ -## 2. Replace Data Value with Object????????????????? +## 2. Replace Data Value with ObjectԶȡֵ -???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ڿڣü򵥵ʾ򵥵ſĽУһЩһЩΪһʼѵ绰ֵַ绰Ҫʽȡš֮Ϊ -## 3. Change Value to Reference?????????????????? +## 3. Change Value to Referenceֵijö -?????????????I???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +˴ȵʵ滻ΪͬһҪһΨһ󣬹ҪһѾбҪһʱȲбǷѾڸöڣ򷵻бе󣻷½һӵбУظö -## 4. Change Reference to value?????????????????? +## 4. Change Reference to valueöΪֵ -?? Change Value to Reference ????????????????????????????????????????????????????????????????????????I????????????????? + Change Value to Reference ෴ֵиdzҪԣDzɱģɱʾҪı󣬱һµĶ滻ɶ󣬶޸ľɶ -???????????? equals() ?? hashCode() ???? +ҪΪֵʵ equals() hashCode() -## 5. Replace Array with Object????????????? +## 5. Replace Array with ObjectԶȡ飩 -????????????????????????????? +һ飬еԪظԴͬĶ -??????I?????????????????????????????????????????????????????????? +Զ滻飬еÿԪأһֶʾҲ⡣ -## 6. Duplicate Observed Data???????????????????? +## 6. Duplicate Observed Dataֵݡ -??????????????? GUI ??????????????????????????? +һЩ GUI ؼУҪЩݡ -??????????????????????????????? Oberver ??????????????????? GUI ??????????????? +ݸֵһУһ Oberver ģʽͬ GUI ڵظݡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e024bd7e-fb4e-4239-9451-9a6227f50b00.jpg) -## 7. Change Unidirectional Association to Bidirectional?????????????????????? +## 7. Change Unidirectional Association to BidirectionalΪ˫ -?????????????????????????????????????? +඼ҪԷʱʹ˫ -???????????????? Order ???? Customer??Order ?????? Customer??Customer ???????? Order ??????????????? +ֱ࣬Ϊ Order Ϳͻ CustomerOrder CustomerCustomer ҲҪ Order 鿴ж顣 ```java class Order{ @@ -517,59 +517,59 @@ class Curstomer{ } ``` -??????????? Curstomer ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ע⵽ Curstomer ƹϵԭĸƹϵijһIJôɺ߸ƹϵһԶϵɵһһƹϵ -## 8. Change Bidirectional Association to Unidirectional?????????????????????? +## 8. Change Bidirectional Association to Unidirectional˫Ϊ -?? Change Unidirectional Association to Bidirectiona ????????? + Change Unidirectional Association to Bidirectiona Ϊ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +˫άɱߣҲ⡣˫Ӻɡʬ󡱣ijѾˣȴϵͳУΪûûȫȫ -## 9. Replace Magic Number with Symbolic Constant??????????????????? +## 9. Replace Magic Number with Symbolic Constant泣ȡħ -???????????????????????????????????????????????????? +һΪ泣λ -## 10. Encapsulate Field???????? +## 10. Encapsulate FieldװֶΣ -public ????????? private??????????????????? +public ֶӦΪ privateṩӦķʺ -## 11. Encapsulate Collection?????????? +## 11. Encapsulate Collectionװϣ -????????????????????????????????????????/?????????????????????????????????????????????????????????????????????????? +ؼϵһֻṩ/ƳԪصĺؼû޸ļݶӵȴһ֪ -## 12. Replace Record with Data Class?????????????????? +## 12. Replace Record with Data Classȡ¼ -## 13. Replace Type Code with Class??????????????? +## 13. Replace Type Code with Classȡ룩 -?????????????????????????????????????????????????I????????????????????????? switch ??????????? Replace Conditional with Polymorphism ??? switch????????????? Replace Type Code with Subcalss ?? Replace Type Code with State/Strategy ????????? +һֵ룬ӰΪһ滻ֵ롣 switch УҪʹ Replace Conditional with Polymorphism ȥ switchȱ Replace Type Code with Subcalss Replace Type Code with State/Strategy ȥ롣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/27c2e0b3-8f95-453d-bedc-6398a8566ce9.jpg) -## 14. Replace Type Code with Subcalsses????????????????? +## 14. Replace Type Code with Subcalssesȡ룩 -?????????????????????????????????????????????????? +һɱ룬ӰΪȡ롣 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c41d3977-e0e7-4ee4-93e1-d84f1ae3e20e.jpg) -## 15. Replace Type Code with State/Strategy ???? State/Strategy ????????? +## 15. Replace Type Code with State/Strategy State/Strategy ȡ룩 -??????????????????????????????????????????????? +һɱ룬ӰΪ״̬ȡ롣 -?? Replace Type Code with Subcalsses ???????? Replace Type Code with State/Strategy ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? + Replace Type Code with Subcalsses Replace Type Code with State/Strategy Ƕ̬ɱģǰ̳ͨеķʽʵ֣ͨϵķʽʵ֡Ϊɱ䣬̳ͨеķʽһһı䣬ôҪıµĶȡɶ󣬶ͻԸıµĶ󡣵ͨϵķʽıõ״̬Ǻ׵ġ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/81fd1d6f-a3b2-4160-9a0a-1f7cb50ba440.jpg) -## 16. Replace Subclass with Fields?????????????? +## 16. Replace Subclass with Fieldsֶȡࣩ -????????????????????????????????????? +Ψһֻڡسݡĺϡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f2e0cee9-ecdc-4a96-853f-d9f6a1ad6ad1.jpg) -# ????? ?????????? +# ھ ʽ -## 1. Decompose Conditional??????????????? +## 1. Decompose Conditionalֽʽ -???????????????????????? if??then??else ??????????????????????????? +һӵ䣬Դ ifthenelse зֱ ```java if(data.befor(SUMMER_START) || data.after(SUMMER_END)) @@ -583,11 +583,11 @@ if(notSummer(date)) else charge = summerCharge(quantity); ``` -## 2. Consolidate Conditional Expression??????????????? +## 2. Consolidate Conditional Expressionϲʽ -???????????????????????????? +һϵԣõͬ -??????????????????????????????????????????????????????????? +ЩԺϲΪһʽʽΪһ ```java double disabilityAmount(){ @@ -604,11 +604,11 @@ double disabilityAmount(){ } ``` -## 3. Consolidate Duplicate Conditional Fragments ????????????????? +## 3. Consolidate Duplicate Conditional Fragments ϲظƬΣ -?????????????????????????????????? +ʽÿ֧ͬһδ롣 -??????????????????????????? +ظƵʽ֮⡣ ```java if (isSpecialDeal()){ @@ -629,17 +629,17 @@ if (isSpecialDeal()) { send(); ``` -## 4. Remove Control Flag???????????? +## 4. Remove Control FlagƳƱǣ -?????????????????????????????????????? +һϵвʽУijСƱǡá -?? break?? ??? return ??????????????? + break return ȡƱǡ -## 5. Replace Nested Conditional with Guard Clauses ????????????????????????? +## 5. Replace Nested Conditional with Guard Clauses ȡǶʽ -????????????????????????????????????????????????????????????????????????@????????????????guard clauses???? +ij亱ӦõڸΪʱ̴Ӻзأĵ鳣Ϊ䡱guard clauses -????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʽֱͨʽһʽǣз֧ΪڶʽǣʽṩĴֻһΪDzʹ ```java double getPayAmount() { @@ -665,9 +665,9 @@ double getPayAmount() { }; ``` -## 6. Replace Conditional with Polymorphism ??????????????????? +## 6. Replace Conditional with Polymorphism Զ̬ȡʽ -??????????????????????????????????????????????????????????????????????? Replace Type Code with Subclass ?? Replace Type Code with State/Strategy ???????????? +ʽÿ֧ŽһڵĸдУȻԭʼΪҪʹ Replace Type Code with Subclass Replace Type Code with State/Strategy ̳н ```java double getSpeed() { @@ -685,18 +685,18 @@ double getSpeed() { ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1c8432c8-2552-457f-b117-1da36c697221.jpg) -## 7. Introduce Null Object??????Null???? +## 7. Introduce Null ObjectNull -?? null ??I? null ?????????????????????????????????????????????? + null ֵ滻Ϊ null ĺôڣҪѯʶǷΪգֱӵþС ```java if (customer == null) plan = BillingPlan.basic(); else plan = customer.getPlan(); ``` -## 8. Introduce Assertion?????????? +## 8. Introduce Assertionԣ -????????????????????????????????????????????????????? +Զȷijּ衣ֻڿУƷвжԡ ```java double getExpenseLimit() { @@ -712,25 +712,25 @@ double getExpenseLimit() { } ``` -# ????? ????????? +# ʮ 򻯺 -## 1. Rename Method???????????? +## 1. Rename Method -?????????????????????? +ʹܽͺ; -## 2. Add Parameter?????????? +## 2. Add ParameterӲ -??????????????????????????? +ʹҪͨûijϢ -## 3. Remove Parameter??????????? +## 3. Remove ParameterƳ -?? Add Parameter ??????????????????????????? + Add Parameter ෴õõķʽijϢ -## 4. Separate Query from Modifier??????????????????????? +## 4. Separate Query from Modifierѯ޸ĺ룩 -????????????????????????????????? +ijض״ֵ̬޸Ķ״̬ -?????????????????????????????????????????????????????????????????????????????????? +Ӧͬĺһѯһ޸ġκзֵĺӦпõĸá ```java getTotalOutstandingAndSetReadyForSummaries(); @@ -741,11 +741,11 @@ getTotalOutstanding(); setReadyForSummaries(); ``` -## 5. Parameterize Method?????????????? +## 5. Parameterize MethodЯ -???????????????????????????????????????????????? +ɺƵĹںȴ˲ֵͬ -??????????????????????????????? +һԲЩֵͬ ```java fivePercentRaise(); @@ -755,11 +755,11 @@ tenPercentRaise(); raise(percentage); ``` -## 6. Replace Parameter with Explicit Methods???????????????????? +## 6. Replace Parameter with Explicit Methodsȷȡ -???????????????????????????????????? +һȫȡڲֵȡͬΪ -???????????????????????????????????? +Ըòÿһֵһ ```java void setValue(String name, int value){ @@ -784,11 +784,11 @@ void setWidth(int arg){ } ``` -## 7. Preserve Whole Object??????????????? +## 7. Preserve Whole Objectֶ -???????????????????????????????????????????????? +ijȡֵΪijһκʱIJ -??????????????? +Ϊ ```java int low = daysTempRange().getLow(); @@ -800,11 +800,11 @@ withinPlan = plan.withinRange(low,high); withinPlan = plan.withinRange(daysTempRange()); ``` -## 8. Replace Parameter with Methods???????????????? +## 8. Replace Parameter with MethodsԺȡ -??????????????????????????????????????????????????????????????????????????????????????? +ijýΪݸһܸòĺҲܹǰһ -?????????????????????????????????????????? +òȥֱӵǰһ ```java int basePrice = _quantity * _itemPrice; @@ -817,37 +817,37 @@ int basePrice = _quantity * _itemPrice; double finalPrice = discountedPrice (basePrice); ``` -## 9. Introduce Parameter Object????????????? +## 9. Introduce Parameter Object -???????????????????????????????? Data Clumps?? +ijЩǺȻͬʱ֣Щ Data Clumps -???????????????????? +һȡЩ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/08738dd0-ae8e-404a-ba78-a6b1b7d225b3.jpg) -## 10. Remove Setting Method?????????????? +## 10. Remove Setting MethodƳֵ -???????????????????????????????????? +еijֶӦڶ󴴽ʱֵȻͲٸı䡣 -????????????????????????????????? final?? +ȥֶεֵֶΪ final -## 11. Hide Method??????????? +## 11. Hide Methodغ -??????????????????????????????? +һûбκõ -????????????? private?? +޸Ϊ private -## 12. Replace Constructor with Factory Method ?????????????????????? +## 12. Replace Constructor with Factory Method Թȡ캯 -??????????????????????????????????? +ϣڴʱ򵥵Ľ -?????????I??????????? +캯滻Ϊ -## 13. Encapsulate Downcast????????????? +## 13. Encapsulate Downcastװתͣ -????????????????????????????????????????downcast???? +ijصĶҪɺִתͣdowncast -????????????????????? +תͶƵС ```java Object lastReading(){ @@ -860,17 +860,17 @@ Reading lastReading(){ } ``` -## 14. Replace Error Code with Exception ??????????????? +## 14. Replace Error Code with Exception 쳣ȡ룩 -??????????????????????????????????????? +ijһضĴ룬Աʾijִ -?????????????????????????????????????????? +쳣쳣ͨʹֿʹ⡣ -## 15. Replace Exception with Test?????????????? +## 15. Replace Exception with TestԲȡ쳣 -????????????????????????????????????????? +һ߿Ԥȼ׳һ쳣 -??????????????????????????? +޸ĵߣʹڵú֮ǰ顣 ```java double getValueForPeriod(int periodNumber) { @@ -887,25 +887,25 @@ double getValueForPeriod(int periodNumber) { return values[periodNumber]; ``` -# ?????? ?????????? +# ʮһ ϵ -## 1. Pull Up Field?????????? +## 1. Pull Up Fieldֶƣ -??????????????????? +ӵֶͬΡ -??????????????? +ֶࡣ -## 2. Pull Up Method??????????? +## 2. Pull Up Methodƣ -????????????????????????????????? +ЩڸвȫͬĽ -?????????????? +úࡣ -## 3. Pull Up Constructor Body????????????????? +## 3. Pull Up Constructor Body캯ƣ -????????????????????????????????????????? +ڸӵһЩ캯ǵı弸ȫһ¡ -????????????????????????????????????????? +ڳ½һ캯๹캯е ```java class Manager extends Employee... @@ -924,56 +924,56 @@ public Manager(String name, String id, int grade) { } ``` -## 4. Push Down Method??????????? +## 4. Push Down Methodƣ -????????????????????????? +еijֻ벿йء -????????????????????????? +ƵصЩȥ -## 5. Push Down Field?????????? +## 5. Push Down Fieldֶƣ -???????????????????????????? +еijֶֻõ -??????????????????????????? +ֶƵҪЩȥ -## 6. Extract Subclass?????????? +## 6. Extract Subclassࣩ -????????????????????? +еijЩֻijЩʵõ -????????????????????????????????????????? +½һ࣬˵һƵС -## 7. Extract Superclass?????????? +## 7. Extract Superclassࣩ -????????????????? +ԡ -??????????????????????????????????? +Ϊཨһ࣬ͬࡣ -## 8. Extract Interface?????????? +## 8. Extract Interfaceӿڣ -???????????????????????????????????????????? +ɿͻʹӿеͬһӼĽӿвͬ -???????????????????????????? +ͬӼһӿС -## 9. Collapse Hierarchy????????????? +## 9. Collapse Hierarchy۵̳ϵ -????????????????????? +̫֮ -??????????^ +ǺΪһ塣 -## 10. Form Template Method???????????? +## 10. Form Template Methodģ庯 -?????????????????????????????????????????????????????????????????????? +һЩ࣬ӦijЩͬ˳ִƵIJϸͬ -????????????????????????????????????????????????????????????????????????????????????(??????) +ЩֱŽУǶͬǩԭҲͱͬˡȻԭࡣ(ģ巽ģʽ) -## 11. Replace Inheritance with Delegation ????????????? +## 11. Replace Inheritance with Delegation ίȡ̳У -??????????????????????????????????????????????? +ijֻʹóӿеһ֣ǸҪ̳жݡ -???????????????????????????????????????????????????????????????????? +½һֶԱ泬࣬ຯĶίг࣬Ȼȥ֮ļ̳йϵ -## 12. Replace Delegation with Inheritance ???????????? +## 12. Replace Delegation with Inheritance Լ̳ȡίУ -?????????????????????????????????????????????????? +֮ʹίйϵΪӿڱд༫򵥵ίк -??????????????? +ί̳ࡣ diff --git a/notes/面向对象思想.md b/notes/面向对象思想.md index 0cf0b96e..0403888d 100644 --- a/notes/面向对象思想.md +++ b/notes/面向对象思想.md @@ -1,75 +1,75 @@ * [S.O.L.I.D](#solid) - * [1. ??????????](#1-??????????) - * [2. ?????????](#2-?????????) - * [3. ?????I???](#3-?????I???) - * [4. ?????????](#4-?????????) - * [5. ???????????](#5-???????????) -* [???????????](#???????????) - * [1. ???](#1-???) - * [2. ???](#2-???) - * [3. ???](#3-???) + * [1. һԭ](#1-һԭ) + * [2. ŷԭ](#2-ŷԭ) + * [3. 滻ԭ](#3-滻ԭ) + * [4. ӿڷԭ](#4-ӿڷԭ) + * [5. ԭ](#5-ԭ) +* [װ̳С̬](#װ̳ж̬) + * [1. װ](#1-װ) + * [2. ̳](#2-̳) + * [3. ̬](#3-̬) * [UML](#uml) - * [1. ???](#1-???) - * [2. ????](#2-????) -* [??????](#??????) + * [1. ͼ](#1-ͼ) + * [2. ʱͼ](#2-ʱͼ) +* [ο](#ο) # S.O.L.I.D -S.O.L.I.D???????????????(OOD&OOP)??????????????(Programming Priciple)??????????? +S.O.L.I.Dƺͱ(OOD&OOP)мҪԭ(Programming Priciple)ĸд -|?? |?? |???????| +|д |ȫƴ |ķ| | -- | -- | -- | -|SRP| The Single Responsibility Principle |??????????| -|OCP| The Open Closed Principle | ?????????| -|LSP| The Liskov Substitution Principle |?????I???| -|ISP| The Interface Segregation Principle |?????????| -|DIP| The Dependency Inversion Principle |???????????| +|SRP| The Single Responsibility Principle |һԭ| +|OCP| The Open Closed Principle | ŷԭ| +|LSP| The Liskov Substitution Principle |滻ԭ| +|ISP| The Interface Segregation Principle |ӿڷԭ| +|DIP| The Dependency Inversion Principle |ԭ| -## 1. ?????????? +## 1. һԭ -????????????????????????????????????????????????????????????????????????????????????????????????????? +Ҫ޸ijʱԭֻһ仰˵һֻһΣҪе͵εʱ򣬾Ҫֽࡣ -## 2. ????????? +## 2. ŷԭ -????????????????????????????????????????????????????????????? +ʵӦǿչ޸ĵġҲ˵չǿŵģ޸Ƿյġ -## 3. ?????I??? +## 3. 滻ԭ -????????????????????I???????????????????????? is-a ????? +һʵӦܹ滻κ䳬ʵʱ֮ž is-a ϵ -## 4. ????????? +## 4. ӿڷԭ -??????????????????????????????????????????????????????????????? +ǿûȥЩDzʹõĽӿڡ仰˵ʹöרŵĽӿڱʹõһܽӿҪá -## 5. ??????????? +## 5. ԭ -1. ??????????????????????????????????? -2. ???????????????????????????????? +1. ߲ģ鲻ӦڵͲģ飬߶Ӧڳ +2. ӦϸڣϸӦڳ -# ??????????? +# װ̳С̬ -?????????????????????????????? +װ̳С̬ԡ -## 1. ??? +## 1. װ -?????????????????????????????????????????q??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +óͽݺͻݵIJװһʹ乹һɷָĶʵ壬ݱڳ͵ڲܵڲϸڣֻһЩӿʹ֮ⲿϵû֪ڲϸڣͨöṩĽӿʸö -????????????? +װô -1. ?????????????????? +1. õķװܹϡ -2. ???????????????????? +2. ڲĽṹ޸ġ -3. ???????????????????? +3. ԶԳԱиȷĿơ -4. ???????????????? +4. Ϣʵϸڡ -???? Person ???? name??gender??age ???????????????? get() ?????????? Person ????? name ????? gender ????????????? age ????????? age ???????? work() ??????? + Person װ namegenderage ԣֻͨ get() ȡһ Person name Ժ gender ԣ޷ȡ age ԣ age ԿԹ work() ʹá -??? gender ??????? int ??????????՛????????????????????????????????????????????????????????????????????????????????? +ע⵽ gender ʹ int ͽд洢װʹûעⲻʵϸڡҪ޸ʹõʱҲڲӰͻ˴½С ```java public class Person { @@ -95,25 +95,25 @@ public class Person { } ``` -## 2. ??? +## 2. ̳ -???????? **is-a** ????????? Cat ?? Animal ??????? is-a ???????????? Cat ????? Animal???????? Animal ?? private ???????????? +̳ʵ **is-a** ϵ Cat Animal һ is-a ϵ˿Խ Cat ̳ AnimalӶ Animal private Ժͷ -Cat ??????? Animal ????????????????? Animal ???? Cat ?????????????????????? **???????**?? +Cat Ե Animal ʹãҲǿʹ Animal Cat תΪΪ **ת** -??????????????I??????????????????????I???????????????????????? is-a ????? +̳Ӧѭ滻ԭ򣺵һʵӦܹ滻κ䳬ʵʱ֮ž is-a ϵ ```java Animal animal = new Cat(); ``` -## 3. ??? +## 3. ̬ -?????????????????????????????????????????????????????????????????????????????????????????????????????? +̬Ϊʱ̬ʱ̬ʱ̬Ҫָװʱָ̬жĶָľڼȷ -???????????????1. ???2. ???????????3. ???????? +̬1. ̳У2. Ǹ෽3. ת͡ -????????????????Instrument????????????Wind ?? Percussion????????????? play() ???????????? main() ???????????? Instrument ?????? Wind ?? Percussion ?????? Instrument ??????? play() ????????????????????????????? play() ???????????? Instrument ???????? +ĴУࣨInstrumentࣺWind PercussionǶ play() main() ʹø Instrument Wind Percussion Instrument õ play() ʱִʵö play() Instrument ķ ```java public class Instrument { @@ -151,157 +151,157 @@ public class Music { # UML -## 1. ??? +## 1. ͼ -**1.1 ??????** +**1.1 ̳** -????????????: ??????generalize????????realize????????? is-a ????? +̳ʽ: generalizeʵ֣realizeΪ is-a ϵ -?? ???????(generalization) + ϵ(generalization) -?????????? +Ӿм̳ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/29badd92-109f-4f29-abb9-9857f5973928.png) -?? ?????(realize) + ʵֹϵ(realize) -??????????????? +ӳ߽ӿм̳ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4b16e1d3-3a60-472c-9756-2f31b1c48abe.png) -**1.2 ????????** +**1.2 Ͳ** -?? ?????(aggregation) + ۺϹϵ(aggregation) -??????????????????????????????????????????I?????????????????????? B ?? A ???? +ʾɲɣͲֲǿģ岻˲ֻǻڡ±ʾ B A ɣ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/34259bb8-ca3a-4872-8771-9e946782d9c3.png) -?? ?????(composition) + Ϲϵ(composition) -??????????????????????????????????I??????????????????????M?????????????????????????????????????????????????????????????????? +;ۺϲͬͲǿģ岻˲Ҳˡ繫˾Ͳţ˾û˲žͲˡǹ˾ԱھۺϹϵˣΪ˾ûԱڡ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7dda050d-ac35-4f47-9f51-18f18ed6fa9a.png) -**1.3 ?????** +**1.3 ໥ϵ** -?? ???????(association) + ϵ(association) -?????????????????????????????????????????????????????????????????????????? 1 ?? 1????? 1??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʾ֮ͬйһ־̬ϵй̵״̬޹أʼͿȷҲ 1 1 1ԶֹϵʾѧѧУһֹϵһѧУкܶѧһѧֻһѧУһֶһĹϵпʼ֮ǰͿȷ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4ccd294c-d6b2-421b-839e-d88336ff5fb7.png) -?? ???????(dependency) + ϵ(dependency) -???????????????, ???????????????????????????????????????????????????????????????????????????????? +͹ϵͬ, ϵйõġһΪĹ߷IJ롣˫ʱһֲõơ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/47ca2614-509f-476e-98fc-50ec9f9d43c0.png) -## 2. ???? +## 2. ʱͼ -**2.1 ????** +**2.1 ** -?????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʱͼ˶֮䴫Ϣʱ˳ʾΪ˳ҪͨĽעǶ󣩣ӶѰIJ -**2.2 ?????????** +**2.2 ֮սʱͼ** -??????????????????????? +ߴ±ʾʱƽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/80c5aff8-fc46-4810-aeaa-215b5c60a003.png) -???????????????????????????????????? +ɼͨʱͼ֪ÿ² ```java -publc class ???? { - public void ??(); +publc class { + public void Ӧս(); } -publc class ???? { - public void ??????(); - public void ???????(); - private void ?ڈ???(); +publc class { + public void ⶨ(); + public void Ȩ(); + private void 趫(); } -public class ???? { - public void ?????G??(); +public class { + public void G(); } -public class ??? { - public void ??????????(); +public class ŷ { + public void ؾǰ(); } -public class ??? { - public void ???????(); +public class Ȩ { + public void (); } ``` -**2.3 ???????????????** +**2.3 ͼʱͼ֮Ĺϵ** -????????????????????????? +ͼʾûĽǶ -????????????????????????????????????? +ʱͼǴӼĽǶȣĽ -**2.4 ?????????????** +**2.4 ͼʱͼĹϵ** -???????????????????????????????????? +ͼϵͳľ̬ṹʱͼϵͳĶ̬Ϊ -**2.5 ?????????** +**2.5 ʱͼ** -?? ???? + -???????????? +ֱʽ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/25b8adad-2ef6-4f30-9012-c306b4e49897.png) -???????????????????? +ڻͼʱӦѭԭ -1. ????????????????????? +1. ѽƵĶ󾡿ܵؿ£ -2. ???????????????????????????????????????????????? +2. ѳʼĶʱһߣߡ -?? ?????? + -??????????????????????????????? +ߴӶĴʼʱֹ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b7b0eac6-e7ea-4fb6-8bfb-95fec6f235e2.png) -?? ??? + Ϣ -????????????????????????????? +֮ĽʽͨϢʵֵġ -?????4??????? +Ϣ4ͣ -1\. ???????????????????? +1\. Ϣͬ첽 ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a13b62da-0fa8-4224-a615-4cadacc08871.png) -2\. ???????????????????????????????????? +2\. ͬϢϢ֮ҪͣȴӦ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/33821037-dc40-4266-901c-e5b38e618426.png) -3\. ????????????????????????? +3\. 첽ϢϢ֮Ҫȴ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/dec6c6cc-1b5f-44ed-b8fd-464fcf849dac.png) -4\. ?????????????? +4\. Ϣѡ -?? ???? + -??????????????????????????????????????? +ϵķʾ״̬ʱ䴦״̬ ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6ab5de9b-1c1e-4118-b2c3-fb6c7ed7de6f.png) -# ?????? +# ο -- Java ?????? +- Java ˼ -- [???????????SOLID???](http://www.cnblogs.com/shanyou/archive/2009/09/21/1570716.html) +- [ƵSOLIDԭ](http://www.cnblogs.com/shanyou/archive/2009/09/21/1570716.html) -- [????UML?????????](http://design-patterns.readthedocs.io/zh_CN/latest/read_uml.html#generalization) +- [UMLͼʱͼ](http://design-patterns.readthedocs.io/zh_CN/latest/read_uml.html#generalization) -- [UML?????????????????sequence diagram](http://www.cnblogs.com/wolf-sun/p/UML-Sequence-diagram.html) +- [UMLϵСʱͼ˳ͼsequence diagram](http://www.cnblogs.com/wolf-sun/p/UML-Sequence-diagram.html) -- [?????????????????------???????????](http://blog.csdn.net/jianyuerensheng/article/details/51602015) +- [------װ̳С̬](http://blog.csdn.net/jianyuerensheng/article/details/51602015) diff --git a/notes/黑客与画家.md b/notes/黑客与画家.md index ca3464f3..a50f6453 100644 --- a/notes/黑客与画家.md +++ b/notes/黑客与画家.md @@ -1,135 +1,135 @@ -* [??????](#??????) -* [?????????????](#?????????????) -* [???????](#???????) -* [????????](#????????) -* [?????](#?????) -* [???????](#???????) +* [](#) +* [ΪʲôӲܻӭ](#ΪʲôӲܻӭ) +* [ڿ뻭](#ڿ뻭) +* [˵Ļ](#˵Ļ) +* [һ·](#һ·) +* [о](#о) -# ?????? +# -????????????????????? +ߣҵ֮ -??? hack ?????????????????????????????? hacker????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʼ hack ָĽ취Щܸɵ˱Ϊ hackerڿͰص㣺桢̡̽񡣺ڿ͵ĺļֵۣšʹá˺ڿָЩרҼԱ -????????????????????????????????????????????????????????????????????????????????????????????????????????? +ںڿͳϵͳ˱ЩƻϵͳϵȻƻϵͳ˳ΪͣĺڿͲôøá -# ????????????? +# ΪʲôӲܻӭ -?????????????????????????????????????????????????????????? +Ϊӡ̡͡أӡܻ͡ӭء -??????????????????????????????????????????????????????????????????A??????????????????????????????????????????????????????????????????????????????????????????? +⣺Ȼ̸ߣôΪʲôܻӭķΪҪܻӭҪͶܴʱ侫ȥûôʱ;ܻӭǸںȤ£Ļȡ -???????????? +۸ӵԭ -- ???????????????????????????????????????????? - > 11 ?????????????????????????????????????????? ??????????????????????????????????????????????????????????????????????????? 11 ???????????????????????????????? ??????????????????????????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????????????????????????????????? +- ĺӸӲ̣ӲܻӭζűӺ۸ + > 11 ǰСɼҳӵӰޡ DzDzСѧͬѧ뷨Ǻ߲оӰ졣СѧҵԺοʼ仯 11 ң𽥰ѼͥѰˡ ͬпһµ磬ΪǸҪģȼҪʵϣڼ븸ĸͻ ǸµӣҲȷʵںǸ硣ǣڣԼһdzԭʼ硣ϻûаѶͯ״̬ ˶̵ضԴˡĥӵԭεһ֩һúܺ档һ˲֪֮ǰĥһ֡ -- ??????????? - > ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????^????????????????????????????????????????????????????????????????????????????????????????????? +- Ϊ͹Լ + > κȼƶУЩԼûŵ˾ͻͨŰеµͻԼݡѾʶΪԭеײǶԴпȺ塣ܻӭĺӲ۸ӣDzҪԼ󲿷ֵ۸ԴһȼѧЩǵм㡣 -- ?????????????????? - > ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +- Ϊ˺ܻӭ˽ͬˡ + > ûʲôһͬĵ˸ʹŽˡͺñһͣѡĹھƣΪҳһˣµ˲ĴڣҲԴһһȺһһӣӸµ۸ͻѱ˴ϵһ𹥻һˣ˾ͶԼˡΪʲôӵǿ¼йصԭһӣ㣬һȺ˵Űһ˵Űпöࡣ -????????????n??????? -> ??????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ѧУʦֻǰѽ鵱ɹ +> ѧУʦ䡣ܼҪĵǷ˶ԼӦôλáȻ÷жԣܲҪŹ˺¼Ϳˡ -???????????????????????????????????????????????????????????????? +֮ӻᱻѺöԴΪܹӰ죬ҪЩӰ졣 -????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +˰ѺѧУܿѹΪഺڵļӰ죬ʵѧУƵ⣬ѧУֻǰѺȦǵܣȻѧУڲź̡ܶ -???????????????????????????????? -> ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ȥĺӸͶᣬҲܹѧ᱾졣 +> ȥУһĽɫҵʱǰ궼ijʽѧͽijijũׯijҾϡDzᱻӵһԣԼСᡣdzĵͼԱǰƺҲ𾴳ˣΪ˶ǿüרңᴫҪѧϰļܡĴ꣬ǵļҳңԶİ칫µĹһ֪ǿѧУҵδµĹкϵ -??????????????????????????????????????????????????????????????????????????? +ѧУʹǽˣûѹȥලôʦѧ˫Ƿ¡ -?????????????????????????????????? +ûڵĶ֣ǾͻѶԷ֡ -????????????????????????????? -> ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +˱˺ܶõĶ硰˸񡱡 +> ӿܶһֱбҵ󣬲ȥѧָǣҴľֻǼѡҶõ۶֮Աǣ硰˸񡱡ֱΪ˱ЩʡЩƺͬһ˼һЩΪ߱νġ˸񡱺ֱܵ͡佱DzǴһͷţḡһԽĴţߡ˸񡱺ֱ͡ӣԸҪǡ -# ??????? +# ڿ뻭 -?????????????????????????????????? +ڿͺͻһңڽд -?????????????????????????????????????????????????????? -> ???????????????????????????????malleable????????????????????????????????????????????????????????????????????????????????????????????????????????????????static typing????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????j?????????????????????????????????????????????????????????????????????????????????????????????? +ڿͱȽϲűֿԶ̬չҿǦʻһ޸ĸġ +> ҪӦ̬չmalleable˼ģѾõijӦһ֧Ǧʣһֱ֧ʡҶѧУ̵̣ô̬ͣstatic typingһĸǣʶĺڿͣûһϲþ̬Ա̡ҪһֿͿĨĸĵԣDzΣһʢֱ͵IJ豭СԼϥϣΪһ˿ı̸Ŭѡȷƥ䣬ԼԵòܵ -????????????????????????????????? -> ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +벢ǿѧоѧҲһдһֺô롣 +> ߲ͬڿѧңһкܶô˲Ϊ̬ͷ⣬ȥһĥѧҵ⣬ǾǡѧĶʼɡѧÿһˣﶼѧұԼҾãѧԼҲһ㡣ĽǿѧԼĹŪÿȥѧѧ̫ܲӰ졣ǣԽȻѧķչԽΪһص⡣ -????????????????????????????????????????????????????????????? +ڿΪԼϲ²ҿԼ칤Լ¡ -????????????????????????????? -> ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +Ȱ̵˲ɱĻῪԼĿ +> ԳԱʱҪעҵʱдʲôΪ㲻һ£㲻ܰ㣬ҪỌ̇̄ͲɱػῪԼĿ -????????????????????????????????? -> ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ڿͲǻеļˣһλߡ +> ڿֻһʵ쵼־ļˣְǸݹ˵д룬ôʵһˮĹһģͷڵͷ˶ѡǣڿһߣµľͲǻеԵĹ߱С -# ???????? +# ˵Ļ -???????????????????????????????????????????????????????????????????????????????????? +еĵ¹ȻںǻĵģֽȥΥЩ¹˵һЩʵۣԼ鷳֮С -????????????????????????????????????????????????????????????????????????????????????????? +ԼͬԼĹ۵㣬ȷŻõ˵ͬܿǣЩ۵DZ˸Լģ˵ʲôԼʲô -??????????????????????????????????????????????????? +˵ĻȷĻΪԣijЩ˱ס -????????????????????????????????????????????????????????????????????????????? -> ??????????????????????????????????????????????????????????????]????????????? +ЩһȷԵ۱Ϊа˵dzǼϱǩĿΪ˷ɱǡ +> ðʥ顢˶ʷϳıǩıǩ˷绯塢ƻȡ -???????????????????????????????????????????????????????????????????????????????????? +ɵƳЩеȽײˣDzߵȽײ㹻űԼ棬ֲͬڵ͵Ƚײ㣬ʵȥС -????????????????????????????????????????????????????????? +й۵ĵһ׷ΪԼڲͬڶԼڲͬ -??????????????????????????????? +ѧҪй۵㣬Ӷȡͻơ -???????????????????????????????????????? -> ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +Ϊ˵ijЩøԼûDZ˵ +> ʱҪף˼ȳԸҪеһҪЩ˱ףһҪѻ˵ܿǴӴҲ޷Ե˼ˡΪȡõķ˼֮仮һȷĽߡ룬DzһҪ˵Ҿ͹ԼĬĬ˼Щ޷뷨˼һ֯Ҫ﷢һ˵񶷾ֲĵһ򣬾DzҪᵽ񶷾ֲ -???????????????????????????????????? +ȷĹ۵ʱ˵Լûá -????????????????????????????????????????????????????????????????? +ǻΪԼ˼뿪ţһޣΪʲôȷʲôǴġ -??????????????? -> ????????????????????????????????????????????????????????????????????????????????????????????????????? +ʶԼ +> ͯƣʱܻƢΪ֪Ϊʲô˽Ǹ˵״⣬޹أ˵һ䡰ûϵֻˡ -# ????? +# һ· -?????????????? +ŵ -1. ????????????????????????????????????????????????????? -2. ?????????????????????????????????????????????? -3. ???????????????????? -4. ??????????????????????????????? -5. ?????????????????????????????????????????????????? -6. ??????? bug ?????????? bug ???? -7. ?????????????????????????????????? +1. Ҫûϵͳ˲Ҫרŵļ֪ʶʹʹã +2. ҪӲԴ٣ֻҪ豸У +3. 㣬ʱؾͿʹã +4. õݶʧΪݱƶˣ +5. ڶûȫ֪¼ʹã +6. ׵ bug ֣ bug ޸ģ +7. רŵļԱΪӰȫ -bug ????????????????????????????????????????????? bug ?????????? +bug Խ췢ԽãΪڶ̣û죬 bug ķҲ졣 -?????????????????? bug?? -> ????????????????? bug ????????????????????????????????????????????????????????????????????????????????????????? bug ???????????????????? bug ?????????????????????????????????????????????????????????????????????????? bug ?????????????????????????????????????????????????????????? bug ??????????????????????????????????????????????????????????????????????????????? +߼ûѰ bug +> ΪԺ󣬴 bug Ǻ²ŻᷢĸܵӰûǸ߼ʹߣϲЩõġѶȴIJ߼ʹ߶ bug ̶ȱȽϸߣЩ bug ڿ¹ܵĹģЩ¹ҪģǾ͸ˡʵϣΪ bug ֻ࣬оһЩӵĹԺŻǣԸ߼ʹΪ bug еܵ⡣Ǵ绰ͷʱһʤߵĿǣŭӣǻǵ÷һ -?????????????????????????????????????????? bug?? +õļ֧ǣͷԱͼԱúܽҵ޸ bug -????????????????????????????????????????????????????????????????????? +һõ뷨ʵֹ뷨˾ȥʵȽ֮߸ܹô -???????????????????????????????????????????????? -> ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +Ṻûʹ˵ܹгӰ +> һĵ˾кôġ۶٣ЩûԶṺûʹõ棬㲢ûκʧʵϣ㷴׬ˣΪڶһûгӰ͸һЩûܱҵԺͻǮ -# ??????? +# о -???????????????????????? +׷Ǻãо׷¡ -????????????????????????????????????????????????????????????????????????????????? +ûҪûûܲ˽ԼҪʲôûҪûĶ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +Ϊѧо׷ȷѧҲΪö߸׶һָ鷳֤ǻѡֱӼ֤ҲΪģ˱ҲͨƲġ -?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +ʯĶƯȴѣ޷ϵĽеҲҪֵ̣һֱʯһֻǽÿôͲһֺõıԡ -????????????????????????????????????????????????????????????????????????? -> ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? +Ӧóԭ񻭻һü߻һ׼ȷȻ𲽼ӹ +> һ˼룬ҲԱΪʥĸǡģʽҪ󾡿óԭͣȻŻĹ۵ӦõȵijƷԺһ¡صгʥĸǽһ˶Ա;ϮҲûйϵڻĭʱڣҵ˾ΪģʽԻǰ̡ -???????????????????????????????????????????????????????????????????????????????????????????????????????????? +ijºܷζôĶͻܷζٵԭܹóԱиߵʿӶǸȤȥʵ֡ҲΪٷҪ