CS-Notes/notes/剑指 offer 题解.md

1811 lines
47 KiB
Markdown
Raw Normal View History

2018-02-13 23:02:10 +08:00
<!-- GFM-TOC -->
2018-02-20 10:40:05 +08:00
* [ǰ<EFBFBD><EFBFBD>](<><C7B0>)
* [<EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>֪ʶ](#<23>ڶ<EFBFBD><DAB6><EFBFBD>-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>֪ʶ)
* [2. ʵ<><CAB5> Singleton](#2-ʵ<><CAB5>-singleton)
* [3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#3-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [4. <20><>ά<EFBFBD><CEAC><EFBFBD><EFBFBD><EFBFBD>еIJ<D0B5><C4B2><EFBFBD>](#4-<2D><>ά<EFBFBD><CEAC><EFBFBD><EFBFBD><EFBFBD>еIJ<D0B5><C4B2><EFBFBD>)
* [5. <20><EFBFBD>ո<EFBFBD>](#5-<2D><EFBFBD>ո<EFBFBD>)
* [6. <20><>β<EFBFBD><CEB2>ͷ<EFBFBD><CDB7>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>](#6-<2D><>β<EFBFBD><CEB2>ͷ<EFBFBD><CDB7>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>)
* [7. <20>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#7-<2D>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [8. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#8-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [9. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջʵ<D5BB>ֶ<EFBFBD><D6B6><EFBFBD>](#9-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջʵ<D5BB>ֶ<EFBFBD><D6B6><EFBFBD>)
* [10.1 쳲<><ECB3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#101-쳲<><ECB3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [10.2 <20><>̨<EFBFBD><CCA8>](#102-<2D><>̨<EFBFBD><CCA8>)
* [10.3 <20><>̬<EFBFBD><CCAC>̨<EFBFBD><CCA8>](#103-<2D><>̬<EFBFBD><CCAC>̨<EFBFBD><CCA8>)
* [10.4 <20><><EFBFBD>θ<EFBFBD><CEB8><EFBFBD>](#104-<2D><><EFBFBD>θ<EFBFBD><CEB8><EFBFBD>)
* [11. <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>](#11-<2D><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>)
* [12. <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>·<EFBFBD><C2B7>](#12-<2D><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>·<EFBFBD><C2B7>)
* [13. <20><><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>˶<EFBFBD><CBB6><EFBFBD>Χ](#13-<2D><><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>˶<EFBFBD><CBB6><EFBFBD>Χ)
* [14. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#14-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [15. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20>ĸ<EFBFBD><C4B8><EFBFBD>](#15-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-1-<2D>ĸ<EFBFBD><C4B8><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>)
* [16. <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>η<EFBFBD>](#16-<2D><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>η<EFBFBD>)
* [18. ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8>Ľ<EFBFBD><C4BD><EFBFBD>](#18-ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8>Ľ<EFBFBD><C4BD><EFBFBD>)
* [19. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽƥ<CABD><C6A5>](#19-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽƥ<CABD><C6A5>)
* [20. <20><>ʾ<EFBFBD><CABE>ֵ<EFBFBD><D6B5><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>](#20-<2D><>ʾ<EFBFBD><CABE>ֵ<EFBFBD><D6B5><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>)
* [21. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>ż<EFBFBD><C5BC>ǰ<EFBFBD><C7B0>](#21-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>ż<EFBFBD><C5BC>ǰ<EFBFBD><C7B0>)
* [22. <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> k <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#22-<2D><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>-k-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [23. <20><><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD>](#23-<2D><><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD>)
* [24. <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>](#24-<2D><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>)
* [25. <20>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#25-<2D>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [26. <20><><EFBFBD><EFBFBD><EFBFBD>ӽṹ](#26-<2D><><EFBFBD><EFBFBD><EFBFBD>ӽṹ)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˼·](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˼·)
* [27. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>](#27-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>)
* [28.1 <20>ԳƵĶ<C6B5><C4B6><EFBFBD><EFBFBD><EFBFBD>](#281-<2D>ԳƵĶ<C6B5><C4B6><EFBFBD><EFBFBD><EFBFBD>)
* [28.2 ƽ<><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#282-ƽ<><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [29. ˳ʱ<CBB3><CAB1><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>](#29-˳ʱ<CBB3><CAB1><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>)
* [30. <20><><EFBFBD><EFBFBD> min <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ](#30-<2D><><EFBFBD><EFBFBD>-min-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ)
* [31. ջ<><D5BB>ѹ<EFBFBD><EFBFBD><EBA1A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#31-ջ<><D5BB>ѹ<EFBFBD><EFBFBD><EBB5AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [32.1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>´<EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#321-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>´<EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [32.3 <20>Ѷ<EFBFBD><D1B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>ɶ<EFBFBD><C9B6><EFBFBD>](#323--<2D>Ѷ<EFBFBD><D1B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>ɶ<EFBFBD><C9B6><EFBFBD>)
* [32.3 <20><>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#323-<2D><>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [33. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#33-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [34. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD>Ϊijһֵ<D2BB><D6B5>·<EFBFBD><C2B7>](#34-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD>Ϊijһֵ<D2BB><D6B5>·<EFBFBD><C2B7>)
* [35. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD>](#35-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD>)
* [36. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#36-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [37. <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#37-<2D><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [38. <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#38-<2D>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>Ż<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Ϳռ<CDBF>Ч<EFBFBD><D0A7>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D>Ż<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Ϳռ<CDBF>Ч<EFBFBD><D0A7>)
* [39. <20><><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#39-<2D><><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [40. <20><>С<EFBFBD><D0A1> K <20><><EFBFBD><EFBFBD>](#40-<2D><>С<EFBFBD><D0A1>-k-<2D><><EFBFBD><EFBFBD>)
* [41.1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>λ<EFBFBD><CEBB>](#411-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>λ<EFBFBD><CEBB>)
* [14.2 <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>е<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD>ַ<EFBFBD>](#142-<2D>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>е<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD>ַ<EFBFBD>)
* [42. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#42-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [43. <20><> 1 <20><> n <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD>ֵĴ<D6B5><C4B4><EFBFBD>](#43-<2D><>-1-<2D><>-n-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-1-<2D><><EFBFBD>ֵĴ<D6B5><C4B4><EFBFBD>)
* [45. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ų<EFBFBD><C5B3><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>](#45-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ų<EFBFBD><C5B3><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>)
* [49. <20><><EFBFBD><EFBFBD>](#49-<2D><><EFBFBD><EFBFBD>)
* [50. <20><>һ<EFBFBD><D2BB>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5>ַ<EFBFBD>λ<EFBFBD><CEBB>](#50-<2D><>һ<EFBFBD><D2BB>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5>ַ<EFBFBD>λ<EFBFBD><CEBB>)
* [51. <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#51-<2D><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [52. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#52-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>еĸ<D0B5><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD><EFBFBD><EFBFBD>еĸ<D0B5><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [53 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ֵĴ<D6B5><C4B4><EFBFBD>](#53-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ֵĴ<D6B5><C4B4><EFBFBD>)
* [54. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD> k <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#54-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>-k-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [55 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#55-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [56. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5><EFBFBD><EFBFBD><EFBFBD>](#56-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5><EFBFBD><EFBFBD><EFBFBD>)
* [57.1 <20><>Ϊ S <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#571-<2D><>Ϊ-s-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [57.2 <20><>Ϊ S <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#572-<2D><>Ϊ-s-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [58.1 <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>](#581-<2D><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>)
* [58.2 <20><><EFBFBD><EFBFBD>ת<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>](#582-<2D><><EFBFBD><EFBFBD>ת<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>)
* [59. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>ֵ](#59-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>ֵ)
* [61. <20>˿<EFBFBD><CBBF><EFBFBD>˳<EFBFBD><CBB3>](#61-<2D>˿<EFBFBD><CBBF><EFBFBD>˳<EFBFBD><CBB3>)
* [62. ԲȦ<D4B2><C8A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD>µ<EFBFBD><C2B5><EFBFBD>](#62-ԲȦ<D4B2><C8A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD>µ<EFBFBD><C2B5><EFBFBD>)
* [63. <20><>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#63-<2D><>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [64. <20><> 1+2+3+...+n](#64-<2D><>-1+2+3++n)
* [65. <20><><EFBFBD>üӼ<C3BC><D3BC>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>ӷ<EFBFBD>](#65-<2D><><EFBFBD>üӼ<C3BC><D3BC>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>ӷ<EFBFBD>)
* [66. <20><><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD><CBBB><EFBFBD><EFBFBD><EFBFBD>](#66-<2D><><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD><CBBB><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>԰<EFBFBD><D4B0><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>԰<EFBFBD><D4B0><EFBFBD>)
* [67. <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#67-<2D><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [68. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͹<EFBFBD><CDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#68-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͹<EFBFBD><CDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
2018-02-13 23:02:10 +08:00
<!-- GFM-TOC -->
2018-02-20 10:40:05 +08:00
# ǰ<><C7B0>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺܶ<EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> Leetcode <20>г<EFBFBD><D0B3>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һЩ<D2BB><D0A9><EFBFBD><EFBFBD>˼·<CBBC>Ͳ<EFBFBD><CDB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD>ˡ<EFBFBD><CBA1><EFBFBD><EFBFBD>Կ<EFBFBD><D4BF><EFBFBD><EFBFBD><EFBFBD>ָ Offer<65><72><EFBFBD><EFBFBD><E9B1BE><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>˼·<CBBC><C2B7><EFBFBD><EFBFBD><EFBFBD>߿<EFBFBD> [Leetcode <20><><EFBFBD><EFBFBD> ](https://github.com/CyC2018/InterviewNotes/blob/master/notes/Leetcode%20%E9%A2%98%E8%A7%A3.md)<29><><EFBFBD>Ƚ<EFBFBD><C8BD>Ƽ<EFBFBD><C6BC>ȿ<EFBFBD> Leetcode <20><><EFBFBD><EFBFBD><E2A3AC>Ϊ<EFBFBD>Ƚ<EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD>ٿ<EFBFBD><D9BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>ͺܼ<CDBA><DCBC><EFBFBD><EFBFBD>ˡ<EFBFBD>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
# <20>ڶ<EFBFBD><DAB6><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>֪ʶ
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
## 2. ʵ<><CAB5> Singleton
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
**<2A><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>˽<EFBFBD>о<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳٻ<EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺô<EFBFBD><EFBFBD>ǣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD>Ͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˽<EFBFBD>о<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD>̻߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD>ܹ<EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> if(uniqueInstance == null) <20>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9A3AC>ô<EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD> uniqueInstance ˽<>о<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
```java
public class Singleton {
private static Singleton uniqueInstance;
private Singleton() {
}
public static Singleton getUniqueInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}
```
**<2A>̲߳<DFB3><CCB2><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ**
ֻ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD> getUniqueInstance() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ø÷<C3B8><C3B7><EFBFBD>һ<EFBFBD><D2BB>ֻ<EFBFBD><D6BB>һ<EFBFBD><D2BB><EFBFBD>̷߳<DFB3><CCB7>ʣ<EFBFBD><CAA3>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD> uniqueInstance <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ֻ<EFBFBD><D6BB>һ<EFBFBD><D2BB><EFBFBD>߳̽<DFB3><CCBD><EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD>ϻ<EFBFBD><CFBB><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>˷ѡ<CBB7>
```java
public static synchronized Singleton getUniqueInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
```
**<2A>̲߳<DFB3><CCB2><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
```java
private static Singleton uniqueInstance = new Singleton();
```
**<2A>̲߳<DFB3><CCB2><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӷ<EFBFBD> getUniqueInstance() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>Ҫ<EFBFBD><D2AA> uniqueInstance = new Singleton(); <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɡ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD> uniqueInstance <20>Ƿ<EFBFBD><C7B7>Ѿ<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
```java
public class Singleton {
private volatile static Singleton uniqueInstance;
private Singleton() {
}
public static synchronized Singleton getUniqueInstance() {
if (uniqueInstance == null) {
synchronized (Singleton.class) {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
}
}
return uniqueInstance;
}
}
```
## 3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ n <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD> 0 <20><> n-1 <20>ķ<EFBFBD>Χ<EFBFBD>ڡ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ijЩ<C4B3><D0A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD>֪<EFBFBD><D6AA><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8>ġ<EFBFBD>Ҳ<EFBFBD><D2B2>֪<EFBFBD><D6AA>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD>Ρ<EFBFBD><CEA1><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD> <20><><EFBFBD><EFBFBD><E7A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBB3A4>Ϊ 7 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {2, 3, 1, 0, 2, 5, 3}<7D><><EFBFBD><EFBFBD>ô<EFBFBD><C3B4>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2<><32>
**<2A><><EFBFBD><EFBFBD>˼·**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> [0, n-1] <20><>Χ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><E2A3AC><EFBFBD>Խ<EFBFBD>ֵΪ i <20><>Ԫ<EFBFBD>طŵ<D8B7><C5B5><EFBFBD> i <20><>λ<EFBFBD><CEBB><EFBFBD>ϡ<EFBFBD>
2018-02-14 04:18:23 +08:00
```java
public boolean duplicate(int numbers[], int length, int[] duplication) {
for (int i = 0; i < length; i++) {
while (numbers[i] != i && numbers[i] != numbers[numbers[i]]) {
swap(numbers, i, numbers[i]);
}
if (numbers[i] != i && numbers[i] == numbers[numbers[i]]) {
duplication[0] = numbers[i];
return true;
}
}
return false;
}
private void swap(int[] numbers, int i, int j) {
int t = numbers[i];
numbers[i] = numbers[j];
numbers[j] = t;
}
```
2018-02-20 10:40:05 +08:00
## 4. <20><>ά<EFBFBD><CEAC><EFBFBD><EFBFBD><EFBFBD>еIJ<D0B5><C4B2><EFBFBD>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ά<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>ÿһ<EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿһ<EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD>µ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ά<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public boolean Find(int target, int [][] array) {
if (array == null || array.length == 0 || array[0].length == 0) return false;
int m = array.length, n = array[0].length;
int row = 0, col = n - 1;
while (row < m && col >= 0) {
if (target == array[row][col]) return true;
else if (target < array[row][col]) col--;
else row++;
}
return false;
}
```
2018-02-20 10:40:05 +08:00
## 5. <20><EFBFBD>ո<EFBFBD>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĿո<EFBFBD><EFBFBD><EFBFBD>ɡ<EFBFBD>%20<32><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E7A3AC><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>Ϊ We Are Happy. <20>򾭹<EFBFBD><F2BEADB9>滻֮<E6BBBB><D6AE><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>Ϊ We%20Are%20Happy<70><79>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
**<2A><>ĿҪ<C4BF><D2AA>**
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD> O(1) <20>Ŀռ<D5BC>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public String replaceSpace(StringBuffer str) {
2018-02-14 04:18:23 +08:00
int n = str.length();
for (int i = 0; i < n; i++) {
2018-02-20 10:40:05 +08:00
if (str.charAt(i) == ' ') str.append(" "); // β<><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-14 04:18:23 +08:00
}
int idxOfOriginal = n - 1;
int idxOfNew = str.length() - 1;
while (idxOfOriginal >= 0 && idxOfNew > idxOfOriginal) {
if (str.charAt(idxOfOriginal) == ' ') {
str.setCharAt(idxOfNew--, '0');
str.setCharAt(idxOfNew--, '2');
str.setCharAt(idxOfNew--, '%');
} else {
str.setCharAt(idxOfNew--, str.charAt(idxOfOriginal));
}
idxOfOriginal--;
2018-02-13 23:02:10 +08:00
}
2018-02-14 04:18:23 +08:00
return str.toString();
2018-02-13 23:02:10 +08:00
}
```
2018-02-20 10:40:05 +08:00
## 6. <20><>β<EFBFBD><CEB2>ͷ<EFBFBD><CDB7>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Collections.reverse().
2018-02-13 23:02:10 +08:00
```java
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> ret = new ArrayList<>();
while (listNode != null) {
ret.add(listNode.val);
listNode = listNode.next;
}
Collections.reverse(ret);
return ret;
}
```
2018-02-20 10:40:05 +08:00
<EFBFBD>ݹ<EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> ret = new ArrayList<>();
if(listNode != null) {
ret.addAll(printListFromTailToHead(listNode.next));
ret.add(listNode.val);
}
return ret;
}
```
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD>ʹ<EFBFBD>ÿ⺯<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD>ʹ<EFBFBD>õݹ<EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>巨Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԡ<EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
2018-02-20 10:40:05 +08:00
ListNode head = new ListNode(-1); // ͷ<><CDB7><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
ListNode cur = listNode;
while (cur != null) {
ListNode next = cur.next;
cur.next = head.next;
head.next = cur;
cur = next;
}
ArrayList<Integer> ret = new ArrayList<>();
head = head.next;
while (head != null) {
ret.add(head.val);
head = head.next;
}
return ret;
}
```
2018-02-20 10:40:05 +08:00
## 7. <20>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
return reConstructBinaryTree(pre, 0, pre.length - 1, in, 0, in.length - 1);
}
private TreeNode reConstructBinaryTree(int[] pre, int preL, int preR, int[] in, int inL, int inR) {
if(preL > preR || inL > inR) return null;
TreeNode root = new TreeNode(pre[preL]);
if (preL != preR) {
int idx = inL;
while (idx <= inR && in[idx] != root.val) idx++;
int leftTreeLen = idx - inL;
root.left = reConstructBinaryTree(pre, preL + 1, preL + leftTreeLen, in, inL, inL + leftTreeLen - 1);
root.right = reConstructBinaryTree(pre, preL + leftTreeLen + 1, preR, in, inL + leftTreeLen + 1, inR);
}
return root;
}
```
2018-02-20 10:40:05 +08:00
## 8. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҷ<EFBFBD><EFBFBD>ء<EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD>㣬ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>򸸽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>
2018-02-14 04:18:23 +08:00
```java
public TreeLinkNode GetNext(TreeLinkNode pNode) {
if (pNode == null) return null;
if (pNode.right != null) {
pNode = pNode.right;
while (pNode.left != null) pNode = pNode.left;
return pNode;
} else {
TreeLinkNode parent = pNode.next;
while (parent != null) {
if (parent.left == pNode) return parent;
pNode = pNode.next;
parent = pNode.next;
}
}
return null;
}
```
2018-02-20 10:40:05 +08:00
## 9. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջʵ<D5BB>ֶ<EFBFBD><D6B6><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
```
2018-02-20 10:40:05 +08:00
## 10.1 쳲<><ECB3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
private int[] fib = new int[40];
public Solution() {
fib[1] = 1;
fib[2] = 2;
for (int i = 2; i < fib.length; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
}
public int Fibonacci(int n) {
return fib[n];
}
```
2018-02-20 10:40:05 +08:00
## 10.2 <20><>̨<EFBFBD><CCA8>
2018-02-13 23:02:10 +08:00
```java
public int JumpFloor(int target) {
if (target == 1) return 1;
int[] dp = new int[target];
dp[0] = 1;
dp[1] = 2;
for (int i = 2; i < dp.length; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[target - 1];
}
```
2018-02-20 10:40:05 +08:00
## 10.3 <20><>̬<EFBFBD><CCAC>̨<EFBFBD><CCA8>
2018-02-13 23:02:10 +08:00
```java
public int JumpFloorII(int target) {
int[] dp = new int[target];
Arrays.fill(dp, 1);
for (int i = 1; i < target; i++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[j];
}
}
return dp[target - 1];
}
```
2018-02-20 10:40:05 +08:00
## 10.4 <20><><EFBFBD>θ<EFBFBD><CEB8><EFBFBD>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2\*1 <20><>С<EFBFBD><D0A1><EFBFBD>κ<EFBFBD><CEBA>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD>Ǹ<EFBFBD><C7B8><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE>Ρ<EFBFBD><CEA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> n <20><> 2\*1 <20><>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5>ظ<EFBFBD><D8B8><EFBFBD>һ<EFBFBD><D2BB> 2\*n <20>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>Σ<EFBFBD><CEA3>ܹ<EFBFBD><DCB9>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public int RectCover(int target) {
if (target <= 2) return target;
return RectCover(target - 1) + RectCover(target - 2);
}
```
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
## 11. <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɸ<EFBFBD>Ԫ<EFBFBD>ذᵽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĩβ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>dz<EFBFBD>֮Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ǵݼ<C7B5><DDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>СԪ<D0A1>ء<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {3, 4, 5, 1, 2} Ϊ {1, 2, 3, 4, 5} <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>СֵΪ 1<><31> NOTE<54><45><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD> 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>СΪ 0<><30><EFBFBD><EFBFBD><EBB7B5> 0<><30>
2018-02-14 04:18:23 +08:00
```java
public int minNumberInRotateArray(int[] array) {
if (array.length == 0) return 0;
for (int i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1]) return array[i + 1];
}
return 0;
}
```
2018-02-20 10:40:05 +08:00
## 12. <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>·<EFBFBD><C2B7>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԴӾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>ʼ<EFBFBD><EFBFBD>ÿһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ھ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˾<EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ijһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ø<EFBFBD><EFBFBD>ӡ<EFBFBD> <20><><EFBFBD><EFBFBD> a b c e s f c s a d e e <20><><EFBFBD><EFBFBD><EFBFBD>а<EFBFBD><D0B0><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD> "bcced" <20><>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǿ<EFBFBD><C7BE><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD> "abcb" ·<><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD> b ռ<><D5BC><EFBFBD>˾<EFBFBD><CBBE><EFBFBD><EFBFBD>еĵ<D0B5>һ<EFBFBD>еڶ<D0B5><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴν<D9B4><CEBD><EFBFBD><EFBFBD>ø<EFBFBD><C3B8>ӡ<EFBFBD>
2018-02-14 04:18:23 +08:00
```java
private int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
if (rows == 0 || cols == 0) return false;
char[][] m = new char[rows][cols];
for (int i = 0, idx = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
m[i][j] = matrix[idx++];
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (backtracking(m, rows, cols, str, new boolean[rows][cols], 0, i, j)) return true;
}
}
return false;
}
private boolean backtracking(char[][] m, int rows, int cols, char[] str, boolean[][] used, int path, int r, int c) {
if (path == str.length) return true;
if (r < 0 || r >= rows || c < 0 || c >= cols) return false;
if (m[r][c] != str[path]) return false;
if (used[r][c]) return false;
used[r][c] = true;
for (int i = 0; i < next.length; i++) {
if (backtracking(m, rows, cols, str, used, path + 1, r + next[i][0], c + next[i][1])) return true;
}
used[r][c] = false;
return false;
}
```
2018-02-20 10:40:05 +08:00
## 13. <20><><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>˶<EFBFBD><CBB6><EFBFBD>Χ
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> m <20>к<EFBFBD> n <20>еķ<D0B5><C4B7><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˴<EFBFBD><CBB4><EFBFBD><EFBFBD><EFBFBD> 0, 0 <20>ĸ<EFBFBD><C4B8>ӿ<EFBFBD>ʼ<EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>ÿһ<C3BF><D2BB>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD><D2A3>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>һ<EFBFBD>񣬵<EFBFBD><F1A3ACB5>Dz<EFBFBD><C7B2>ܽ<EFBFBD><DCBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ֮<CEBB>ʹ<EFBFBD><CDB4><EFBFBD> k <20>ĸ<EFBFBD><C4B8>ӡ<EFBFBD> <20><><EFBFBD><EFBFBD><E7A3AC> k Ϊ 18 ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD><EFBFBD><EBB7BD><EFBFBD><EFBFBD>35, 37<33><37><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 3+5+3+7 = 18<31><38><EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD><C7A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܽ<EFBFBD><DCBD><EFBFBD><EBB7BD><EFBFBD><EFBFBD>35, 38<33><38><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 3+5+3+8 = 19<31><39><EFBFBD><EFBFBD><EFBFBD>ʸû<CAB8><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD><EFB5BD><EFBFBD>ٸ<EFBFBD><D9B8><EFBFBD><EFBFBD>ӣ<EFBFBD>
2018-02-14 04:18:23 +08:00
```java
private int cnt = 0;
private int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
private int[][] digitSum;
public int movingCount(int threshold, int rows, int cols) {
initDigitSum(rows, cols);
dfs(new boolean[rows][cols], threshold, rows, cols, 0, 0);
return cnt;
}
private void dfs(boolean[][] visited, int threshold, int rows, int cols, int r, int c) {
if (r < 0 || r >= rows || c < 0 || c >= cols) return;
if (visited[r][c]) return;
visited[r][c] = true;
if (this.digitSum[r][c] > threshold) return;
this.cnt++;
for (int i = 0; i < this.next.length; i++) {
dfs(visited, threshold, rows, cols, r + next[i][0], c + next[i][1]);
}
}
private void initDigitSum(int rows, int cols) {
int[] digitSumOne = new int[Math.max(rows, cols)];
for (int i = 0; i < digitSumOne.length; i++) {
int n = i;
while (n > 0) {
digitSumOne[i] += n % 10;
n /= 10;
}
}
this.digitSum = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
this.digitSum[i][j] = digitSumOne[i] + digitSumOne[j];
}
}
}
```
2018-02-20 10:40:05 +08:00
## 14. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><EFBFBD>ɶ<EFBFBD><EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>ÿ<EFBFBD>εij<EFBFBD><EFBFBD>ȳ˻<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**<2A><><EFBFBD><EFBFBD>˼·**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܶ<EFBFBD><EFBFBD>ü<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 3 <20><><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3><EFBFBD>Ϊ 1 <20><><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><CBA3>ʹ<EFBFBD><CDB4>Ѿ<EFBFBD><D1BE>кó<D0BA><C3B3><EFBFBD>Ϊ 3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ó<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EBB3A4>Ϊ 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3><EFBFBD><EFBFBD>γ<EFBFBD><CEB3><EFBFBD>Ϊ 2 <20><><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
```java
int maxProductAfterCuttin(int length) {
if (length < 2) return 0;
if (length == 2) return 1;
if (length == 3) return 2;
int timesOf3 = length / 3;
if (length - timesOf3 * 3 == 1) timesOf3--;
int timesOf2 = (length - timesOf3 * 3) / 2;
return (int) (Math.pow(3, timesOf3)) * (int) (Math.pow(2, timesOf2));
}
```
## 15. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20>ĸ<EFBFBD><C4B8><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public int NumberOf1(int n) {
return Integer.bitCount(n);
}
```
2018-02-20 10:40:05 +08:00
n&(n-1) <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5> n <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><CDB5><EFBFBD>һλ<D2BB><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD>Ʊ<EFBFBD>ʾ 10110100<30><30><EFBFBD><EFBFBD>ȥ 1 <20>õ<EFBFBD> 10110011<31><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD> 10110000<30><30>
2018-02-13 23:02:10 +08:00
```java
public int NumberOf1(int n) {
int cnt = 0;
while (n != 0) {
cnt++;
n &= (n - 1);
}
return cnt;
}
2018-02-14 04:18:23 +08:00
2018-02-13 23:02:10 +08:00
```
2018-02-20 10:40:05 +08:00
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
## 16. <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>η<EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public double Power(double base, int exponent) {
if (exponent == 0) return 1;
if (exponent == 1) return base;
boolean isNegative = false;
if (exponent < 0) {
exponent = -exponent;
isNegative = true;
}
double pow = Power(base * base, exponent / 2);
if (exponent % 2 != 0) pow = pow * base;
return isNegative ? 1 / pow : pow;
}
```
2018-02-20 10:40:05 +08:00
## 18. ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8>Ľ<EFBFBD><C4BD><EFBFBD>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
```java
public ListNode deleteDuplication(ListNode pHead) {
if (pHead == null) return null;
if (pHead.next == null) return pHead;
if (pHead.val == pHead.next.val) {
ListNode next = pHead.next;
while (next != null && pHead.val == next.val) {
next = next.next;
}
return deleteDuplication(next);
} else {
pHead.next = deleteDuplication(pHead.next);
return pHead;
}
}
```
## 19. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽƥ<CABD><C6A5>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> '.' <20><> '\*' <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ģʽ<C4A3>е<EFBFBD><D0B5>ַ<EFBFBD> '.' <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD> '\*' <20><>ʾ<EFBFBD><CABE>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Գ<EFBFBD><D4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Σ<EFBFBD><CEA3><EFBFBD><EFBFBD><EFBFBD> 0 <20>Σ<EFBFBD><CEA3><EFBFBD> <20>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD>У<EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>ָ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD> "aaa" <20><>ģʽ "a.a" <20><> "ab\*ac\*a" ƥ<><EFBFBD><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> "aa.a" <20><> "ab\*a" <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
2018-02-14 04:18:23 +08:00
```java
public boolean match(char[] str, char[] pattern) {
int n = str.length, m = pattern.length;
boolean[][] dp = new boolean[n + 1][m + 1];
dp[0][0] = true;
for (int i = 1; i <= m; i++) {
if (pattern[i - 1] == '*') dp[0][i] = dp[0][i - 2];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (str[i - 1] == pattern[j - 1] || pattern[j - 1] == '.') dp[i][j] = dp[i - 1][j - 1];
else if (pattern[j - 1] == '*') {
if (pattern[j - 2] != str[i - 1] && pattern[j - 2] != '.') dp[i][j] = dp[i][j - 2];
else dp[i][j] = dp[i][j - 1] || dp[i][j - 2] || dp[i - 1][j];
}
}
}
return dp[n][m];
}
```
2018-02-20 10:40:05 +08:00
## 20. <20><>ʾ<EFBFBD><CABE>ֵ<EFBFBD><D6B5><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD> "+100","5e2","-123","3.1416" <20><> "-1E-16" <20><><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ֵ<EFBFBD><D6B5> <20><><EFBFBD><EFBFBD> "12e","1a3.14","1.2.3","+-5" <20><> "12e+4.3" <20><><EFBFBD><EFBFBD><EFBFBD>ǡ<EFBFBD>
2018-02-14 04:18:23 +08:00
```java
public boolean isNumeric(char[] str) {
String string = String.valueOf(str);
return string.matches("[\\+-]?[0-9]*(\\.[0-9]*)?([eE][\\+-]?[0-9]+)?");
}
```
2018-02-20 10:40:05 +08:00
## 21. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>ż<EFBFBD><C5BC>ǰ<EFBFBD><C7B0>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
ʱ<EFBFBD><EFBFBD>Ӷ<EFBFBD> : O(n<sup>2</sup>)
<EFBFBD>ռ<EFBFBD>Ӷ<EFBFBD> : O(1)
2018-02-13 23:02:10 +08:00
```java
public void reOrderArray(int[] array) {
int n = array.length;
for (int i = 0; i < n; i++) {
if (array[i] % 2 == 0) {
int nextOddIdx = i + 1;
while (nextOddIdx < n && array[nextOddIdx] % 2 == 0) nextOddIdx++;
if (nextOddIdx == n) break;
int nextOddVal = array[nextOddIdx];
for (int j = nextOddIdx; j > i; j--) {
array[j] = array[j - 1];
}
array[i] = nextOddVal;
}
}
}
```
2018-02-20 10:40:05 +08:00
ʱ<EFBFBD><EFBFBD>Ӷ<EFBFBD> : O(n)
<EFBFBD>ռ<EFBFBD>Ӷ<EFBFBD> : O(n)
```java
public void reOrderArray(int[] array) {
int oddCnt = 0;
for (int num : array) if (num % 2 == 1) oddCnt++;
int[] copy = array.clone();
int i = 0, j = oddCnt;
for (int num : copy) {
if (num % 2 == 1) array[i++] = num;
else array[j++] = num;
}
}
```
## 22. <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> k <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-14 04:18:23 +08:00
```java
public ListNode FindKthToTail(ListNode head, int k) {
if (head == null) return null;
ListNode fast, slow;
fast = slow = head;
while (fast != null && k-- > 0) fast = fast.next;
if (k > 0) return null;
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
```
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
## 23. <20><><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
2018-02-14 04:18:23 +08:00
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null) return null;
ListNode slow = pHead, fast = pHead;
while (fast != null && fast.next != null) {
fast = fast.next.next;
2018-02-13 23:02:10 +08:00
slow = slow.next;
2018-02-14 04:18:23 +08:00
if (slow == fast) {
fast = pHead;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
2018-02-13 23:02:10 +08:00
}
2018-02-14 04:18:23 +08:00
return null;
2018-02-13 23:02:10 +08:00
}
```
2018-02-20 10:40:05 +08:00
## 24. <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public ListNode ReverseList(ListNode head) {
ListNode newList = new ListNode(-1);
while (head != null) {
ListNode next = head.next;
head.next = newList.next;
newList.next = head;
head = next;
}
return newList.next;
}
```
2018-02-20 10:40:05 +08:00
## 25. <20>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public ListNode Merge(ListNode list1, ListNode list2) {
ListNode head = new ListNode(-1);
ListNode cur = head;
while (list1 != null && list2 != null) {
if (list1.val < list2.val) {
cur.next = list1;
list1 = list1.next;
} else {
cur.next = list2;
list2 = list2.next;
}
cur = cur.next;
}
if (list1 != null) cur.next = list1;
if (list2 != null) cur.next = list2;
return head.next;
}
```
2018-02-20 10:40:05 +08:00
## 26. <20><><EFBFBD><EFBFBD><EFBFBD>ӽṹ
2018-02-13 23:02:10 +08:00
```java
public boolean HasSubtree(TreeNode root1, TreeNode root2) {
if (root1 == null || root2 == null) return false;
return isSubtree(root1, root2) || HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);
}
private boolean isSubtree(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) return true;
if (root1 == null) return false;
if (root2 == null) return true;
if (root1.val != root2.val) return false;
return isSubtree(root1.left, root2.left) && isSubtree(root1.right, root2.right);
}
```
2018-02-20 10:40:05 +08:00
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˼·
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
## 27. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public void Mirror(TreeNode root) {
if (root == null) return;
TreeNode t = root.left;
root.left = root.right;
root.right = t;
Mirror(root.left);
Mirror(root.right);
}
```
2018-02-20 10:40:05 +08:00
## 28.1 <20>ԳƵĶ<C6B5><C4B6><EFBFBD><EFBFBD><EFBFBD>
2018-02-14 04:18:23 +08:00
```java
boolean isSymmetrical(TreeNode pRoot) {
if (pRoot == null) return true;
return isSymmetrical(pRoot.left, pRoot.right);
}
boolean isSymmetrical(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return true;
if (t1 == null || t2 == null) return false;
if (t1.val != t2.val) return false;
return isSymmetrical(t1.left, t2.right) && isSymmetrical(t1.right, t2.left);
}
```
2018-02-20 10:40:05 +08:00
## 28.2 ƽ<><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
```java
private boolean isBalanced = true;
public boolean IsBalanced_Solution(TreeNode root) {
height(root);
return isBalanced;
}
private int height(TreeNode root) {
if (root == null) return 0;
int left = height(root.left);
int right = height(root.right);
if (Math.abs(left - right) > 1) isBalanced = false;
return 1 + Math.max(left, right);
}
```
## 29. ˳ʱ<CBB3><CAB1><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public ArrayList<Integer> printMatrix(int[][] matrix) {
ArrayList<Integer> ret = new ArrayList<>();
int r1 = 0, r2 = matrix.length - 1, c1 = 0, c2 = matrix[0].length - 1;
while (r1 <= r2 && c1 <= c2) {
for (int i = c1; i <= c2; i++) ret.add(matrix[r1][i]);
for (int i = r1 + 1; i <= r2; i++) ret.add(matrix[i][c2]);
if (r1 != r2) for (int i = c2 - 1; i >= c1; i--) ret.add(matrix[r2][i]);
if (c1 != c2) for (int i = r2 - 1; i > r1; i--) ret.add(matrix[i][c1]);
r1++; r2--; c1++; c2--;
}
return ret;
}
```
2018-02-20 10:40:05 +08:00
## 30. <20><><EFBFBD><EFBFBD> min <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ
2018-02-13 23:02:10 +08:00
```java
private Stack<Integer> stack = new Stack<>();
private Stack<Integer> minStack = new Stack<>();
private int min = Integer.MAX_VALUE;
public void push(int node) {
stack.push(node);
if (min > node) min = node;
minStack.push(min);
}
public void pop() {
stack.pop();
minStack.pop();
min = minStack.peek();
}
public int top() {
return stack.peek();
}
public int min() {
return minStack.peek();
}
```
2018-02-20 10:40:05 +08:00
## 31. ջ<><D5BB>ѹ<EFBFBD><EFBFBD><EBA1A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public boolean IsPopOrder(int[] pushA, int[] popA) {
int n = pushA.length;
Stack<Integer> stack = new Stack<>();
for (int i = 0, j = 0; i < n; i++) {
stack.push(pushA[i]);
while (j < n && stack.peek() == popA[j]) {
stack.pop();
j++;
}
}
return stack.isEmpty();
}
```
2018-02-20 10:40:05 +08:00
## 32.1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>´<EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
ArrayList<Integer> ret = new ArrayList<>();
if (root == null) return ret;
queue.add(root);
while (!queue.isEmpty()) {
int cnt = queue.size();
for (int i = 0; i < cnt; i++) {
TreeNode t = queue.poll();
if (t.left != null) queue.add(t.left);
if (t.right != null) queue.add(t.right);
ret.add(t.val);
}
}
return ret;
}
```
2018-02-20 10:40:05 +08:00
## 32.3 <20>Ѷ<EFBFBD><D1B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>ɶ<EFBFBD><C9B6><EFBFBD>
```java
ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
if (pRoot == null) return ret;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(pRoot);
while (!queue.isEmpty()) {
int cnt = queue.size();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < cnt; i++) {
TreeNode node = queue.poll();
list.add(node.val);
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
ret.add(list);
}
return ret;
}
```
## 32.3 <20><>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
```java
public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
if (pRoot == null) return ret;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(pRoot);
boolean reverse = false;
while (!queue.isEmpty()) {
int cnt = queue.size();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < cnt; i++) {
TreeNode node = queue.poll();
list.add(node.val);
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
if (reverse) {
Collections.reverse(list);
reverse = false;
} else {
reverse = true;
}
ret.add(list);
}
return ret;
}
```
## 33. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public boolean VerifySquenceOfBST(int[] sequence) {
if (sequence.length == 0) return false;
return verify(sequence, 0, sequence.length - 1);
}
private boolean verify(int[] sequence, int start, int end) {
if (end - start <= 1) return true;
int rootVal = sequence[end];
int cutIdx = start;
while (cutIdx < end) {
if (sequence[cutIdx] > rootVal) break;
cutIdx++;
}
for (int i = cutIdx + 1; i < end; i++) {
if (sequence[i] < rootVal) return false;
}
return verify(sequence, start, cutIdx - 1) && verify(sequence, cutIdx, end - 1);
}
```
2018-02-20 10:40:05 +08:00
## 34. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD>Ϊijһֵ<D2BB><D6B5>·<EFBFBD><C2B7>
2018-02-13 23:02:10 +08:00
```java
private ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) {
dfs(root, target, 0, new ArrayList<>());
return ret;
}
private void dfs(TreeNode node, int target, int curSum, ArrayList<Integer> path) {
if (node == null) return;
curSum += node.val;
path.add(node.val);
if (curSum == target && node.left == null && node.right == null) {
ret.add(new ArrayList(path));
} else {
dfs(node.left, target, curSum, path);
dfs(node.right, target, curSum, path);
}
path.remove(path.size() - 1);
}
```
2018-02-20 10:40:05 +08:00
## 35. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>нڵ<EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>һ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ڵ㣬<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ڵ㣩<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD>ƺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> head<61><64><EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>벻Ҫ<EBB2BB><D2AA><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD>еĽڵ<C4BD><DAB5><EFBFBD><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7>ؿգ<D8BF>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD>ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƵĽڵ㡣
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f8b12555-967b-423d-a84e-bc9eff104b8b.jpg)
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ը<EFBFBD><EFBFBD>ƽڵ<EFBFBD><EFBFBD><EFBFBD> random <20><><EFBFBD>ӽ<EFBFBD><D3BD>и<EFBFBD>ֵ<EFBFBD><D6B5>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7b877a2a-8fd1-40d8-a34c-c445827300b8.jpg)
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD>
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b2b6253c-c701-4b30-aff4-bc3c713542a7.jpg)
2018-02-13 23:02:10 +08:00
```java
public RandomListNode Clone(RandomListNode pHead) {
if (pHead == null) return null;
2018-02-20 10:40:05 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>½ڵ<C2BD>
2018-02-13 23:02:10 +08:00
RandomListNode cur = pHead;
while (cur != null) {
RandomListNode node = new RandomListNode(cur.label);
node.next = cur.next;
cur.next = node;
cur = node.next;
}
2018-02-20 10:40:05 +08:00
// <20><><EFBFBD><EFBFBD> random <20><><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
cur = pHead;
while (cur != null) {
RandomListNode clone = cur.next;
if (cur.random != null) {
clone.random = cur.random.next;
}
cur = clone.next;
}
2018-02-20 10:40:05 +08:00
// <20><><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
RandomListNode pCloneHead = pHead.next;
cur = pHead;
while (cur.next != null) {
RandomListNode t = cur.next;
cur.next = t.next;
cur = t;
}
return pCloneHead;
}
```
2018-02-20 10:40:05 +08:00
## 36. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD>µĽ<EFBFBD><EFBFBD>㣬ֻ<EFBFBD>ܵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
private TreeNode pre = null;
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree == null) return null;
inOrder(pRootOfTree);
while(pRootOfTree.left != null) pRootOfTree = pRootOfTree.left;
return pRootOfTree;
}
private void inOrder(TreeNode node) {
if(node == null) return;
inOrder(node.left);
node.left = pre;
if(pre != null) pre.right = node;
pre = node;
inOrder(node.right);
}
```
2018-02-20 10:40:05 +08:00
## 37. <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-14 04:18:23 +08:00
```java
private String serizeString = "";
String Serialize(TreeNode root) {
if (root == null) return "#";
return root.val + " " + Serialize(root.left) + " "
+ Serialize(root.right);
}
TreeNode Deserialize(String str) {
this.serizeString = str;
return Deserialize();
}
private TreeNode Deserialize() {
if (this.serizeString.length() == 0) return null;
int idx = this.serizeString.indexOf(" ");
if (idx == -1) return null;
String sub = this.serizeString.substring(0, idx);
this.serizeString = this.serizeString.substring(idx + 1);
if (sub.equals("#")) {
return null;
}
int val = Integer.valueOf(sub);
TreeNode t = new TreeNode(val);
t.left = Deserialize();
t.right = Deserialize();
return t;
}
```
2018-02-20 10:40:05 +08:00
## 38. <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD> , <20><><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD> abc, <20><><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD> a, b, c <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD> abc, acb, bac, bca, cab <20><> cba<62><61>
2018-02-13 23:02:10 +08:00
```java
private ArrayList<String> ret = new ArrayList<>();
public ArrayList<String> Permutation(String str) {
if (str.length() == 0) return new ArrayList<>();
char[] chars = str.toCharArray();
Arrays.sort(chars);
backtracking(chars, new boolean[chars.length], "");
return ret;
}
private void backtracking(char[] chars, boolean[] used, String s) {
if (s.length() == chars.length) {
ret.add(s);
return;
}
for (int i = 0; i < chars.length; i++) {
if (used[i]) continue;
2018-02-20 10:40:05 +08:00
if (i != 0 && chars[i] == chars[i - 1] && !used[i - 1]) continue; // <20><>֤<EFBFBD><D6A4><EFBFBD>ظ<EFBFBD>
2018-02-13 23:02:10 +08:00
used[i] = true;
backtracking(chars, used, s + chars[i]);
used[i] = false;
}
}
```
2018-02-20 10:40:05 +08:00
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>Ż<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Ϳռ<CDBF>Ч<EFBFBD><D0A7>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
## 39. <20><><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public int MoreThanHalfNum_Solution(int[] array) {
int cnt = 1, num = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] == num) cnt++;
else cnt--;
if (cnt == 0) {
num = array[i];
cnt = 1;
}
}
cnt = 0;
for (int i = 0; i < array.length; i++) {
if (num == array[i]) cnt++;
}
return cnt > array.length / 2 ? num : 0;
}
```
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
## 40. <20><>С<EFBFBD><D0A1> K <20><><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>СΪ k <20><>С<EFBFBD><D0A1><EFBFBD>ѡ<EFBFBD>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
ʱ<EFBFBD><EFBFBD>Ӷȣ<EFBFBD>O(nlgk)
<EFBFBD>ռ<EFBFBD>Ӷȣ<EFBFBD>O(k)
2018-02-13 23:02:10 +08:00
```java
public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
if (k > input.length || k <= 0) return new ArrayList<>();
PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2 - o1);
for (int num : input) {
pq.add(num);
if (pq.size() > k) {
pq.poll();
}
}
ArrayList<Integer> ret = new ArrayList<>(pq);
return ret;
}
```
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
ʱ<EFBFBD><EFBFBD>Ӷȣ<EFBFBD>O(n)
<EFBFBD>ռ<EFBFBD>Ӷȣ<EFBFBD>O(1)
2018-02-13 23:02:10 +08:00
```java
public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
if (k > input.length || k <= 0) return new ArrayList<>();
int kthSmallest = findKthSmallest(input, k - 1);
ArrayList<Integer> ret = new ArrayList<>();
for (int num : input) {
if(num <= kthSmallest && ret.size() < k) ret.add(num);
}
return ret;
}
public int findKthSmallest(int[] nums, int k) {
int lo = 0;
int hi = nums.length - 1;
while (lo < hi) {
int j = partition(nums, lo, hi);
if (j < k) {
lo = j + 1;
} else if (j > k) {
hi = j - 1;
} else {
break;
}
}
return nums[k];
}
private int partition(int[] a, int lo, int hi) {
int i = lo;
int j = hi + 1;
while (true) {
while (i < hi && less(a[++i], a[lo])) ;
while (j > lo && less(a[lo], a[--j])) ;
if (i >= j) {
break;
}
exch(a, i, j);
}
exch(a, lo, j);
return j;
}
private void exch(int[] a, int i, int j) {
final int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
private boolean less(int v, int w) {
return v < w;
}
```
2018-02-20 10:40:05 +08:00
## 41.1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>λ<EFBFBD><CEBB>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD>εõ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD>ż<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>
```java
private PriorityQueue<Integer> maxHeap = new PriorityQueue<>((o1, o2) -> o2-o1); // ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD><DFB2><EFBFBD>
private PriorityQueue<Integer> minHeep = new PriorityQueue<>(); // ʵ<><CAB5><EFBFBD>ұ߲<D2B1><DFB2>֣<EFBFBD><D6A3>ұ߲<D2B1><DFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD><DFB2><EFBFBD>
private int cnt = 0;
public void Insert(Integer num) {
// <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD><D1B4><EFBFBD>ƽ<EFBFBD><C6BD>״̬
if(cnt % 2 == 0) {
// Ϊż<CEAA><C5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>²<EFBFBD><C2B2><EFBFBD><EBB5BD>С<EFBFBD>ѣ<EFBFBD><D1A3>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɸѡ<C9B8><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܱ<EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD>ض<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD>е<EFBFBD>Ԫ<EFBFBD><D4AA>
maxHeap.add(num);
minHeep.add(maxHeap.poll());
} else {
minHeep.add(num);
maxHeap.add(minHeep.poll());
}
cnt++;
}
public Double GetMedian() {
if(cnt % 2 == 0) {
return (maxHeap.peek() + minHeep.peek()) / 2.0;
} else {
return (double) minHeep.peek();
}
}
```
## 14.2 <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>е<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD>ַ<EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>һ<EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD> "go" ʱ<><CAB1><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5>ַ<EFBFBD><D6B7><EFBFBD> "g"<22><><EFBFBD><EFBFBD><EFBFBD>Ӹ<EFBFBD><D3B8>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>google" ʱ<><CAB1><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5>ַ<EFBFBD><D6B7><EFBFBD> "l"<22><>
```java
//Insert one char from stringstream
private int[] cnts = new int[256];
private Queue<Character> queue = new LinkedList<>();
public void Insert(char ch) {
cnts[ch]++;
queue.add(ch);
while (!queue.isEmpty() && cnts[queue.peek()] > 1) {
queue.poll();
}
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce() {
if (queue.isEmpty()) return '#';
return queue.peek();
}
```
## 42. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public int FindGreatestSumOfSubArray(int[] array) {
if(array.length == 0) return 0;
int ret = Integer.MIN_VALUE;
int sum = 0;
for(int num : array) {
if(sum <= 0) sum = num;
else sum += num;
ret = Math.max(ret, sum);
}
return ret;
}
```
2018-02-20 10:40:05 +08:00
## 43. <20><> 1 <20><> n <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD>ֵĴ<D6B5><C4B4><EFBFBD>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD><EFBFBD><EFBFBD>[Leetcode : 233. Number of Digit One](https://leetcode.com/problems/number-of-digit-one/discuss/64381/4+-lines-O(log-n)-C++JavaPython)
2018-02-13 23:02:10 +08:00
```java
public int NumberOf1Between1AndN_Solution(int n) {
int cnt = 0;
for (int m = 1; m <= n; m *= 10) {
int a = n / m, b = n % m;
cnt += (a + 8) / 10 * m + (a % 10 == 1 ? b + 1 : 0);
}
return cnt;
}
```
2018-02-20 10:40:05 +08:00
## 45. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ų<EFBFBD><C5B3><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ų<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><EFBFBD>ƴ<EFBFBD>ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {3<><33>32<33><32>321}<7D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ųɵ<C5B3><C9B5><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>Ϊ 321323<32><33>
2018-02-13 23:02:10 +08:00
```java
public String PrintMinNumber(int[] numbers) {
int n = numbers.length;
String[] nums = new String[n];
for (int i = 0; i < n; i++) nums[i] = numbers[i] + "";
Arrays.sort(nums, (s1, s2) -> (s1 + s2).compareTo(s2 + s1));
String ret = "";
for (String str : nums) ret += str;
return ret;
}
```
2018-02-20 10:40:05 +08:00
## 49. <20><><EFBFBD><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2<><32>3 <20><> 5 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ugly Number<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 6<><36>8 <20><><EFBFBD>dz<EFBFBD><C7B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 14 <20><><EFBFBD>ǣ<EFBFBD><C7A3><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 7<><37> ϰ<><CFB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󰴴<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>ĵ<EFBFBD> N <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public int GetUglyNumber_Solution(int index) {
if (index <= 6) return index;
int i2 = 0, i3 = 0, i5 = 0;
int cnt = 1;
int[] dp = new int[index];
dp[0] = 1;
while (cnt < index) {
int n2 = dp[i2] * 2, n3 = dp[i3] * 3, n5 = dp[i5] * 5;
int tmp = Math.min(n2, Math.min(n3, n5));
dp[cnt++] = tmp;
if (tmp == n2) i2++;
if (tmp == n3) i3++;
if (tmp == n5) i5++;
}
return dp[index - 1];
}
```
2018-02-20 10:40:05 +08:00
## 50. <20><>һ<EFBFBD><D2BB>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5>ַ<EFBFBD>λ<EFBFBD><CEBB>
2018-02-13 23:02:10 +08:00
```java
public int FirstNotRepeatingChar(String str) {
int[] cnts = new int[256];
for (int i = 0; i < str.length(); i++) cnts[str.charAt(i)]++;
for (int i = 0; i < str.length(); i++) if (cnts[str.charAt(i)] == 1) return i;
return -1;
}
```
2018-02-20 10:40:05 +08:00
## 51. <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
private long cnt = 0;
public int InversePairs(int[] array) {
mergeSortUp2Down(array, 0, array.length - 1);
return (int) (cnt % 1000000007);
}
private void mergeSortUp2Down(int[] a, int start, int end) {
if (end - start < 1) return;
int mid = start + (end - start) / 2;
mergeSortUp2Down(a, start, mid);
mergeSortUp2Down(a, mid + 1, end);
merge(a, start, mid, end);
}
private void merge(int[] a, int start, int mid, int end) {
int[] tmp = new int[end - start + 1];
int i = start, j = mid + 1, k = 0;
while (i <= mid || j <= end) {
if (i > mid) tmp[k] = a[j++];
else if (j > end) tmp[k] = a[i++];
else if (a[i] < a[j]) tmp[k] = a[i++];
else {
tmp[k] = a[j++];
2018-02-20 10:40:05 +08:00
this.cnt += mid - i + 1; // a[i] > a[j] <20><>˵<EFBFBD><CBB5> a[i...mid] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> a[j]
2018-02-13 23:02:10 +08:00
}
k++;
}
for (k = 0; k < tmp.length; k++) {
a[start + k] = tmp[k];
}
}
```
2018-02-20 10:40:05 +08:00
## 52. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
ListNode l1 = pHead1, l2 = pHead2;
while (l1 != l2) {
if (l1 == null) l1 = pHead2;
else l1 = l1.next;
if (l2 == null) l2 = pHead1;
else l2 = l2.next;
}
return l1;
}
```
2018-02-20 10:40:05 +08:00
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>еĸ<D0B5><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
## 53 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ֵĴ<D6B5><C4B4><EFBFBD>
2018-02-14 04:18:23 +08:00
2018-02-13 23:02:10 +08:00
```java
public int GetNumberOfK(int[] array, int k) {
int l = 0, h = array.length - 1;
while (l <= h) {
int m = l + (h - l) / 2;
if (array[m] >= k) h = m - 1;
else l = m + 1;
}
int cnt = 0;
while (l < array.length && array[l++] == k) cnt++;
return cnt;
}
```
2018-02-20 10:40:05 +08:00
## 54. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD> k <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
2018-02-14 04:18:23 +08:00
TreeNode ret;
int cnt = 0;
TreeNode KthNode(TreeNode pRoot, int k) {
inorder(pRoot, k);
return ret;
}
private void inorder(TreeNode root, int k) {
if (root == null) return;
if (cnt > k) return;
inorder(root.left, k);
cnt++;
if (cnt == k) ret = root;
inorder(root.right, k);
2018-02-13 23:02:10 +08:00
}
```
2018-02-20 10:40:05 +08:00
## 55 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
2018-02-14 04:18:23 +08:00
public int TreeDepth(TreeNode root) {
2018-02-13 23:02:10 +08:00
if (root == null) return 0;
2018-02-14 04:18:23 +08:00
return 1 + Math.max(TreeDepth(root.left), TreeDepth(root.right));
2018-02-13 23:02:10 +08:00
}
```
2018-02-20 10:40:05 +08:00
## 56. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5><EFBFBD><EFBFBD><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Σ<EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
**<2A><><EFBFBD><EFBFBD>˼·**
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>ϱض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һλ<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ͬ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
diff &= -diff <20>õ<EFBFBD><C3B5><EFBFBD> diff <20><><EFBFBD>Ҳ಻Ϊ 0 <20><>λ<EFBFBD><CEBB>Ҳ<EFBFBD><D2B2><EFBFBD>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>Ҳ಻ͬ<E0B2BB><CDAC><EFBFBD><EFBFBD>һλ<D2BB><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һλ<D2BB>Ϳ<EFBFBD><CDBF>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>ֿ<EFBFBD><D6BF><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public void FindNumsAppearOnce(int[] array, int num1[], int num2[]) {
int diff = 0;
for (int num : array) diff ^= num;
2018-02-20 10:40:05 +08:00
// <20>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>һλ
2018-02-13 23:02:10 +08:00
diff &= -diff;
for (int num : array) {
if ((num & diff) == 0) num1[0] ^= num;
else num2[0] ^= num;
}
}
```
2018-02-20 10:40:05 +08:00
## 57.1 <20><>Ϊ S <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S<><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD>ǵĺ<C7B5><C4BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S<><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵĺ͵<C4BA><CDB5><EFBFBD> S<><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij˻<C4B3><CBBB><EFBFBD>С<EFBFBD>ġ<EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public ArrayList<Integer> FindNumbersWithSum(int[] array, int sum) {
int i = 0, j = array.length - 1;
while (i < j) {
int cur = array[i] + array[j];
if (cur == sum) return new ArrayList<Integer>(Arrays.asList(array[i], array[j]));
else if (cur < sum) i++;
else j--;
}
return new ArrayList<Integer>();
}
```
2018-02-20 10:40:05 +08:00
## 57.2 <20><>Ϊ S <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD>Ϊ 100 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 18, 19, 20, 21, 22
```java
public ArrayList<ArrayList<Integer>> FindContinuousSequence(int sum) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
int start = 1, end = 2;
int mid = sum / 2;
int curSum = 3;
while (start <= mid && end < sum) {
if (curSum > sum) {
curSum -= start;
start++;
} else if (curSum < sum) {
end++;
curSum += end;
} else {
ArrayList<Integer> list = new ArrayList<>();
for (int i = start; i <= end; i++) {
list.add(i);
}
ret.add(list);
curSum -= start;
start++;
end++;
curSum += end;
}
}
return ret;
}
```
## 58.1 <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD>룺"I am a student."
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"student. a am I"
2018-02-13 23:02:10 +08:00
```java
public String ReverseSentence(String str) {
if (str.length() == 0) return str;
int n = str.length();
char[] chars = str.toCharArray();
int start = 0, end = 0;
while (end <= n) {
if (end == n || chars[end] == ' ') {
reverse(chars, start, end - 1);
start = end + 1;
}
end++;
}
reverse(chars, 0, n - 1);
return new String(chars);
}
private void reverse(char[] c, int start, int end) {
while (start < end) {
char t = c[start];
c[start] = c[end];
c[end] = t;
start++;
end--;
}
}
```
2018-02-20 10:40:05 +08:00
## 58.2 <20><><EFBFBD><EFBFBD>ת<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S<><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> K λ<><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD> S=<3D><>abcXYZdef<65><66>, Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 λ<><CEBB><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>XYZdefabc<62><63><EFBFBD><EFBFBD>
2018-02-14 04:18:23 +08:00
```java
public String LeftRotateString(String str, int n) {
if (str.length() == 0) return "";
char[] c = str.toCharArray();
reverse(c, 0, n - 1);
reverse(c, n, c.length - 1);
reverse(c, 0, c.length - 1);
return new String(c);
}
private void reverse(char[] c, int i, int j) {
while (i < j) {
char t = c[i];
c[i] = c[j];
c[j] = t;
i++;
j--;
}
}
```
2018-02-20 10:40:05 +08:00
## 59. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>ֵ
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵĴ<EFBFBD>С<EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {2, 3, 4, 2, 6, 2, 5, 1} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵĴ<DAB5>С 3<><33><EFBFBD><EFBFBD>ôһ<C3B4><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 6 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>ֱ<EFBFBD>Ϊ {4, 4, 6, 6, 6, 5}<7D><>
```java
public ArrayList<Integer> maxInWindows(int[] num, int size) {
ArrayList<Integer> ret = new ArrayList<>();
if (size > num.length || size < 1) return ret;
PriorityQueue<Integer> heap = new PriorityQueue<Integer>((o1, o2) -> o2 - o1);
for (int i = 0; i < size; i++) heap.add(num[i]);
ret.add(heap.peek());
for (int i = 1; i + size - 1 < num.length; i++) {
heap.remove(num[i - 1]);
heap.add(num[i + size - 1]);
ret.add(heap.peek());
}
return ret;
}
```
## 61. <20>˿<EFBFBD><CBBF><EFBFBD>˳<EFBFBD><CBB3>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>С<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>СΪ 0<><30><EFBFBD>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>ӡ<EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public boolean isContinuous(int[] numbers) {
if (numbers.length < 5) return false;
Arrays.sort(numbers);
int cnt = 0;
for (int num : numbers) if (num == 0) cnt++;
for (int i = cnt; i < numbers.length - 1; i++) {
if (numbers[i + 1] == numbers[i]) return false;
int cut = numbers[i + 1] - numbers[i] - 1;
if (cut > cnt) return false;
cnt -= cut;
}
return true;
}
```
2018-02-20 10:40:05 +08:00
## 62. ԲȦ<D4B2><C8A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD>µ<EFBFBD><C2B5><EFBFBD>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȧ<EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD> , <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>һ<EFBFBD><D2BB><EFBFBD><EFBFBD> m, <20>ñ<EFBFBD><C3B1><EFBFBD>Ϊ 0 <20><>С<EFBFBD><D0A1><EFBFBD>ѿ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD>κ<EFBFBD><CEBA><EFBFBD> m-1 <20><><EFBFBD>Ǹ<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>г<EFBFBD><D0B3>׸<EFBFBD> , Ȼ<><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD> , <20><><EFBFBD>Ҳ<EFBFBD><D2B2>ٻص<D9BB>Ȧ<EFBFBD><C8A6> , <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>С<EFBFBD><D0A1><EFBFBD>ѿ<EFBFBD>ʼ , <20><><EFBFBD><EFBFBD> 0...m-1 <20><><EFBFBD><EFBFBD> .... <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ .... ֱ<><D6B1>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>С<EFBFBD><D0A1><EFBFBD><EFBFBD> , <20><><EFBFBD>Բ<EFBFBD><D4B2>ñ<EFBFBD><C3B1>ݡ<EFBFBD>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
**<2A><><EFBFBD><EFBFBD>˼·**
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
Լɪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public int LastRemaining_Solution(int n, int m) {
if (n == 0) return -1;
if (n == 1) return 0;
return (LastRemaining_Solution(n - 1, m) + m) % n;
}
```
2018-02-20 10:40:05 +08:00
## 63. <20><>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
```java
public int maxProfit(int[] prices) {
int n = prices.length;
if(n == 0) return 0;
int soFarMin = prices[0];
int max = 0;
for(int i = 1; i < n; i++) {
if(soFarMin > prices[i]) soFarMin = prices[i];
else max = Math.max(max, prices[i] - soFarMin);
}
return max;
}
```
## 64. <20><> 1+2+3+...+n
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD> 1+2+3+...+n<><6E>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ó˳<C3B3><CBB3><EFBFBD><EFBFBD><EFBFBD>for<6F><72>while<6C><65>if<69><66>else<73><65>switch<63><68>case <20>ȹؼ<C8B9><D8BC>ּ<EFBFBD><D6BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>䣨A?B:C<><43>
2018-02-13 23:02:10 +08:00
```java
public int Sum_Solution(int n) {
if(n == 0) return 0;
return n + Sum_Solution(n - 1);
}
```
2018-02-20 10:40:05 +08:00
## 65. <20><><EFBFBD>üӼ<C3BC><D3BC>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>ӷ<EFBFBD>
2018-02-13 23:02:10 +08:00
2018-02-20 10:40:05 +08:00
a ^ b <20><>ʾû<CABE>п<EFBFBD><D0BF>ǽ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺͣ<C4BA>(a & b) << 1 <EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> (a & b) << 1 <EFBFBD><EFBFBD><EFBFBD>ұ߻<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> 0<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݹ飬<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ұߵ<EFBFBD> 0 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 0<EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public int Add(int num1, int num2) {
if(num2 == 0) return num1;
return Add(num1 ^ num2, (num1 & num2) << 1);
}
```
2018-02-20 10:40:05 +08:00
## 66. <20><><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD><CBBB><EFBFBD><EFBFBD><EFBFBD>
**<2A><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>**
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> A[0, 1,..., n-1], <20><EFBFBD><EBB9B9>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> B[0, 1,..., n-1], <20><><EFBFBD><EFBFBD> B <20>е<EFBFBD>Ԫ<EFBFBD><D4AA> B[i]=A[0]\*A[1]\*...\*A[i-1]\*A[i+1]\*...\*A[n-1]<5D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
public int[] multiply(int[] A) {
int n = A.length;
int[][] dp = new int[n][n];
for (int i = 0; i < n; i++) {
dp[i][i] = A[i];
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
dp[i][j] = dp[i][j - 1] * A[j];
}
}
int[] B = new int[n];
Arrays.fill(B, 1);
for (int i = 0; i < n; i++) {
if (i != 0) B[i] *= dp[0][i - 1];
if (i != n - 1) B[i] *= dp[i + 1][n - 1];
}
return B;
}
```
2018-02-20 10:40:05 +08:00
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>԰<EFBFBD><D4B0><EFBFBD>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
## 67. <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
2018-02-14 04:18:23 +08:00
public int StrToInt(String str) {
if (str.length() == 0) return 0;
char[] chars = str.toCharArray();
boolean isNegative = chars[0] == '-';
int ret = 0;
for (int i = 0; i < chars.length; i++) {
if (i == 0 && (chars[i] == '+' || chars[i] == '-')) continue;
if (chars[i] < '0' || chars[i] > '9') return 0;
ret = ret * 10 + (chars[i] - '0');
2018-02-13 23:02:10 +08:00
}
2018-02-14 04:18:23 +08:00
return isNegative ? -ret : ret;
2018-02-13 23:02:10 +08:00
}
```
2018-02-20 10:40:05 +08:00
## 68. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͹<EFBFBD><CDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-14 04:18:23 +08:00
2018-02-20 10:40:05 +08:00
<EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͹<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:02:10 +08:00
```java
2018-02-20 10:40:05 +08:00
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
if(root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
return root;
2018-02-13 23:02:10 +08:00
}
```