├── LICENSE ├── LeetCode-SQL ├── LeetCode1141.sql ├── LeetCode184.sql ├── LeetCode196.sql ├── LeetCode511.sql ├── LeetCode512.sql ├── LeetCode534.sql ├── LeetCode550.sql ├── LeetCode570.sql ├── LeetCode574.sql ├── LeetCode577.sql ├── LeetCode578.sql ├── LeetCode580.sql ├── LeetCode584.sql ├── LeetCode586.sql ├── LeetCode595.sql ├── LeetCode595_2.sql ├── LeetCode596.sql ├── LeetCode607.sql ├── LeetCode627.sql └── README.md ├── LeetCode结构 ├── ConstrucTree.java ├── ListNode.java ├── ListNodeTest.java ├── TreeNode.java └── TreeOperation.java ├── LeetCode题解 ├── Lc176.sql ├── Lc426.java ├── Lc82.java ├── Lc98.java ├── LeetCode10.java ├── LeetCode1001.java ├── LeetCode1004.java ├── LeetCode101.java ├── LeetCode1014.java ├── LeetCode102.java ├── LeetCode1020.java ├── LeetCode103.java ├── LeetCode1038.java ├── LeetCode104.java ├── LeetCode107.java ├── LeetCode1081.java ├── LeetCode11.java ├── LeetCode110.java ├── LeetCode111.java ├── LeetCode112.java ├── LeetCode113.java ├── LeetCode1137.java ├── LeetCode114.java ├── LeetCode1143.java ├── LeetCode115.java ├── LeetCode116.java ├── LeetCode117.java ├── LeetCode118.java ├── LeetCode1189.java ├── LeetCode1189_2.java ├── LeetCode1190.java ├── LeetCode12.java ├── LeetCode121.java ├── LeetCode122.java ├── LeetCode1231.java ├── LeetCode124.java ├── LeetCode125.java ├── LeetCode129.java ├── LeetCode1302.java ├── LeetCode132.java ├── LeetCode1342.java ├── LeetCode135.java ├── LeetCode136.java ├── LeetCode1380.java ├── LeetCode139.java ├── LeetCode14.java ├── LeetCode1405.java ├── LeetCode141.java ├── LeetCode142.java ├── LeetCode143.java ├── LeetCode143_2.java ├── LeetCode144.java ├── LeetCode1447.java ├── LeetCode145.java ├── LeetCode148.java ├── LeetCode15.java ├── LeetCode151.java ├── LeetCode152.java ├── LeetCode153.java ├── LeetCode1567.java ├── LeetCode16.java ├── LeetCode160.java ├── LeetCode165.java ├── LeetCode169.java ├── LeetCode172.java ├── LeetCode174.java ├── LeetCode175.sql ├── LeetCode176.sql ├── LeetCode176_2.sql ├── LeetCode177.sql ├── LeetCode178.sql ├── LeetCode1791.java ├── LeetCode17_14.java ├── LeetCode180.sql ├── LeetCode182.sql ├── LeetCode182_2.sql ├── LeetCode183.sql ├── LeetCode184.sql ├── LeetCode191.java ├── LeetCode198.java ├── LeetCode1984.java ├── LeetCode199.java ├── LeetCode1991.java ├── LeetCode20.java ├── LeetCode2006.java ├── LeetCode2016.java ├── LeetCode2043.java ├── LeetCode206.java ├── LeetCode209.java ├── LeetCode2100.java ├── LeetCode2104.java ├── LeetCode213.java ├── LeetCode222.java ├── LeetCode226.java ├── LeetCode229.java ├── LeetCode23.java ├── LeetCode230.java ├── LeetCode232.java ├── LeetCode234.java ├── LeetCode236.java ├── LeetCode23_2.java ├── LeetCode24_2.java ├── LeetCode24_3.java ├── LeetCode25.java ├── LeetCode257.java ├── LeetCode258.java ├── LeetCode26.java ├── LeetCode264.java ├── LeetCode264_2.java ├── LeetCode27.java ├── LeetCode287.java ├── LeetCode297.java ├── LeetCode297_2.java ├── LeetCode3.java ├── LeetCode30.java ├── LeetCode300.java ├── LeetCode309.java ├── LeetCode316.java ├── LeetCode322.java ├── LeetCode328.java ├── LeetCode35.java ├── LeetCode354.java ├── LeetCode377.java ├── LeetCode39.java ├── LeetCode394.java ├── LeetCode402.java ├── LeetCode415.java ├── LeetCode42.java ├── LeetCode426.java ├── LeetCode42_2.java ├── LeetCode438.java ├── LeetCode440.java ├── LeetCode445.java ├── LeetCode445_2.java ├── LeetCode45.java ├── LeetCode450.java ├── LeetCode451.java ├── LeetCode45_2.java ├── LeetCode46.java ├── LeetCode468.java ├── LeetCode47.java ├── LeetCode470.java ├── LeetCode473.java ├── LeetCode474.java ├── LeetCode486.java ├── LeetCode486_2.java ├── LeetCode498.java ├── LeetCode5.java ├── LeetCode50.java ├── LeetCode504.java ├── LeetCode509.java ├── LeetCode51.java ├── LeetCode516.java ├── LeetCode518.java ├── LeetCode521.java ├── LeetCode525.java ├── LeetCode53.java ├── LeetCode532.java ├── LeetCode534.sql ├── LeetCode540.java ├── LeetCode55.java ├── LeetCode553.java ├── LeetCode554.java ├── LeetCode572.java ├── LeetCode58.java ├── LeetCode589.java ├── LeetCode589_2.java ├── LeetCode590.java ├── LeetCode6.java ├── LeetCode6000.java ├── LeetCode6001.java ├── LeetCode61.java ├── LeetCode63.java ├── LeetCode66.java ├── LeetCode662.java ├── LeetCode673.java ├── LeetCode678.java ├── LeetCode688.java ├── LeetCode694.java ├── LeetCode695.java ├── LeetCode698.java ├── LeetCode6_2.java ├── LeetCode7.java ├── LeetCode70.java ├── LeetCode704.java ├── LeetCode71.java ├── LeetCode718.java ├── LeetCode72.java ├── LeetCode720.java ├── LeetCode735.java ├── LeetCode739.java ├── LeetCode740.java ├── LeetCode746.java ├── LeetCode76.java ├── LeetCode767.java ├── LeetCode80.java ├── LeetCode82.java ├── LeetCode82_2.java ├── LeetCode83.java ├── LeetCode838.java ├── LeetCode845.java ├── LeetCode845_2.java ├── LeetCode86.java ├── LeetCode88.java ├── LeetCode884.java ├── LeetCode9.java ├── LeetCode90.java ├── LeetCode917.java ├── LeetCode918.java ├── LeetCode92.java ├── LeetCode92_2.java ├── LeetCode94.java ├── LeetCode958.java ├── LeetCode958_2.java ├── LeetCode969.java ├── LeetCode994.java ├── Leetcode1448.java ├── Leetcode1706.java ├── Leetcode24.java ├── Leetcode537.java ├── Leetcode717.java └── Leetcode98.java ├── README.md ├── Review ├── CodingInterviews22.java ├── CodingInterviews47.java ├── Lc105.java ├── Lc116.java ├── Lc135.java ├── Lc139.java ├── Lc152.java ├── Lc17.java ├── Lc178.sql ├── Lc199.java ├── Lc200.java ├── Lc207.java ├── Lc207_2.java ├── Lc21.java ├── Lc22.java ├── Lc236.java ├── Lc287.java ├── Lc295.java ├── Lc329.java ├── Lc394.java ├── Lc402.java ├── Lc41.java ├── Lc416.java ├── Lc426.java ├── Lc45.java ├── Lc470.java ├── Lc470_.java ├── Lc474.java ├── Lc494.java ├── Lc494_2.java ├── Lc50.java ├── Lc516.java ├── Lc540.java ├── Lc55.java ├── Lc560.java ├── Lc572.java ├── Lc586.sql ├── Lc69.java ├── Lc739.java ├── Lc79.java ├── Lc8.java ├── Lc82.java ├── Lc958.java ├── LeetCode0101.java ├── LeetCode10.java ├── LeetCode100.java ├── LeetCode1004.java ├── LeetCode101.java ├── LeetCode102.java ├── LeetCode1022.java ├── LeetCode102_2.java ├── LeetCode104.java ├── LeetCode105.java ├── LeetCode10_2.java ├── LeetCode11.java ├── LeetCode110.java ├── LeetCode112.java ├── LeetCode113.java ├── LeetCode114.java ├── LeetCode1143.java ├── LeetCode115.java ├── LeetCode115_2.java ├── LeetCode116.java ├── LeetCode1219.java ├── LeetCode122.java ├── LeetCode123.java ├── LeetCode1231.java ├── LeetCode124.java ├── LeetCode125.java ├── LeetCode129.java ├── LeetCode131.java ├── LeetCode131_2.java ├── LeetCode135.java ├── LeetCode137.java ├── LeetCode138.java ├── LeetCode138_2.java ├── LeetCode139.java ├── LeetCode1414.java ├── LeetCode1414_2.java ├── LeetCode142.java ├── LeetCode143.java ├── LeetCode144.java ├── LeetCode15.java ├── LeetCode151.java ├── LeetCode152.java ├── LeetCode15_2.java ├── LeetCode16.java ├── LeetCode160.java ├── LeetCode16_2.java ├── LeetCode17.java ├── LeetCode172.java ├── LeetCode1725.java ├── LeetCode1748.java ├── LeetCode1763.java ├── LeetCode176_2.sql ├── LeetCode178.sql ├── LeetCode184.sql ├── LeetCode19.java ├── LeetCode197.sql ├── LeetCode199.java ├── LeetCode2.java ├── LeetCode200.java ├── LeetCode2000.java ├── LeetCode207.java ├── LeetCode208.java ├── LeetCode21.java ├── LeetCode213.java ├── LeetCode22.java ├── LeetCode221.java ├── LeetCode222.java ├── LeetCode222_2.java ├── LeetCode226.java ├── LeetCode229.java ├── LeetCode23.java ├── LeetCode234.java ├── LeetCode234_1.java ├── LeetCode234_2.java ├── LeetCode234_3.java ├── LeetCode235.java ├── LeetCode236.java ├── LeetCode239.java ├── LeetCode239_2.java ├── LeetCode26.java ├── LeetCode264.java ├── LeetCode27.java ├── LeetCode279.java ├── LeetCode283.java ├── LeetCode283_2.java ├── LeetCode287.java ├── LeetCode292.java ├── LeetCode295.java ├── LeetCode3.java ├── LeetCode300.java ├── LeetCode31.java ├── LeetCode316.java ├── LeetCode31_2.java ├── LeetCode32.java ├── LeetCode328.java ├── LeetCode329.java ├── LeetCode33.java ├── LeetCode34.java ├── LeetCode35.java ├── LeetCode377.java ├── LeetCode384.java ├── LeetCode39.java ├── LeetCode394.java ├── LeetCode395.java ├── LeetCode4.java ├── LeetCode40.java ├── LeetCode402.java ├── LeetCode40_2.java ├── LeetCode41.java ├── LeetCode415.java ├── LeetCode416.java ├── LeetCode42.java ├── LeetCode426.java ├── LeetCode42_2.java ├── LeetCode43.java ├── LeetCode440.java ├── LeetCode45.java ├── LeetCode450.java ├── LeetCode46.java ├── LeetCode47.java ├── LeetCode470.java ├── LeetCode473.java ├── LeetCode474.java ├── LeetCode48.java ├── LeetCode49.java ├── LeetCode494.java ├── LeetCode498.java ├── LeetCode50.java ├── LeetCode51.java ├── LeetCode516.java ├── LeetCode518.java ├── LeetCode51_2.java ├── LeetCode53.java ├── LeetCode534.sql ├── LeetCode54.java ├── LeetCode540.java ├── LeetCode543.java ├── LeetCode55.java ├── LeetCode557.java ├── LeetCode557_2.java ├── LeetCode56.java ├── LeetCode560.java ├── LeetCode572.java ├── LeetCode580.sql ├── LeetCode586.sql ├── LeetCode59.java ├── LeetCode61.java ├── LeetCode62.java ├── LeetCode63.java ├── LeetCode64.java ├── LeetCode662.java ├── LeetCode673.java ├── LeetCode69.java ├── LeetCode698.java ├── LeetCode698_2.java ├── LeetCode7.java ├── LeetCode70.java ├── LeetCode704.java ├── LeetCode72.java ├── LeetCode739.java ├── LeetCode74.java ├── LeetCode75.java ├── LeetCode75_2.java ├── LeetCode76.java ├── LeetCode767.java ├── LeetCode79.java ├── LeetCode7_2.java ├── LeetCode8.java ├── LeetCode80.java ├── LeetCode82.java ├── LeetCode83.java ├── LeetCode83_2.java ├── LeetCode83_3.java ├── LeetCode88.java ├── LeetCode8_2.java ├── LeetCode9.java ├── LeetCode912_2.java ├── LeetCode93.java ├── LeetCode94.java ├── LeetCode958.java ├── LeetCode98.java ├── LeetCode98_2.java ├── LeetCode994.java ├── LeetCode9_2.java ├── Offer10.java ├── Offer14.java ├── Offer14_2.java ├── Offer20.java ├── Offer26.java ├── Offer31.java ├── Offer33_2.java ├── Offer34.java ├── Offer35.java ├── Offer38.java ├── Offer4.java ├── Offer40.java ├── Offer44.java ├── Offer54.java ├── Offer56.java ├── Offer56_2.java ├── Offer57.java ├── Offer60.java ├── Offer62.java ├── Offer64.java ├── Offer66.java ├── OfferInterviews20.java ├── OfferInterviews54.java ├── OfferInterviews56.java ├── OfferInterviews56_2.java ├── OfferInterviews60.java ├── OfferInterviews61.java ├── README.md └── Top100 │ ├── Lc.java │ ├── Lc1.java │ ├── Lc10.java │ ├── Lc1004.java │ ├── Lc101.java │ ├── Lc102.java │ ├── Lc102_2.java │ ├── Lc102_DFS.java │ ├── Lc102_Queue.java │ ├── Lc104.java │ ├── Lc105.java │ ├── Lc11.java │ ├── Lc113.java │ ├── Lc1143.java │ ├── Lc124.java │ ├── Lc128.java │ ├── Lc131.java │ ├── Lc132.java │ ├── Lc135.java │ ├── Lc138.java │ ├── Lc139.java │ ├── Lc141.java │ ├── Lc1448.java │ ├── Lc146.java │ ├── Lc146_2.java │ ├── Lc15.java │ ├── Lc151.java │ ├── Lc160.java │ ├── Lc169.java │ ├── Lc176.java │ ├── Lc176.sql │ ├── Lc176_2.sql │ ├── Lc179.sql │ ├── Lc179_2.java │ ├── Lc184.sql │ ├── Lc189.java │ ├── Lc19.java │ ├── Lc2.java │ ├── Lc20.java │ ├── Lc200.java │ ├── Lc206.java │ ├── Lc208.java │ ├── Lc21.java │ ├── Lc21_2.java │ ├── Lc22.java │ ├── Lc221.java │ ├── Lc226.java │ ├── Lc22_2.java │ ├── Lc23.java │ ├── Lc23_2.java │ ├── Lc25.java │ ├── Lc253.java │ ├── Lc26.java │ ├── Lc264.java │ ├── Lc283.java │ ├── Lc297.java │ ├── Lc3.java │ ├── Lc31.java │ ├── Lc32.java │ ├── Lc322.java │ ├── Lc377.java │ ├── Lc39.java │ ├── Lc394.java │ ├── Lc3_2.java │ ├── Lc4.java │ ├── Lc402.java │ ├── Lc41.java │ ├── Lc415.java │ ├── Lc416.java │ ├── Lc42.java │ ├── Lc426.java │ ├── Lc43.java │ ├── Lc440.java │ ├── Lc450.java │ ├── Lc45_2.java │ ├── Lc46.java │ ├── Lc46_2.java │ ├── Lc47.java │ ├── Lc474.java │ ├── Lc48.java │ ├── Lc48_2.java │ ├── Lc49.java │ ├── Lc494.java │ ├── Lc49_2.java │ ├── Lc5.java │ ├── Lc51.java │ ├── Lc518.java │ ├── Lc53.java │ ├── Lc540.java │ ├── Lc543.java │ ├── Lc554.java │ ├── Lc580.sql │ ├── Lc586.java │ ├── Lc5_2.java │ ├── Lc69.java │ ├── Lc698.java │ ├── Lc70.java │ ├── Lc72.java │ ├── Lc75.java │ ├── Lc76.java │ ├── Lc767.java │ ├── Lc8.java │ ├── Lc82.java │ ├── Lc83.java │ ├── Lc8_2.java │ ├── Lc9.java │ ├── Lc90.java │ ├── Lc93.java │ ├── Lc98.java │ ├── Lc994.java │ ├── Lc9_2.java │ ├── Lcc23.java │ ├── Lcc92.java │ ├── Lcc92_2.java │ ├── LeetCode1.java │ ├── LeetCode1004.java │ ├── LeetCode101.java │ ├── LeetCode102.java │ ├── LeetCode102_2.java │ ├── LeetCode103.java │ ├── LeetCode105.java │ ├── LeetCode113.java │ ├── LeetCode1143.java │ ├── LeetCode121.java │ ├── LeetCode122.java │ ├── LeetCode1231.java │ ├── LeetCode124.java │ ├── LeetCode128.java │ ├── LeetCode1302.java │ ├── LeetCode135.java │ ├── LeetCode136.java │ ├── LeetCode138.java │ ├── LeetCode138_2.java │ ├── LeetCode139.java │ ├── LeetCode14.java │ ├── LeetCode142.java │ ├── LeetCode143.java │ ├── LeetCode145.java │ ├── LeetCode145_2.java │ ├── LeetCode148.java │ ├── LeetCode152.java │ ├── LeetCode160.java │ ├── LeetCode162.java │ ├── LeetCode169.java │ ├── LeetCode179.java │ ├── LeetCode184.sql │ ├── LeetCode184_2.sql │ ├── LeetCode189.java │ ├── LeetCode19.java │ ├── LeetCode197.sql │ ├── LeetCode198.java │ ├── LeetCode200.java │ ├── LeetCode207.java │ ├── LeetCode207_2.java │ ├── LeetCode208.java │ ├── LeetCode22.java │ ├── LeetCode221.java │ ├── LeetCode221_2.java │ ├── LeetCode23.java │ ├── LeetCode234.java │ ├── LeetCode234_2.java │ ├── LeetCode236.java │ ├── LeetCode239.java │ ├── LeetCode240.java │ ├── LeetCode264.java │ ├── LeetCode279.java │ ├── LeetCode283.java │ ├── LeetCode295.java │ ├── LeetCode297.java │ ├── LeetCode30.java │ ├── LeetCode300.java │ ├── LeetCode31.java │ ├── LeetCode316.java │ ├── LeetCode32.java │ ├── LeetCode322.java │ ├── LeetCode329.java │ ├── LeetCode33.java │ ├── LeetCode34.java │ ├── LeetCode377.java │ ├── LeetCode394.java │ ├── LeetCode394_2.java │ ├── LeetCode4.java │ ├── LeetCode40.java │ ├── LeetCode402.java │ ├── LeetCode40_2.java │ ├── LeetCode41.java │ ├── LeetCode415.java │ ├── LeetCode416.java │ ├── LeetCode42.java │ ├── LeetCode426.java │ ├── LeetCode42_2.java │ ├── LeetCode43.java │ ├── LeetCode440.java │ ├── LeetCode448.java │ ├── LeetCode45.java │ ├── LeetCode450.java │ ├── LeetCode450_2.java │ ├── LeetCode45_2.java │ ├── LeetCode46.java │ ├── LeetCode470.java │ ├── LeetCode474.java │ ├── LeetCode494.java │ ├── LeetCode498.java │ ├── LeetCode4_2.java │ ├── LeetCode5.java │ ├── LeetCode50.java │ ├── LeetCode516.java │ ├── LeetCode540.java │ ├── LeetCode543.java │ ├── LeetCode55.java │ ├── LeetCode550.sql │ ├── LeetCode56.java │ ├── LeetCode560.java │ ├── LeetCode560_2.java │ ├── LeetCode572_2.java │ ├── LeetCode580.sql │ ├── LeetCode586.sql │ ├── LeetCode62.java │ ├── LeetCode662.java │ ├── LeetCode673.java │ ├── LeetCode69.java │ ├── LeetCode698.java │ ├── LeetCode704.java │ ├── LeetCode72.java │ ├── LeetCode75.java │ ├── LeetCode76.java │ ├── LeetCode767.java │ ├── LeetCode8.java │ ├── LeetCode82.java │ ├── LeetCode83.java │ ├── LeetCode88.java │ ├── LeetCode88_2.java │ ├── LeetCode912.java │ ├── LeetCode93.java │ ├── LeetCode94.java │ ├── LeetCode94_2.java │ ├── LeetCode98.java │ ├── Offer14.java │ ├── Offer20.java │ ├── Offer22.java │ ├── Offer33.java │ ├── Offer38.java │ ├── Offer44.java │ ├── Offer51.java │ ├── Offer54.java │ ├── Offer54_2.java │ ├── Offer56_2.java │ ├── Offer57.java │ ├── Offer57_2.java │ ├── Offer62.java │ ├── Offer62_2.java │ ├── Offer66.java │ ├── OfferInterviews20.java │ ├── OfferInterviews33.java │ ├── OfferInterviews44.java │ ├── OfferInterviews60.java │ ├── OfferInterviews66.java │ ├── README.md │ └── Top1.java ├── Top100 ├── README.md ├── Top1.java ├── Top10.java ├── Top101.java ├── Top102.java ├── Top104.java ├── Top105.java ├── Top105_2.java ├── Top11.java ├── Top114.java ├── Top121.java ├── Top124.java ├── Top128.java ├── Top128_2.java ├── Top136.java ├── Top136_2.java ├── Top139.java ├── Top141.java ├── Top142.java ├── Top146.java ├── Top148.java ├── Top148_2.java ├── Top15.java ├── Top152_2.java ├── Top155.java ├── Top155_2.java ├── Top155_3.java ├── Top160.java ├── Top169.java ├── Top169_2.java ├── Top17.java ├── Top17_2.java ├── Top19.java ├── Top198.java ├── Top1_1.java ├── Top2.java ├── Top20.java ├── Top200.java ├── Top200_2.java ├── Top206.java ├── Top206_2.java ├── Top207.java ├── Top208.java ├── Top21.java ├── Top215.java ├── Top215_2.java ├── Top22.java ├── Top221.java ├── Top226.java ├── Top23.java ├── Top234.java ├── Top234_2.java ├── Top236.java ├── Top238.java ├── Top239.java ├── Top240.java ├── Top253.java ├── Top253_2.java ├── Top279.java ├── Top279_2.java ├── Top283.java ├── Top287.java ├── Top287_2.java ├── Top297.java ├── Top3.java ├── Top300.java ├── Top301.java ├── Top309.java ├── Top31.java ├── Top312.java ├── Top32.java ├── Top322.java ├── Top33.java ├── Top337.java ├── Top34.java ├── Top347.java ├── Top39.java ├── Top394.java ├── Top399.java ├── Top399_2.java ├── Top4.java ├── Top406.java ├── Top406_2.java ├── Top42.java ├── Top437.java ├── Top437_2.java ├── Top438.java ├── Top448.java ├── Top448_2.java ├── Top46.java ├── Top461.java ├── Top48.java ├── Top494.java ├── Top494_2.java ├── Top4_2.java ├── Top5.java ├── Top53.java ├── Top538.java ├── Top543.java ├── Top543_2.java ├── Top55.java ├── Top55_2.java ├── Top56.java ├── Top560.java ├── Top581.java ├── Top617.java ├── Top62.java ├── Top621.java ├── Top64.java ├── Top647.java ├── Top70.java ├── Top72.java ├── Top739.java ├── Top739_2.java ├── Top75.java ├── Top76_2.java ├── Top78.java ├── Top79.java ├── Top84.java ├── Top84_2.java ├── Top85.java ├── Top94.java ├── Top96.java └── Top98.java ├── TopInterview题解 ├── README.md ├── TopInterview.java ├── TopInterview1.java ├── TopInterview10.java ├── TopInterview101.java ├── TopInterview102.java ├── TopInterview103.java ├── TopInterview104.java ├── TopInterview105.java ├── TopInterview108.java ├── TopInterview11.java ├── TopInterview116.java ├── TopInterview118.java ├── TopInterview121.java ├── TopInterview121_2.java ├── TopInterview122.java ├── TopInterview123.java ├── TopInterview124.java ├── TopInterview125.java ├── TopInterview127.java ├── TopInterview127_2.java ├── TopInterview128.java ├── TopInterview13.java ├── TopInterview130.java ├── TopInterview131.java ├── TopInterview134.java ├── TopInterview134_2.java ├── TopInterview136.java ├── TopInterview138.java ├── TopInterview139.java ├── TopInterview14.java ├── TopInterview140.java ├── TopInterview141.java ├── TopInterview146.java ├── TopInterview148.java ├── TopInterview149.java ├── TopInterview14_2.java ├── TopInterview15.java ├── TopInterview150.java ├── TopInterview152.java ├── TopInterview152_2.java ├── TopInterview155.java ├── TopInterview160.java ├── TopInterview162.java ├── TopInterview163.java ├── TopInterview166.java ├── TopInterview169.java ├── TopInterview169_2.java ├── TopInterview17.java ├── TopInterview171.java ├── TopInterview172.java ├── TopInterview179.java ├── TopInterview188.java ├── TopInterview189.java ├── TopInterview189_2.java ├── TopInterview19.java ├── TopInterview190.java ├── TopInterview198.java ├── TopInterview2.java ├── TopInterview20.java ├── TopInterview200.java ├── TopInterview202.java ├── TopInterview204_2.java ├── TopInterview206.java ├── TopInterview207.java ├── TopInterview208.java ├── TopInterview21.java ├── TopInterview210.java ├── TopInterview212.java ├── TopInterview215.java ├── TopInterview217.java ├── TopInterview218.java ├── TopInterview22.java ├── TopInterview227.java ├── TopInterview227_2.java ├── TopInterview23.java ├── TopInterview230.java ├── TopInterview234.java ├── TopInterview236.java ├── TopInterview237.java ├── TopInterview238.java ├── TopInterview239.java ├── TopInterview240.java ├── TopInterview242.java ├── TopInterview242_2.java ├── TopInterview251.java ├── TopInterview253.java ├── TopInterview26.java ├── TopInterview268.java ├── TopInterview269.java ├── TopInterview277.java ├── TopInterview279.java ├── TopInterview28.java ├── TopInterview283.java ├── TopInterview285.java ├── TopInterview289.java ├── TopInterview29.java ├── TopInterview295.java ├── TopInterview3.java ├── TopInterview300.java ├── TopInterview308.java ├── TopInterview309.java ├── TopInterview315.java ├── TopInterview322.java ├── TopInterview322_2.java ├── TopInterview324.java ├── TopInterview326.java ├── TopInterview328.java ├── TopInterview329.java ├── TopInterview33.java ├── TopInterview334.java ├── TopInterview34.java ├── TopInterview340.java ├── TopInterview341.java ├── TopInterview344.java ├── TopInterview347.java ├── TopInterview348.java ├── TopInterview348_2.java ├── TopInterview350.java ├── TopInterview36.java ├── TopInterview371.java ├── TopInterview378.java ├── TopInterview38.java ├── TopInterview380.java ├── TopInterview384.java ├── TopInterview387.java ├── TopInterview395.java ├── TopInterview41.java ├── TopInterview412.java ├── TopInterview42.java ├── TopInterview44.java ├── TopInterview454.java ├── TopInterview46.java ├── TopInterview48.java ├── TopInterview49.java ├── TopInterview5.java ├── TopInterview50.java ├── TopInterview53.java ├── TopInterview53_2.java ├── TopInterview54.java ├── TopInterview55.java ├── TopInterview56.java ├── TopInterview62.java ├── TopInterview66.java ├── TopInterview69.java ├── TopInterview7.java ├── TopInterview70.java ├── TopInterview714.java ├── TopInterview73.java ├── TopInterview75.java ├── TopInterview75_2.java ├── TopInterview76.java ├── TopInterview78.java ├── TopInterview79.java ├── TopInterview7_2.java ├── TopInterview8.java ├── TopInterview84.java ├── TopInterview88.java ├── TopInterview88_2.java ├── TopInterview91.java ├── TopInterview94.java ├── TopInterview98.java └── Trie.java ├── 剑指Offer题解 ├── CodingInterviews10.java ├── CodingInterviews10_2.java ├── CodingInterviews11.java ├── CodingInterviews12.java ├── CodingInterviews13.java ├── CodingInterviews14_1.java ├── CodingInterviews14_2.java ├── CodingInterviews17.java ├── CodingInterviews18.java ├── CodingInterviews20.java ├── CodingInterviews206.java ├── CodingInterviews21.java ├── CodingInterviews22.java ├── CodingInterviews24.java ├── CodingInterviews25.java ├── CodingInterviews27.java ├── CodingInterviews28.java ├── CodingInterviews29.java ├── CodingInterviews3-1.java ├── CodingInterviews3.java ├── CodingInterviews30.java ├── CodingInterviews31.java ├── CodingInterviews32(1).java ├── CodingInterviews32.java ├── CodingInterviews32_2.java ├── CodingInterviews32_3.java ├── CodingInterviews33.java ├── CodingInterviews33_2.java ├── CodingInterviews34.java ├── CodingInterviews38.java ├── CodingInterviews4.java ├── CodingInterviews40.java ├── CodingInterviews42.java ├── CodingInterviews47.java ├── CodingInterviews49.java ├── CodingInterviews5.java ├── CodingInterviews50.java ├── CodingInterviews51.java ├── CodingInterviews52.java ├── CodingInterviews53.java ├── CodingInterviews54.java ├── CodingInterviews55.java ├── CodingInterviews55_1.java ├── CodingInterviews56.java ├── CodingInterviews56_1.java ├── CodingInterviews57.java ├── CodingInterviews58.java ├── CodingInterviews58_2.java ├── CodingInterviews6.java ├── CodingInterviews61.java ├── CodingInterviews62.java ├── CodingInterviews63.java ├── CodingInterviews64.java ├── CodingInterviews65.java ├── CodingInterviews67.java ├── CodingInterviews68_2.java ├── CodingInterviews7.java ├── CodingInterviews9.java ├── CodingInterviewsII23.java ├── CodingInterviewsII6.java ├── CodingInterviewsII7.java ├── CodingInterviewsII8.java ├── Interviews0106.java ├── Interviews0205.java ├── Interviews0208.java ├── Interviews0302.java ├── Interviews0304.java ├── Interviews2.java ├── Interviews6.java ├── Interviews7.java ├── Interviews9.java ├── Offer32_3.java ├── OfferInterviews21.java ├── OfferInterviews27.java ├── OfferInterviews42.java ├── OfferInterviews56.java ├── OfferInterviews56_2.java ├── OfferInterviews59.java ├── OfferInterviews60.java ├── OfferInterviews61.java ├── OfferInterviews61_2.java ├── OfferInterviews62.java ├── OfferInterviews64.java ├── OfferInterviews66.java ├── OfferInterviews67.java ├── OfferInterviewsII.java └── OfferInterviewsII38.java └── 各公司笔试题 ├── AlibabaInterviews1.java ├── AlibabaInterviews924.java ├── BankOfXingyeInterviews01.java ├── BeikeInterviews.java ├── HahaCloud1.java ├── JiangsuDX1.java ├── JiangsuDX2.java ├── JiangsuDX3.java ├── Kedaxunfei1.java ├── Meituan3.java ├── MihayouInterviews.java ├── PddInterviews.java ├── PddInterviews3.java ├── Tencent926_1.java ├── Trend1.java ├── Trend2.java ├── WangyiInterviews1.java ├── WangyiInterviews2.java ├── YidianInterviews1.java └── Yongyou0724.java /LeetCode-SQL/LeetCode1141.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select activity_date as day, count(distinct user_id) as active_users 3 | from Activity 4 | where datediff('2019-07-27', activity_date) >= 0 and datediff('2019-07-27', activity_date) < 30 5 | group by activity_date 6 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode184.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select b.name as Department, a.name as Employee, a.salary as Salary 3 | from Employee a 4 | left join Department b 5 | on a.departmentId = b.id 6 | where (a.departmentId, a.salary) in ( 7 | select departmentId, max(salary) 8 | from Employee 9 | group by departmentId 10 | ) 11 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode196.sql: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int nthUglyNumber(int n) { 3 | int[] res = new int[n + 1]; 4 | res[1] = 1; 5 | int n2 = 1, n3 = 1, n5 = 1; 6 | for (int index = 2; index <= n; index++) { 7 | int a = res[n2] * 2, b = res[n3] * 3, c = res[n5] * 5; 8 | int min = Math.min(Math.min(a, b), c); 9 | if (a == min) n2++; 10 | if (b == min) n3++; 11 | if (c == min) n5++; 12 | res[index] = min; 13 | } 14 | return res[n]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode511.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select player_id, min(event_date) as first_login 3 | from Activity 4 | group by player_id 5 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode512.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select player_id, device_id 3 | from Activity 4 | where (player_id, event_date) in ( 5 | select player_id, min(event_date) 6 | from Activity 7 | group by player_id 8 | ) 9 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode534.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select t1.player_id, t1.event_date, sum(t2.games_played) as games_played_so_far 3 | from Activity t1, Activity t2 4 | where t1.player_id = t2.player_id 5 | and t1.event_date >= t2.event_date 6 | group by t1.player_id, t1.event_date; 7 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode550.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select round(avg(a.event_date is not null), 2) as fraction 3 | from ( 4 | select player_id, min(event_date) as login 5 | from Activity 6 | group by player_id 7 | ) as b 8 | left join Activity a 9 | on b.player_id = a.player_id and datediff(a.event_date, b.login) = 1 10 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode570.sql: -------------------------------------------------------------------------------- 1 | select b.name as name 2 | from Employee a 3 | left join Employee b 4 | on a.managerId = b.id 5 | group by b.name 6 | having count(b.name) >= 5 7 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode574.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select name 3 | from ( 4 | select candidateId as id 5 | from Vote 6 | group by candidateId 7 | order by count(candidateId) desc 8 | limit 1 9 | ) as win, Candidate a 10 | where win.id = a.id 11 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode577.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select ta.name, tb.bonus 3 | from Employee ta 4 | left join Bonus tb 5 | on ta.empId = tb.empId 6 | where tb.bonus < 1000 or tb.bonus is null 7 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode578.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select question_id as survey_log 3 | from SurveyLog 4 | group by question_id 5 | order by sum(if(action = 'answer', 1, 0)) / sum(if(action = 'show', 1, 0)) desc, question_id asc 6 | limit 1; 7 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode580.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select a.dept_name, count(b.student_id) as student_number 3 | from Department a 4 | left join Student b 5 | on a.dept_id = b.dept_id 6 | group by dept_name 7 | order by student_number desc, dept_name asc 8 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode584.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select name 3 | from customer 4 | where referee_id <> 2 or referee_id is null 5 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode586.sql: -------------------------------------------------------------------------------- 1 | select customer_number 2 | from Orders 3 | group by customer_number 4 | order by count(*) desc 5 | limit 1 6 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode595.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select name, population, area 3 | from World 4 | where area >= 3000000 5 | or population >= 25000000 6 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode595_2.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select name, population, area 3 | from World 4 | where area >= 3000000 5 | 6 | union 7 | 8 | select name, population, area 9 | from World 10 | where population >= 25000000 11 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode596.sql: -------------------------------------------------------------------------------- 1 | select class 2 | from Courses 3 | group by class 4 | having count(distinct student) >= 5 5 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode607.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select name 3 | from SalesPerson 4 | where sales_id not in ( 5 | select ta.sales_id 6 | from Orders ta 7 | left join Company tb 8 | on ta.com_id = tb.com_id 9 | where tb.name = 'RED' 10 | ) 11 | -------------------------------------------------------------------------------- /LeetCode-SQL/LeetCode627.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | update Salary set sex = if(sex = 'm', 'f', 'm') 3 | -------------------------------------------------------------------------------- /LeetCode-SQL/README.md: -------------------------------------------------------------------------------- 1 | ## 本部分为 SQL 专题,里面的题目均来自 LeetCode 2 | * 本题集将包含会员题 3 | -------------------------------------------------------------------------------- /LeetCode结构/ListNode.java: -------------------------------------------------------------------------------- 1 | public class ListNode { 2 | int val; 3 | ListNode next; 4 | 5 | ListNode(int x) { 6 | val = x; 7 | } 8 | } -------------------------------------------------------------------------------- /LeetCode结构/TreeNode.java: -------------------------------------------------------------------------------- 1 | public class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | TreeNode(){}; 6 | TreeNode(int val){ this.val = val;} 7 | TreeNode(int val,TreeNode left,TreeNode right){ 8 | this.val = val; 9 | this.left = left; 10 | this.right = right; 11 | 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode题解/Lc176.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select ( 3 | select distinct salary 4 | from Employee 5 | order by salary desc 6 | limit 1 offset 1 7 | ) as SecondHighestSalary 8 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode1004.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestOnes(int[] nums, int k) { 3 | int len = nums.length; 4 | int zero = 0; 5 | int left = 0, right = 0, res = 0;; 6 | while (right < len) { 7 | int a = nums[right++]; 8 | if (a == 0) zero++; 9 | while (zero > k) { 10 | if (nums[left++] == 0) zero--; 11 | } 12 | res = Math.max(res, right - left); 13 | } 14 | return res; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode101.java: -------------------------------------------------------------------------------- 1 | public class LeetCode101 { 2 | public boolean isSymmetric(TreeNode root) { 3 | if (root == null){ 4 | return true; 5 | } 6 | TreeNode r = root; 7 | return onOrder(root , r); 8 | } 9 | 10 | public boolean onOrder(TreeNode root , TreeNode r){ 11 | if (root == null && r == null) 12 | return true; 13 | if (root == null || r == null) 14 | return false; 15 | return root.val == r.val && onOrder(root.left , r.right) && onOrder(root.right , r.left); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode1014.java: -------------------------------------------------------------------------------- 1 | public class LeetCode1014 { 2 | public int maxScoreSightseeingPair(int[] values) { 3 | int len = values.length; 4 | if (len == 2) 5 | return values[1] + values[0] - 1; 6 | int pre_max = values[0], res = 0; 7 | for (int i = 1; i < len; i++) { 8 | res = Math.max(res, pre_max + values[i] - i); 9 | pre_max = Math.max(pre_max, values[i] + i); 10 | } 11 | return res; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode104.java: -------------------------------------------------------------------------------- 1 | public class LeetCode104 { 2 | public int maxDepth(TreeNode root) { 3 | if (root == null) 4 | return 0; 5 | int lLen = maxDepth(root.left) , rLen = maxDepth(root.right); 6 | return Math.max(lLen , rLen) + 1; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode11.java: -------------------------------------------------------------------------------- 1 | public class LeetCode11 { 2 | public int maxArea(int[] height) { 3 | int i = 0, j = height.length - 1, res = 0; 4 | while(i < j){ 5 | res = height[i] < height[j] ? 6 | Math.max(res, (j - i) * height[i++]): 7 | Math.max(res, (j - i) * height[j--]); 8 | } 9 | return res; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode111.java: -------------------------------------------------------------------------------- 1 | public class LeetCode111 { 2 | public int minDepth(TreeNode root) { 3 | if (root == null) 4 | return 0; 5 | int lLen = minDepth(root.left) , rLen = minDepth(root.right); 6 | return (lLen == 0 || rLen == 0) ? lLen + rLen + 1 : Math.min(lLen , rLen) + 1; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode112.java: -------------------------------------------------------------------------------- 1 | public class LeetCode112 { 2 | public boolean hasPathSum(TreeNode root , int targetSum){ 3 | if (root == null){ 4 | return false; 5 | } 6 | int sum = targetSum - root.val; 7 | if (sum == 0 && root.right == null && root.left == null){ 8 | return true; 9 | } 10 | 11 | return hasPathSum(root.left,sum) || hasPathSum(root.right,sum); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode1137.java: -------------------------------------------------------------------------------- 1 | public class LeetCode1137 { 2 | public int tribonacci(int n) { 3 | int a = 0, b = 1, c = 1, m, l; 4 | if (n <= 1) 5 | return n; 6 | if (n == 2) 7 | return 1; 8 | for (int i = 2; i < n; i++) { 9 | if (a >= b){ 10 | m = c; 11 | c = b; 12 | b = m; 13 | }else { 14 | m = c; 15 | c = a; 16 | a = m; 17 | } 18 | c = a + b + c; 19 | } 20 | return c; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode122.java: -------------------------------------------------------------------------------- 1 | public class LeetCode122 { 2 | public int maxProfit(int[] prices) { 3 | int len = prices.length; 4 | if (len == 1) 5 | return 0; 6 | int[] dp = new int[len]; 7 | int res = 0; 8 | for (int i = 1; i < len; i++) { 9 | if (prices[i] > prices[i - 1]){ 10 | res += prices[i] - prices[i - 1]; 11 | } 12 | } 13 | return res; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode129.java: -------------------------------------------------------------------------------- 1 | public class LeetCode129 { 2 | int sum = 0 ; 3 | public int sumNumbers(TreeNode root) { 4 | int cur = 0; 5 | returnSum(root , cur); 6 | return sum; 7 | } 8 | 9 | public void returnSum(TreeNode root , int cur){ 10 | if (root == null){ 11 | return; 12 | } 13 | cur = cur * 10 + root.val; 14 | if (root.left == null && root.right == null){ 15 | sum += cur; 16 | } 17 | returnSum(root.left , cur); 18 | returnSum(root.right , cur); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode1342.java: -------------------------------------------------------------------------------- 1 | package Review; 2 | 3 | public class LeetCode1342 { 4 | public int numberOfSteps(int num) { 5 | int count = 0; 6 | while (num != 0){ 7 | if (num % 2 == 0) 8 | num /= 2; 9 | else 10 | num -= 1; 11 | count++; 12 | } 13 | return count; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode141.java: -------------------------------------------------------------------------------- 1 | public class LeetCode141 { 2 | public boolean hasCycle(ListNode head) { 3 | ListNode p = head,pt = head.next; 4 | if (head == null || head.next == null) 5 | return false; 6 | while ( p != pt){ 7 | if (p == null || pt == null || pt.next == null){ 8 | return false; 9 | } 10 | p = p.next; 11 | pt = pt.next.next; 12 | } 13 | return true; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode142.java: -------------------------------------------------------------------------------- 1 | public class LeetCode142 { 2 | public ListNode detectCycle(ListNode head) { 3 | if (head == null || head.next == null){ 4 | return null; 5 | } 6 | ListNode cur = head; 7 | Set set = new HashSet(); 8 | while (cur != null){ 9 | if (set.contains(cur)){ 10 | return cur; 11 | }else { 12 | set.add(cur); 13 | } 14 | cur = cur.next; 15 | } 16 | return null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode144.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class LeetCode144 { 5 | public List preorderTraversal(TreeNode root) { 6 | List resList = new ArrayList<>(); 7 | preOrder(root , resList); 8 | return resList; 9 | } 10 | public void preOrder(TreeNode root , List list){ 11 | if (root == null) 12 | return; 13 | list.add(root.val); 14 | preOrder(root.left , list); 15 | preOrder(root.right , list); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode1447.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.List; 3 | 4 | public class LeetCode1447 { 5 | public List simplifiedFractions(int n) { 6 | List res = new LinkedList<>(); 7 | for (int i = 1; i < n; i++) { 8 | for (int j = i + 1; j <= n; j++) { 9 | if (gcd(i, j) == 1){ 10 | res.add(i + "/" + j); 11 | } 12 | } 13 | } 14 | return res; 15 | } 16 | 17 | public int gcd(int a, int b){ 18 | return b == 0 ? a : gcd(b, a % b); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode145.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class LeetCode145 { 5 | public List postorderTraversal(TreeNode root) { 6 | List list = new ArrayList<>(); 7 | postOrder(root , list); 8 | return list; 9 | } 10 | public void postOrder(TreeNode root , List list){ 11 | if (root == null) 12 | return; 13 | postOrder(root.left , list); 14 | postOrder(root.right , list); 15 | list.add(root.val); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode151.java: -------------------------------------------------------------------------------- 1 | public class LeetCode151 { 2 | public String reverseWords(String s) { 3 | s = s.trim(); 4 | String res = ""; 5 | String[] newStr = s.split(" "); 6 | for (int i = newStr.length - 1; i >= 0 ; i--) { 7 | if (newStr[i].length() == 0) 8 | continue; 9 | res += newStr[i] + " "; 10 | } 11 | res = res.trim(); 12 | return res; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode153.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMin(int[] nums) { 3 | int len = nums.length; 4 | int left = 0, right = len - 1; 5 | while (left <= right) { 6 | int mid = left + (right - left) / 2; 7 | int a = nums[left]; 8 | int b = nums[right]; 9 | int c = nums[mid]; 10 | if (a > b) { 11 | if (c < b) right = mid; 12 | else left = mid + 1; 13 | } else { 14 | return a; 15 | } 16 | } 17 | return -1; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode169.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class LeetCode169 { 4 | public int majorityElement(int[] nums) { 5 | Arrays.sort(nums); 6 | return nums[nums.length / 2]; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode172.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int trailingZeroes(int n) { 3 | int count = 0; 4 | while (n > 0) { 5 | n /= 5; 6 | count += n; 7 | } 8 | return count; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode174.java: -------------------------------------------------------------------------------- 1 | public class LeetCode174 { 2 | public int maxProfit(int[] prices, int fee) { 3 | int n = prices.length; 4 | int[][] dp = new int[n][2]; 5 | dp[0][0] = 0; 6 | dp[0][1] = -prices[0]; 7 | for (int i = 1; i < n; ++i) { 8 | dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee); 9 | dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); 10 | } 11 | return dp[n - 1][0]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode175.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select firstName, lastName, city, state 3 | from Person 4 | left join Address 5 | on Person.personId = Address.personId 6 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode176.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select distinct max(salary) as SecondHighestSalary 3 | from Employee 4 | where salary < ( 5 | select distinct max(salary) 6 | from Employee 7 | ) 8 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode176_2.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select ( 3 | select distinct salary 4 | from Employee 5 | order by salary desc 6 | limit 1 offset 1 7 | ) as SecondHighestSalary 8 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode177.sql: -------------------------------------------------------------------------------- 1 | CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT 2 | BEGIN 3 | set N := N - 1; 4 | RETURN ( 5 | # Write your MySQL query statement below. 6 | select salary 7 | from Employee 8 | group by salary 9 | order by salary desc 10 | limit N, 1 11 | ); 12 | END 13 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode178.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select score, ( 3 | select count(distinct b.score) 4 | from Scores b 5 | where b.score >= a.score 6 | ) as 'rank' 7 | from Scores a 8 | order by score desc 9 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode1791.java: -------------------------------------------------------------------------------- 1 | public class LeetCode1791_2 { 2 | public int findCenter(int[][] edges) { 3 | int a = edges[0][0], b = edges[0][1]; 4 | if (a == edges[1][0] || b == edges[1][1]) 5 | return a; 6 | else 7 | return b; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode17_14.java: -------------------------------------------------------------------------------- 1 | package Review; 2 | 3 | import java.util.PriorityQueue; 4 | 5 | public class LeetCode17_14 { 6 | public int[] smallestK(int[] arr, int k) { 7 | PriorityQueue priorityQueue = new PriorityQueue<>((o1, o2) -> o1 - o2); 8 | for (int a : arr) { 9 | priorityQueue.add(a); 10 | } 11 | int[] res = new int[k]; 12 | for (int i = 0; i < k; i++) { 13 | res[i] = priorityQueue.poll(); 14 | } 15 | return res; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode180.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select distinct a.Num as ConsecutiveNums 3 | from 4 | Logs a, 5 | Logs b, 6 | Logs c 7 | where a.Id = b.Id - 1 8 | and b.Id = c.Id - 1 9 | and a.Num = b.Num 10 | and b.Num = c.Num 11 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode182.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select Email 3 | from Person 4 | group by Email 5 | having count(Email) > 1 6 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode182_2.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select Email 3 | from ( 4 | select Email, count(Email) as num 5 | from Person 6 | group by Email 7 | ) as n 8 | where n.num > 1 9 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode183.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select c.name as Customers 3 | from Customers as c 4 | left join Orders as o 5 | on c.Id = o.CustomerId 6 | where o.CustomerId is null 7 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode191.java: -------------------------------------------------------------------------------- 1 | public class LeetCode191 { 2 | public int hammingWeight(int n) { 3 | int count = 0; 4 | String a = Integer.toBinaryString(n); 5 | for (int i = 0; i < a.length(); i++) { 6 | if (a.charAt(i) == '1') 7 | count ++; 8 | } 9 | return count; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode1984.java: -------------------------------------------------------------------------------- 1 | package Review; 2 | 3 | import java.util.Arrays; 4 | 5 | public class LeetCode1984 { 6 | public int minimumDifference(int[] nums, int k) { 7 | int len = nums.length; 8 | Arrays.sort(nums); 9 | int left = 0, right = k - 1; 10 | int min = Integer.MAX_VALUE; 11 | while (right < len){ 12 | int gap = nums[right] - nums[left]; 13 | min = Math.min(min, gap); 14 | left++; 15 | right++; 16 | } 17 | return min; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode2016.java: -------------------------------------------------------------------------------- 1 | public class LeetCode2016 { 2 | public int maximumDifference(int[] nums) { 3 | int len = nums.length; 4 | int min = Integer.MAX_VALUE, max = -1; 5 | for (int a : nums) { 6 | min = Math.min(min, a); 7 | if (a > min) 8 | max = Math.max(a - min, max); 9 | } 10 | return max; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode2104.java: -------------------------------------------------------------------------------- 1 | public class LeetCode2104 { 2 | public long subArrayRanges(int[] nums) { 3 | int n = nums.length; 4 | long ans = 0; 5 | for (int i = 0; i < n; i++) { 6 | int min = nums[i], max = nums[i]; 7 | for (int j = i + 1; j < n; j++) { 8 | min = Math.min(min, nums[j]); 9 | max = Math.max(max, nums[j]); 10 | ans += max - min; 11 | } 12 | } 13 | return ans; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode222.java: -------------------------------------------------------------------------------- 1 | public class LeetCode222 { 2 | 3 | int count = 0; 4 | 5 | public int countNodes(TreeNode root) { 6 | if (root == null) 7 | return 0; 8 | preOrder(root); 9 | return count; 10 | } 11 | 12 | public void preOrder(TreeNode root){ 13 | if (root == null){ 14 | return; 15 | } 16 | count ++; 17 | preOrder(root.left); 18 | preOrder(root.right); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode226.java: -------------------------------------------------------------------------------- 1 | public class LeetCode226 { 2 | public TreeNode invertTree(TreeNode root) { 3 | postTrav(root); 4 | return root; 5 | } 6 | 7 | public void postTrav(TreeNode root){ 8 | if (root == null) 9 | return; 10 | if (root.left == null && root.right == null){ 11 | return; 12 | } 13 | postTrav(root.left); 14 | postTrav(root.right); 15 | TreeNode p = root.right; 16 | root.right = root.left; 17 | root.left = p; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode234.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(ListNode head) { 3 | ListNode p = head; 4 | int count = 0 , i , j , k; 5 | List list = new ArrayList(); 6 | while (p != null){ 7 | count++; 8 | list.add(p.val); 9 | p = p.next; 10 | } 11 | for (i = 0 , j = count-1 ; i <= j ; i++ , j--){ 12 | if (list.get(i) != list.get(j)) 13 | return false; 14 | } 15 | return true; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode24_3.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode swapPairs(ListNode head) { 13 | if (head == null || head.next == null) return head; 14 | ListNode p = head.next; 15 | head.next = swapPairs(p.next); 16 | p.next = head; 17 | return p; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode258.java: -------------------------------------------------------------------------------- 1 | public class LeetCode258 { 2 | public int addDigits(int num) { 3 | int res = 0; 4 | while (num > 0){ 5 | res += num % 10; 6 | num /= 10; 7 | if (num <= 0){ 8 | if (res < 10) 9 | return res; 10 | num = res; 11 | res = 0; 12 | } 13 | } 14 | return res; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode26.java: -------------------------------------------------------------------------------- 1 | public class LeetCode26 { 2 | public int removeDuplicates(int[] nums) { 3 | int i, j = 0, n = nums.length; 4 | for (i = 0; i < n; i++) { 5 | if (nums[i] != nums[j]){ 6 | nums[++j] = nums[i]; 7 | } 8 | } 9 | return j + 1; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode264_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int nthUglyNumber(int n) { 3 | int[] res = new int[n + 1]; 4 | res[1] = 1; 5 | for (int idx = 2, i2 = 1, i3 = 1, i5 = 1; idx <= n; idx++) { 6 | int a = res[i2] * 2, b = res[i3] * 3, c = res[i5] * 5; 7 | int min = Math.min(a, Math.min(b, c)); 8 | if (a == min) i2++; 9 | if (b == min) i3++; 10 | if (c == min) i5++; 11 | res[idx] = min; 12 | } 13 | return res[n]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode27.java: -------------------------------------------------------------------------------- 1 | public class LeetCode27 { 2 | public int removeElement(int[] nums, int val) { 3 | int left = 0, right = 0,len = nums.length; 4 | while (right < len){ 5 | if (nums[right] == val){ 6 | right++; 7 | }else { 8 | nums[left] = nums[right]; 9 | left++; 10 | right++; 11 | } 12 | 13 | } 14 | return left; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode287.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findDuplicate(int[] nums) { 3 | int len = nums.length, fast = 0, slow = 0; 4 | while (true) { 5 | fast = nums[nums[fast]]; 6 | slow = nums[slow]; 7 | if (fast == slow) { 8 | slow = 0; 9 | while (nums[fast] != nums[slow]) { 10 | fast = nums[fast]; 11 | slow = nums[slow]; 12 | } 13 | return nums[slow]; 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode3.java: -------------------------------------------------------------------------------- 1 | public int lengthOfLongestSubstring(String s) { 2 | int[] record = new int[128]; 3 | Arrays.fill(record , -1); 4 | int fast = 0 , slow = 0 , max = 0; 5 | char ch; 6 | for (int i = 0; i < s.length(); i++) { 7 | ch = s.charAt(i); 8 | if (record[ch] > -1){ 9 | slow = Math.max(slow , record[ch] + 1); 10 | } 11 | record[ch] = fast ++; 12 | max = Math.max(max , fast - slow); 13 | } 14 | return max; 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode322.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int coinChange(int[] coins, int amount) { 3 | int[] dp = new int[amount + 1]; 4 | Arrays.fill(dp, amount + 1); 5 | dp[0] = 0; 6 | for (int coin : coins) { 7 | for (int i = coin; i <= amount; i++) { 8 | dp[i] = Math.min(dp[i], dp[i - coin] + 1); 9 | } 10 | } 11 | return dp[amount] > amount ? -1 : dp[amount]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode35.java: -------------------------------------------------------------------------------- 1 | public class LeetCode35 { 2 | public int searchInsert(int[] nums, int target) { 3 | for(int i = 0; i < nums.length;i++){ 4 | if(nums[i] >= target){ 5 | return i; 6 | } 7 | } 8 | return nums.length; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode377.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int combinationSum4(int[] nums, int target) { 3 | int[] dp = new int[target + 1]; 4 | dp[0] = 1; 5 | for (int i = 1; i <= target; i++) { 6 | for (int a : nums) { 7 | if (i >= a) dp[i] += dp[i - a]; 8 | } 9 | } 10 | return dp[target]; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode415.java: -------------------------------------------------------------------------------- 1 | public class LeetCode415 { 2 | public String addStrings(String num1, String num2) { 3 | StringBuilder sb = new StringBuilder(); 4 | int temp = 0, m = num1.length() - 1, n = num2.length() - 1; 5 | while (m >= 0 || n >= 0 || temp != 0){ 6 | if (m >= 0) 7 | temp += num1.charAt(m--) - '0'; 8 | if (n >= 0) 9 | temp += num2.charAt(n--) - '0'; 10 | sb.append(temp % 10); 11 | temp /= 10; 12 | } 13 | return sb.reverse().toString(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode426.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | Node pre, head; 3 | public Node treeToDoublyList(Node root) { 4 | if (root == null) return null; 5 | dfs(root); 6 | head.left = pre; 7 | pre.right = head; 8 | return head; 9 | } 10 | 11 | private void dfs(Node cur) { 12 | if (cur == null) return; 13 | dfs(cur.left); 14 | if (pre != null) pre.right = cur; 15 | else head = cur; 16 | cur.left = pre; 17 | pre = cur; 18 | dfs(cur.right); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode45.java: -------------------------------------------------------------------------------- 1 | public class LeetCode45 { 2 | public int jump(int[] nums) { 3 | int end = 0, maxPosition = 0, steps = 0, len = nums.length; 4 | for (int i = 0; i < len - 1; i++) { 5 | maxPosition = Math.max(maxPosition, nums[i] + i); 6 | if (i == end){ 7 | end = maxPosition; 8 | steps ++; 9 | } 10 | } 11 | return steps; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode45_2.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class LeetCode45_2 { 4 | public int jump(int[] nums) { 5 | int[] dp = new int[nums.length]; 6 | Arrays.fill(dp, 100000); 7 | dp[0] = 0; 8 | for (int i = 0; i < nums.length; i++) { 9 | for (int j = i + 1; j <= i + nums[i] && j < nums.length; j++) { 10 | dp[j] = Math.min(dp[i] + 1, dp[j]); 11 | } 12 | } 13 | return dp[nums.length - 1]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode470.java: -------------------------------------------------------------------------------- 1 | /** 2 | * The rand7() API is already defined in the parent class SolBase. 3 | * public int rand7(); 4 | * @return a random integer in the range 1 to 7 5 | */ 6 | class Solution extends SolBase { 7 | public int rand10() { 8 | while (true) { 9 | int res = (rand7() - 1) * 7 + rand7(); 10 | if (res <= 40) return res % 10 + 1; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode486_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean PredictTheWinner(int[] nums) { 3 | int len = nums.length; 4 | int[][] dp = new int[len][len]; 5 | for (int i = 0; i < len; i++) { 6 | dp[i][i] = nums[i]; 7 | } 8 | for (int i = len - 2; i >= 0; i--) { 9 | for (int j = i + 1; j < len; j++) { 10 | dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]); 11 | } 12 | } 13 | return dp[0][len - 1] >= 0; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode50.java: -------------------------------------------------------------------------------- 1 | public class LeetCode50 { 2 | public double myPow(double x, int n) { 3 | return n >= 0 ? quickMul(x, n) : 1.0 / quickMul(x, n); 4 | } 5 | 6 | private double quickMul(double x, long n) { 7 | if (n == 0) 8 | return 1.0; 9 | double y = quickMul(x, n / 2); 10 | return n % 2 == 0 ? y * y : y * y * x; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode509.java: -------------------------------------------------------------------------------- 1 | public class LeetCode509 { 2 | public int fib(int n) { 3 | int i = 1, j = 1, m; 4 | if (n <= 1) 5 | return n; 6 | if ( n == 2) 7 | return 1; 8 | for (int k = 2; k < n; k++) { 9 | j = i + j; 10 | m = j; 11 | j = i; 12 | i = m; 13 | } 14 | return i; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode518.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int change(int amount, int[] coins) { 3 | int[] dp = new int[amount + 1]; 4 | dp[0] = 1; 5 | for (int coin : coins) { 6 | for (int i = coin; i < amount + 1; i++) { 7 | dp[i] += dp[i - coin]; 8 | } 9 | } 10 | return dp[amount]; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode521.java: -------------------------------------------------------------------------------- 1 | public class LeetCode521 { 2 | public int findLUSlength(String a, String b) { 3 | if (a.equals(b)) 4 | return -1; 5 | if (a.length() > b.length()) 6 | return a.length(); 7 | return b.length(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode53.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class LeetCode53 { 4 | public int maxSubArray(int[] nums) { 5 | int len = nums.length; 6 | if (len == 0) 7 | return nums[0]; 8 | int[] dp = new int[len]; 9 | dp[0] = nums[0]; 10 | for (int i = 1; i < len; i++) { 11 | dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]); 12 | } 13 | return Arrays.stream(dp).max().getAsInt(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode534.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select a.player_id, a.event_date, sum(b.games_played) as games_played_so_far 3 | from Activity a, Activity b 4 | where a.player_id = b.player_id 5 | and a.event_date >= b.event_date 6 | group by a.player_id, a.event_date 7 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode58.java: -------------------------------------------------------------------------------- 1 | public class LeetCode58 { 2 | public int lengthOfLastWord(String s) { 3 | int end = s.length() - 1; 4 | while(end >= 0 && s.charAt(end) == ' ') end--; 5 | if(end < 0) return 0; 6 | int start = end; 7 | while(start >= 0 && s.charAt(start) != ' ') start--; 8 | return end - start; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode589.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class LeetCode589 { 5 | List res = new ArrayList<>(); 6 | public List preorder(Node root) { 7 | dfs(root); 8 | return res; 9 | } 10 | 11 | private void dfs(Node root) { 12 | if (root == null) 13 | return; 14 | res.add(root.val); 15 | for (Node node : root.children) { 16 | dfs(node); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode590.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class LeetCode590 { 5 | List res = new ArrayList<>(); 6 | public List postorder(Node root) { 7 | if (root == null) 8 | return res; 9 | dfs(root); 10 | res.add(root.val); 11 | return res; 12 | } 13 | 14 | private void dfs(Node root) { 15 | if (root == null) 16 | return; 17 | for (Node node : root.children) { 18 | dfs(node); 19 | res.add(node.val); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode678.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean checkValidString(String s) { 3 | int len = s.length(); 4 | int left = 0, right = 0; 5 | for (int i = 0; i < len; i++) { 6 | left += s.charAt(i) == ')' ? -1 : 1; 7 | right += s.charAt(len - i - 1) == '(' ? -1 : 1; 8 | if (left < 0 || right < 0) return false; 9 | } 10 | return true; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode7.java: -------------------------------------------------------------------------------- 1 | public class LeetCode7 { 2 | public int reverse(int x) { 3 | long xx = (long)x, temp1, res = 0; 4 | while (x != 0){ 5 | temp1 = x % 10; 6 | x = x / 10; 7 | res = res * 10 + temp1; 8 | } 9 | return (int)res == res ? (int)res : 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode70.java: -------------------------------------------------------------------------------- 1 | public class LeetCode70 { 2 | 3 | public int climbStairs(int n) { 4 | int i = 1, j = 2, m; 5 | if (n <= 2) 6 | return n; 7 | for (int k = 2; k < n; k++) { 8 | m = i; 9 | i = j; 10 | j = m; 11 | j = i + j; 12 | } 13 | return j; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode704.java: -------------------------------------------------------------------------------- 1 | package Review; 2 | 3 | public class LeetCode704 { 4 | public int search(int[] nums, int target) { 5 | int len = nums.length; 6 | int left = 0, right = len - 1; 7 | while (left < right){ 8 | int mid = left + (right - left) / 2; 9 | int temp = nums[mid]; 10 | if (temp == target) 11 | return mid; 12 | if (temp > target) 13 | right = mid - 1; 14 | else 15 | left = mid + 1; 16 | } 17 | return -1; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode718.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findLength(int[] nums1, int[] nums2) { 3 | int h = nums1.length, l = nums2.length; 4 | int[][] dp = new int[h + 1][l + 1]; 5 | int res = 0; 6 | for (int i = 1; i <= h; i++) { 7 | for (int j = 1; j <= l; j++) { 8 | if (nums1[i - 1] == nums2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; 9 | res = Math.max(res, dp[i][j]); 10 | } 11 | } 12 | return res; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode739.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] dailyTemperatures(int[] temperatures) { 3 | int len = temperatures.length; 4 | Stack stack = new Stack<>(); 5 | stack.push(0); 6 | int[] res = new int[len]; 7 | for (int i = 1; i < len; i++) { 8 | while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) { 9 | int saveTemper = stack.pop(); 10 | res[saveTemper] = i - saveTemper; 11 | } 12 | stack.push(i); 13 | } 14 | return res; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode746.java: -------------------------------------------------------------------------------- 1 | public class LeetCode746 { 2 | 3 | public int minCostClimbingStairs(int[] cost) { 4 | int len = cost.length; 5 | if (len == 0) 6 | return 0; 7 | int[] dp = new int[len]; 8 | dp[0] = cost[0]; 9 | dp[1] = cost[1]; 10 | for (int i = 2; i < len; i++) { 11 | dp[i] = Math.min(dp[i-1], dp[i - 2]) + cost[i]; 12 | } 13 | if (dp[len - 1] > dp[len - 2]){ 14 | return dp[len - 2]; 15 | }else { 16 | return dp[len - 1]; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode80.java: -------------------------------------------------------------------------------- 1 | public class LeetCode80 { 2 | public int removeDuplicates(int[] nums) { 3 | int len = nums.length; 4 | if (len <= 2) 5 | return len; 6 | int index = 2; 7 | for (int i = 2; i < len; i++) { 8 | if (nums[i] != nums[index - 2]){ 9 | nums[index++] = nums[i]; 10 | } 11 | } 12 | return index; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode82_2.java: -------------------------------------------------------------------------------- 1 | public ListNode deleteDuplicates(ListNode head) { 2 | if (head == null) return head; 3 | ListNode dummy = new ListNode(0); 4 | dummy.next = head; 5 | ListNode p = dummy, q = head; 6 | while (q != null) { 7 | if (q.next == null || q.val != q.next.val) { 8 | p.next = q; 9 | p = q; 10 | } 11 | while (q.next != null && q.val == q.next.val) q = q.next; 12 | q = q.next; 13 | } 14 | p.next = null; 15 | return dummy.next; 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode88.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class LeetCode88 { 4 | public void merge(int[] nums1, int m, int[] nums2, int n) { 5 | int nn = 0; 6 | for (int i = m; i < n + m; i++) { 7 | nums1[i] = nums2[nn++]; 8 | } 9 | Arrays.sort(nums1); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode9.java: -------------------------------------------------------------------------------- 1 | public class LeetCode9 { 2 | public boolean isPalindrome(int x) { 3 | if (x < 0) 4 | return false; 5 | int y = 0, temp1, temp2 = x; 6 | while (x > 0){ 7 | temp1 = x % 10; 8 | x = x / 10; 9 | y = y * 10 + temp1; 10 | } 11 | return temp2 == y; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode题解/LeetCode94.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class LeetCode94 { 5 | public List inorderTraversal(TreeNode root) { 6 | List list = new ArrayList(); 7 | inorder(root , list); 8 | return list; 9 | } 10 | public void inorder(TreeNode root , List list){ 11 | if (root == null){ 12 | return; 13 | } 14 | inorder(root.left , list); 15 | list.add(root.val); 16 | inorder(root.right , list); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode题解/Leetcode1448.java: -------------------------------------------------------------------------------- 1 | public class LeetCode1448 { 2 | int res = 0; 3 | public int goodNodes(TreeNode root) { 4 | preOrder(root, Integer.MIN_VALUE); 5 | return res; 6 | } 7 | 8 | private void preOrder(TreeNode node, int max) { 9 | if (node == null) 10 | return; 11 | if (max <= node.val) 12 | res++; 13 | max = Math.max(max, node.val); 14 | preOrder(node.left, max); 15 | preOrder(node.right, max); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode题解/Leetcode717.java: -------------------------------------------------------------------------------- 1 | public class LeetCode717 { 2 | public boolean isOneBitCharacter(int[] bits) { 3 | int len = bits.length; 4 | int n = 0; 5 | while (n < len){ 6 | if (bits[n] == 1){ 7 | if (n + 2 < len){ 8 | n += 2; 9 | } else 10 | return false; 11 | } else { 12 | if (n == len - 1) 13 | return true; 14 | n++; 15 | } 16 | } 17 | return false; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Review/CodingInterviews22.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class CodingInterviews22_2 { 4 | public ListNode getKthFromEnd(ListNode head, int k) { 5 | if (k == 0) 6 | return head; 7 | if (head.next == null) 8 | return head; 9 | ListNode p = head; 10 | for (int i = 0; i < k; i++) { 11 | p = p.next; 12 | } 13 | ListNode headindex = head; 14 | while (p != null){ 15 | p = p.next; 16 | headindex = headindex.next; 17 | } 18 | return headindex; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Review/Lc139.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean wordBreak(String s, List wordDict) { 3 | int len = s.length(); 4 | boolean[] dp = new boolean[len + 1]; 5 | dp[0] = true; 6 | for (int i = 1; i <= len; i++) { 7 | for (String a : wordDict) { 8 | int size = a.length(); 9 | if (i - size >= 0 && s.substring(i - size, i).equals(a)) { 10 | dp[i] = dp[i] || dp[i - size]; 11 | } 12 | } 13 | } 14 | return dp[len]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/Lc152.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProduct(int[] nums) { 3 | int len = nums.length; 4 | int imax = 1, imin = 1, max = Integer.MIN_VALUE; 5 | for (int i = 0; i < len; i++) { 6 | if (nums[i] < 0) { 7 | int temp = imin; 8 | imin = imax; 9 | imax = temp; 10 | } 11 | imax = Math.max(nums[i], nums[i] * imax); 12 | imin = Math.min(nums[i], nums[i] * imin); 13 | max = Math.max(max, imax); 14 | } 15 | return max; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Lc178.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select score, ( 3 | select count(distinct b.score) 4 | from Scores b 5 | where b.score >= a.score 6 | ) as "rank" 7 | from Scores a 8 | order by score desc 9 | -------------------------------------------------------------------------------- /Review/Lc22.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private List res = new ArrayList<>(); 3 | private int n; 4 | public List generateParenthesis(int n) { 5 | this.n = n; 6 | back(0, 0, ""); 7 | return res; 8 | } 9 | 10 | private void back(int left, int right, String s) { 11 | if (s.length() == 2 * n) { 12 | res.add(s); 13 | return; 14 | } 15 | if (left < n) back(left + 1, right, s + "("); 16 | if (right < left) back(left, right + 1, s + ")"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/Lc287.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findDuplicate(int[] nums) { 3 | int len = nums.length; 4 | int slow = nums[0], fast = nums[0]; 5 | while (true) { 6 | slow = nums[slow]; 7 | fast = nums[nums[fast]]; 8 | if (slow == fast) { 9 | slow = nums[0]; 10 | while (slow != fast) { 11 | slow = nums[slow]; 12 | fast = nums[fast]; 13 | } 14 | return slow; 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/Lc416.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canPartition(int[] nums) { 3 | int len = nums.length, sum = 0; 4 | for (int a : nums) sum += a; 5 | if (sum % 2 == 1) return false; 6 | int target = sum / 2; 7 | boolean[] dp = new boolean[target + 1]; 8 | dp[0] = true; 9 | for (int a : nums) { 10 | for (int i = target; i >= a; i--) { 11 | dp[i] = dp[i] || dp[i - a]; 12 | } 13 | } 14 | return dp[target]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/Lc45.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int jump(int[] nums) { 3 | int len = nums.length; 4 | int max = 0, step = 0, end = 0; 5 | for (int i = 0; i < len - 1; i++) { 6 | max = Math.max(max, i + nums[i]); 7 | if (i == end) { 8 | end = max; 9 | step++; 10 | } 11 | } 12 | return step; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Lc470.java: -------------------------------------------------------------------------------- 1 | /** 2 | * The rand7() API is already defined in the parent class SolBase. 3 | * public int rand7(); 4 | * @return a random integer in the range 1 to 7 5 | */ 6 | class Solution extends SolBase { 7 | public int rand10() { 8 | while (true) { 9 | int res = (rand7() - 1) * 7 + rand7(); 10 | if (res <= 40) return res % 10 + 1; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/Lc470_.java: -------------------------------------------------------------------------------- 1 | /** 2 | * The rand7() API is already defined in the parent class SolBase. 3 | * public int rand7(); 4 | * @return a random integer in the range 1 to 7 5 | */ 6 | class Solution extends SolBase { 7 | public int rand10() { 8 | while (true) { 9 | int res = (rand7() - 1) * 7 + rand7(); 10 | if (res <= 40) return res % 10 + 1; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/Lc494.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findTargetSumWays(int[] nums, int target) { 3 | int len = nums.length, sum = 0; 4 | for (int a : nums) sum += a; 5 | if (Math.abs(sum) < Math.abs(target) || (sum + target) % 2 == 1) return 0; 6 | int tar = (sum + target) / 2; 7 | int[] dp = new int[tar + 1]; 8 | dp[0] = 1; 9 | for (int a : nums) { 10 | for (int i = tar; i >= a; i--) { 11 | dp[i] += dp[i - a]; 12 | } 13 | } 14 | return dp[tar]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/Lc494_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findTargetSumWays(int[] nums, int target) { 3 | int len = nums.length; 4 | int sum = 0; 5 | for (int a : nums) sum += a; 6 | if (Math.abs(target) > Math.abs(sum) || (target + sum) % 2 == 1) return 0; 7 | int tar = (sum + target) / 2; 8 | int[] dp = new int[tar + 1]; 9 | dp[0] = 1; 10 | for (int n um : nums) { 11 | for (int i = tar; i >= num; i--) { 12 | dp[i] += dp[i - num]; 13 | } 14 | } 15 | return dp[tar]; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Lc50.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double myPow(double x, int n) { 3 | return n > 0 ? dfs(x, n) : 1 / dfs(x, n); 4 | } 5 | 6 | private double dfs(double x, int n) { 7 | if (n == 0) return 1.0; 8 | double res = dfs(x, n / 2); 9 | return n % 2 == 0 ? res * res : x * res * res; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Review/Lc55.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canJump(int[] nums) { 3 | int len = nums.length; 4 | int flag = nums[0]; 5 | for (int i = 0; i < len; i++) { 6 | if (flag >= i) { 7 | flag = Math.max(flag, i + nums[i]); 8 | } else { 9 | return false; 10 | } 11 | } 12 | return true; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Lc560.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subarraySum(int[] nums, int k) { 3 | int len = nums.length; 4 | Map map = new HashMap<>(); 5 | map.put(0, 1); 6 | int sum = 0, res = 0; 7 | for (int i = 0; i < len; i++) { 8 | sum += nums[i]; 9 | if (map.containsKey(sum - k)) { 10 | res += map.get(sum - k); 11 | } 12 | map.put(sum, map.getOrDefault(sum, 0) + 1); 13 | } 14 | return res; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/Lc586.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select customer_number 3 | from Orders 4 | group by customer_number 5 | order by count(*) desc 6 | limit 1; 7 | -------------------------------------------------------------------------------- /Review/Lc69.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int mySqrt(int x) { 3 | int left = 0, right = x, res = 0; 4 | while (left <= right) { 5 | int mid = left + (right - left) / 2; 6 | if ((long)mid * mid <= x) { 7 | res = Math.max(res, mid); 8 | left = mid + 1; 9 | } else { 10 | right = mid - 1; 11 | } 12 | } 13 | return res; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/Lc739.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] dailyTemperatures(int[] temperatures) { 3 | int len = temperatures.length; 4 | int[] res = new int[len]; 5 | Stack stack = new Stack<>(); 6 | for (int i = 0; i < len; i++) { 7 | while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) { 8 | int cur = stack.pop(); 9 | res[cur] = i - cur; 10 | } 11 | stack.add(i); 12 | } 13 | return res; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/LeetCode0101.java: -------------------------------------------------------------------------------- 1 | package Review; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class LeetCode0101 { 6 | public boolean isUnique(String astr) { 7 | ArrayList arrayList = new ArrayList<>(); 8 | for (int i = 0; i < astr.length(); i++) { 9 | char a = astr.charAt(i); 10 | if (arrayList.contains(a)) 11 | return false; 12 | else 13 | arrayList.add(a); 14 | } 15 | return true; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/LeetCode1004.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestOnes(int[] nums, int k) { 3 | int len = nums.length, left = 0, right = 0; 4 | int zero = 0, res = 0; 5 | while (right < len) { 6 | int a = nums[right++]; 7 | if (a == 0) zero++; 8 | while (zero > k) { 9 | int b = nums[left++]; 10 | if (b == 0) zero--; 11 | } 12 | res = Math.max(res, right - left); 13 | } 14 | return res; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/LeetCode11.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxArea(int[] height) { 3 | int len = height.length; 4 | int left = 0, right = len - 1; 5 | int max = 0; 6 | while (left < right) { 7 | max = Math.max(max, height[left] > height[right] ? (right - left) * height[right--] : (right - left) * height[left++]); 8 | } 9 | return max; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Review/LeetCode110.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isBalanced(TreeNode root) { 3 | return dfs(root) != -1; 4 | } 5 | 6 | private int dfs(TreeNode root) { 7 | if (root == null) return 0; 8 | int left = dfs(root.left); 9 | if (left == -1) return -1; 10 | int right = dfs(root.right); 11 | if (right == -1) return -1; 12 | return Math.abs(left - right) < 2 ? Math.max(left, right) + 1 : -1; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/LeetCode1143.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestCommonSubsequence(String text1, String text2) { 3 | int h = text1.length(), l = text2.length(); 4 | int[][] dp = new int[h + 1][l + 1]; 5 | for (int i = 1; i <= h; i++) { 6 | for (int j = 1; j <= l; j++) { 7 | if (text1.charAt(i - 1) == text2.charAt(j - 1)) dp[i][j] = dp[i - 1][j - 1] + 1; 8 | else dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); 9 | } 10 | } 11 | return dp[h][l]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/LeetCode115.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numDistinct(String s, String t) { 3 | int h = t.length(), l = s.length(); 4 | int[][] dp = new int[h + 1][l + 1]; 5 | for (int i = 0; i <= l; i++) dp[0][i] = 1; 6 | for (int i = 1; i <= h; i++) { 7 | for (int j = 1; j <= l; j++) { 8 | if (s.charAt(j - 1) == t.charAt(i - 1)) dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1]; 9 | else dp[i][j] = dp[i][j - 1]; 10 | } 11 | } 12 | return dp[h][l]; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/LeetCode122.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices) { 3 | int len = prices.length; 4 | int[][] dp = new int[len][2]; 5 | dp[0][0] = 0; 6 | dp[0][1] = -prices[0]; 7 | for (int i = 1; i < len; i++) { 8 | dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]); 9 | dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); 10 | } 11 | return dp[len - 1][0]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/LeetCode137.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean wordBreak(String s, List wordDict) { 3 | int len = s.length(); 4 | boolean[] dp = new boolean[len + 1]; 5 | dp[0] = true; 6 | for (int i = 1; i <= len; i++) { 7 | for (String a : wordDict) { 8 | int size = a.length(); 9 | if (i >= size && s.substring(i - size, i).equals(a)) { 10 | dp[i] = dp[i] || dp[i - size]; 11 | } 12 | } 13 | } 14 | return dp[len]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/LeetCode139.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean wordBreak(String s, List wordDict) { 3 | int len = s.length(); 4 | boolean[] dp = new boolean[len + 1]; 5 | dp[0] = true; 6 | for (int i = 1; i <= len; i++) { 7 | for (String a : wordDict) { 8 | int size = a.length(); 9 | if (i >= size && s.substring(i - size, i).equals(a)) { 10 | dp[i] = dp[i] || dp[i - size]; 11 | } 12 | } 13 | } 14 | return dp[len]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/LeetCode1414_2.java: -------------------------------------------------------------------------------- 1 | package Review; 2 | 3 | public class LeetCode1414 { 4 | public int findMinFibonacciNumbers(int k) { 5 | int a = 1, b = 1; 6 | while (b <= k){ 7 | int c = a + b; 8 | a = b; 9 | b = c; 10 | } 11 | int res = 0; 12 | while (k != 0){ 13 | if (k >= b){ 14 | k -= b; 15 | res++; 16 | } 17 | int c = b - a; 18 | b = a; 19 | a = c; 20 | } 21 | return res; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Review/LeetCode151.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String reverseWords(String s) { 3 | s = s.trim(); 4 | String[] strs = s.split(" "); 5 | int len = strs.length; 6 | StringBuilder sb = new StringBuilder(); 7 | for (int i = len - 1; i >= 0; i--) { 8 | String temp = strs[i]; 9 | if (temp.length() > 0 && !temp.equals(" ")) { 10 | sb.append(temp); 11 | if (i != 0) sb.append(" "); 12 | } 13 | 14 | } 15 | return sb.toString(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/LeetCode152.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProduct(int[] nums) { 3 | int len = nums.length; 4 | int res = Integer.MIN_VALUE; 5 | int sum = 1; 6 | for (int num : nums) { 7 | sum *= num; 8 | res = Math.max(res, sum); 9 | if (num == 0) sum = 1; 10 | } 11 | sum = 1; 12 | for (int i = len - 1; i >= 0; i--) { 13 | sum *= nums[i]; 14 | res = Math.max(res, sum); 15 | if (nums[i] == 0) sum = 1; 16 | } 17 | return res; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Review/LeetCode172.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int trailingZeroes(int n) { 3 | int res = 0; 4 | while (n > 0) { 5 | n /= 5; 6 | res += n; 7 | } 8 | return res; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Review/LeetCode1725.java: -------------------------------------------------------------------------------- 1 | package Review; 2 | 3 | import java.util.HashMap; 4 | 5 | public class LeetCode1725 { 6 | public int countGoodRectangles(int[][] rectangles) { 7 | HashMap hashMap = new HashMap<>(); 8 | int maxlen = 0; 9 | for (int[] a : rectangles) { 10 | int temp = Math.min(a[0], a[1]); 11 | maxlen = Math.max(maxlen, temp); 12 | hashMap.put(temp, hashMap.getOrDefault(temp, 0) + 1); 13 | } 14 | return hashMap.get(maxlen); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/LeetCode1748.java: -------------------------------------------------------------------------------- 1 | package Review; 2 | 3 | import java.util.HashMap; 4 | 5 | public class LeetCode1748 { 6 | public int sumOfUnique(int[] nums) { 7 | HashMap hashMap = new HashMap<>(); 8 | for (int a : nums) { 9 | hashMap.put(a, hashMap.getOrDefault(a, 0) + 1); 10 | } 11 | int res = 0; 12 | for (int a : hashMap.keySet()) { 13 | if (hashMap.get(a) == 1) 14 | res += a; 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/LeetCode176_2.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select max(salary) as SecondHighestSalary 3 | from Employee 4 | where salary < ( 5 | select max(salary) 6 | from Employee 7 | ) 8 | -------------------------------------------------------------------------------- /Review/LeetCode178.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select score, ( 3 | select count(distinct b.score) 4 | from Scores b 5 | where b.score >= a.score 6 | ) as 'rank' 7 | from Scores a 8 | order by score desc 9 | -------------------------------------------------------------------------------- /Review/LeetCode184.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select b.name as Department, a.name as Employee, a.salary as Salary 3 | from Employee a 4 | left join Department b 5 | on a.departmentId = b.id 6 | where (a.departmentId, a.salary) in ( 7 | select departmentId, max(salary) 8 | from Employee 9 | group by departmentId 10 | ) 11 | -------------------------------------------------------------------------------- /Review/LeetCode197.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select b.id as Id 3 | from Weather a, 4 | Weather b 5 | where datediff(b.recordDate, a.recordDate) = 1 6 | and b.Temperature > a.Temperature 7 | -------------------------------------------------------------------------------- /Review/LeetCode213.java: -------------------------------------------------------------------------------- 1 | package Review; 2 | 3 | public class LeetCode231 { 4 | public boolean isPowerOfTwo(int n) { 5 | return n > 0 && (n & -n) == n; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Review/LeetCode222.java: -------------------------------------------------------------------------------- 1 | public class LeetCode222 { 2 | int res = 0; 3 | public int countNodes(TreeNode root) { 4 | if (root == null) 5 | return 0; 6 | dfs(root); 7 | return res; 8 | } 9 | 10 | private void dfs(TreeNode root) { 11 | if (root == null) 12 | return; 13 | res++; 14 | dfs(root.left); 15 | dfs(root.right); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/LeetCode235.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | 11 | class Solution { 12 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 13 | while ((root.val - p.val) * (root.val - q.val) > 0) { 14 | root = root.val > p.val ? root.left : root.right; 15 | } 16 | return root; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/LeetCode26.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeDuplicates(int[] nums) { 3 | int len = nums.length, res = 1; 4 | if (len == 1) return 1; 5 | int left = 0, right = 0; 6 | while (right < len) { 7 | while (right < len && nums[left] == nums[right]) right++; 8 | if (right < len) { 9 | nums[++left] = nums[right]; 10 | res++; 11 | } 12 | } 13 | return res; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/LeetCode264.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int nthUglyNumber(int n) { 3 | int[] res = new int[n + 1]; 4 | res[1] = 1; 5 | for (int i2 = 1, i3 = 1, i5 = 1, index = 2; index <= n; index++) { 6 | int a = res[i2] * 2, b = res[i3] * 3, c = res[i5] * 5; 7 | int min = Math.min(a, Math.min(b, c)); 8 | if (min == a) i2++; 9 | if (min == b) i3++; 10 | if (min == c) i5++; 11 | res[index] = min; 12 | } 13 | return res[n]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/LeetCode279.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numSquares(int n) { 3 | int[] dp = new int[n + 1]; 4 | Arrays.fill(dp, n + 1); 5 | dp[0] = 0; 6 | for (int i = 1; i <= Math.sqrt(n); i++) { 7 | for (int j = 1; j <= n; j++) { 8 | if (j >= i * i) { 9 | dp[j] = Math.min(dp[j], dp[j - i * i] + 1); 10 | } 11 | } 12 | } 13 | return dp[n]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/LeetCode283.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void moveZeroes(int[] nums) { 3 | if (nums == null) return; 4 | int i = 0; 5 | for (int j = 0; j < nums.length; j++) { 6 | if (nums[j] != 0) { 7 | if (i != j) { 8 | int temp = nums[j]; 9 | nums[j] = nums[i]; 10 | nums[i] = temp; 11 | } 12 | i++; 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/LeetCode283_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void moveZeroes(int[] nums) { 3 | int index = 0, len = nums.length; 4 | for (int a : nums) { 5 | if (a != 0) nums[index++] = a; 6 | } 7 | while (index < len) { 8 | nums[index++] = 0; 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Review/LeetCode287.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findDuplicate(int[] nums) { 3 | int len = nums.length; 4 | int slow = 0, fast = 0; 5 | while (true) { 6 | fast = nums[nums[fast]]; 7 | slow = nums[slow]; 8 | if (fast == slow) { 9 | slow = 0; 10 | while (nums[slow] != nums[fast]) { 11 | fast = nums[fast]; 12 | slow = nums[slow]; 13 | } 14 | return nums[slow]; 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/LeetCode292.java: -------------------------------------------------------------------------------- 1 | package Review; 2 | 3 | public class LeetCode292 { 4 | public boolean canWinNim(int n) { 5 | return n % 4 != 0; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Review/LeetCode300.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLIS(int[] nums) { 3 | int len = nums.length; 4 | int[] tails = new int[len]; 5 | int res = 0; 6 | for (int num : nums) { 7 | int left = 0, right = res; 8 | while (left < right) { 9 | int mid = left + (right - left) / 2; 10 | if (tails[mid] < num) left = mid + 1; 11 | else right = mid; 12 | } 13 | tails[left] = num; 14 | if (right == res) res++; 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/LeetCode31.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void nextPermutation(int[] nums) { 3 | int len = nums.length; 4 | int i, j; 5 | for (i = len - 2; i >= 0; i--) { 6 | if (nums[i] < nums[i + 1]) break; 7 | } 8 | if (i < 0) { 9 | Arrays.sort(nums); 10 | return; 11 | } 12 | for (j = len - 1; j >= 0; j--) { 13 | if (nums[j] > nums[i]) break; 14 | } 15 | int temp = nums[i]; 16 | nums[i] = nums[j]; 17 | nums[j] = temp; 18 | Arrays.sort(nums, i + 1, len); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Review/LeetCode377.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int combinationSum4(int[] nums, int target) { 3 | int len = nums.length; 4 | int[] dp = new int[target + 1]; 5 | dp[0] = 1; 6 | for (int i = 1; i <= target; i++) { 7 | for (int a : nums) { 8 | if (i >= a) { 9 | dp[i] += dp[i - a]; 10 | } 11 | } 12 | } 13 | return dp[target]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/LeetCode416.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canPartition(int[] nums) { 3 | int len = nums.length, sum = 0; 4 | for (int a : nums) sum += a; 5 | if (sum % 2 == 1) return false; 6 | int target = sum / 2; 7 | boolean[] dp = new boolean[target + 1]; 8 | dp[0] = true; 9 | for (int a : nums) { 10 | for (int j = target; j >= a; j--) { 11 | dp[j] = dp[j] || dp[j - a]; 12 | } 13 | } 14 | return dp[target]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/LeetCode470.java: -------------------------------------------------------------------------------- 1 | /** 2 | * The rand7() API is already defined in the parent class SolBase. 3 | * public int rand7(); 4 | * @return a random integer in the range 1 to 7 5 | */ 6 | class Solution extends SolBase { 7 | public int rand10() { 8 | while (true) { 9 | int res = (rand7() - 1) * 7 + rand7(); 10 | if (res <= 40) return res % 10 + 1; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/LeetCode48.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void rotate(int[][] matrix) { 3 | int len = matrix.length; 4 | for (int i = 0; i < len / 2; i++) { 5 | for (int j = i; j < len - i - 1; j++) { 6 | int temp = matrix[i][j]; 7 | matrix[i][j] = matrix[len - j - 1][i]; 8 | matrix[len - j - 1][i] = matrix[len - i - 1][len - j - 1]; 9 | matrix[len - i - 1][len - j - 1] = matrix[j][len - i - 1]; 10 | matrix[j][len - i - 1] = temp; 11 | } 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/LeetCode494.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findTargetSumWays(int[] nums, int target) { 3 | int len = nums.length, sum = 0; 4 | for (int a : nums) sum += a; 5 | if (Math.abs(sum) < Math.abs(target) || (sum + target) % 2 == 1) return 0; 6 | int tar = (sum + target) / 2; 7 | int[] dp = new int[tar + 1]; 8 | dp[0] = 1; 9 | for (int a : nums) { 10 | for (int i = tar; i >= a; i--) { 11 | dp[i] += dp[i - a]; 12 | } 13 | } 14 | return dp[tar]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/LeetCode50.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double myPow(double x, int n) { 3 | return n > 0 ? dfs(x, n) : 1 / dfs(x, n); 4 | } 5 | 6 | private double dfs(double x, int n) { 7 | if (n == 0) return 1.0; 8 | double res = dfs(x, n / 2); 9 | return n % 2 == 0 ? res * res : res * res * x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Review/LeetCode516.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestPalindromeSubseq(String s) { 3 | int len = s.length(); 4 | int[][] dp = new int[len][len]; 5 | for (int i = 0; i < len; i++) dp[i][i] = 1; 6 | for (int i = len - 1; i >= 0; i--) { 7 | for (int j = i + 1; j < len; j++) { 8 | if (s.charAt(i) == s.charAt(j)) dp[i][j] = dp[i + 1][j - 1] + 2; 9 | else dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]); 10 | } 11 | } 12 | return dp[0][len - 1]; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/LeetCode518.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int change(int amount, int[] coins) { 3 | int len = coins.length; 4 | int[] dp = new int[amount + 1]; 5 | dp[0] = 1; 6 | for (int coin : coins) { 7 | for (int i = coin; i <= amount; i++) { 8 | dp[i] += dp[i - coin]; 9 | } 10 | } 11 | return dp[amount]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/LeetCode53.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSubArray(int[] nums) { 3 | int len = nums.length; 4 | int[] dp = new int[len]; 5 | dp[0] = Math.max(nums[0], 0); 6 | int res = nums[0]; 7 | for (int i = 1; i < len; i++) { 8 | dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]); 9 | res = Math.max(res, dp[i]); 10 | } 11 | return res; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/LeetCode534.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select a.player_id, a.event_date, sum(b.games_played) as games_played_so_far 3 | from Activity a, Activity b 4 | where a.player_id = b.player_id 5 | and a.event_date >= b.event_date 6 | group by a.player_id, a.event_date 7 | -------------------------------------------------------------------------------- /Review/LeetCode55.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canJump(int[] nums) { 3 | int len = nums.length; 4 | int flag = nums[0]; 5 | for (int i = 0; i < len; i++) { 6 | if (flag >= i) { 7 | flag = Math.max(i + nums[i], flag); 8 | } else { 9 | return false; 10 | } 11 | } 12 | return true; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/LeetCode560.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subarraySum(int[] nums, int k) { 3 | int len = nums.length; 4 | Map map = new HashMap<>(); 5 | map.put(0, 1); 6 | int sum = 0, res = 0; 7 | for (int a : nums) { 8 | sum += a; 9 | if (map.containsKey(sum - k)) { 10 | res += map.get(sum - k); 11 | } 12 | map.put(sum, map.getOrDefault(sum, 0) + 1); 13 | } 14 | return res; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/LeetCode580.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select ta.dept_name, count(tb.student_id) as student_number 3 | from Department ta 4 | left join Student tb 5 | on ta.dept_id = tb.dept_id 6 | group by ta.dept_name 7 | order by count(tb.student_id) desc, ta.dept_name asc; 8 | -------------------------------------------------------------------------------- /Review/LeetCode586.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select customer_number 3 | from Orders 4 | group by customer_number 5 | having count(*) = ( 6 | select count(*) 7 | from Orders 8 | group by customer_number 9 | order by count(*) desc 10 | limit 1 11 | ) 12 | -------------------------------------------------------------------------------- /Review/LeetCode62.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int uniquePaths(int m, int n) { 3 | int[][] dp = new int[m][n]; 4 | for (int i = 0; i < m; i++) { 5 | for (int j = 0; j < n; j++) { 6 | if (i == 0 || j == 0) { 7 | dp[i][j] = 1; 8 | continue; 9 | } 10 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 11 | } 12 | } 13 | return dp[m - 1][n - 1]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/LeetCode69.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int mySqrt(int x) { 3 | int left = 0, right = x, res = 0; 4 | while (left <= right) { 5 | int mid = left + (right - left) / 2; 6 | if ((long) mid * mid <= x) { 7 | res = mid; 8 | left = mid + 1; 9 | } else { 10 | right = mid - 1; 11 | } 12 | } 13 | return res; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/LeetCode7.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reverse(int x) { 3 | int res = 0, last = 0; 4 | while (x != 0) { 5 | int cur = x % 10; 6 | last = res; 7 | res = res * 10 + cur; 8 | if (res / 10 != last) return 0; 9 | x /= 10; 10 | } 11 | return res; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/LeetCode70.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int climbStairs(int n) { 3 | int a = 1, b = 2; 4 | if (n == 1) return 1; 5 | if (n == 2) return 2; 6 | for (int i = 0; i < n - 2; i++) { 7 | int temp = a + b; 8 | a = b; 9 | b = temp; 10 | } 11 | return b; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/LeetCode704.java: -------------------------------------------------------------------------------- 1 | public class LeetCode704 { 2 | public int search(int[] nums, int target) { 3 | int len = nums.length; 4 | int left = 0, right = len - 1; 5 | while (left <= right){ 6 | int mid = left + (right - left) / 2; 7 | if (nums[mid] == target) 8 | return mid; 9 | if (nums[mid] > target){ 10 | right = mid - 1; 11 | } else { 12 | left = mid + 1; 13 | } 14 | } 15 | return -1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/LeetCode739.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] dailyTemperatures(int[] temperatures) { 3 | int len = temperatures.length; 4 | int[] res = new int[len]; 5 | Stack stack = new Stack<>(); 6 | stack.push(0); 7 | for (int i = 0; i < len; i++) { 8 | while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) { 9 | int save = stack.pop(); 10 | res[save] = i - save; 11 | } 12 | stack.push(i); 13 | } 14 | return res; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/LeetCode74.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean searchMatrix(int[][] matrix, int target) { 3 | int h = 0, l = matrix[0].length - 1; 4 | while (h < matrix.length && l >= 0) { 5 | if (target == matrix[h][l]) return true; 6 | if (target > matrix[h][l]) h++; 7 | else l--; 8 | } 9 | return false; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Review/LeetCode75_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void sortColors(int[] nums) { 3 | int len = nums.length, n0 = 0, n1 = 0; 4 | for (int i = 0; i < len; i++) { 5 | int num = nums[i]; 6 | nums[i] = 2; 7 | if (num < 2) nums[n1++] = 1; 8 | if (num < 1) nums[n0++] = 0; 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Review/LeetCode7_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reverse(int x) { 3 | int res = 0; 4 | while (x != 0) { 5 | int cur = x % 10; 6 | if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && cur > 7)) return 0; 7 | if (res < Integer.MIN_VALUE / 10 || (res == Integer.MIN_VALUE / 10 && cur < -8)) return 0; 8 | res = res * 10 + cur; 9 | x /= 10; 10 | } 11 | return res; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/LeetCode80.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeDuplicates(int[] nums) { 3 | int len = nums.length; 4 | if (len <= 2) return len; 5 | int index = 2; 6 | for (int i = 2; i < len; i++) { 7 | if (nums[i] != nums[index - 2]) { 8 | nums[index++] = nums[i]; 9 | } 10 | } 11 | return index; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/LeetCode83_3.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode deleteDuplicates(ListNode head) { 3 | if (head == null || head.next == null) return head; 4 | head.next = deleteDuplicates(head.next); 5 | return head.val == head.next.val ? head.next : head; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Review/LeetCode88.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void merge(int[] nums1, int m, int[] nums2, int n) { 3 | int index = nums1.length - 1, left = m - 1, right = n - 1; 4 | while (index >= 0) { 5 | if (left >= 0 && right >= 0 && nums1[left] >= nums2[right] || right < 0) { 6 | nums1[index--] = nums1[left--]; 7 | } else if (left >= 0 && right >= 0 && nums1[left] < nums2[right] || left < 0) { 8 | nums1[index--] = nums2[right--]; 9 | } 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Review/LeetCode9.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(int x) { 3 | String s = x + ""; 4 | int len = s.length(), left = 0, right = len - 1; 5 | while (left < right) { 6 | if (s.charAt(left) != s.charAt(right)) return false; 7 | left++; 8 | right--; 9 | } 10 | return true; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Review/LeetCode912_2.java: -------------------------------------------------------------------------------- 1 | package Review; 2 | 3 | import java.util.PriorityQueue; 4 | 5 | public class LeetCode912_2 { 6 | public int[] sortArray(int[] nums) { 7 | PriorityQueue priorityQueue = new PriorityQueue<>((o1, o2) -> o1 - o2); 8 | for (int a : nums) { 9 | priorityQueue.add(a); 10 | } 11 | for (int i = 0; i < nums.length; i++) { 12 | nums[i] = priorityQueue.poll(); 13 | } 14 | return nums; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/LeetCode9_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(int x) { 3 | int num = 0, pre = x; 4 | if (x < 0) return false; 5 | x = Math.abs(x); 6 | while (x != 0) { 7 | num = num * 10 + x % 10; 8 | x /= 10; 9 | } 10 | return num == pre; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Review/Offer10.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numWays(int n) { 3 | int a = 1, b = 2; 4 | if (n == 0) return a; 5 | if (n == 1) return a; 6 | if (n == 2) return b; 7 | int sum = 0; 8 | for (int i = 3; i <= n; i++) { 9 | sum = (a + b) % (int)(1e9 + 7); 10 | a = b; 11 | b = sum; 12 | } 13 | return sum; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/Offer14.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int cuttingRope(int n) { 3 | int[] dp = new int[n + 1]; 4 | dp[2] = 1; 5 | for (int i = 3; i <= n; i++) { 6 | for (int j = 2; j stack = new Stack<>(); 5 | int index = 0; 6 | for (int a : pushed) { 7 | stack.push(a); 8 | while (!stack.isEmpty() && popped[index] == stack.peek()) { 9 | stack.pop(); 10 | index++; 11 | } 12 | } 13 | return stack.isEmpty(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/Offer4.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean findNumberIn2DArray(int[][] matrix, int target) { 3 | int h = matrix.length; 4 | if (h == 0) return false; 5 | int l = matrix[0].length; 6 | int m = 0, n = l - 1; 7 | while (m < h && n >= 0) { 8 | if (matrix[m][n] > target) { 9 | n--; 10 | } else if (matrix[m][n] < target) { 11 | m++; 12 | } else return true; 13 | } 14 | return false; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/Offer44.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findNthDigit(int n) { 3 | int time = 1; 4 | long start = 1, count = 9; 5 | while (n > count) { 6 | n -= count; 7 | time++; 8 | start *= 10; 9 | count = time * start * 9; 10 | } 11 | long num = start + (n - 1) / time; 12 | return Long.toString(num).charAt((n - 1) % time) - '0'; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Offer54.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int k, res; 3 | public int kthLargest(TreeNode root, int k) { 4 | if (root == null) return 0; 5 | this.k = k; 6 | dfs(root); 7 | return res; 8 | } 9 | 10 | private void dfs(TreeNode root) { 11 | if (root == null) return; 12 | dfs(root.right); 13 | if (k == 0) return; 14 | if (--k == 0) res = root.val; 15 | dfs(root.left); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Offer56.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] singleNumbers(int[] nums) { 3 | int x = 0, y = 0, n = 0, m = 1; 4 | for (int num : nums) { 5 | n ^= num; 6 | } 7 | while ((n & m) == 0) m <<= 1; 8 | for (int num : nums) { 9 | if ((num & m) == 0) x ^= num; 10 | else y ^= num; 11 | } 12 | return new int[]{x, y}; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Offer60.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double[] dicesProbability(int n) { 3 | double[] pre = new double[6]; 4 | Arrays.fill(pre, 1.0 / 6.0); 5 | for (int i = 2; i <= n; i++) { 6 | double[] temp = new double[5 * i + 1]; 7 | for (int j = 0; j < pre.length; j++) { 8 | for (int k = 0; k < 6; k++) { 9 | temp[j + k] += pre[j] / 6.0; 10 | } 11 | } 12 | pre = temp; 13 | } 14 | return pre; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/Offer62.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lastRemaining(int n, int m) { 3 | int res = 0; 4 | for (int i = 2; i <= n; i++) { 5 | res = (res + m) % i; 6 | } 7 | return res; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Review/Offer64.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int res; 3 | public int sumNums(int n) { 4 | boolean x = n > 1 && sumNums(n - 1) > 0; 5 | res += n; 6 | return res; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Review/Offer66.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] constructArr(int[] a) { 3 | int len = a.length; 4 | int[] res = new int[len]; 5 | Arrays.fill(res, 1); 6 | int temp = 1; 7 | for (int i = 1; i < len; i++) { 8 | temp *= a[i - 1]; 9 | res[i] *= temp; 10 | } 11 | temp = 1; 12 | for (int i = len - 2; i >= 0; i--) { 13 | temp *= a[i + 1]; 14 | res[i] *= temp; 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/OfferInterviews56.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] singleNumbers(int[] nums) { 3 | int len = nums.length, x = 0, y = 0, n = 0, m = 1; 4 | for (int num : nums) n ^= num; 5 | while ((n & m) == 0) m <<= 1; 6 | for (int num : nums) { 7 | if ((num & m) != 0) x ^= num; 8 | else y ^= num; 9 | } 10 | return new int[] {x, y}; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Review/OfferInterviews56_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | int len = nums.length; 4 | int[] bitSum = new int[32]; 5 | for (int num : nums) { 6 | int bitMask = 1; 7 | for (int i = 31; i >= 0; i--) { 8 | if ((num & bitMask) != 0) bitSum[i]++; 9 | bitMask <<= 1; 10 | } 11 | } 12 | int res = 0; 13 | for (int i = 0; i < 32; i++) { 14 | res <<= 1; 15 | res += bitSum[i] % 3; 16 | } 17 | return res; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Review/OfferInterviews60.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double[] dicesProbability(int n) { 3 | double[] pre = new double[6]; 4 | Arrays.fill(pre, 1.0 / 6.0); 5 | for (int i = 2; i <= n; i++) { 6 | double[] temp = new double[i * 5 + 1]; 7 | for (int j = 0; j < pre.length; j++) { 8 | for (int k = 0; k < 6; k++) { 9 | temp[j + k] += pre[j] / 6.0; 10 | } 11 | } 12 | pre = temp; 13 | } 14 | return pre; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/OfferInterviews61.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isStraight(int[] nums) { 3 | Set set = new HashSet<>(); 4 | int min = 14, max = -1; 5 | for (int a : nums) { 6 | if (a == 0) continue; 7 | min = Math.min(min, a); 8 | max = Math.max(max, a); 9 | if (set.contains(a)) return false; 10 | set.add(a); 11 | } 12 | return (max - min) < 5; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/README.md: -------------------------------------------------------------------------------- 1 | # 🥂 本题单主要用于复习之前做过的题目 2 | ### 温故而知新 3 | * 本题单包括自己之前做过感觉生疏的题目和比较好的题目 4 | * 题单包含 LeetCode 官方出的《腾讯精选 50 题》 5 | ### 除夕快乐🎉 6 | 7 | 8 | ### 📅 进度表 9 | 10 | | ⏰ 记录时间 | ✏️ 完成题目数 | 11 | | :--------: | :----------: | 12 | | 2022.1.27 | Start | 13 | -------------------------------------------------------------------------------- /Review/Top100/Lc1004.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestOnes(int[] nums, int k) { 3 | int len = nums.length; 4 | int res = 0, zero = 0; 5 | int left = 0, right = 0; 6 | while (right < len) { 7 | int a = nums[right++]; 8 | if (a == 0) zero++; 9 | while (zero > k) { 10 | if (nums[left++] == 0) zero--; 11 | } 12 | res = Math.max(res, right - left); 13 | } 14 | return res; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/Top100/Lc11.java: -------------------------------------------------------------------------------- 1 | package Top100Review; 2 | 3 | public class Lc11 { 4 | public int maxArea(int[] height) { 5 | int max = Integer.MIN_VALUE; 6 | int len = height.length; 7 | int left = 0, right = len - 1; 8 | while (left < right){ 9 | max = Math.max(max, height[left] > height[right] ? ((right - left) * height[right--]) : (right - left) * height[left++]); 10 | } 11 | return max; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/Top100/Lc128.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestConsecutive(int[] nums) { 3 | Set set = new HashSet<>(); 4 | for (int a : nums) set.add(a); 5 | int ans = 0; 6 | for (int a : set) { 7 | if (!set.contains(a - 1)){ 8 | int cur = a, count = 0; 9 | while (set.contains(cur++)) count++; 10 | ans = Math.max(ans, count); 11 | } 12 | } 13 | return ans; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/Top100/Lc139.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean wordBreak(String s, List wordDict) { 3 | int len = s.length(); 4 | boolean[] dp = new boolean[len + 1]; 5 | dp[0] = true; 6 | for (int i = 1; i <= len; i++) { 7 | for (String str : wordDict) { 8 | int lens = str.length(); 9 | if (i >= lens && s.substring(i - lens, i).equals(str)) dp[i] = dp[i] || dp[i - lens]; 10 | } 11 | } 12 | return dp[len]; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/Lc1448.java: -------------------------------------------------------------------------------- 1 | public class LeetCode1448 { 2 | int res = 0; 3 | public int goodNodes(TreeNode root) { 4 | if (root == null) 5 | return 0; 6 | dfs(root, Integer.MIN_VALUE); 7 | return res; 8 | } 9 | 10 | private void dfs(TreeNode node, int max) { 11 | if (node == null) 12 | return; 13 | if (node.val >= max) 14 | res++; 15 | dfs(node.left, Math.max(max, node.val)); 16 | dfs(node.right, Math.max(max, node.val)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/Top100/Lc151.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String reverseWords(String s) { 3 | s = s.trim(); 4 | String[] strs = s.split(" "); 5 | int len = strs.length; 6 | StringBuilder sb = new StringBuilder(); 7 | for (int i = len - 1; i >= 0; i--) { 8 | if (strs[i].length() == 0) continue; 9 | sb.append(strs[i]); 10 | if (i != 0) sb.append(" "); 11 | } 12 | return sb.toString(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/Lc169.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int majorityElement(int[] nums) { 3 | int len = nums.length; 4 | int count = 0, index = 0; 5 | for (int i = 0; i < len; i++) { 6 | if (count == 0) index = nums[i]; 7 | if (nums[i] == index) count ++; 8 | else count--; 9 | } 10 | return index; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Review/Top100/Lc176.java: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select ( 3 | select distinct salary 4 | from Employee 5 | order by salary desc 6 | limit 1 offset 1 7 | ) as SecondHighestSalary 8 | -------------------------------------------------------------------------------- /Review/Top100/Lc176.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select max(salary) as SecondHighestSalary 3 | from Employee 4 | where salary < ( 5 | select max(salary) 6 | from Employee 7 | ) 8 | -------------------------------------------------------------------------------- /Review/Top100/Lc176_2.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select ( 3 | select distinct salary 4 | from Employee 5 | order by salary desc 6 | limit 1 offset 1 7 | ) as SecondHighestSalary 8 | -------------------------------------------------------------------------------- /Review/Top100/Lc179.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select b.id as Id 3 | from Weather a, 4 | Weather b 5 | where datediff(b.recordDate, a.recordDate) = 1 6 | and b.Temperature > a.Temperature 7 | -------------------------------------------------------------------------------- /Review/Top100/Lc184.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select b.name as Department, a.name as Employee, a.salary as Salary 3 | from Employee a 4 | left join Department b 5 | on a.departmentId = b.id 6 | where (a.departmentId, a.salary) in ( 7 | select departmentId, max(salary) 8 | from Employee 9 | group by departmentId 10 | ) 11 | -------------------------------------------------------------------------------- /Review/Top100/Lc189.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void rotate(int[] nums, int k) { 3 | int len = nums.length; 4 | k %= len; 5 | reverseNums(nums, 0, len - 1); 6 | reverseNums(nums, 0, k - 1); 7 | reverseNums(nums, k, len - 1); 8 | } 9 | 10 | private void reverseNums(int[] nums, int left, int right) { 11 | while (left < right) { 12 | int temp = nums[left]; 13 | nums[left] = nums[right]; 14 | nums[right] = temp; 15 | left++; 16 | right--; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Review/Top100/Lc19.java: -------------------------------------------------------------------------------- 1 | package Top100Review; 2 | 3 | public class Lc19 { 4 | public ListNode removeNthFromEnd(ListNode head, int n) { 5 | ListNode p = head, q = head; 6 | if (p.next == null && n == 1) 7 | return null; 8 | for (int i = 0; i < n; i++) { 9 | q = q.next; 10 | } 11 | if (q == null) 12 | return head.next; 13 | while (q.next != null){ 14 | q = q.next; 15 | p = p.next; 16 | } 17 | p.next = p.next.next; 18 | return head; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Review/Top100/Lc206.java: -------------------------------------------------------------------------------- 1 | public class LeetCode206 { 2 | public ListNode reverseList(ListNode head) { 3 | if (head == null || head.next == null) 4 | return head; 5 | ListNode cur = reverseList(head.next); 6 | head.next.next = head; 7 | head.next = null; 8 | return cur; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Review/Top100/Lc21_2.java: -------------------------------------------------------------------------------- 1 | package Top100Review; 2 | 3 | public class Lc21_2 { 4 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 5 | if (l1 == null) 6 | return l2; 7 | else if (l2 == null) 8 | return l1; 9 | else if (l1.val <= l2.val){ 10 | l1.next = mergeTwoLists(l1.next, l2); 11 | return l1; 12 | } else { 13 | l2.next = mergeTwoLists(l2.next, l1); 14 | return l2; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Top100/Lc26.java: -------------------------------------------------------------------------------- 1 | public class LeetCode26 { 2 | public int removeDuplicates(int[] nums) { 3 | int len = nums.length; 4 | int left = 1, right = 1; 5 | while (right < len){ 6 | while (right < len && nums[right] == nums[right - 1]) 7 | right++; 8 | if (right >= len) 9 | return left; 10 | nums[left] = nums[right]; 11 | left++; 12 | right++; 13 | } 14 | return left; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/Top100/Lc264.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int nthUglyNumber(int n) { 3 | int[] res = new int[n + 1]; 4 | res[1] = 1; 5 | int i2 = 1, i3 = 1, i5 = 1; 6 | for (int index = 2; index <= n; index++) { 7 | int a = res[i2] * 2; 8 | int b = res[i3] * 3; 9 | int c = res[i5] * 5; 10 | int min = Math.min(Math.min(a,b), c); 11 | if (min == a) i2++; 12 | if (min == b) i3++; 13 | if (min == c) i5++; 14 | res[index] = min; 15 | } 16 | return res[n]; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/Top100/Lc283.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void moveZeroes(int[] nums) { 3 | int len = nums.length, index = 0; 4 | for (int i = 0; i < len; i++) { 5 | if (nums[i] != 0) { 6 | nums[index++] = nums[i]; 7 | } 8 | } 9 | while (index < len) nums[index++] = 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Review/Top100/Lc377.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int combinationSum4(int[] nums, int target) { 3 | int len = nums.length; 4 | int[] dp = new int[target + 1]; 5 | dp[0] = 1; 6 | for (int i = 1; i <= target; i++) { 7 | for (int a : nums) { 8 | if (i >= a) { 9 | dp[i] += dp[i - a]; 10 | } 11 | } 12 | } 13 | return dp[target]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/Top100/Lc416.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canPartition(int[] nums) { 3 | int len = nums.length; 4 | int sum = 0, target = 0; 5 | for (int i = 0; i < len; i++) sum += nums[i]; 6 | if (sum % 2 == 1) return false; 7 | else target = sum / 2; 8 | boolean[] dp = new boolean[target + 1]; 9 | dp[0] = true; 10 | for (int a : nums) { 11 | for (int i = target; i >= a; i--) { 12 | dp[i] = dp[i] || dp[i - a]; 13 | } 14 | } 15 | return dp[target]; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Top100/Lc45_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int jump(int[] nums) { 3 | int len = nums.length; 4 | int end = 0, step = 0, flag = nums[0]; 5 | for (int i = 0; i < len - 1; i++) { 6 | flag = Math.max(flag, i + nums[i]); 7 | if (i == end) { 8 | step++; 9 | end = flag; 10 | } 11 | } 12 | return step; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/Lc48.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void rotate(int[][] matrix) { 3 | int n = matrix.length; 4 | for (int i = 0; i < n / 2; i++) { 5 | for (int j = i; j < n - i - 1; j++) { 6 | int temp = matrix[i][j]; 7 | matrix[i][j] = matrix[n - j - 1][i]; 8 | matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]; 9 | matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]; 10 | matrix[j][n - i - 1] = temp; 11 | } 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/Lc48_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void rotate(int[][] matrix) { 3 | int len = matrix.length; 4 | for (int i = 0; i < len / 2; i++) { 5 | for (int j = i; j < len - i - 1; j++) { 6 | int temp = matrix[i][j]; 7 | matrix[i][j] = matrix[len - j - 1][i]; 8 | matrix[len - j - 1][i] = matrix[len - i - 1][len - j - 1]; 9 | matrix[len - i - 1][len - j - 1] = matrix[j][len - i - 1]; 10 | matrix[j][len - i - 1] = temp; 11 | } 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/Lc494.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findTargetSumWays(int[] nums, int target) { 3 | int len = nums.length, sum = 0; 4 | for (int a : nums) sum += a; 5 | if (Math.abs(target) > Math.abs(sum) || (target + sum) % 2 == 1) return 0; 6 | int tar = (target + sum) / 2; 7 | int[] dp = new int[tar + 1]; 8 | dp[0] = 1; 9 | for (int a : nums) { 10 | for (int i = tar; i >= a; i--) { 11 | dp[i] += dp[i - a]; 12 | } 13 | } 14 | return dp[tar]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/Top100/Lc518.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int change(int amount, int[] coins) { 3 | int[] dp = new int[amount + 1]; 4 | dp[0] = 1; 5 | for (int coin : coins) { 6 | for (int i = coin; i < amount + 1; i++) { 7 | dp[i] += dp[i - coin]; 8 | } 9 | } 10 | return dp[amount]; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Review/Top100/Lc53.java: -------------------------------------------------------------------------------- 1 | public class LeetCode53 { 2 | public int maxSubArray(int[] nums) { 3 | int len = nums.length; 4 | int[] dp = new int[len]; 5 | if (len < 2) 6 | return nums[0]; 7 | dp[0] = nums[0]; 8 | int ans = dp[0]; 9 | for (int i = 1; i < len; i++) { 10 | dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]); 11 | ans = Math.max(ans, dp[i]); 12 | } 13 | return ans; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/Top100/Lc580.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select ta.dept_name, count(tb.student_id) as student_number 3 | from Department ta 4 | left join Student tb 5 | on ta.dept_id = tb.dept_id 6 | group by ta.dept_name 7 | order by student_number desc, dept_name asc; 8 | -------------------------------------------------------------------------------- /Review/Top100/Lc586.java: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select customer_number 3 | from Orders 4 | group by customer_number 5 | order by count(*) desc 6 | limit 1; 7 | -------------------------------------------------------------------------------- /Review/Top100/Lc69.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int mySqrt(int x) { 3 | int left = 0, right = x, res = 0; 4 | while (left <= right) { 5 | int mid = left + (right - left) / 2; 6 | if ((long) mid * mid <= x) { 7 | res = mid; 8 | left = mid + 1; 9 | } else { 10 | right = mid - 1; 11 | } 12 | } 13 | return res; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/Top100/Lc70.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int climbStairs(int n) { 3 | int a = 1, b = 2; 4 | if (n == 1) return a; 5 | if (n == 2) return b; 6 | for (int i = 2; i < n; i++) { 7 | int c = a + b; 8 | a = b; 9 | b = c; 10 | } 11 | return b; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/Top100/Lc83.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode deleteDuplicates(ListNode head) { 13 | if (head == null || head.next == null) return head; 14 | head.next = deleteDuplicates(head.next); 15 | return head.val == head.next.val ? head.next : head; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Top100/Lc9.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(int x) { 3 | if (x < 0) return false; 4 | String s = x + ""; 5 | int len = s.length(); 6 | int left = 0, right = len - 1; 7 | while (left < right) { 8 | if (left < right && s.charAt(left) == s.charAt(right)) { 9 | left++; 10 | right--; 11 | continue; 12 | } else { 13 | return false; 14 | } 15 | } 16 | return true; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/Top100/Lc9_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(int x) { 3 | if (x < 0) return false; 4 | int dummy = 0, n = x; 5 | while (n > 0) { 6 | dummy = dummy * 10 + n % 10; 7 | n /= 10; 8 | } 9 | return dummy == x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Review/Top100/Lcc92_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLIS(int[] nums) { 3 | int len = nums.length; 4 | int[] tails = new int[len]; 5 | int res = 0; 6 | for (int num : nums) { 7 | int i = 0, j = res; 8 | while (i < j) { 9 | int m = i + (j - i) / 2; 10 | if (tails[m] < num) i = m + 1; 11 | else j = m; 12 | } 13 | tails[i] = num; 14 | if (res == j) res++; 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | Map map = new HashMap<>(); 4 | int len = nums.length; 5 | int[] res = new int[2]; 6 | for (int i = 0; i < len; i++) { 7 | int a = nums[i]; 8 | if (map.containsKey(target - a)) { 9 | res[0] = i; 10 | res[1] = map.get(target - a); 11 | return res; 12 | } 13 | map.put(a, i); 14 | } 15 | return null; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode1004.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestOnes(int[] nums, int k) { 3 | int len = nums.length; 4 | int left = 0, right = 0; 5 | int max = 0, zero = 0; 6 | while (right < len) { 7 | int a = nums[right++]; 8 | if (a == 0) zero++; 9 | while (zero > k) { 10 | int b = nums[left++]; 11 | if (b == 0) zero--; 12 | } 13 | max = Math.max(max, right - left); 14 | } 15 | return max; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode121.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices) { 3 | int len = prices.length; 4 | int[][] dp = new int[len][2]; 5 | dp[0][0] = 0; 6 | dp[0][1] = prices[0]; 7 | for (int i = 1; i < len; i++) { 8 | dp[i][0] = Math.max(dp[i - 1][0], prices[i] - dp[i - 1][1]); 9 | dp[i][1] = Math.min(prices[i], dp[i - 1][1]); 10 | } 11 | return dp[len - 1][0]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode122.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices) { 3 | int len = prices.length; 4 | int[][] dp = new int[len][2]; 5 | dp[0][0] = 0; 6 | dp[0][1] = -prices[0]; 7 | for (int i = 1; i < len; i++) { 8 | dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]); 9 | dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); 10 | } 11 | return dp[len - 1][0]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode136.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | int len = nums.length; 4 | for (int i = 1; i < len; i++) { 5 | nums[0] ^= nums[i]; 6 | } 7 | return nums[0]; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode139.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean wordBreak(String s, List wordDict) { 3 | int len = s.length(); 4 | boolean[] dp = new boolean[len + 1]; 5 | dp[0] = true; 6 | for (int i = 1; i <= len; i++) { 7 | for (int j = 0; j < i; j++) { 8 | if (dp[j] == true && wordDict.contains(s.substring(j, i))) { 9 | dp[i] = true; 10 | break; 11 | } 12 | } 13 | } 14 | return dp[len]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode14.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String longestCommonPrefix(String[] strs) { 3 | int len = strs.length; 4 | if (len == 0) return ""; 5 | int l = strs[0].length(); 6 | for (int i = 0; i < l; i++) { 7 | char a = strs[0].charAt(i); 8 | for (int j = 1; j < len; j++) { 9 | if (i >= strs[j].length() || strs[j].charAt(i) != a) return strs[0].substring(0, i); 10 | } 11 | } 12 | return strs[0]; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode145.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numWays(int n) { 3 | int a = 1, b = 2; 4 | if (n == 0) return a; 5 | if (n == 1) return a; 6 | if (n == 2) return b; 7 | int sum = 0; 8 | for (int i = 3; i <= n; i++) { 9 | sum = (a + b) % (int)(1e9 + 7); 10 | a = b; 11 | b = sum; 12 | } 13 | return sum; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode152.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProduct(int[] nums) { 3 | int len = nums.length; 4 | int imax = 1, imin = 1, max = Integer.MIN_VALUE; 5 | for (int i = 0; i < len; i++) { 6 | if (nums[i] < 0) { 7 | int temp = imax; 8 | imax = imin; 9 | imin = temp; 10 | } 11 | imax = Math.max(nums[i], imax * nums[i]); 12 | imin = Math.min(nums[i], imin * nums[i]); 13 | max = Math.max(max, imax); 14 | } 15 | return max; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode169.java: -------------------------------------------------------------------------------- 1 | public class LeetCode169 { 2 | public int majorityElement(int[] nums) { 3 | int len = nums.length; 4 | int index = 0, count = 0; 5 | for (int a : nums) { 6 | if (count == 0){ 7 | index = a; 8 | count++; 9 | continue; 10 | } 11 | if (index != a){ 12 | count--; 13 | } else { 14 | count++; 15 | } 16 | } 17 | return index; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode184.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select b.name as Department, a.name as Employee, a.salary as Salary 3 | from Employee a 4 | left join 5 | Department b 6 | on a.departmentId = b.id 7 | where (a.departmentId, a.salary) in ( 8 | select departmentId, max(salary) 9 | from Employee 10 | group by departmentId 11 | ) 12 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode184_2.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select tb.name as Department, ta.name as Employee, ta.salary as Salary 3 | from Employee as ta 4 | left join Department as tb 5 | on ta.departmentId = tb.id 6 | where (ta.departmentId, ta.salary) in ( 7 | select departmentId, max(salary) 8 | from Employee 9 | group by departmentId 10 | ) 11 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode189.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void rotate(int[] nums, int k) { 3 | int len = nums.length; 4 | k %= len; 5 | reverse(nums, 0, len - 1); 6 | reverse(nums, 0, k - 1); 7 | reverse(nums, k, len - 1); 8 | } 9 | 10 | private void reverse(int[] nums, int left, int right) { 11 | while (left < right) { 12 | int temp = nums[left]; 13 | nums[left] = nums[right]; 14 | nums[right] = temp; 15 | left++; 16 | right--; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode197.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select b.id 3 | from Weather a, Weather b 4 | where datediff(b.recordDate, a.recordDate) = 1 5 | and b.Temperature > a.Temperature 6 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode198.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rob(int[] nums) { 3 | int len = nums.length; 4 | int[] dp = new int[len]; 5 | dp[0] = nums[0]; 6 | int res = dp[0]; 7 | for (int i = 1; i < len; i++) { 8 | dp[i] = Math.max(dp[i - 1], (i - 2 >= 0 ? dp[i - 2] : 0) + nums[i]); 9 | res = Math.max(res, dp[i]); 10 | } 11 | return res; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode22.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | List res = new ArrayList<>(); 3 | private int n; 4 | public List generateParenthesis(int n) { 5 | this.n = n; 6 | dfs(0, 0, ""); 7 | return res; 8 | } 9 | 10 | private void dfs(int left, int right, String s) { 11 | if (s.length() == 2 * n) { 12 | res.add(s); 13 | return; 14 | } 15 | if (left < n) dfs(left + 1, right, s + "("); 16 | if (right < left) dfs(left, right + 1, s + ")"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode239.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] maxSlidingWindow(int[] nums, int k) { 3 | int len = nums.length; 4 | LinkedList queue = new LinkedList<>(); 5 | int[] res = new int[len - k + 1]; 6 | for (int i = 0; i < len; i++) { 7 | while (!queue.isEmpty() && nums[queue.peekLast()] < nums[i]) queue.removeLast(); 8 | queue.addLast(i); 9 | if (queue.peek() < i - k + 1) queue.poll(); 10 | if (i - k + 1 >= 0) res[i - k + 1] = nums[queue.peek()]; 11 | } 12 | return res; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode240.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean searchMatrix(int[][] matrix, int target) { 3 | int h = matrix.length, l = matrix[0].length; 4 | int x = 0, y = l - 1; 5 | while (x < h && y >= 0) { 6 | if (x > h || y < 0) return false; 7 | if (x < h && y >= 0 && matrix[x][y] == target) return true; 8 | if (x < h && y >= 0 && matrix[x][y] > target) y--; 9 | if (x < h && y >= 0 && matrix[x][y] < target) x++; 10 | } 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode264.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int nthUglyNumber(int n) { 3 | int[] res = new int[n + 1]; 4 | res[1] = 1; 5 | for (int index = 2, i2 = 1, i3 = 1, i5 = 1; index <= n; index++) { 6 | int a = res[i2] * 2, b = res[i3] * 3, c = res[i5] * 5; 7 | int min = Math.min(a, Math.min(b, c)); 8 | if (a == min) i2++; 9 | if (b == min) i3++; 10 | if (c == min) i5++; 11 | res[index] = min; 12 | } 13 | return res[n]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode279.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numSquares(int n) { 3 | int[] dp = new int[n + 1]; 4 | Arrays.fill(dp, n + 1); 5 | dp[0] = 0; 6 | for (int i = 1; i <= Math.sqrt(n); i++) { 7 | for (int j = i * i; j <= n; j++) { 8 | dp[j] = Math.min(dp[j], dp[j - i * i] + 1); 9 | } 10 | } 11 | return dp[n]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode283.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void moveZeroes(int[] nums) { 3 | int len = nums.length, index = 0; 4 | for (int a : nums) { 5 | if (a != 0) nums[index++] = a; 6 | } 7 | while (index < len) nums[index++] = 0; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode300.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLIS(int[] nums) { 3 | int len = nums.length; 4 | int[] tails = new int[len]; 5 | int res = 0; 6 | for (int num : nums) { 7 | int left = 0, right = res; 8 | while (left < right) { 9 | int mid = left + (right - left) / 2; 10 | if (tails[mid] < num) left = mid + 1; 11 | else right = mid; 12 | } 13 | tails[left] = num; 14 | if (res == right) res++; 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode377.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int combinationSum4(int[] nums, int target) { 3 | int len = nums.length; 4 | int[] dp = new int[target + 1]; 5 | dp[0] = 1; 6 | for (int i = 1; i <= target; i++) { 7 | for (int a : nums) { 8 | if (i >= a) { 9 | dp[i] += dp[i - a]; 10 | } 11 | } 12 | } 13 | return dp[target]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode416.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canPartition(int[] nums) { 3 | int len = nums.length; 4 | int sum = 0; 5 | for (int a : nums) sum += a; 6 | if (sum % 2 == 1) return false; 7 | int target = sum / 2; 8 | boolean[] dp = new boolean[target + 1]; 9 | dp[0] = true; 10 | for (int a : nums) { 11 | for (int i = target; i >= a; i--) { 12 | dp[i] = dp[i] || dp[i - a]; 13 | } 14 | } 15 | return dp[target]; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode448.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List findDisappearedNumbers(int[] nums) { 3 | List res = new ArrayList<>(); 4 | int len = nums.length; 5 | for (int i = 0; i < len; i++) { 6 | int cur = Math.abs(nums[i]); 7 | nums[cur - 1] = - Math.abs(nums[cur - 1]); 8 | } 9 | for (int i = 0; i < len; i++) { 10 | if (nums[i] > 0) res.add(i + 1); 11 | } 12 | return res; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode45.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int jump(int[] nums) { 3 | int len = nums.length; 4 | int[] dp = new int[len]; 5 | dp[0] = 0; 6 | for (int i = 0; i < len; i++) { 7 | int count = nums[i]; 8 | for (int j = i + 1; j < len && j <= i + count; j++) { 9 | if (dp[j] == 0){ 10 | dp[j] = dp[i] + 1; 11 | } else { 12 | dp[j] = Math.min(dp[j], dp[i] + 1); 13 | } 14 | } 15 | } 16 | return dp[len - 1]; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode450_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findNthDigit(int n) { 3 | int time = 1; 4 | long start = 1, count = 9; 5 | while (n > count) { 6 | n -= count; 7 | time++; 8 | start *= 10; 9 | count = time * start * 9; 10 | } 11 | long num = start + (n - 1) / time; 12 | return Long.toString(num).charAt((n - 1) % time) - '0'; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode45_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int jump(int[] nums) { 3 | int len = nums.length, end = 0, max = 0, res = 0; 4 | for (int i = 0; i < len - 1; i++) { 5 | max = Math.max(nums[i] + i, max); 6 | if (i == end) { 7 | end = max; 8 | res++; 9 | } 10 | } 11 | return res; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode470.java: -------------------------------------------------------------------------------- 1 | /** 2 | * The rand7() API is already defined in the parent class SolBase. 3 | * public int rand7(); 4 | * @return a random integer in the range 1 to 7 5 | */ 6 | class Solution extends SolBase { 7 | public int rand10() { 8 | while (true) { 9 | int res = (rand7() - 1) * 7 + rand7(); 10 | if (res <= 40) return res % 10 + 1; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode494.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findTargetSumWays(int[] nums, int target) { 3 | int len = nums.length; 4 | int sum = 0; 5 | for (int a : nums) sum += a; 6 | if (Math.abs(target) > Math.abs(sum) || (target + sum) % 2 == 1) return 0; 7 | int tar = (target + sum) / 2; 8 | int[] dp = new int[tar + 1]; 9 | dp[0] = 1; 10 | for (int a : nums) { 11 | for (int i = tar; i >= a; i--) { 12 | dp[i] += dp[i - a]; 13 | } 14 | } 15 | return dp[tar]; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode50.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double myPow(double x, int n) { 3 | return n > 0 ? dfs(x, n) : 1 / dfs(x, n); 4 | } 5 | 6 | private double dfs(double x, int n) { 7 | if (n == 0) return 1.0; 8 | double res = dfs(x, n / 2); 9 | return n % 2 == 0 ? res * res : res * res * x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode516.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestPalindromeSubseq(String s) { 3 | int len = s.length(); 4 | int[][] dp = new int[len][len]; 5 | for (int i = 0; i < len; i++) dp[i][i] = 1; 6 | for (int i = len - 1; i >= 0; i--) { 7 | for (int j = i + 1; j < len; j++) { 8 | if (s.charAt(i) == s.charAt(j)) dp[i][j] = dp[i + 1][j - 1] + 2; 9 | else dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]); 10 | } 11 | } 12 | return dp[0][len - 1]; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode55.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canJump(int[] nums) { 3 | int len = nums.length; 4 | int flag = nums[0]; 5 | for (int i = 0; i < len; i++) { 6 | if (i <= flag) { 7 | flag = Math.max(flag, i + nums[i]); 8 | } else { 9 | return false; 10 | } 11 | } 12 | return true; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode550.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select round(avg(b.player_id is not null), 2) as fraction 3 | from ( 4 | select player_id, min(event_date) as login 5 | from Activity 6 | group by player_id 7 | ) a 8 | left join Activity b 9 | on a.player_id = b.player_id and datediff (b.event_date, a.login) = 1 10 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode560.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subarraySum(int[] nums, int k) { 3 | int len = nums.length; 4 | int[] preSum = new int[len + 1]; 5 | preSum[0] = 0; 6 | for (int i = 0; i < len; i++) { 7 | preSum[i + 1] = preSum[i] + nums[i]; 8 | } 9 | int count = 0; 10 | for (int i = 0; i < len; i++) { 11 | for (int j = i; j < len; j++) { 12 | if (preSum[j + 1] - preSum[i] == k) count++; 13 | } 14 | } 15 | return count; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode560_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subarraySum(int[] nums, int k) { 3 | int len = nums.length; 4 | int res = 0, sum = 0; 5 | Map map = new HashMap<>(); 6 | map.put(0, 1); 7 | for (int a : nums) { 8 | sum += a; 9 | if (map.containsKey(sum - k)) res += map.get(sum - k); 10 | map.put(sum, map.getOrDefault(sum, 0) + 1); 11 | } 12 | return res; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode580.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select ta.dept_name, count(tb.dept_id) as student_number 3 | from Department ta 4 | left join Student tb 5 | on ta.dept_id = tb.dept_id 6 | group by ta.dept_name 7 | order by student_number desc, dept_name asc 8 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode586.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select customer_number 3 | from Orders 4 | group by customer_number 5 | order by count(*) desc 6 | limit 1 7 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode62.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int uniquePaths(int m, int n) { 3 | int[][] dp = new int[m][n]; 4 | for (int i = 0; i < m; i++) { 5 | dp[i][0] = 1; 6 | } 7 | for (int j = 0; j < n; j++) { 8 | dp[0][j] = 1; 9 | } 10 | for (int i = 1; i < m; i++) { 11 | for (int j = 1; j < n; j++) { 12 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 13 | } 14 | } 15 | return dp[m - 1][n - 1]; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode69.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLIS(int[] nums) { 3 | int len = nums.length; 4 | int[] tails = new int[len]; 5 | int res = 0; 6 | for (int num : nums) { 7 | int left = 0, right = res; 8 | while (left < right) { 9 | int mid = left + (right - left) / 2; 10 | if (tails[mid] < num) left = mid + 1; 11 | else right = mid; 12 | } 13 | tails[left] = num; 14 | if (res == right) res++; 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode704.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | int len = nums.length; 4 | int left = 0, right = len - 1; 5 | while (left <= right) { 6 | int mid = left + (right - left) / 2; 7 | if (nums[mid] == target) return mid; 8 | if (nums[mid] < target) left = mid + 1; 9 | else right = mid - 1; 10 | } 11 | return -1; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode75.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void sortColors(int[] nums) { 3 | int len = nums.length, n0 = 0, n1 = 0; 4 | for (int i = 0; i < len; i++) { 5 | int num = nums[i]; 6 | nums[i] = 2; 7 | if (num < 2) nums[n1++] = 1; 8 | if (num < 1) nums[n0++] = 0; 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode83.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode deleteDuplicates(ListNode head) { 13 | if (head == null || head.next == null) return head; 14 | head.next = deleteDuplicates(head.next); 15 | return head.val == head.next.val ? head.next : head; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode88.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void merge(int[] nums1, int m, int[] nums2, int n) { 3 | int l1 = m - 1, l2 = n - 1, t = nums1.length - 1; 4 | while (t >= 0) { 5 | if (l1 >= 0 && l2 >= 0 && nums1[l1] >= nums2[l2] || l2 < 0) { 6 | nums1[t--] = nums1[l1--]; 7 | } else if (l1 >= 0 && l2 >= 0 && nums1[l1] < nums2[l2] || l1 < 0) { 8 | nums1[t--] = nums2[l2--]; 9 | } 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Review/Top100/LeetCode88_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void merge(int[] nums1, int m, int[] nums2, int n) { 3 | int index = nums1.length - 1, i = m - 1, j = n - 1; 4 | while (index >= 0) { 5 | int a = i >= 0 ? nums1[i] : Integer.MIN_VALUE; 6 | int b = j >= 0 ? nums2[j] : Integer.MIN_VALUE; 7 | if (a > b) { 8 | nums1[index--] = a; 9 | i--; 10 | } else { 11 | nums1[index--] = b; 12 | j--; 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/Top100/Offer14.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int cuttingRope(int n) { 3 | int[] dp = new int[n + 1]; 4 | dp[2] = 1; 5 | for (int i = 3; i <= n; i++) { 6 | for (int j = 2; j < i; j++) { 7 | dp[i] = Math.max(dp[i], Math.max(dp[i - j] * j, (i - j) * j)); 8 | } 9 | } 10 | return dp[n]; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Review/Top100/Offer44.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findNthDigit(int n) { 3 | int time = 1; 4 | long start = 1, count = 9; 5 | while (n > count) { 6 | n -= count; 7 | time++; 8 | start *= 10; 9 | count = time * start * 9; 10 | } 11 | long num = start + (n - 1) / time; 12 | return Long.toString(num).charAt((n - 1) % time) - '0'; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/Offer56_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | if (nums.length == 0) return -1; 4 | int[] bitSum = new int[32]; 5 | int res = 0; 6 | for (int num : nums) { 7 | int bitMask = 1; 8 | for (int i = 31; i >= 0; i--) { 9 | if ((num & bitMask) != 0) bitSum[i]++; 10 | bitMask <<= 1; 11 | } 12 | } 13 | for (int i = 0; i < 32; i++) { 14 | res <<= 1; 15 | res += bitSum[i] % 3; 16 | } 17 | return res; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Review/Top100/Offer62.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lastRemaining(int n, int m) { 3 | int res = 0; 4 | for (int i = 2; i <= n; i++) { 5 | res = (res + m) % i; 6 | } 7 | return res; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Review/Top100/Offer62_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lastRemaining(int n, int m) { 3 | int res = 0; 4 | for (int i = 2; i <= n; i++) { 5 | res = (res + m) % i; 6 | } 7 | return res; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Review/Top100/Offer66.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] constructArr(int[] a) { 3 | int len = a.length; 4 | int[] b = new int[len]; 5 | Arrays.fill(b, 1); 6 | int temp = 1; 7 | for (int i = 1; i < len; i++) { 8 | temp *= a[i - 1]; 9 | b[i] *= temp; 10 | } 11 | temp = 1; 12 | for (int i = len - 2; i >= 0; i--) { 13 | temp *= a[i + 1]; 14 | b[i] *= temp; 15 | } 16 | return b; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/Top100/OfferInterviews44.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findNthDigit(int n) { 3 | int time = 1; 4 | long start = 1, count =9; 5 | while (n > count) { 6 | n -= count; 7 | time++; 8 | start *= 10; 9 | count = time * start * 9; 10 | } 11 | long num = start + (n - 1) / time; 12 | return Long.toString(num).charAt((n - 1) % time) - '0'; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Review/Top100/OfferInterviews60.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double[] dicesProbability(int n) { 3 | double[] pre = new double[6]; 4 | Arrays.fill(pre, 1.0 / 6.0); 5 | for (int i = 2; i <= n; i++) { 6 | double[] temp = new double[i * 5 + 1]; 7 | for (int j = 0; j < pre.length; j++) { 8 | for (int k = 0; k < 6; k++) { 9 | temp[j + k] += pre[j] / 6.0; 10 | } 11 | } 12 | pre = temp; 13 | } 14 | return pre; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Review/Top100/OfferInterviews66.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] constructArr(int[] a) { 3 | int len = a.length; 4 | int[] b = new int[len]; 5 | Arrays.fill(b, 1); 6 | int temp = 1; 7 | for (int i = 1; i < len; i++) { 8 | temp *= a[i - 1]; 9 | b[i] *= temp; 10 | } 11 | temp = 1; 12 | for (int i = len - 2; i >= 0; i--) { 13 | temp *= a[i + 1]; 14 | b[i] *= temp; 15 | } 16 | return b; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Review/Top100/README.md: -------------------------------------------------------------------------------- 1 | ## 🏸 温故而知新之 Top100 题单 2 | 3 | #### 在做题的时候发现有些题目开始有点遗忘,所以有了这个复习计划,计划是在平时做题的时候也复习一下之前做的 Top100 的题单题目,这样更有利于融会贯通,举一反三! 4 | * 题单主要是为了复习 5 | * 尽可能使用最优解 6 | * 为了尽量模拟面试时白板写代码,所以提交的代码都是直接在 LeetCode 上白板书写的,脱离 IDEA 环境 7 | 8 | ### 📅 进度表 9 | 10 | | ⏰ 记录时间 | ✏️ 完成题目数 | 🗓 完成百分比 | 11 | | :----: | :----: | :----: | 12 | | 2022.2.7 | 1/100 | 1% | 13 | -------------------------------------------------------------------------------- /Top100/Top1.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class Top100_1 { 4 | public int[] twoSum(int[] nums, int target) { 5 | int[] res = new int[2]; 6 | for (int i = 0; i < nums.length; i++) { 7 | for (int j = i + 1; j < nums.length; j++) { 8 | if (nums[i] + nums[j] == target){ 9 | res[0] = i; 10 | res[1] = j; 11 | } 12 | } 13 | } 14 | return res; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Top100/Top104.java: -------------------------------------------------------------------------------- 1 | public class Top104 { 2 | public int maxDepth(TreeNode root) { 3 | if (root == null) 4 | return 0; 5 | return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Top100/Top11.java: -------------------------------------------------------------------------------- 1 | public class Top11 { 2 | public int maxArea(int[] height) { 3 | int left = 0, right = height.length - 1, res = 0; 4 | while (left < right){ 5 | res = Math.max(res, height[left] > height[right]? (right - left) * height[right--]: (right - left) * height[left++]); 6 | } 7 | return res; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Top100/Top114.java: -------------------------------------------------------------------------------- 1 | public class Top114 { 2 | TreeNode pre = null; 3 | public void flatten(TreeNode root) { 4 | while (root != null){ 5 | TreeNode left = root.left; 6 | if (left != null){ 7 | while (left.right != null){ 8 | left = left.right; 9 | } 10 | left.right = root.right; 11 | root.right = root.left; 12 | root.left = null; 13 | } 14 | root = root.right; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Top100/Top121.java: -------------------------------------------------------------------------------- 1 | public class Top121 { 2 | public int maxProfit(int[] prices) { 3 | int[] dp = new int[prices.length]; 4 | dp[0] = 0; 5 | int max = 0; 6 | for (int i = 1; i < prices.length; i++) { 7 | dp[i] = Math.max(dp[i - 1] - prices[i - 1] + prices[i], 0); 8 | if (dp[i] > max) 9 | max = dp[i]; 10 | } 11 | return max; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Top100/Top136.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Set; 3 | 4 | public class Top136 { 5 | public int singleNumber(int[] nums) { 6 | HashMap hashMap = new HashMap<>(); 7 | for (int i: 8 | nums) { 9 | hashMap.put(i, hashMap.getOrDefault(i, 0) + 1); 10 | } 11 | Set set = hashMap.keySet(); 12 | for (int i: 13 | set) { 14 | if (hashMap.get(i) == 1) 15 | return i; 16 | } 17 | return 0; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Top100/Top136_2.java: -------------------------------------------------------------------------------- 1 | public class Top136_2 { 2 | public int singleNumber(int[] nums) { 3 | for (int i = 1; i < nums.length; i++) { 4 | nums[0] ^= nums[i]; 5 | } 6 | return nums[0]; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Top100/Top141.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | import java.util.List; 4 | 5 | public class Top141 { 6 | public boolean hasCycle(ListNode head) { 7 | if (head == null) 8 | return false; 9 | if (head.next == null) 10 | return false; 11 | ListNode p = head, q = head; 12 | while (p != null && q != null && q.next != null){ 13 | p = p.next; 14 | q = q.next.next; 15 | if (p == q) 16 | return true; 17 | } 18 | return false; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Top100/Top160.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top160 { 4 | public ListNode getIntersectionNode(ListNode headA, ListNode headB) { 5 | ListNode p = headA, q = headB; 6 | while (p != null || q != null){ 7 | if (p == null && q == null) 8 | return null; 9 | if (p == null) 10 | p = headB; 11 | if (q == null) 12 | q = headA; 13 | if (p == q) 14 | return q; 15 | p = p.next; 16 | q = q.next; 17 | } 18 | return null; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Top100/Top169_2.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top169_2 { 4 | public int majorityElement(int[] nums) { 5 | int pivot = nums[0], m = 0; 6 | for (int i = 0; i < nums.length; i++) { 7 | if (m <= 0) 8 | pivot = nums[i]; 9 | m += pivot == nums[i] ? 1 : -1; 10 | } 11 | return pivot; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Top100/Top198.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top198 { 4 | public int rob(int[] nums) { 5 | int len = nums.length; 6 | if (len == 1) 7 | return nums[0]; 8 | int[] dp = new int[len]; 9 | dp[0] = nums[0]; 10 | dp[1] = Math.max(dp[0], nums[1]); 11 | for (int i = 2; i < len; i++) { 12 | dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); 13 | } 14 | return dp[len - 1]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Top100/Top1_1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | HashMap hashMap = new HashMap<>(); 4 | for (int i = 0; i < nums.length; i++) { 5 | if (hashMap.containsKey(target - nums[i])){ 6 | int[] res = new int[]{hashMap.get(target - nums[i]), i}; 7 | return res; 8 | } 9 | hashMap.put(nums[i], i); 10 | } 11 | return new int[0]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Top100/Top206_2.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top206_2 { 4 | public ListNode reverseList(ListNode head) { 5 | if(head == null || head.next == null) 6 | return head; 7 | ListNode cur = reverseList(head.next); 8 | head.next.next = head; 9 | head.next = null; 10 | return cur; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Top100/Top215.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Top215 { 6 | public int findKthLargest(int[] nums, int k) { 7 | Arrays.sort(nums); 8 | return nums[nums.length - k]; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Top100/Top226.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top226 { 4 | public TreeNode invertTree(TreeNode root) { 5 | if (root == null) 6 | return null; 7 | TreeNode l = invertTree(root.left); 8 | TreeNode r = invertTree(root.right); 9 | root.left = r; 10 | root.right = l; 11 | return root; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Top100/Top238.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top238 { 4 | public int[] productExceptSelf(int[] nums) { 5 | int[] res = new int[nums.length]; 6 | int p = 1, q = 1; 7 | for (int i = 0; i < nums.length; i++) { 8 | res[i] = p; 9 | p *= nums[i]; 10 | } 11 | for (int i = nums.length - 1; i > 0; i--) { 12 | q *= nums[i]; 13 | res[i - 1] *= q; 14 | } 15 | return res; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Top100/Top279_2.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top279_2 { 4 | public int numSquares(int n) { 5 | int[] dp = new int[n + 1]; 6 | dp[0] = 0; 7 | for (int i = 1; i <= n; i++) { 8 | dp[i] = i; 9 | for (int j = 1; i - j * j >= 0; j++) { 10 | dp[i] = Math.min(dp[i], dp[i - j * j] + 1); 11 | } 12 | } 13 | return dp[n]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Top100/Top283.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top283 { 4 | int[] nums; 5 | public void moveZeroes(int[] nums) { 6 | int left = 0, right = 0; 7 | this.nums = nums; 8 | while (right < nums.length){ 9 | if (nums[right] != 0){ 10 | swap(left, right); 11 | left++; 12 | } 13 | right++; 14 | } 15 | } 16 | public void swap(int i, int j){ 17 | int temp = nums[i]; 18 | nums[i] = nums[j]; 19 | nums[j] = temp; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Top100/Top287.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Top287 { 6 | public int findDuplicate(int[] nums) { 7 | Arrays.sort(nums); 8 | for (int i = 1; i < nums.length; i++) { 9 | if (nums[i - 1] == nums[i]) 10 | return nums[i]; 11 | } 12 | return 0; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Top100/Top287_2.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top287_2 { 4 | public int findDuplicate(int[] nums) { 5 | int p = nums[0], q = nums[nums[0]]; 6 | while (p != q){ 7 | p = nums[p]; 8 | q = nums[nums[q]]; 9 | } 10 | p = 0; 11 | while (p != q){ 12 | p = nums[p]; 13 | q = nums[q]; 14 | } 15 | return p; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Top100/Top406_2.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class Top406_2 { 8 | public int[][] reconstructQueue(int[][] people) { 9 | Arrays.sort(people, (o1,o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0]); 10 | List arrayList = new ArrayList<>(); 11 | for (int[] a: 12 | people) { 13 | arrayList.add(a[1], a); 14 | } 15 | return arrayList.toArray(new int[arrayList.size()][2]); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Top100/Top448_2.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Top448_2 { 7 | public List findDisappearedNumbers(int[] nums) { 8 | List res = new ArrayList<>(); 9 | int len = nums.length; 10 | for (int i = 0; i < len; i++) { 11 | int index = (nums[i] - 1) % len; 12 | nums[index] += len; 13 | } 14 | for (int i = 0; i < len; i++) { 15 | if (nums[i] <= len) 16 | res.add(i + 1); 17 | } 18 | return res; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Top100/Top53.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class Top53 { 4 | public int maxSubArray(int[] nums) { 5 | int[] dp = new int[nums.length]; 6 | dp[0] = nums[0]; 7 | for (int i = 1; i < nums.length; i++) { 8 | if (nums[i] > dp[i - 1] + nums[i]){ 9 | dp[i] = nums[i]; 10 | }else { 11 | dp[i] = dp[i - 1] + nums[i]; 12 | } 13 | } 14 | return Arrays.stream(dp).max().getAsInt(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Top100/Top538.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top538 { 4 | int cur; 5 | public TreeNode convertBST(TreeNode root) { 6 | this.cur = 0; 7 | dfs(root); 8 | return root; 9 | } 10 | public void dfs(TreeNode node){ 11 | if (node == null) 12 | return; 13 | dfs(node.right); 14 | node.val = node.val + cur; 15 | cur = node.val; 16 | dfs(node.left); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Top100/Top543_2.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top543_2 { 4 | int res = Integer.MIN_VALUE; 5 | public int diameterOfBinaryTree(TreeNode root) { 6 | dfs(root); 7 | return res; 8 | } 9 | 10 | public int dfs(TreeNode node){ 11 | if (node == null) 12 | return 0; 13 | int left = dfs(node.left); 14 | int right = dfs(node.right); 15 | res = Math.max(res, left + right); 16 | return Math.max(left, right) + 1; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Top100/Top62.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class Top62 { 4 | public int uniquePaths(int m, int n) { 5 | int[][] dp = new int[m][n]; 6 | for (int i = 0; i < m; i++) { 7 | dp[i][0] = 1; 8 | } 9 | for (int i = 0; i < n; i++) { 10 | dp[0][i] = 1; 11 | } 12 | for (int i = 1; i < m; i++) { 13 | for (int j = 1; j < n; j++) { 14 | dp[i][j] = dp[i][j - 1] + dp[i - 1][j]; 15 | } 16 | } 17 | return dp[m - 1][n - 1]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Top100/Top647.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top647 { 4 | int ress = 0; 5 | public int countSubstrings(String s) { 6 | int len = s.length(); 7 | int res = len; 8 | for (int i = 0; i < len; i++) { 9 | res(s, i, i); 10 | res(s, i, i + 1); 11 | } 12 | return ress; 13 | } 14 | public void res(String s, int left, int right){ 15 | while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)){ 16 | left--; 17 | right++; 18 | ress++; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Top100/Top70.java: -------------------------------------------------------------------------------- 1 | public class Top70 { 2 | public int climbStairs(int n) { 3 | int a = 1, b = 2; 4 | if (n == 1) 5 | return a; 6 | if (n == 2) 7 | return b; 8 | for (int i = 2; i < n; i++) { 9 | int temp = b; 10 | b = a + b; 11 | a = temp; 12 | } 13 | return b; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Top100/Top739.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class Top739 { 4 | public int[] dailyTemperatures(int[] temperatures) { 5 | int len = temperatures.length; 6 | int[] res = new int[len]; 7 | res[len - 1] = 0; 8 | for (int i = 0; i < len - 1; i++) { 9 | for (int j = i + 1; j < len; j++) { 10 | if (temperatures[j] > temperatures[i]){ 11 | res[i] = j - i; 12 | break; 13 | } 14 | } 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Top100/Top94.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.LinkedList; 3 | import java.util.List; 4 | 5 | public class Top94 { 6 | List res = new LinkedList<>(); 7 | public List inorderTraversal(TreeNode root) { 8 | if (root == null) 9 | return res; 10 | mid(root); 11 | return res; 12 | } 13 | public void mid(TreeNode treeNode){ 14 | if (treeNode == null) 15 | return; 16 | mid(treeNode.left); 17 | res.add(treeNode.val); 18 | mid(treeNode.right); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Top100/Top96.java: -------------------------------------------------------------------------------- 1 | public class Top96 { 2 | public int numTrees(int n) { 3 | long res = 1; 4 | for (int i = 1; i < n; i++) { 5 | res = res * 2 * ( 2 * i + 1) / (i + 2); 6 | } 7 | return (int)res; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview104.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview104 { 4 | public int maxDepth(TreeNode root) { 5 | if (root == null) 6 | return 0; 7 | return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview11.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview11 { 4 | public int maxArea(int[] height) { 5 | int max = Integer.MIN_VALUE; 6 | int len = height.length; 7 | int left = 0, right = len - 1; 8 | while (left < right){ 9 | max = Math.max(max, height[left] > height[right] ? (right - left) * height[right--] : (right - left) * height[left++]); 10 | } 11 | return max; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview116.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview116 { 4 | public Node connect(Node root) { 5 | dfs(root); 6 | return root; 7 | } 8 | public void dfs(Node node){ 9 | if (node == null || node.left == null) 10 | return; 11 | node.left.next = node.right; 12 | if (node.next != null){ 13 | node.right.next = node.next.left; 14 | } 15 | dfs(node.left); 16 | dfs(node.right); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview121.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview121 { 4 | public int maxProfit(int[] prices) { 5 | int max = 0; 6 | int len = prices.length; 7 | int[] dp = new int[len]; 8 | dp[0] = 0; 9 | for (int i = 1; i < len; i++) { 10 | dp[i] = Math.max(0, dp[i - 1] - prices[i - 1] + prices[i]); 11 | max = Math.max(max, dp[i]); 12 | } 13 | return max; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview121_2.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview121_2 { 4 | public int maxProfit(int[] prices) { 5 | int len = prices.length; 6 | int[][] dp = new int[len][2]; 7 | dp[0][0] = 0; 8 | dp[0][1] = -prices[0]; 9 | for (int i = 1; i < len; i++) { 10 | dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]); 11 | dp[i][1] = Math.max(dp[i - 1][1], -prices[i]); 12 | } 13 | return dp[len - 1][0]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview122.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview122 { 4 | public int maxProfit(int[] prices) { 5 | int len = prices.length; 6 | int[][] dp = new int[len][2]; 7 | dp[0][0] = 0; 8 | dp[0][1] = -prices[0]; 9 | for (int i = 1; i < len; i++) { 10 | dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]); 11 | dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); 12 | } 13 | return dp[len - 1][0]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview124.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview124 { 4 | int max; 5 | public int maxPathSum(TreeNode root) { 6 | this.max = Integer.MIN_VALUE; 7 | last(root); 8 | return max; 9 | } 10 | public int last(TreeNode node){ 11 | if (node == null) 12 | return 0; 13 | int left = Math.max(last(node.left), 0); 14 | int right = Math.max(last(node.right), 0); 15 | max = Math.max(max, left + right + node.val); 16 | return Math.max(left, right) + node.val; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview134_2.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview134_2 { 4 | public int canCompleteCircuit(int[] gas, int[] cost) { 5 | int len = gas.length; 6 | int free = 0; 7 | int min = Integer.MAX_VALUE; 8 | int min_index = 0; 9 | for (int i = 0; i < len; i++) { 10 | free += gas[i] - cost[i]; 11 | if (free < min){ 12 | min = free; 13 | min_index = i; 14 | } 15 | } 16 | return free < 0 ? -1 : (min_index + 1) % len; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview136.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview136 { 4 | public int singleNumber(int[] nums) { 5 | int len = nums.length; 6 | for (int i = 1; i < len; i++) { 7 | nums[0] ^= nums[i]; 8 | } 9 | return nums[0]; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview141.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview141 { 4 | public boolean hasCycle(ListNode head) { 5 | if (head == null || head.next == null) 6 | return false; 7 | ListNode p = head; 8 | ListNode q = head; 9 | while (p != null && q != null && q.next != null){ 10 | p = p.next; 11 | q = q.next.next; 12 | if (p == q) 13 | return true; 14 | } 15 | return false; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview169.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | import java.util.Arrays; 4 | 5 | public class TopInterview169 { 6 | public int majorityElement(int[] nums) { 7 | Arrays.sort(nums); 8 | return nums[nums.length / 2]; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview169_2.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview169_2 { 4 | public int majorityElement(int[] nums) { 5 | int len = nums.length; 6 | int m = 0, pivot = 0; 7 | for (int i = 0; i < len; i++) { 8 | if (m == 0) 9 | pivot = nums[i]; 10 | m += pivot == nums[i] ? 1 : -1; 11 | } 12 | return pivot; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview171.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview171 { 4 | public int titleToNumber(String columnTitle) { 5 | long res = 0, sum = 0; 6 | char[] chars = columnTitle.toCharArray(); 7 | int len = chars.length; 8 | for (int i = 0; i < len; i++) { 9 | sum *= 26; 10 | sum += chars[i] - 'A' + 1; 11 | } 12 | return (int) sum; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview172.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview172 { 4 | public int trailingZeroes(int n) { 5 | int count = 0; 6 | while (n > 0){ 7 | n /= 5; 8 | count += n; 9 | } 10 | return count; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview189.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview189 { 4 | public void rotate(int[] nums, int k) { 5 | int len = nums.length; 6 | if (k > len) 7 | k %= len; 8 | int[] newNums = new int[len]; 9 | for (int i = 0; i < len; i++) { 10 | newNums[i] = nums[i]; 11 | } 12 | for (int i = 0; i < k; i++) { 13 | nums[i] = newNums[len - k + i]; 14 | } 15 | for (int i = k; i < len; i++) { 16 | nums[i] = newNums[i - k]; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview190.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview190 { 4 | public int reverseBits(int n) { 5 | int rev = 0; 6 | for (int i = 0; i < 32 && n != 0; ++i) { 7 | rev |= (n & 1) << (31 - i); 8 | n >>>= 1; 9 | } 10 | return rev; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview198.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview198 { 4 | public int rob(int[] nums) { 5 | int len = nums.length; 6 | if (len == 1) 7 | return nums[0]; 8 | int[] dp = new int[len]; 9 | dp[0] = nums[0]; 10 | dp[1] = Math.max(dp[0], nums[1]); 11 | for (int i = 2; i < len; i++) { 12 | dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); 13 | } 14 | return dp[len - 1]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview206.java: -------------------------------------------------------------------------------- 1 | package Top100; 2 | 3 | public class TopInterview206 { 4 | public ListNode reverseList(ListNode head) { 5 | ListNode newHead = new ListNode(0); 6 | ListNode p = head, q = newHead.next; 7 | while (p != null){ 8 | newHead.next = p; 9 | p = p.next; 10 | newHead.next.next = q; 11 | q = newHead.next; 12 | } 13 | return newHead.next; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview217.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | import java.util.HashSet; 4 | 5 | public class TopInterview217 { 6 | public boolean containsDuplicate(int[] nums) { 7 | HashSet hashSet = new HashSet<>(); 8 | int len = nums.length; 9 | for (int i = 0; i < len; i++) { 10 | int a = nums[i]; 11 | if (hashSet.contains(a)) 12 | return true; 13 | hashSet.add(a); 14 | } 15 | return false; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview237.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview237 { 4 | public void deleteNode(ListNode node) { 5 | int temp = node.next.val; 6 | node.val = temp; 7 | node.next = node.next.next; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview238.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview238 { 4 | public int[] productExceptSelf(int[] nums) { 5 | int len = nums.length; 6 | int[] res = new int[len]; 7 | int p = 1, q = 1; 8 | for (int i = 0; i < len; i++) { 9 | res[i] = p; 10 | p *= nums[i]; 11 | } 12 | for (int i = len - 1; i > 0; i--) { 13 | q *= nums[i]; 14 | res[i - 1] *= q; 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview268.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview268 { 4 | public int missingNumber(int[] nums) { 5 | int len = nums.length; 6 | int sum = 0; 7 | for (int i = 0; i <= len; i++) { 8 | sum += i; 9 | } 10 | int temp = 0; 11 | for (int i = 0; i < len; i++) { 12 | temp += nums[i]; 13 | } 14 | return sum - temp; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview279.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview279 { 4 | public int numSquares(int n) { 5 | int[] dp = new int[n + 1]; 6 | dp[0] = 0; 7 | for (int i = 1; i <= n; i++) { 8 | dp[i] = i; 9 | for (int j = 1; j * j <= i; j++) { 10 | dp[i] = Math.min(dp[i], dp[i - j * j] + 1); 11 | } 12 | } 13 | return dp[n]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview283.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview283 { 4 | public void moveZeroes(int[] nums) { 5 | int len = nums.length; 6 | int left = 0, right = 0; 7 | while (right < len){ 8 | if (nums[right] != 0){ 9 | swap(nums, left, right); 10 | left++; 11 | } 12 | right++; 13 | } 14 | } 15 | 16 | public void swap(int[] nums, int i, int j){ 17 | int temp = nums[i]; 18 | nums[i] = nums[j]; 19 | nums[j] = temp; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview326.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview326 { 4 | public boolean isPowerOfThree(int n) { 5 | if (n <= 0) 6 | return false; 7 | while (n % 3 == 0) 8 | n /= 3; 9 | return n == 1; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview328.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview328 { 4 | public ListNode oddEvenList(ListNode head) { 5 | if (head == null) 6 | return null; 7 | ListNode odd = head, evenHead = head.next, even = evenHead; 8 | while (even != null && even.next != null){ 9 | odd.next = even.next; 10 | odd = odd.next; 11 | even.next = odd.next; 12 | even = even.next; 13 | } 14 | odd.next = evenHead; 15 | return head; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview344.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview344 { 4 | public void reverseString(char[] s) { 5 | int len = s.length; 6 | int left = 0, right = len - 1; 7 | while (left < right){ 8 | char temp = s[right]; 9 | s[right] = s[left]; 10 | s[left] = temp; 11 | left++; 12 | right--; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview371.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview371 { 4 | public int getSum(int a, int b) { 5 | while (b != 0) { 6 | int carry = (a & b) << 1; 7 | a = a ^ b; 8 | b = carry; 9 | } 10 | return a; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview53.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | import java.util.Arrays; 4 | 5 | public class TopInterview53 { 6 | public int maxSubArray(int[] nums) { 7 | int len = nums.length; 8 | int[] dp = new int[len]; 9 | dp[0] = nums[0]; 10 | for (int i = 1; i < len; i++) { 11 | dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]); 12 | } 13 | return Arrays.stream(dp).max().getAsInt(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview53_2.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview53 { 4 | public int maxSubArray(int[] nums) { 5 | int len = nums.length; 6 | int[] dp = new int[len]; 7 | dp[0] = nums[0]; 8 | int max = dp[0]; 9 | for (int i = 1; i < len; i++) { 10 | dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]); 11 | max = Math.max(max, dp[i]); 12 | } 13 | return max; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview55.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview55 { 4 | public boolean canJump(int[] nums) { 5 | int len = nums.length; 6 | int[] dp = new int[len]; 7 | dp[0] = nums[0]; 8 | for (int i = 1; i < len; i++) { 9 | if (dp[i - 1] >= i){ 10 | dp[i] = Math.max(dp[i - 1], nums[i] + i); 11 | }else{ 12 | return false; 13 | } 14 | } 15 | return true; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview69.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview69 { 4 | public int mySqrt(int x) { 5 | int left = 0, right = x, res = -1; 6 | while (left <= right){ 7 | int mid = left + (right - left) / 2; 8 | if ((long) mid * mid <= x){ 9 | res = mid; 10 | left = mid + 1; 11 | }else { 12 | right = mid - 1; 13 | } 14 | } 15 | return res; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview70.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview70 { 4 | public int climbStairs(int n) { 5 | int a = 1, b = 2; 6 | if (n == 1) 7 | return a; 8 | if (n == 2) 9 | return b; 10 | int res = 0; 11 | for (int i = 2; i < n; i++) { 12 | res = a + b; 13 | a = b; 14 | b = res; 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview714.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview714 { 4 | public int maxProfit(int[] prices, int fee) { 5 | int len = prices.length; 6 | int[][] dp = new int[len][2]; 7 | dp[0][0] = 0; 8 | dp[0][1] = -prices[0]; 9 | for (int i = 1; i < len; i++) { 10 | dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee); 11 | dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); 12 | } 13 | return dp[len - 1][0]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview88.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | import java.util.Arrays; 4 | 5 | public class TopInterview88 { 6 | public void merge(int[] nums1, int m, int[] nums2, int n) { 7 | for (int i = m; i < nums1.length; i++) { 8 | nums1[i] = nums2[i - m]; 9 | } 10 | Arrays.sort(nums1); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview88_2.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | public class TopInterview88_2 { 4 | public void merge(int[] nums1, int m, int[] nums2, int n) { 5 | int p1 = m - 1, p2 = n - 1, len = m + n; 6 | while (p2 >= 0){ 7 | if (p1 < 0 || nums2[p2] >= nums1[p1]){ 8 | nums1[--len] = nums2[p2--]; 9 | }else { 10 | nums1[--len] = nums1[p1--]; 11 | } 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /TopInterview题解/TopInterview94.java: -------------------------------------------------------------------------------- 1 | package TopInterview; 2 | 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | 6 | public class TopInterview94 { 7 | List list = new LinkedList<>(); 8 | public List inorderTraversal(TreeNode root) { 9 | inorder(root); 10 | return list; 11 | } 12 | public void inorder(TreeNode node){ 13 | if (node == null) 14 | return; 15 | inorder(node.left); 16 | list.add(node.val); 17 | inorder(node.right); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews10.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews10 { 2 | public int fib(int n) { 3 | if (n == 0) 4 | return 0; 5 | if (n == 1 || n == 2) 6 | return 1; 7 | int a = 1 , b = 1 , c; 8 | for (int i = 2; i < n; i++) { 9 | a = (a + b) % 1000000007; 10 | c = b; 11 | b = a; 12 | a = c; 13 | } 14 | return b; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews10_2.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews10_2 { 2 | 3 | public int numWays(int n) { 4 | if (n == 0 || n == 1) 5 | return 1; 6 | int a = 1 , b = 1 , c = 0; 7 | for (int i = 1; i < n; i++) { 8 | a = (a + b) % 1000000007; 9 | c = a; 10 | a = b; 11 | b = c; 12 | } 13 | return b; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews11.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews11 { 2 | 3 | public int minArray(int[] numbers) { 4 | int len = numbers.length , min = numbers[0]; 5 | if (len == 1){ 6 | return numbers[0]; 7 | } 8 | for (int i = 1; i < len; i++) { 9 | if (numbers[i - 1] > numbers[i]){ 10 | min = numbers[i]; 11 | } 12 | } 13 | return min; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews14_1.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews14_1 { 2 | public int cuttingRope(int n) { 3 | if (n == 2) 4 | return 1; 5 | if (n == 3) 6 | return 2; 7 | int res = 1; 8 | while (n >= 3){ 9 | res *= 3; 10 | n = n - 3; 11 | } 12 | if (n == 1){ 13 | res = (res / 3) * 2 * 2; 14 | } 15 | if (n == 2){ 16 | res = res * n; 17 | } 18 | return res; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews14_2.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews14_2 { 2 | public int cuttingRope(int n) { 3 | if(n == 2) return 1; 4 | if(n == 3) return 2; 5 | long result = 1; 6 | while(n > 4){ 7 | n -= 3; 8 | result *= 3; 9 | result %= 1000000007; 10 | } 11 | return (int)(n * result % 1000000007); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews17.java: -------------------------------------------------------------------------------- 1 | class CodingInterviews17 { 2 | public int[] printNumbers(int n) { 3 | int num = (int)Math.pow(10, n); 4 | int[] result = new int[num - 1]; 5 | for(int i = 0; i < num - 1; i++) result[i] = i + 1; 6 | return result; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews206.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews206 { 2 | public ListNode reverseList(ListNode head) { 3 | if (head == null) 4 | return null; 5 | if (head.next == null) 6 | return head; 7 | ListNode newHead = new ListNode(-1), p = head,q = head; 8 | newHead.next = null; 9 | while (p != null){ 10 | q = p.next; 11 | p.next = newHead.next; 12 | newHead.next = p; 13 | p = q; 14 | } 15 | return newHead.next; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews22.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews22 { 2 | public ListNode getKthFromEnd(ListNode head, int k) { 3 | if (head == null || k == 0) 4 | return null; 5 | if (head.next == null) 6 | return head; 7 | ListNode p = head, q = head; 8 | while (k-- > 0){ 9 | q = q.next; 10 | } 11 | while (q != null){ 12 | q = q.next; 13 | p = p.next; 14 | } 15 | return p; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews24.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews24 { 2 | public ListNode reverseList(ListNode head) { 3 | if (head == null) 4 | return null; 5 | if (head.next == null) 6 | return head; 7 | ListNode newHead = new ListNode(-1), p = head,q = head; 8 | newHead.next = null; 9 | while (p != null){ 10 | q = p.next; 11 | p.next = newHead.next; 12 | newHead.next = p; 13 | p = q; 14 | } 15 | return newHead.next; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews27.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews27 { 2 | public TreeNode mirrorTree(TreeNode root) { 3 | lastOrder(root); 4 | return root; 5 | } 6 | public void lastOrder(TreeNode root){ 7 | if (root == null) 8 | return; 9 | if (root.left == null && root.right == null) 10 | return; 11 | lastOrder(root.left); 12 | lastOrder(root.right); 13 | TreeNode p = root.left; 14 | root.left = root.right; 15 | root.right = p; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews3-1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isUnique(String astr) { 3 | if(astr == null){ 4 | return false; 5 | } 6 | Map map = new HashMap<>(); 7 | for(int i = 0; i< astr.length();i++){ 8 | if(map.containsKey(astr.charAt(i))){ 9 | return false; 10 | } 11 | map.put(astr.charAt(i),i); 12 | } 13 | return true; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews3.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | 3 | public class CodingInterviews3 { 4 | public int findRepeatNumber(int[] nums) { 5 | HashSet hashSet = new HashSet<>(); 6 | int p; 7 | for (int i = 0; i < nums.length; i++) { 8 | p = nums[i]; 9 | if (hashSet.contains(p)){ 10 | return p; 11 | }else { 12 | hashSet.add(p); 13 | } 14 | } 15 | return 0; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews31.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean validateStackSequences(int[] pushed, int[] popped) { 3 | Stack stack = new Stack<>(); 4 | int i = 0; 5 | for (int n : pushed) { 6 | stack.push(n); 7 | while (!stack.isEmpty() && stack.peek() == popped[i]) { 8 | stack.pop(); 9 | i++; 10 | } 11 | } 12 | return stack.isEmpty(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews33.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | 3 | public class CodingInterviews33 { 4 | public boolean verifyPostorder(int[] postorder) { 5 | Stack stack = new Stack<>(); 6 | int root = Integer.MAX_VALUE; 7 | for(int i = postorder.length - 1; i >= 0; i--) { 8 | if(postorder[i] > root) return false; 9 | while(!stack.isEmpty() && stack.peek() > postorder[i]) 10 | root = stack.pop(); 11 | stack.add(postorder[i]); 12 | } 13 | return true; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews40.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class CodingInterviews40 { 4 | public int[] getLeastNumbers(int[] arr, int k) { 5 | int[] vec = new int[k]; 6 | Arrays.sort(arr); 7 | for (int i = 0; i < k; ++i) { 8 | vec[i] = arr[i]; 9 | } 10 | return vec; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews49.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int nthUglyNumber(int n) { 3 | int[] res = new int[n + 1]; 4 | res[1] = 1; 5 | int n2 = 1, n3 = 1, n5 = 1; 6 | for (int index = 2; index <= n; index++) { 7 | int a = res[n2] * 2, b = res[n3] * 3, c = res[n5] * 5; 8 | int min = Math.min(a, Math.min(b, c)); 9 | if (a == min) n2++; 10 | if (b == min) n3++; 11 | if (c == min) n5++; 12 | res[index] = min; 13 | } 14 | return res[n]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews50.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class CodingInterviews50 { 4 | public char firstUniqChar(String s) { 5 | int[] count = new int[128]; 6 | Arrays.fill(count , 0); 7 | for (int i = 0; i < s.length(); i++) { 8 | count[(int)s.charAt(i)]++; 9 | } 10 | for (int i = 0; i < s.length(); i++) { 11 | if (count[(int)s.charAt(i)] == 1){ 12 | return s.charAt(i); 13 | } 14 | } 15 | return ' '; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews53.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews53 { 2 | 3 | public int missingNumber(int[] nums) { 4 | int i = 0, j = nums.length - 1; 5 | while (i <= j){ 6 | int n = (i + j) / 2; 7 | if (nums[n] == n){ 8 | i = n + 1; 9 | }else { 10 | j = n - 1; 11 | } 12 | } 13 | return i; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews54.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | public class CodingInterviews54 { 3 | ArrayList list = new ArrayList<>(); 4 | public int kthLargest(TreeNode root, int k) { 5 | if (root == null) 6 | return -1; 7 | inOrder(root); 8 | return list.get(list.size() - k); 9 | } 10 | public void inOrder(TreeNode root){ 11 | if (root == null) 12 | return; 13 | inOrder(root.left); 14 | list.add(root.val); 15 | inOrder(root.right); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews55_1.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews55_1 { 2 | public int maxDepth(TreeNode root) { 3 | return lastOrder(root); 4 | } 5 | public int lastOrder(TreeNode root){ 6 | if (root == null) 7 | return 0; 8 | int left = lastOrder(root.left); 9 | int right = lastOrder(root.right); 10 | return left > right ? left + 1 : right + 1; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews56_1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] singleNumbers(int[] nums) { 3 | int ret = 0; 4 | for (int n : nums) { 5 | ret ^= n; 6 | } 7 | int div = 1; 8 | while ((div & ret) == 0) { 9 | div <<= 1; 10 | } 11 | int a = 0, b = 0; 12 | for (int n : nums) { 13 | if ((div & n) != 0) { 14 | a ^= n; 15 | } else { 16 | b ^= n; 17 | } 18 | } 19 | return new int[]{a, b}; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews58.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews58 { 2 | public String reverseLeftWords(String s, int n) { 3 | return s.substring(n, s.length()) + s.substring(0, n); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews58_2.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews58 { 2 | public String reverseWords(String s) { 3 | s = s.trim(); 4 | String[] strs = s.split(" "); 5 | String res = ""; 6 | for (int i = strs.length - 1; i > 0; i--) { 7 | if (strs[i].length() == 0){ 8 | continue; 9 | } 10 | res += strs[i] + " "; 11 | } 12 | res += strs[0]; 13 | return res; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews62.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews62 { 2 | public int lastRemaining(int n, int m) { 3 | int p = 0; 4 | for (int i = 2; i <= n; i++) { 5 | p = (p + m) % i; 6 | } 7 | return p; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews64.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int sumNums(int n) { 3 | return (n+1)*n/2; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews65.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews65 { 2 | public int add(int a, int b) { 3 | while(b != 0) { // 当进位为 0 时跳出 4 | int c = (a & b) << 1; // c = 进位 5 | a ^= b; // a = 非进位和 6 | b = c; // b = 进位 7 | } 8 | return a; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /剑指Offer题解/CodingInterviews68_2.java: -------------------------------------------------------------------------------- 1 | public class CodingInterviews68_2 { 2 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 3 | if (root == null || root == p || root == q) 4 | return root; 5 | TreeNode left = lowestCommonAncestor(root.left, p, q); 6 | TreeNode right = lowestCommonAncestor(root.right, p, q); 7 | if (left == null) 8 | return right; 9 | if (right == null) 10 | return left; 11 | return root; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /剑指Offer题解/Interviews9.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isFlipedString(String s1, String s2) { 3 | if(s1.length() != s2.length()) { 4 | return false; 5 | } 6 | String s = s2 + s2; 7 | return s.contains(s1); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /剑指Offer题解/OfferInterviews27.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | class Solution { 11 | public TreeNode mirrorTree(TreeNode root) { 12 | if (root == null) return null; 13 | TreeNode left = mirrorTree(root.left); 14 | TreeNode right = mirrorTree(root.right); 15 | root.left = right; 16 | root.right = left; 17 | return root; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /剑指Offer题解/OfferInterviews42.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSubArray(int[] nums) { 3 | int len = nums.length; 4 | int[] dp = new int[len]; 5 | dp[0] = nums[0]; 6 | int res = dp[0]; 7 | for (int i = 1; i < len; i++) { 8 | dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]); 9 | res = Math.max(res, dp[i]); 10 | } 11 | return res; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /剑指Offer题解/OfferInterviews56.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] singleNumbers(int[] nums) { 3 | int x = 0, y = 0, n = 0, m = 1; 4 | for (int num : nums) n ^= num; 5 | while ((n & m) == 0) m <<= 1; 6 | for (int num : nums) { 7 | if ((num & m) == 0) x ^= num; 8 | else y ^= num; 9 | } 10 | return new int[] {x, y}; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /剑指Offer题解/OfferInterviews60.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double[] dicesProbability(int n) { 3 | double[] pre = new double[6]; 4 | Arrays.fill(pre, 1.0 / 6.0); 5 | for (int i = 2; i <= n; i++) { 6 | double[] temp = new double[i * 5 + 1]; 7 | for (int j = 0; j < pre.length; j++) { 8 | for (int k = 0; k < 6; k++) { 9 | temp[j + k] += pre[j] / 6.0; 10 | } 11 | } 12 | pre = temp; 13 | } 14 | return pre; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /剑指Offer题解/OfferInterviews61.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isStraight(int[] nums) { 3 | Arrays.sort(nums); 4 | int len = nums.length; 5 | if (nums[len - 1] == 0) return true; 6 | int pivot = 0; 7 | for (int i = 0; i < 4; i++) { 8 | if (nums[i] == 0) pivot++; 9 | else { 10 | if (nums[i] == nums[i + 1]) return false; 11 | } 12 | } 13 | if (nums[4] - nums[pivot] < 5) return true; 14 | return false; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /剑指Offer题解/OfferInterviews61_2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isStraight(int[] nums) { 3 | int len = nums.length; 4 | int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; 5 | Set set = new HashSet<>(); 6 | for (int a : nums) { 7 | if (a == 0) continue; 8 | if (a < min) min = a; 9 | if (a > max) max = a; 10 | if (set.contains(a)) return false; 11 | set.add(a); 12 | } 13 | return (max - min) < 5; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /剑指Offer题解/OfferInterviews62.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lastRemaining(int n, int m) { 3 | int res = 0; 4 | for (int i = 2; i <= n; i++) { 5 | res = (res + m) % i; 6 | } 7 | return res; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /剑指Offer题解/OfferInterviews64.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int res; 3 | public int sumNums(int n) { 4 | boolean x = n > 1 && sumNums(n - 1) > 0; 5 | res += n; 6 | return res; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /剑指Offer题解/OfferInterviews66.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] constructArr(int[] a) { 3 | int len = a.length; 4 | int[] b = new int[len]; 5 | Arrays.fill(b, 1); 6 | int temp = 1; 7 | for (int i = 1; i < len; i++) { 8 | temp *= a[i - 1]; 9 | b[i] *= temp; 10 | } 11 | temp = 1; 12 | for (int i = len - 2; i >= 0; i--) { 13 | temp *= a[i + 1]; 14 | b[i] *= temp; 15 | } 16 | return b; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /剑指Offer题解/OfferInterviewsII38.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] dailyTemperatures(int[] temperatures) { 3 | int len = temperatures.length; 4 | int[] res = new int[len]; 5 | Stack stack = new Stack<>(); 6 | for (int i = 0; i < len; i++) { 7 | while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) { 8 | int save = stack.pop(); 9 | res[save] = i - save; 10 | } 11 | stack.push(i); 12 | } 13 | return res; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /各公司笔试题/BankOfXingyeInterviews01.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Xingye { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | String input = scanner.next(); 7 | String[] nums = input.split(","); 8 | int sum = 0, temp_sum = 0; 9 | for (int i = 0; i < nums.length; i++) { 10 | temp_sum += Integer.parseInt(nums[i]); 11 | } 12 | for (int i = 0; i < nums.length + 1; i++) { 13 | sum += i; 14 | } 15 | System.out.println(sum - temp_sum); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /各公司笔试题/JiangsuDX2.java: -------------------------------------------------------------------------------- 1 | public int analysis(String expression) { 2 | 3 | int result = 0; 4 | String str = ""; 5 | for (int i = 0; i < expression.length(); i++) { 6 | char ch = expression.charAt(i); 7 | if (ch >= '0' && ch <= '9') { 8 | str += ch; 9 | } else { 10 | result += Integer.parseInt(str); 11 | str = "" + ch; 12 | } 13 | } 14 | if (str != "") { 15 | result += Integer.parseInt(str); 16 | } 17 | return result; 18 | } 19 | --------------------------------------------------------------------------------