├── .gitignore ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── LICENSE ├── LeetCode ├── DifficultyFigure.png ├── LanguageFigure.png ├── README.md ├── TopicFigure.png ├── c │ ├── 1025.c │ ├── 1064.c │ ├── 116.c │ ├── 263.c │ ├── 342.c │ ├── 598.c │ ├── 908.c │ └── 991.c ├── cpp │ ├── 1.cpp │ ├── 100.cpp │ ├── 1001.cpp │ ├── 1002.cpp │ ├── 1003.cpp │ ├── 1005.cpp │ ├── 1006.cpp │ ├── 1007.cpp │ ├── 1008.cpp │ ├── 1009.cpp │ ├── 101.cpp │ ├── 1010.cpp │ ├── 1011.cpp │ ├── 1012.cpp │ ├── 1013.cpp │ ├── 1014.cpp │ ├── 1015.cpp │ ├── 1017.cpp │ ├── 1018.cpp │ ├── 1019.cpp │ ├── 102.cpp │ ├── 1020.cpp │ ├── 1021.cpp │ ├── 1022.cpp │ ├── 1023.cpp │ ├── 1024.cpp │ ├── 1025.cpp │ ├── 1026.cpp │ ├── 1027.cpp │ ├── 1028.cpp │ ├── 1029.cpp │ ├── 103.cpp │ ├── 1030.cpp │ ├── 1031.cpp │ ├── 1032.cpp │ ├── 1033.cpp │ ├── 1034.cpp │ ├── 1035.cpp │ ├── 1037.cpp │ ├── 1038.cpp │ ├── 104.cpp │ ├── 1041.cpp │ ├── 1042.cpp │ ├── 1046.cpp │ ├── 1047.cpp │ ├── 1048.cpp │ ├── 105.cpp │ ├── 1051.cpp │ ├── 1052.cpp │ ├── 1053.cpp │ ├── 1054.cpp │ ├── 106.cpp │ ├── 1064.cpp │ ├── 1065.cpp │ ├── 107.cpp │ ├── 1071.cpp │ ├── 1072.cpp │ ├── 1073.cpp │ ├── 108.cpp │ ├── 109.cpp │ ├── 11.cpp │ ├── 110.cpp │ ├── 111.cpp │ ├── 112.cpp │ ├── 113.cpp │ ├── 114.cpp │ ├── 116.cpp │ ├── 117.cpp │ ├── 118.cpp │ ├── 119.cpp │ ├── 120.cpp │ ├── 121.cpp │ ├── 122.cpp │ ├── 127.cpp │ ├── 128.cpp │ ├── 129.cpp │ ├── 13.cpp │ ├── 130.cpp │ ├── 131.cpp │ ├── 136.cpp │ ├── 138.cpp │ ├── 139.cpp │ ├── 14.cpp │ ├── 141.cpp │ ├── 142.cpp │ ├── 143.cpp │ ├── 144.cpp │ ├── 145.cpp │ ├── 15.cpp │ ├── 153.cpp │ ├── 155.cpp │ ├── 16.cpp │ ├── 160.cpp │ ├── 167.cpp │ ├── 169.cpp │ ├── 17.cpp │ ├── 173.cpp │ ├── 18.cpp │ ├── 189.cpp │ ├── 19.cpp │ ├── 191.cpp │ ├── 198.cpp │ ├── 199.cpp │ ├── 2.cpp │ ├── 20.cpp │ ├── 200.cpp │ ├── 203.cpp │ ├── 204.cpp │ ├── 206.cpp │ ├── 207.cpp │ ├── 208.cpp │ ├── 209.cpp │ ├── 21.cpp │ ├── 210.cpp │ ├── 211.cpp │ ├── 213.cpp │ ├── 215.cpp │ ├── 216.cpp │ ├── 217.cpp │ ├── 219.cpp │ ├── 22.cpp │ ├── 221.cpp │ ├── 222.cpp │ ├── 225.cpp │ ├── 226.cpp │ ├── 228.cpp │ ├── 230.cpp │ ├── 231.cpp │ ├── 232.cpp │ ├── 234.cpp │ ├── 235.cpp │ ├── 237.cpp │ ├── 238.cpp │ ├── 24.cpp │ ├── 242.cpp │ ├── 25.cpp │ ├── 257.cpp │ ├── 258.cpp │ ├── 26.cpp │ ├── 260.cpp │ ├── 263.cpp │ ├── 264.cpp │ ├── 268.cpp │ ├── 27.cpp │ ├── 278.cpp │ ├── 279.cpp │ ├── 28.cpp │ ├── 283.cpp │ ├── 287.cpp │ ├── 289.cpp │ ├── 290.cpp │ ├── 3.cpp │ ├── 300.cpp │ ├── 303.cpp │ ├── 304.cpp │ ├── 306.cpp │ ├── 307.cpp │ ├── 309.cpp │ ├── 31.cpp │ ├── 313.cpp │ ├── 322.cpp │ ├── 326.cpp │ ├── 328.cpp │ ├── 33.cpp │ ├── 337.cpp │ ├── 338.cpp │ ├── 34.cpp │ ├── 342.cpp │ ├── 343.cpp │ ├── 344.cpp │ ├── 347.cpp │ ├── 349.cpp │ ├── 35.cpp │ ├── 350.cpp │ ├── 357.cpp │ ├── 36.cpp │ ├── 367.cpp │ ├── 368.cpp │ ├── 37.cpp │ ├── 371.cpp │ ├── 374.cpp │ ├── 376.cpp │ ├── 378.cpp │ ├── 380.cpp │ ├── 389.cpp │ ├── 39.cpp │ ├── 392.cpp │ ├── 4.cpp │ ├── 40.cpp │ ├── 401.cpp │ ├── 404.cpp │ ├── 406.cpp │ ├── 409.cpp │ ├── 413.cpp │ ├── 414.cpp │ ├── 415.cpp │ ├── 416.cpp │ ├── 419.cpp │ ├── 429.cpp │ ├── 430.cpp │ ├── 435.cpp │ ├── 437.cpp │ ├── 438.cpp │ ├── 441.cpp │ ├── 442.cpp │ ├── 445.cpp │ ├── 447.cpp │ ├── 448.cpp │ ├── 449.cpp │ ├── 450.cpp │ ├── 452.cpp │ ├── 455.cpp │ ├── 46.cpp │ ├── 461.cpp │ ├── 463.cpp │ ├── 467.cpp │ ├── 47.cpp │ ├── 473.cpp │ ├── 474.cpp │ ├── 475.cpp │ ├── 476.cpp │ ├── 48.cpp │ ├── 485.cpp │ ├── 49.cpp │ ├── 492.cpp │ ├── 494.cpp │ ├── 495.cpp │ ├── 496.cpp │ ├── 5.cpp │ ├── 500.cpp │ ├── 501.cpp │ ├── 503.cpp │ ├── 506.cpp │ ├── 508.cpp │ ├── 509.cpp │ ├── 51.cpp │ ├── 513.cpp │ ├── 515.cpp │ ├── 516.cpp │ ├── 52.cpp │ ├── 520.cpp │ ├── 521.cpp │ ├── 523.cpp │ ├── 524.cpp │ ├── 526.cpp │ ├── 529.cpp │ ├── 53.cpp │ ├── 532.cpp │ ├── 537.cpp │ ├── 538.cpp │ ├── 54.cpp │ ├── 542.cpp │ ├── 543.cpp │ ├── 547.cpp │ ├── 55.cpp │ ├── 551.cpp │ ├── 556.cpp │ ├── 557.cpp │ ├── 559.cpp │ ├── 56.cpp │ ├── 560.cpp │ ├── 561.cpp │ ├── 563.cpp │ ├── 565.cpp │ ├── 566.cpp │ ├── 572.cpp │ ├── 575.cpp │ ├── 576.cpp │ ├── 58.cpp │ ├── 581.cpp │ ├── 589.cpp │ ├── 59.cpp │ ├── 590.cpp │ ├── 594.cpp │ ├── 598.cpp │ ├── 6.cpp │ ├── 60.cpp │ ├── 605.cpp │ ├── 606.cpp │ ├── 609.cpp │ ├── 61.cpp │ ├── 611.cpp │ ├── 617.cpp │ ├── 62.cpp │ ├── 621.cpp │ ├── 623.cpp │ ├── 628.cpp │ ├── 63.cpp │ ├── 637.cpp │ ├── 638.cpp │ ├── 64.cpp │ ├── 643.cpp │ ├── 646.cpp │ ├── 647.cpp │ ├── 648.cpp │ ├── 649.cpp │ ├── 650.cpp │ ├── 652.cpp │ ├── 653.cpp │ ├── 654.cpp │ ├── 657.cpp │ ├── 66.cpp │ ├── 661.cpp │ ├── 662.cpp │ ├── 665.cpp │ ├── 667.cpp │ ├── 669.cpp │ ├── 670.cpp │ ├── 671.cpp │ ├── 673.cpp │ ├── 674.cpp │ ├── 682.cpp │ ├── 684.cpp │ ├── 687.cpp │ ├── 69.cpp │ ├── 690.cpp │ ├── 693.cpp │ ├── 695.cpp │ ├── 696.cpp │ ├── 697.cpp │ ├── 698.cpp │ ├── 70.cpp │ ├── 700.cpp │ ├── 701.cpp │ ├── 703.cpp │ ├── 704.cpp │ ├── 707.cpp │ ├── 709.cpp │ ├── 71.cpp │ ├── 714.cpp │ ├── 717.cpp │ ├── 718.cpp │ ├── 724.cpp │ ├── 725.cpp │ ├── 728.cpp │ ├── 729.cpp │ ├── 73.cpp │ ├── 731.cpp │ ├── 733.cpp │ ├── 739.cpp │ ├── 74.cpp │ ├── 740.cpp │ ├── 743.cpp │ ├── 744.cpp │ ├── 746.cpp │ ├── 747.cpp │ ├── 748.cpp │ ├── 75.cpp │ ├── 754.cpp │ ├── 756.cpp │ ├── 762.cpp │ ├── 763.cpp │ ├── 764.cpp │ ├── 765.cpp │ ├── 766.cpp │ ├── 769.cpp │ ├── 77.cpp │ ├── 771.cpp │ ├── 775.cpp │ ├── 78.cpp │ ├── 783.cpp │ ├── 784.cpp │ ├── 785.cpp │ ├── 787.cpp │ ├── 788.cpp │ ├── 79.cpp │ ├── 791.cpp │ ├── 792.cpp │ ├── 795.cpp │ ├── 80.cpp │ ├── 801.cpp │ ├── 802.cpp │ ├── 804.cpp │ ├── 806.cpp │ ├── 807.cpp │ ├── 811.cpp │ ├── 813.cpp │ ├── 814.cpp │ ├── 817.cpp │ ├── 819.cpp │ ├── 82.cpp │ ├── 824.cpp │ ├── 825.cpp │ ├── 83.cpp │ ├── 830.cpp │ ├── 831.cpp │ ├── 832.cpp │ ├── 833.cpp │ ├── 834.cpp │ ├── 837.cpp │ ├── 838.cpp │ ├── 840.cpp │ ├── 841.cpp │ ├── 844.cpp │ ├── 846.cpp │ ├── 848.cpp │ ├── 849.cpp │ ├── 852.cpp │ ├── 853.cpp │ ├── 856.cpp │ ├── 859.cpp │ ├── 86.cpp │ ├── 860.cpp │ ├── 861.cpp │ ├── 863.cpp │ ├── 865.cpp │ ├── 867.cpp │ ├── 868.cpp │ ├── 869.cpp │ ├── 870.cpp │ ├── 872.cpp │ ├── 873.cpp │ ├── 875.cpp │ ├── 876.cpp │ ├── 877.cpp │ ├── 88.cpp │ ├── 880.cpp │ ├── 881.cpp │ ├── 883.cpp │ ├── 884.cpp │ ├── 885.cpp │ ├── 886.cpp │ ├── 888.cpp │ ├── 889.cpp │ ├── 89.cpp │ ├── 890.cpp │ ├── 892.cpp │ ├── 894.cpp │ ├── 896.cpp │ ├── 897.cpp │ ├── 9.cpp │ ├── 90.cpp │ ├── 900.cpp │ ├── 904.cpp │ ├── 905.cpp │ ├── 908.cpp │ ├── 911.cpp │ ├── 912.cpp │ ├── 914.cpp │ ├── 917.cpp │ ├── 919.cpp │ ├── 92.cpp │ ├── 921.cpp │ ├── 922.cpp │ ├── 925.cpp │ ├── 926.cpp │ ├── 929.cpp │ ├── 93.cpp │ ├── 931.cpp │ ├── 933.cpp │ ├── 938.cpp │ ├── 94.cpp │ ├── 941.cpp │ ├── 942.cpp │ ├── 944.cpp │ ├── 946.cpp │ ├── 947.cpp │ ├── 949.cpp │ ├── 95.cpp │ ├── 951.cpp │ ├── 953.cpp │ ├── 954.cpp │ ├── 957.cpp │ ├── 958.cpp │ ├── 96.cpp │ ├── 961.cpp │ ├── 962.cpp │ ├── 965.cpp │ ├── 966.cpp │ ├── 967.cpp │ ├── 969.cpp │ ├── 970.cpp │ ├── 971.cpp │ ├── 973.cpp │ ├── 974.cpp │ ├── 976.cpp │ ├── 977.cpp │ ├── 978.cpp │ ├── 979.cpp │ ├── 98.cpp │ ├── 981.cpp │ ├── 982.cpp │ ├── 983.cpp │ ├── 984.cpp │ ├── 985.cpp │ ├── 986.cpp │ ├── 987.cpp │ ├── 988.cpp │ ├── 989.cpp │ ├── 990.cpp │ ├── 991.cpp │ ├── 993.cpp │ ├── 994.cpp │ ├── 995.cpp │ ├── 997.cpp │ ├── 998.cpp │ └── 999.cpp ├── csharp │ └── 1025.cs ├── golang │ ├── 1.go │ ├── 1025.go │ ├── 551.go │ ├── 807.go │ ├── 892.go │ ├── 925.go │ ├── 985.go │ └── 991.go ├── java │ ├── 1025.java │ ├── 1064.java │ ├── 122.java │ ├── 263.java │ ├── 908.java │ └── 991.java ├── javascript │ └── 1025.js ├── mysql │ ├── 175.sql │ ├── 181.sql │ ├── 182.sql │ ├── 183.sql │ ├── 196.sql │ ├── 197.sql │ ├── 595.sql │ ├── 596.sql │ ├── 620.sql │ └── 627.sql ├── python │ ├── 1013.py │ ├── 1014.py │ ├── 1015.py │ ├── 1020.py │ ├── 1021.py │ ├── 1022.py │ ├── 1025.py │ ├── 1042.py │ ├── 1046.py │ ├── 1047.py │ ├── 1048.py │ ├── 1051.py │ ├── 1052.py │ ├── 1053.py │ ├── 1054.py │ ├── 1064.py │ ├── 1065.py │ ├── 1071.py │ ├── 1073.py │ ├── 116.py │ ├── 136.py │ ├── 138.py │ ├── 14.py │ ├── 141.py │ ├── 160.py │ ├── 231.py │ ├── 258.py │ ├── 263.py │ ├── 264.py │ ├── 28.py │ ├── 283.py │ ├── 290.py │ ├── 300.py │ ├── 313.py │ ├── 342.py │ ├── 406.py │ ├── 415.py │ ├── 448.py │ ├── 461.py │ ├── 506.py │ ├── 598.py │ ├── 609.py │ ├── 67.py │ ├── 709.py │ ├── 728.py │ ├── 791.py │ ├── 80.py │ ├── 806.py │ ├── 905.py │ ├── 908.py │ ├── 921.py │ ├── 938.py │ ├── 946.py │ └── 991.py └── python3 │ ├── 1.py │ ├── 100.py │ ├── 1002.py │ ├── 1003.py │ ├── 1006.py │ ├── 1009.py │ ├── 1010.py │ ├── 1011.py │ ├── 1012.py │ ├── 1013.py │ ├── 1014.py │ ├── 1015.py │ ├── 1017.py │ ├── 1018.py │ ├── 1019.py │ ├── 1020.py │ ├── 1021.py │ ├── 1022.py │ ├── 1023.py │ ├── 1024.py │ ├── 1025.py │ ├── 1026.py │ ├── 1029.py │ ├── 1030.py │ ├── 1031.py │ ├── 1033.py │ ├── 1036.py │ ├── 1038.py │ ├── 1042.py │ ├── 1046.py │ ├── 1047.py │ ├── 1048.py │ ├── 1051.py │ ├── 1052.py │ ├── 1053.py │ ├── 1054.py │ ├── 106.py │ ├── 1064.py │ ├── 1065.py │ ├── 1071.py │ ├── 1073.py │ ├── 11.py │ ├── 121.py │ ├── 122.py │ ├── 125.py │ ├── 127.py │ ├── 129.py │ ├── 130.py │ ├── 131.py │ ├── 136.py │ ├── 14.py │ ├── 145.py │ ├── 15.py │ ├── 153.py │ ├── 155.py │ ├── 16.py │ ├── 167.py │ ├── 169.py │ ├── 17.py │ ├── 171.py │ ├── 18.py │ ├── 19.py │ ├── 198.py │ ├── 200.py │ ├── 202.py │ ├── 205.py │ ├── 206.py │ ├── 208.py │ ├── 209.py │ ├── 213.py │ ├── 215.py │ ├── 216.py │ ├── 217.py │ ├── 231.py │ ├── 234.py │ ├── 238.py │ ├── 24.py │ ├── 240.py │ ├── 242.py │ ├── 257.py │ ├── 258.py │ ├── 26.py │ ├── 260.py │ ├── 263.py │ ├── 264.py │ ├── 279.py │ ├── 28.py │ ├── 283.py │ ├── 287.py │ ├── 289.py │ ├── 290.py │ ├── 300.py │ ├── 303.py │ ├── 307.py │ ├── 31.py │ ├── 313.py │ ├── 326.py │ ├── 33.py │ ├── 342.py │ ├── 343.py │ ├── 345.py │ ├── 347.py │ ├── 349.py │ ├── 376.py │ ├── 378.py │ ├── 380.py │ ├── 387.py │ ├── 39.py │ ├── 394.py │ ├── 398.py │ ├── 40.py │ ├── 406.py │ ├── 412.py │ ├── 413.py │ ├── 414.py │ ├── 415.py │ ├── 435.py │ ├── 438.py │ ├── 442.py │ ├── 445.py │ ├── 448.py │ ├── 452.py │ ├── 455.py │ ├── 46.py │ ├── 47.py │ ├── 485.py │ ├── 492.py │ ├── 503.py │ ├── 506.py │ ├── 509.py │ ├── 521.py │ ├── 524.py │ ├── 526.py │ ├── 528.py │ ├── 532.py │ ├── 54.py │ ├── 55.py │ ├── 551.py │ ├── 56.py │ ├── 560.py │ ├── 565.py │ ├── 566.py │ ├── 58.py │ ├── 59.py │ ├── 598.py │ ├── 599.py │ ├── 605.py │ ├── 609.py │ ├── 611.py │ ├── 62.py │ ├── 621.py │ ├── 633.py │ ├── 64.py │ ├── 645.py │ ├── 646.py │ ├── 647.py │ ├── 650.py │ ├── 652.py │ ├── 66.py │ ├── 662.py │ ├── 665.py │ ├── 667.py │ ├── 67.py │ ├── 670.py │ ├── 680.py │ ├── 695.py │ ├── 697.py │ ├── 70.py │ ├── 703.py │ ├── 704.py │ ├── 714.py │ ├── 725.py │ ├── 728.py │ ├── 731.py │ ├── 739.py │ ├── 74.py │ ├── 75.py │ ├── 763.py │ ├── 766.py │ ├── 769.py │ ├── 77.py │ ├── 771.py │ ├── 775.py │ ├── 78.py │ ├── 79.py │ ├── 791.py │ ├── 792.py │ ├── 795.py │ ├── 80.py │ ├── 806.py │ ├── 821.py │ ├── 83.py │ ├── 867.py │ ├── 870.py │ ├── 873.py │ ├── 88.py │ ├── 896.py │ ├── 9.py │ ├── 90.py │ ├── 908.py │ ├── 91.py │ ├── 914.py │ ├── 921.py │ ├── 925.py │ ├── 929.py │ ├── 93.py │ ├── 933.py │ ├── 938.py │ ├── 941.py │ ├── 942.py │ ├── 946.py │ ├── 96.py │ ├── 965.py │ ├── 966.py │ ├── 967.py │ ├── 969.py │ ├── 970.py │ ├── 973.py │ ├── 974.py │ ├── 976.py │ ├── 977.py │ ├── 978.py │ ├── 979.py │ ├── 98.py │ ├── 985.py │ ├── 986.py │ ├── 988.py │ ├── 989.py │ ├── 990.py │ └── 991.py ├── LeetCode_Archiver ├── LocalFile.py ├── Statistic.py ├── __init__.py ├── items.py ├── middlewares.py ├── pipelines.py ├── settings.py └── spiders │ ├── QuestionSetSpider.py │ └── __init__.py ├── README.md ├── config_template.json ├── main.py ├── requirements.sh └── scrapy.cfg /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vscode/ 3 | .log 4 | config.json 5 | Dockerfile 6 | requirements.txt 7 | LeetCode_Archiver/__pycache__ 8 | LeetCode_Archiver/spiders/__pycache__ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "3.6" 4 | script: 5 | - python main.py -------------------------------------------------------------------------------- /LeetCode/DifficultyFigure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chr1sc2y/LeetCode_Archiver/de23e16ead29336b5ee7aa1898a392a5d6463d27/LeetCode/DifficultyFigure.png -------------------------------------------------------------------------------- /LeetCode/LanguageFigure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chr1sc2y/LeetCode_Archiver/de23e16ead29336b5ee7aa1898a392a5d6463d27/LeetCode/LanguageFigure.png -------------------------------------------------------------------------------- /LeetCode/TopicFigure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chr1sc2y/LeetCode_Archiver/de23e16ead29336b5ee7aa1898a392a5d6463d27/LeetCode/TopicFigure.png -------------------------------------------------------------------------------- /LeetCode/c/1025.c: -------------------------------------------------------------------------------- 1 | bool divisorGame(int N) { 2 | return N % 2 == 0 ? true : false; 3 | } -------------------------------------------------------------------------------- /LeetCode/c/1064.c: -------------------------------------------------------------------------------- 1 | int fixedPoint(int* A, int ASize){ 2 | for (int i = 0; i < ASize; ++i) 3 | if (A[i] == i) 4 | return i; 5 | return -1; 6 | } -------------------------------------------------------------------------------- /LeetCode/c/263.c: -------------------------------------------------------------------------------- 1 | bool isUgly(int num){ 2 | if (num == 1) 3 | return true; 4 | while (true) { 5 | if (num == 2 || num == 3 || num == 5) 6 | return true; 7 | int ori = num; 8 | if (ori % 2 == 0) 9 | ori /= 2; 10 | else if (ori % 3 == 0) 11 | ori /= 3; 12 | else if (ori % 5 == 0) 13 | ori /= 5; 14 | if (ori == num) 15 | return false; 16 | num = ori; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/c/342.c: -------------------------------------------------------------------------------- 1 | bool isPowerOfFour(int num){ 2 | if (num <= 0) 3 | return false; 4 | while (num > 1) { 5 | if ((num & 1) || (num & 2)) 6 | return false; 7 | num >>= 2; 8 | } 9 | return true; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /LeetCode/c/598.c: -------------------------------------------------------------------------------- 1 | int maxCount(int m, int n, int** ops, int opsSize, int* opsColSize){ 2 | for (int i = 0; i < opsSize; ++i) { 3 | m = m < ops[i][0] ? m :ops[i][0]; 4 | n = n < ops[i][1] ? n :ops[i][1]; 5 | } 6 | return m * n; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /LeetCode/c/908.c: -------------------------------------------------------------------------------- 1 | int smallestRangeI(int* A, int ASize, int K){ 2 | int max_val = 0, min_val = 10000; 3 | for (int i = 0; i < ASize; ++i) { 4 | max_val = max_val > A[i] ? max_val : A[i]; 5 | min_val = min_val < A[i] ? min_val : A[i]; 6 | } 7 | int res = max_val - min_val - 2 * K; 8 | return 0 > res ? 0 : res; 9 | } -------------------------------------------------------------------------------- /LeetCode/c/991.c: -------------------------------------------------------------------------------- 1 | int brokenCalc(int X, int Y) { 2 | if (X >= Y) 3 | return X - Y; 4 | return Y % 2 ? brokenCalc(X, Y + 1) + 1 : brokenCalc(X, Y / 2) + 1; 5 | } -------------------------------------------------------------------------------- /LeetCode/cpp/1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector &nums, int target) { 4 | unordered_map u; 5 | for (int i = 0; i < nums.size(); ++i) { 6 | int &&temp = target - nums[i]; 7 | if (u.find(temp) != u.end()) 8 | return {u[temp], i}; 9 | u.insert(pair(nums[i], i)); 10 | } 11 | return {}; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/100.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | bool isSameTree(TreeNode* p, TreeNode* q) { 13 | if (!p && !q) 14 | return true; 15 | else if (!p || !q) 16 | return false; 17 | return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right); 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/1003.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string &S) { 4 | auto iter = S.find("abc"); 5 | while (iter != string::npos) { 6 | S.erase(iter, 3); 7 | iter = S.find("abc"); 8 | } 9 | return S.empty(); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode/cpp/1009.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bitwiseComplement(int N) { 4 | if (N == 0) 5 | return 1; 6 | int m = 1, res = 0; 7 | while (N) { 8 | res += m * (N % 2 ^ 1); 9 | N >>= 1; 10 | m <<= 1; 11 | } 12 | return res; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode/cpp/101.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSymmetric(TreeNode *root) { 4 | if (!root) 5 | return true; 6 | return compareNode(root->left, root->right); 7 | } 8 | 9 | bool compareNode(TreeNode *p, TreeNode *q) { 10 | if (!p && !q) 11 | return true; 12 | else if (!p || !q || p->val != q->val) 13 | return false; 14 | return compareNode(p->left, q->right) && compareNode(p->right, q->left); 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/1010.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numPairsDivisibleBy60(vector &time) { 4 | unordered_map match; 5 | int total = 0, n = time.size(); 6 | for (auto &t:time) { 7 | int target = (60 - t % 60) % 60; 8 | if (match.find(target) != match.end()) 9 | total += match[target]; 10 | ++match[t % 60]; 11 | } 12 | return total; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode/cpp/1012.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bitwiseComplement(int N) { 4 | if (N == 0) 5 | return 1; 6 | int m = 1, res = 0; 7 | while (N) { 8 | res += m * (N % 2 ^ 1); 9 | N >>= 1; 10 | m <<= 1; 11 | } 12 | return res; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode/cpp/1013.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canThreePartsEqualSum(vector& A) { 4 | int sum = 0, n = A.size(); 5 | for (int i = 0; i < n; ++i) 6 | sum += A[i]; 7 | if (sum % 3 != 0) 8 | return false; 9 | int part = 0, cum = 0; 10 | sum /= 3; 11 | for (int i = 0; i < n; ++i) { 12 | cum += A[i]; 13 | if (cum == sum) { 14 | ++part; 15 | cum = 0; 16 | } 17 | } 18 | return (part == 3 && cum == 0); 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/1014.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxScoreSightseeingPair(vector &A) { 4 | int prev = A[0] + 0, curr = INT_MIN; 5 | for (int i = 1; i < A.size(); ++i) { 6 | curr = max(curr, A[i] - i + prev); 7 | prev = max(prev, A[i] + i); 8 | } 9 | return curr; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode/cpp/1015.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestRepunitDivByK(int K) { 4 | int n = 0; 5 | for (int i = 1; i <= K; ++i) { 6 | n = n * 10 + 1; 7 | n %= K; 8 | if (n == 0) 9 | return i; 10 | } 11 | return -1; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/cpp/1017.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string baseNeg2(int N) { 4 | if (N == 0 || N == 1) 5 | return to_string(N); 6 | vector res; 7 | while (N) { 8 | res.push_back(N & 1); 9 | N = -(N >> 1); 10 | } 11 | string ret; 12 | for (int i = res.size() - 1; i >= 0; --i) 13 | ret += to_string(res[i]); 14 | return ret; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/cpp/1018.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector prefixesDivBy5(vector &A) { 4 | int sum = 0; 5 | vector ret(A.size(), false); 6 | for (int i = 0; i < A.size(); ++i) { 7 | sum = (sum * 2 + A[i]) % 5; 8 | ret[i] = sum == 0 ? true : false; 9 | } 10 | return ret; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/cpp/1021.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeOuterParentheses(string S) { 4 | string ret; 5 | int empty = 0, start = 0; 6 | for (int i = 0; i < S.size(); ++i) { 7 | char c = S[i]; 8 | if (c == '(') 9 | ++empty; 10 | else if (c == ')') 11 | --empty; 12 | if (empty == 0) { 13 | ret += S.substr(start + 1, i - start - 1); 14 | start = i + 1; 15 | } 16 | } 17 | return ret; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/cpp/1025.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool divisorGame(int N) { 4 | return N % 2 == 0 ? true : false; 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /LeetCode/cpp/1027.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestArithSeqLength(vector &A) { 4 | int n = A.size(), res = 0; 5 | vector> dp(n); 6 | for (int i = 1; i < n; ++i) { 7 | for (int j = 0; j < i; ++j) { 8 | int diff = A[i] - A[j]; 9 | dp[i][diff] = dp[j][diff] + 1; 10 | res = max(res, dp[i][diff]); 11 | } 12 | } 13 | return res + 1; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/cpp/1029.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int twoCitySchedCost(vector> &costs) { 4 | int sum = 0, n = costs.size(); 5 | sort(costs.begin(), costs.end(), [](const vector &c1, const vector &c2) { 6 | return c1[0] - c1[1] < c2[0] - c2[1]; 7 | }); 8 | for (int i = 0; i < n; ++i) 9 | sum += (i < n / 2 ? costs[i][0] : costs[i][1]); 10 | return sum; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/cpp/1035.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxUncrossedLines(vector &A, vector &B) { 4 | int m = A.size(), n = B.size(); 5 | vector> dp(m + 1, vector(n + 1, 0)); 6 | for (int i = 1; i <= m; ++i) { 7 | for (int j = 1; j <= n; ++j) { 8 | if (A[i - 1] == B[j - 1]) 9 | dp[i][j] = dp[i - 1][j - 1] + 1; 10 | else 11 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 12 | } 13 | } 14 | return dp[m][n]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/cpp/1037.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isBoomerang(vector> &points) { 4 | if (points[0] == points[1] || points[0] == points[2] || points[1] == points[2]) 5 | return false; 6 | int x1 = points[0][0], y1 = points[0][1], x2 = points[1][0], y2 = points[1][1], x3 = points[2][0], y3 = points[2][1]; 7 | if (x1 == x2 && x2 == x3) 8 | return false; 9 | if (1.0 * (y2 - y1) / (x2 - x1) == 1.0 * (y3 - y2) / (x3 - x2)) 10 | return false; 11 | return true; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/cpp/1038.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode *bstToGst(TreeNode *root) { 4 | Traverse(root, 0); 5 | return root; 6 | } 7 | 8 | int Traverse(TreeNode *node, int val) { 9 | if (!node) 10 | return val; 11 | int right = Traverse(node->right, val); 12 | node->val += right; 13 | int left = Traverse(node->left, node->val); 14 | return left; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/cpp/104.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDepth(TreeNode *root) { 4 | if (!root) 5 | return 0; 6 | return 1 + max(maxDepth(root->left), maxDepth(root->right)); 7 | } 8 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/1046.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lastStoneWeight(vector &stones) { 4 | priority_queue heap; 5 | for (auto &stone:stones) 6 | heap.push(stone); 7 | while (!heap.empty()) { 8 | int i = heap.top(); 9 | heap.pop(); 10 | if (heap.empty()) 11 | return i; 12 | int j = heap.top(); 13 | heap.pop(); 14 | heap.push(i - j); 15 | } 16 | return heap.top(); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/cpp/1047.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string S) { 4 | string res; 5 | for (int i = 0; i < S.size(); ++i) { 6 | if (res.empty() || res.back() != S[i]) 7 | res += S[i]; 8 | else 9 | res.pop_back(); 10 | } 11 | return res; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/cpp/1051.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int heightChecker(vector &heights) { 4 | auto r = heights; 5 | sort(r.begin(), r.end()); 6 | int res = 0; 7 | for (int i = 0; i < r.size(); ++i) 8 | res += (heights[i] != r[i]); 9 | return res; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode/cpp/1064.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fixedPoint(vector& A) { 4 | for(int i = 0; i> indexPairs(string &text, vector &words) { 4 | vector> res; 5 | for (auto &word:words) { 6 | int i = 0; 7 | int pos = text.find(word); 8 | while (pos != -1) { 9 | res.push_back(vector{pos, static_cast(pos + word.size() - 1)}); 10 | i = pos + 1; 11 | pos = text.find(word, i); 12 | } 13 | } 14 | sort(res.begin(), res.end()); 15 | return res; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/cpp/108.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode *sortedArrayToBST(vector &nums) { 4 | int size = nums.size(); 5 | return helper(nums, 0, size); 6 | } 7 | 8 | TreeNode *helper(vector &nums, const int &head, const int &tail) { 9 | if (head >= tail) 10 | return nullptr; 11 | auto &&index = head + (tail - head) / 2; 12 | TreeNode *cur = new TreeNode(nums[index]); 13 | cur->left = helper(nums, head, index); 14 | cur->right = helper(nums, index + 1, tail); 15 | return cur; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/11.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector &height) { 4 | int i = 0, j = height.size() - 1; 5 | int maxAre = 0; 6 | while (i < j) { 7 | maxAre = max(maxAre, min(height[i], height[j]) * (j - i)); 8 | if (height[i] > height[j]) 9 | --j; 10 | else 11 | ++i; 12 | } 13 | return maxAre; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/cpp/110.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isBalanced(TreeNode *root) { 4 | if (!root) 5 | return true; 6 | int &&left = maxHeight(root->left), &&right = maxHeight(root->right); 7 | return (abs(left - right) <= 1 && isBalanced(root->left) && isBalanced(root->right)); 8 | } 9 | 10 | int maxHeight(TreeNode *node) { 11 | if (!node) 12 | return 0; 13 | return 1 + max(maxHeight(node->left), maxHeight(node->right)); 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/112.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasPathSum(TreeNode *root, int sum) { 4 | if (!root) 5 | return false; 6 | if (sum - root->val == 0 && !root->left && !root->right) 7 | return true; 8 | return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/114.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void flatten(TreeNode *root) { 4 | while (root) { 5 | TreeNode *temp = root->left; 6 | while (temp && temp->right) { 7 | temp = temp->right; 8 | } 9 | if (temp) { 10 | temp->right = root->right; 11 | root->right = root->left; 12 | root->left = nullptr; 13 | } 14 | root = root->right; 15 | } 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/cpp/118.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generate(int numRows) { 4 | int n = numRows; 5 | vector> ret; 6 | for (int i = 0; i < n; ++i) { 7 | vector temp; 8 | for (int j = 0; j < i + 1; ++j) { 9 | if (j == 0 || j == i) 10 | temp.push_back(1); 11 | else 12 | temp.push_back(ret[i - 1][j - 1] + ret[i - 1][j]); 13 | } 14 | ret.push_back(temp); 15 | } 16 | return ret; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/120.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumTotal(vector> &triangle) { 4 | int n = triangle.size(); 5 | vector> dp(n, vector(n, 0)); 6 | for (int j = 0; j < n; ++j) 7 | dp[n - 1][j] = triangle[n - 1][j]; 8 | for (int i = n - 2; i >= 0; --i) 9 | for (int j = 0; j < n; ++j) 10 | dp[i][j] = triangle[i][j] + min(dp[i + 1][j], dp[i + 1][j + 1]); 11 | return dp[0][0]; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/121.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int profit = 0, min_val = INT_MAX; 5 | for(int i = 0; i < prices.size(); ++i) { 6 | min_val = min(min_val, prices[i]); 7 | profit = max(profit, prices[i] - min_val); 8 | } 9 | return profit; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/136.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector &nums) { 4 | unordered_set single; 5 | for (auto num: nums) { 6 | if (single.find(num) == single.end()) 7 | single.insert(num); 8 | else 9 | single.erase(num); 10 | } 11 | for (auto s : single) 12 | return s; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/14.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestCommonPrefix(vector &strs) { 4 | string res; 5 | if (strs.size() == 0) 6 | return res; 7 | for (int i = 0; i < strs[0].size(); ++i) { 8 | for (int j = 1; j < strs.size(); ++j) { 9 | if (i >= strs[j].size() || strs[j][i] != strs[0][i]) 10 | return res; 11 | } 12 | res += strs[0][i]; 13 | } 14 | return res; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/cpp/141.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasCycle(ListNode *head) { 4 | if(!head || !head->next) 5 | return false; 6 | auto slow = head->next, fast = head->next->next; 7 | while (fast && fast->next && fast != slow) { 8 | slow = slow->next; 9 | fast = fast->next->next; 10 | } 11 | return slow == fast; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/cpp/144.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector preorderTraversal(TreeNode *root) { 4 | if (!root) 5 | return {}; 6 | vector vec; 7 | stack que; 8 | que.push(root); 9 | while (!que.empty()) { 10 | TreeNode *temp = que.top(); 11 | vec.push_back(temp->val); 12 | que.pop(); 13 | if (temp->right) que.push(temp->right); 14 | if (temp->left) que.push(temp->left); 15 | } 16 | return vec; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/153.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector nums) { 4 | int i = 0, j = nums.size() - 1, mid = 0; 5 | if (j == -1) 6 | return 0; 7 | while (i < j - 1) { 8 | mid = i + (j - i) / 2; 9 | if (nums[mid] > nums[i] && nums[mid] > nums[j]) 10 | i = mid; 11 | else if (nums[mid] < nums[i] && nums[mid] < nums[j]) 12 | j = mid; 13 | else 14 | return nums[i]; 15 | } 16 | return min(nums[i], nums[j]); 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/160.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 4 | ListNode *a = headA, *b = headB; 5 | while (a != b) { 6 | a = a ? a->next : headB; 7 | b = b ? b->next : headA; 8 | } 9 | return a; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/167.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector &numbers, int target) { 4 | vector ret; 5 | int i = 0, j = numbers.size() - 1; 6 | while (i < j) { 7 | if (numbers[i] + numbers[j] == target) 8 | break; 9 | else if (numbers[i] + numbers[j] > target) 10 | --j; 11 | else 12 | ++i; 13 | } 14 | ret.push_back(i + 1); 15 | ret.push_back(j + 1); 16 | return ret; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/cpp/169.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector &nums) { 4 | int maj = 0, max_n = 0; 5 | unordered_map m; 6 | for (auto n : nums) { 7 | if (m.find(n) == m.end()) 8 | m[n] = 0; 9 | ++m[n]; 10 | if (max_n < m[n]) { 11 | max_n = m[n]; 12 | maj = n; 13 | } 14 | } 15 | return maj; 16 | } 17 | }; 18 | 19 | -------------------------------------------------------------------------------- /LeetCode/cpp/189.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector &nums, int k) { 4 | k %= nums.size(); 5 | while (k > 0) { 6 | int &temp = nums.back(); 7 | nums.pop_back(); 8 | nums.insert(nums.begin(), temp); 9 | --k; 10 | } 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/cpp/19.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *removeNthFromEnd(ListNode *head, int n) { 4 | ListNode *l = new ListNode(0); 5 | l->next = head; 6 | ListNode *fast = head, *slow = head; 7 | ListNode *prev = l; 8 | while (n > 0) { 9 | fast = fast->next; 10 | --n; 11 | } 12 | while (fast) { 13 | fast = fast->next; 14 | prev = slow; 15 | slow = slow->next; 16 | } 17 | prev->next = slow->next; 18 | return l->next; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode/cpp/191.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | int m = 8 * sizeof(uint32_t); 5 | int count = 0; 6 | while (m > 0) { 7 | count += 1 & n; 8 | n = n >> 1; 9 | --m; 10 | } 11 | return count; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/203.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *removeElements(ListNode *head, int val) { 4 | while (head && head->val == val) 5 | head = head->next; 6 | ListNode *curr = head; 7 | while (curr && curr->next) { 8 | if (curr->next->val == val) 9 | curr->next = curr->next->next; 10 | else 11 | curr = curr->next; 12 | } 13 | return head; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/204.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPrimes(int n) { 4 | if(n <= 2) 5 | return 0; 6 | int count = 1; 7 | vector isPrime(n, true); 8 | for(int i = 3; i < n; i += 2) { 9 | if(isPrime[i]) { 10 | ++count; 11 | if(i > sqrt(n)) 12 | continue; 13 | for(int j = i * i; j < n; j += 2 * i) 14 | if(isPrime[j]) 15 | isPrime[j] = false; 16 | } 17 | } 18 | return count; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/206.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *reverseList(ListNode *head) { 4 | if (!head || !head->next) 5 | return head; 6 | ListNode *pre = nullptr, *cur = head, *nex = nullptr; 7 | while (cur) { 8 | nex = cur->next; 9 | cur->next = pre; 10 | pre = cur; 11 | cur = nex; 12 | } 13 | return pre; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/cpp/209.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSubArrayLen(int s, vector &nums) { 4 | int sum = 0, i = 0, j = 0, size = nums.size(), min_len = INT_MAX; 5 | if (size == 0) 6 | return 0; 7 | while (j < size) { 8 | sum += nums[j]; 9 | while (sum >= s) { 10 | sum -= nums[i]; 11 | ++i; 12 | min_len = min(min_len, j - i + 2); 13 | } 14 | ++j; 15 | } 16 | return i == 0 ? 0 : min_len; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/215.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthLargest(vector &nums, int k) { 4 | priority_queue, greater<>> min_heap; 5 | for (auto &m : nums) { 6 | if (min_heap.size() < k || min_heap.top() < m) 7 | min_heap.push(m); 8 | if (min_heap.size() > k) 9 | min_heap.pop(); 10 | } 11 | return min_heap.top(); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/cpp/217.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsDuplicate(vector &nums) { 4 | unordered_set table; 5 | for (auto n: nums) { 6 | if (table.find(n) == table.end()) 7 | table.insert(n); 8 | else 9 | return true; 10 | } 11 | return false; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/219.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsNearbyDuplicate(vector &nums, int k) { 4 | int n = nums.size(); 5 | if (n <= 1) 6 | return false; 7 | map dup; 8 | for (int i = 0; i < n; ++i) { 9 | if ((dup.find(nums[i]) != dup.end()) && (abs(i - dup[nums[i]]) <= k)) 10 | return true; 11 | dup[nums[i]] = i; 12 | } 13 | return false; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/226.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode *invertTree(TreeNode *root) { 4 | if (!root) 5 | return nullptr; 6 | queue que; 7 | que.push(root); 8 | while (!que.empty()) { 9 | TreeNode *temp = que.front(); 10 | swap(temp->left, temp->right); 11 | que.pop(); 12 | if (temp->left) que.push(temp->left); 13 | if (temp->right) que.push(temp->right); 14 | } 15 | return root; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/230.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector vec; 4 | 5 | int kthSmallest(TreeNode *root, int k) { 6 | stack stk; 7 | while (root || !stk.empty()) { 8 | if (root) { 9 | stk.push(root); 10 | root = root->left; 11 | } else { 12 | root = stk.top(); 13 | stk.pop(); 14 | --k; 15 | if (k == 0) 16 | return root->val; 17 | root = root->right; 18 | } 19 | } 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/cpp/231.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfTwo(int n) { 4 | if (n <= 0) 5 | return false; 6 | while ((n & 1) == 0) 7 | n >>= 1; 8 | return n == 1; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LeetCode/cpp/237.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void deleteNode(ListNode *node) { 4 | node->val = node->next->val; 5 | node->next = node->next->next; 6 | } 7 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/238.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector productExceptSelf(vector &nums) { 4 | vector ret(nums.size(), 1); 5 | int temp = 1; 6 | for (int i = 0; i < nums.size() - 1; ++i) 7 | ret[i + 1] = ret[i] * nums[i]; 8 | for (int i = nums.size() - 2; i >= 0; --i) { 9 | temp *= nums[i + 1]; 10 | ret[i] *= temp; 11 | } 12 | return ret; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode/cpp/258.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int num) { 4 | while (num > 9) { 5 | int sum = 0, temp = num; 6 | while (temp > 0) { 7 | sum += temp % 10; 8 | temp /= 10; 9 | } 10 | num = sum; 11 | } 12 | return num; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode/cpp/26.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector &nums) { 4 | auto size = nums.size(); 5 | if (!size) 6 | return 0; 7 | int i = 0, j = 1; 8 | while (j < size) { 9 | if (nums[j] != nums[i]) 10 | nums[++i] = nums[j]; 11 | ++j; 12 | } 13 | return i + 1; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/cpp/260.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector singleNumber(vector &nums) { 4 | int x = 0, s = 1; 5 | for (auto m:nums) 6 | x ^= m; 7 | while ((x & s) == 0) 8 | s <<= 1; 9 | vector ret(2, 0); 10 | for (auto m:nums) 11 | (m & s) == 0 ? ret[0] ^= m : ret[1] ^= m; 12 | return ret; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode/cpp/263.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isUgly(int num) { 4 | if (num < 1) 5 | return false; 6 | while (num % 2 == 0) 7 | num /= 2; 8 | while (num % 3 == 0) 9 | num /= 3; 10 | while (num % 5 == 0) 11 | num /= 5; 12 | if (num == 1) 13 | return true; 14 | return false; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/cpp/268.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int missingNumber(vector &nums) { 4 | int n = nums.size(); 5 | int res = n; 6 | for (int i = 0; i < n; ++i) 7 | res ^= nums[i] ^ i; 8 | return res; 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/27.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeElement(vector& nums, int val) { 4 | int n = 0; 5 | for (int i = 0; i < nums.size(); ++i) { 6 | if (nums[i] != val) 7 | ++n; 8 | else { 9 | nums.erase(nums.begin() + i); 10 | --i; 11 | } 12 | } 13 | return n; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/278.cpp: -------------------------------------------------------------------------------- 1 | bool isBadVersion(int version); 2 | 3 | class Solution { 4 | public: 5 | int firstBadVersion(int n) { 6 | int i = 1, j = n, &&mid = 0; 7 | while (i <= j) { 8 | mid = i + (j - i) / 2; 9 | if (isBadVersion(mid)) 10 | j = mid - 1; 11 | else 12 | i = mid + 1; 13 | } 14 | return i; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/cpp/279.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSquares(int n) { 4 | vector dp(n + 1, INT_MAX); 5 | dp[0] = 0, dp[1] = 1; 6 | for (int i = 2; i <= n; ++i) { 7 | for (int j = 1; j * j <= i; ++j) 8 | dp[i] = min(dp[i], dp[i - j * j] + 1); 9 | } 10 | return dp[n]; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/cpp/28.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int strStr(string &haystack, string &needle) { 4 | int n = haystack.size(), m = needle.size(); 5 | if (m == 0) 6 | return 0; 7 | for (int i = 0; i < n; ++i) { 8 | if (i + m > n) 9 | return -1; 10 | int j = 0; 11 | while (j < m && haystack[i + j] == needle[j]) 12 | ++j; 13 | if (j == m) 14 | return i; 15 | } 16 | return -1; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/cpp/283.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector &nums) { 4 | int index = 0; 5 | for (int i = 0; i < nums.size(); ++i) 6 | if (nums[i] != 0) 7 | swap(nums[index++], nums[i]); 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/287.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(vector &nums) { 4 | auto size = nums.size(); 5 | if (size == 0) 6 | return 0; 7 | int fast = nums[0], slow = nums[0]; 8 | do { 9 | fast = nums[nums[fast]]; 10 | slow = nums[slow]; 11 | } while (fast != slow); 12 | fast = nums[0]; 13 | while (fast != slow) { 14 | slow = nums[slow]; 15 | fast = nums[fast]; 16 | } 17 | return slow; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/cpp/3.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | vector pos(200, -1); 5 | int start = 0, res = 0; 6 | for (int i = 0; i < s.size(); ++i) { 7 | if (pos[s[i]] != -1) { 8 | res = max(res, i - start); 9 | start = max(start, pos[s[i]] + 1); 10 | } 11 | pos[s[i]] = i; 12 | } 13 | res = max(res, static_cast(s.size()) - start); 14 | return res; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/cpp/303.cpp: -------------------------------------------------------------------------------- 1 | class NumArray { 2 | public: 3 | vector sum; 4 | NumArray(vector nums) { 5 | int size = nums.size(); 6 | sum = vector(size + 1, 0); 7 | for (int i = 0; i < size; ++i) 8 | sum[i + 1] = sum[i] + nums[i]; 9 | } 10 | int sumRange(int i, int j) { 11 | return sum[j + 1] - sum[i]; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/326.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfThree(int n) { 4 | if (n <= 0) 5 | return false; 6 | while (n % 3 == 0) 7 | n /= 3; 8 | return n == 1; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LeetCode/cpp/328.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *oddEvenList(ListNode *head) { 4 | if (!head) 5 | return nullptr; 6 | ListNode *odd = head, *even = head->next, *evenHead = head->next; 7 | while (odd && even) { 8 | odd->next = even->next; 9 | if (odd->next) { 10 | even->next = odd->next->next; 11 | odd = odd->next; 12 | } 13 | even = even->next; 14 | } 15 | odd->next = evenHead; 16 | return head; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/338.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int num) { 4 | vector count(num + 1, 0); 5 | for (int i = 1; i <= num; ++i) 6 | count[i] = count[i / 2] + (i % 2); 7 | return count; 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/342.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfFour(int num) { 4 | if (num <= 0) 5 | return false; 6 | while (num > 1) { 7 | if ((num & 1) || (num & 2)) 8 | return false; 9 | num >>= 2; 10 | } 11 | return true; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/cpp/344.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseString(string s) { 4 | int n = s.size(); 5 | for (int i = 0; i < n / 2; ++i) 6 | swap(s[i], s[n - i - 1]); 7 | return s; 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/349.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersection(vector &nums1, vector &nums2) { 4 | unordered_set s; 5 | vector nums; 6 | for (auto n: nums1) 7 | if (s.find(n) == s.end()) 8 | s.insert(n); 9 | for (auto n:nums2) { 10 | if (s.find(n) != s.end()) { 11 | nums.push_back(n); 12 | s.erase(n); 13 | } 14 | } 15 | return nums; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/35.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int searchInsert(vector &nums, int target) { 4 | int i = 0, j = nums.size() - 1, index = 0; 5 | while (i <= j) { 6 | index = (i + j) / 2; 7 | if (nums[index] < target) 8 | i = index + 1; 9 | else if (nums[index] > target) 10 | j = index - 1; 11 | else 12 | return index; 13 | } 14 | return i; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/350.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersect(vector &nums1, vector &nums2) { 4 | unordered_map map1; 5 | for (int i = 0; i < nums1.size(); ++i) 6 | ++map1[nums1[i]]; 7 | for (int i = 0; i < nums2.size(); ++i) { 8 | if (map1[nums2[i]] > 0) 9 | --map1[nums2[i]]; 10 | else { 11 | nums2.erase(nums2.begin() + i); 12 | --i; 13 | } 14 | } 15 | return nums2; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/357.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countNumbersWithUniqueDigits(int n) { 4 | std::vector unique; 5 | unique.push_back(1); 6 | int sum = 1; 7 | for (int i = 1; i <= n; ++i) { 8 | int uni = 9; 9 | for (int j = 1; j < i; ++j) 10 | uni *= (10 - j); 11 | sum += uni; 12 | unique.push_back(sum); 13 | } 14 | return unique[n]; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/367.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPerfectSquare(int num) { 4 | long long i = 0, j = num, &&mid = 0, &&square = 0; 5 | while (i <= j) { 6 | mid = (i + j) / 2; 7 | square = mid * mid; 8 | if (square == num) 9 | return true; 10 | else if (square < num) 11 | i = mid + 1; 12 | else 13 | j = mid - 1; 14 | } 15 | return false; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/371.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getSum(int a, int b) { 4 | int sum = a; 5 | while (b != 0) { 6 | sum = a ^ b; 7 | b = (a & b) << 1; 8 | a = sum; 9 | } 10 | return sum; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/cpp/389.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char findTheDifference(string s, string t) { 4 | char sumS = 0, sumT = 0; 5 | for(int i = 0; i < s.size(); ++i) { 6 | sumS += s[i]; 7 | sumT += t[i]; 8 | } 9 | return sumT + t[s.size()] -sumS; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/392.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string t) { 4 | int i = 0, j = 0; 5 | while (i < s.size() && j < t.size()) { 6 | if (s[i] == t[j]) 7 | ++i; 8 | ++j; 9 | } 10 | if (i != s.size()) 11 | return false; 12 | return true; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode/cpp/404.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumOfLeftLeaves(TreeNode *root) { 4 | if (!root) 5 | return 0; 6 | int sum = 0; 7 | if (root->left && !root->left->left && !root->left->right) 8 | sum += root->left->val; 9 | sum += sumOfLeftLeaves(root->left); 10 | sum += sumOfLeftLeaves(root->right); 11 | return sum; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/409.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestPalindrome(string s) { 4 | unordered_map m; 5 | int num = 0, odd = 0; 6 | for (auto c:s) 7 | ++m[c]; 8 | for (auto iter:m) { 9 | num += 2 * (iter.second / 2); 10 | if (odd == 0) 11 | odd = iter.second % 2; 12 | } 13 | return num + odd; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/413.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfArithmeticSlices(vector &A) { 4 | int size = A.size(); 5 | if (size <= 2) return 0; 6 | int len = 0, num = 0; 7 | for (int i = 1; i < size - 1; ++i) { 8 | if (A[i] - A[i - 1] == A[i + 1] - A[i]) 9 | ++len; 10 | else { 11 | num += len * (len + 1) / 2; 12 | len = 0; 13 | } 14 | } 15 | if (len) 16 | num += len * (len + 1) / 2; 17 | return num; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/414.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int thirdMax(vector &nums) { 4 | set se; 5 | for (auto n:nums) 6 | se.insert(n); 7 | return se.size() >= 3 ? *(--(--(--se.end()))) : *(--se.end()); 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LeetCode/cpp/419.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countBattleships(vector>& board) { 4 | int m = board.size(), n = m == 0 ? 0 : board[0].size(), res = 0; 5 | for (int i = 0; i < m; ++i) 6 | for (int j = 0; j < n; ++j) 7 | if (board[i][j] == 'X' && (i + 1 == m || board[i + 1][j] == '.') && (j + 1 == n || board[i][j + 1] == '.')) 8 | ++res; 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/441.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrangeCoins(int n) { 4 | int &&i = 1; 5 | while (n > 0) 6 | n -= i++; 7 | return n < 0 ? i - 2 : i - 1; 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/442.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDuplicates(vector& nums) { 4 | int size = nums.size(); 5 | vector ret; 6 | for (int i = 0; i < size; ++i) { 7 | nums[abs(nums[i]) - 1] = -nums[abs(nums[i]) - 1]; 8 | if (nums[abs(nums[i]) - 1] > 0) 9 | ret.push_back(abs(nums[i])); 10 | } 11 | return ret; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/cpp/448.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDisappearedNumbers(vector &nums) { 4 | auto size = nums.size(); 5 | for (int i = 0; i < size; ++i) 6 | while (nums[i] != i + 1 && nums[i] != nums[nums[i] - 1]) 7 | swap(nums[i], nums[nums[i] - 1]); 8 | vector ret; 9 | for (int i = 0; i < size; ++i) 10 | if (nums[i] != i + 1) 11 | ret.push_back(i + 1); 12 | return ret; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/455.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findContentChildren(vector& g, vector& s) { 4 | sort(g.begin(), g.end()); 5 | sort(s.begin(), s.end()); 6 | int i = s.size() - 1, j = g.size() - 1; 7 | while (i >= 0 && j >= 0) { 8 | if(s[i] >= g[j]) 9 | --i; 10 | --j; 11 | } 12 | return s.size() - i - 1; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/461.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingDistance(int x, int y) { 4 | int sum = 0; 5 | while(x != 0 || y != 0) { 6 | int i = x % 2, j = y % 2; 7 | cout << i << ' ' << j << endl; 8 | sum += (i ^ j == 1 ? 1 : 0); 9 | cout << sum << endl; 10 | x /= 2; 11 | y /= 2; 12 | } 13 | return sum; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/476.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findComplement(int num) { 4 | int count = 0; 5 | int x = num; 6 | while(x) { 7 | ++count; 8 | x >>= 1; 9 | } 10 | num = ~num; 11 | for(int i = 0; i < 32 - count; ++i) 12 | num <<= 1; 13 | for(int i = 0; i < 32 - count; ++i) 14 | num >>= 1; 15 | return num; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/48.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector> &matrix) { 4 | int size = matrix.size(); 5 | for (int i = 0; i < size; ++i) { 6 | for (int j = i + 1; j < size; ++j) { 7 | swap(matrix[i][j], matrix[j][i]); 8 | } 9 | } 10 | for (int i = 0; i < (size + 1) / 2; ++i) { 11 | for (int j = 0; j < size; ++j) { 12 | swap(matrix[j][i], matrix[j][size - i - 1]); 13 | } 14 | } 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/485.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxConsecutiveOnes(vector &nums) { 4 | int m = 0 , maxM = 0; 5 | for (int i = 0; i < nums.size(); ++i) { 6 | if (nums[i] == 1) { 7 | ++m; 8 | maxM = max(maxM,m); 9 | } 10 | else 11 | m = 0; 12 | } 13 | return maxM; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/cpp/492.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector constructRectangle(int area) { 4 | vector ret(2, 1); 5 | ret[0] = -1e9, ret[1] = 1e9; 6 | int low = 1, high = area; 7 | while (low <= high) { 8 | high = area / low; 9 | if (area % low == 0 && abs(low - high) < abs(ret[0] - ret[1])) { 10 | ret[0] = high; 11 | ret[1] = low; 12 | } 13 | ++low; 14 | } 15 | return ret; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/cpp/495.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPoisonedDuration(vector &timeSeries, int duration) { 4 | int n = timeSeries.size(), total = 0; 5 | if (n == 0) 6 | return 0; 7 | for (int i = 1; i < n; ++i) { 8 | int gap = timeSeries[i] - timeSeries[i - 1]; 9 | if (gap > duration) 10 | total += duration; 11 | else 12 | total += gap; 13 | } 14 | return total + duration; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/503.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector nextGreaterElements(vector &nums) { 4 | int n = nums.size(); 5 | stack s; 6 | vector ret(n, -1); 7 | for (int i = 0; i < n * 2; ++i) { 8 | while (!s.empty() && nums[s.top()] < nums[i % n]) { 9 | ret[s.top()] = nums[i % n]; 10 | s.pop(); 11 | } 12 | if (i < n) 13 | s.push(i); 14 | } 15 | return ret; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/cpp/509.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int N) { 4 | if(N == 0) 5 | return 0; 6 | int i = 0, j = 1; 7 | while (N > 1) { 8 | int temp = i + j; 9 | i = j; 10 | j = temp; 11 | --N; 12 | } 13 | return j; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/cpp/521.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLUSlength(string a, string b) { 4 | return a == b ? -1 : max(a.size(), b.size()); 5 | } 6 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/532.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPairs(vector &nums, int k) { 4 | map knums; 5 | int ret = 0; 6 | for (auto num: nums) 7 | ++knums[num]; 8 | for (auto m:knums) { 9 | if (k == 0 && m.second > 1) 10 | ++ret; 11 | else if (k > 0 && knums.find(m.first - k) != knums.end()) 12 | ++ret; 13 | } 14 | return ret; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/cpp/537.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string complexNumberMultiply(string a, string b) { 4 | int plus1 = a.find('+'), end1 = a.find('i'); 5 | int x1 = stoi(a.substr(0, plus1)), y1 = stoi(a.substr(plus1 + 1, end1 - plus1 - 1)); 6 | int plus2 = b.find('+'), end2 = b.find('i'); 7 | int x2 = stoi(b.substr(0, plus2)), y2 = stoi(b.substr(plus2 + 1, end2 - plus2 - 1)); 8 | int x3 = x1 * x2 + y1 * y2 * -1, y3 = x1 * y2 + x2 * y1; 9 | return to_string(x3) + '+' + to_string(y3) + 'i'; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode/cpp/538.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode *convertBST(TreeNode *root) { 4 | if (!root) 5 | return nullptr; 6 | int m = 0; 7 | Count(root, m); 8 | return root; 9 | } 10 | 11 | void Count(TreeNode *root, int &m) { 12 | if (!root) 13 | return; 14 | Count(root->right, m); 15 | m += root->val; 16 | root->val = m; 17 | Count(root->left, m); 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/55.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canJump(vector &nums) { 4 | int step = 0; 5 | for (int i = 0; i < nums.size() - 1; ++i) { 6 | int curr = i + nums[i]; 7 | step = max(step, curr); 8 | if (step <= i) 9 | return false; 10 | } 11 | return true; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/cpp/551.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkRecord(string s) { 4 | int a = 0, l = 0; 5 | for (int i = 0; i < s.size(); ++i) { 6 | if (s[i] == 'L') 7 | ++l; 8 | else 9 | l = 0; 10 | if (s[i] == 'A') 11 | ++a; 12 | if (a >= 2 || l >= 3) 13 | return false; 14 | } 15 | return true; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/556.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int nextGreaterElement(int n) { 4 | auto nums = to_string(n); 5 | next_permutation(nums.begin(), nums.end()); 6 | auto res = stoll(nums); 7 | return (res <= n || res > INT_MAX ? -1 : static_cast(res)); 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LeetCode/cpp/557.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseWords(string s) { 4 | int n = s.size(); 5 | for (int i = 0; i < n; ++i) { 6 | int j = i + 1;; 7 | while (j < n && s[j] != ' ') 8 | ++j; 9 | for (int k = i; k < (j + i) / 2; ++k) 10 | swap(s[k], s[j + i - k - 1]); 11 | i = j; 12 | } 13 | return s; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/cpp/559.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDepth(Node *root) { 4 | if (!root) 5 | return 0; 6 | int maxLen = 1; 7 | for (auto child : root->children) 8 | maxLen = max(maxLen, 1 + maxDepth(child)); 9 | return maxLen; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode/cpp/560.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraySum(vector &nums, int k) { 4 | int size = nums.size(), res = 0; 5 | for (int i = 0; i < size; ++i) { 6 | int sum = 0; 7 | for (int j = i; j < size; ++j) { 8 | sum += nums[j]; 9 | res += sum == k; 10 | } 11 | } 12 | return res; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode/cpp/561.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrayPairSum(vector &nums) { 4 | sort(nums.begin(), nums.end()); 5 | int sum = 0; 6 | for (int i = 0; i < nums.size(); i += 2) 7 | sum += nums[i]; 8 | return sum; 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/572.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubtree(TreeNode *s, TreeNode *t) { 4 | if (!s) 5 | return false; 6 | return compareNode(s, t) || isSubtree(s->left, t) || isSubtree(s->right, t); 7 | } 8 | 9 | bool compareNode(TreeNode *s, TreeNode *t) { 10 | if (!s && !t) 11 | return true; 12 | else if (!s || !t || s->val != t->val) 13 | return false; 14 | return compareNode(s->left, t->left) && compareNode(s->right, t->right); 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/575.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int distributeCandies(vector &candies) { 4 | unordered_set kind; 5 | for (auto candy : candies) 6 | if (kind.find(candy) == kind.end()) 7 | kind.insert(candy); 8 | return min(kind.size(), candies.size() / 2); 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/58.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLastWord(string s) { 4 | int len = 0, i = s.size() - 1; 5 | while (s[i] == ' ') 6 | --i; 7 | while (i >= 0) { 8 | if (s[i] == ' ') 9 | break; 10 | ++len; 11 | --i; 12 | } 13 | return len; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/589.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector preorder(Node *root) { 4 | vector order; 5 | preorder(root, order); 6 | return order; 7 | } 8 | 9 | void preorder(Node *root, vector &order) { 10 | if (!root) 11 | return; 12 | order.push_back(root->val); 13 | for (auto child:root->children) 14 | preorder(child, order); 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/590.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector postorder(Node *root) { 4 | vector order; 5 | helper(root, order); 6 | return order; 7 | } 8 | 9 | void helper(Node *root, vector &order) { 10 | if (!root) 11 | return; 12 | for (auto child: root->children) 13 | helper(child, order); 14 | order.push_back(root->val); 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/594.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLHS(vector &nums) { 4 | unordered_map count; 5 | for (auto &n:nums) 6 | ++count[n]; 7 | int total = 0; 8 | for (auto &iter:count) 9 | if (count.find(iter.first + 1) != count.end() && iter.second + count[iter.first + 1] > total) 10 | total = iter.second + count[iter.first + 1]; 11 | return total; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/cpp/598.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxCount(int m, int n, vector> &ops) { 4 | for (auto &o:ops) 5 | m = min(m, o[0]), n = min(n, o[1]); 6 | return m * n; 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /LeetCode/cpp/62.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniquePaths(int m, int n) { 4 | vector grid(n, 0); 5 | grid[0] = 1; 6 | for (int i = 0; i < m; ++i) 7 | for (int j = 1; j < n; ++j) 8 | grid[j] += grid[j - 1]; 9 | return grid[n - 1]; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode/cpp/621.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int leastInterval(vector &tasks, int n) { 4 | unordered_map task_map; 5 | for (auto t: tasks) { 6 | ++task_map[t]; 7 | } 8 | int max_num = 0, max_len = 0; 9 | for (auto t: task_map) 10 | max_num = max(max_num, t.second); 11 | for (auto t:task_map) { 12 | if (t.second == max_num) 13 | ++max_len; 14 | } 15 | return std::max((max_num - 1) * (n + 1) + max_len, int(tasks.size())); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/cpp/643.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMaxAverage(vector &nums, int k) { 4 | int n = nums.size(); 5 | if (n == 0) 6 | return 0; 7 | int sum = nums[0], maxSum = sum; 8 | int i = 1; 9 | while (i < k) { 10 | sum += nums[i]; 11 | maxSum = sum; 12 | ++i; 13 | } 14 | for (; i < n; ++i) { 15 | sum = sum + nums[i] - nums[i - k]; 16 | maxSum = max(maxSum, sum); 17 | } 18 | return (maxSum * 1.0) / k; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/647.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSubstrings(string s) { 4 | int size = s.size(); 5 | int count = 0; 6 | vector> dp(size, vector(size, false)); 7 | for (int i = 0; i < size; ++i) { 8 | for (int j = 0; j <= i; ++j) { 9 | dp[j][i] = ((i == j) || (s[i] == s[j] && ((i <= j + 2) || (dp[j + 1][i - 1] == 1)))); 10 | if (dp[j][i]) 11 | ++count; 12 | } 13 | } 14 | return count; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/650.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSteps(int n) { 4 | if (n == 1) 5 | return 0; 6 | vector dp(n + 1, INT_MAX); 7 | dp[1] = 0; 8 | dp[2] = 2; 9 | for (int i = 3; i <= n; ++i) 10 | for (int j = 1; j < i; ++j) 11 | if (i % j == 0) 12 | dp[i] = min(dp[i], dp[j] + i / j); 13 | return dp[n]; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/cpp/66.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector plusOne(vector &digits) { 4 | int extra = 1; 5 | for (int i = digits.size() - 1; i >= 0; --i) { 6 | digits[i] += extra; 7 | extra = digits[i] / 10; 8 | digits[i] %= 10; 9 | } 10 | if (extra == 1) 11 | digits.insert(digits.begin(), 1); 12 | return digits; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode/cpp/665.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkPossibility(vector &nums) { 4 | if (nums.size() <= 2) 5 | return true; 6 | int n = nums.size(), pos = -1; 7 | for (int i = 0; i < n - 1; ++i) { 8 | if (nums[i] > nums[i + 1]) { 9 | if (pos != -1) 10 | return false; 11 | pos = i; 12 | } 13 | } 14 | return pos == -1 || pos == 0 || pos == n - 2 || nums[pos - 1] <= nums[pos + 1] || nums[pos] <= nums[pos + 2]; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/667.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector constructArray(int n, int k) { 4 | vector ret; 5 | for (int i = 1, j = n; i <= j;) { 6 | if (k > 1) 7 | ret.push_back(k-- % 2? i++ : j--); 8 | else 9 | ret.push_back(i++); 10 | } 11 | return ret; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/669.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode *trimBST(TreeNode *root, int L, int R) { 4 | if (!root) 5 | return nullptr; 6 | if (root->val > R) 7 | return trimBST(root->left, L, R); 8 | else if (root->val < L) 9 | return trimBST(root->right, L, R); 10 | root->left = trimBST(root->left, L, R); 11 | root->right = trimBST(root->right, L, R); 12 | return root; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/674.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLengthOfLCIS(vector& nums) { 4 | int n = nums.size(); 5 | if ( n == 0 ) 6 | return 0; 7 | int len = 1; 8 | int maxLen = 1; 9 | for(int i = 1; i < n; ++i) { 10 | if (nums[i] > nums[i - 1]) { 11 | ++len; 12 | maxLen = max(len, maxLen); 13 | } 14 | else 15 | len = 1; 16 | } 17 | return maxLen; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/cpp/69.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mySqrt(int x) { 4 | int i = 0, j = x; 5 | long &&mid = 0,&&square = 0; 6 | while(i <= j) { 7 | mid = (i + j )/ 2; 8 | square = mid *mid; 9 | if(square == x) 10 | return mid; 11 | else if (square < x) 12 | i = mid + 1; 13 | else 14 | j = mid - 1; 15 | } 16 | return j; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/690.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getImportance(vector employees, int id) { 4 | unordered_map em; 5 | for (auto &e:employees) 6 | em[e->id] = e; 7 | return DFS(id, em); 8 | } 9 | 10 | int DFS(int id, unordered_map &em) { 11 | int sum = em[id]->importance; 12 | for (auto &e:em[id]->subordinates) 13 | sum += DFS(e, em); 14 | return sum; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/693.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasAlternatingBits(int n) { 4 | while(n > 0) { 5 | if((n & 1) == ((n >> 1) & 1)) 6 | return false; 7 | n >>= 1; 8 | } 9 | return true; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/696.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countBinarySubstrings(string s) { 4 | int n = s.size(), total = 0, prev = 0, curr = 1; 5 | for (int i = 1; i < n; ++i) { 6 | if (s[i] != s[i - 1]) { 7 | total += min(prev, curr); 8 | prev = curr; 9 | curr = 1; 10 | } else 11 | ++curr; 12 | } 13 | return total + min(prev, curr); 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/70.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | if (n < 2) return 1; 5 | if (n == 2) return 2; 6 | int prev = 1, last = 2, sum; 7 | for (int i = 3; i <= n; ++i) { 8 | sum = prev + last; 9 | prev = last; 10 | last = sum; 11 | } 12 | return sum; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/700.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode *searchBST(TreeNode *root, const int &val) { 4 | if (!root) 5 | return nullptr; 6 | if (root->val == val) 7 | return root; 8 | return val > root->val ? searchBST(root->right, val) : searchBST(root->left, val); 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/704.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector &nums, int target) { 4 | int i = 0, j = nums.size() - 1, index; 5 | while (i <= j) { 6 | int index = (i + j) / 2; 7 | if (nums[index] > target) 8 | j = index - 1; 9 | else if (nums[index] < target) 10 | i = index + 1; 11 | else 12 | return index; 13 | } 14 | return -1; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/709.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string toLowerCase(string str) { 4 | for (int i = 0; i < str.size(); ++i) 5 | str[i] = (str[i] >= 'A' && str[i] <= 'Z' ? str[i] += 'a' - 'A' : str[i]); 6 | return str; 7 | } 8 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/714.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector &prices, int fee) { 4 | auto size = prices.size(); 5 | if (size == 0) 6 | return 0; 7 | vector hold(size, 0), notHold(size, 0); 8 | hold[0] = -prices[0]; 9 | for (int i = 1; i < size; ++i) { 10 | hold[i] = max(hold[i - 1], notHold[i - 1] - prices[i]); 11 | notHold[i] = max(notHold[i - 1], hold[i - 1] + prices[i] - fee); 12 | } 13 | return notHold[size - 1]; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/cpp/717.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isOneBitCharacter(vector &bits) { 4 | int n = bits.size(); 5 | for(int i = 0; i < n; ++i ) { 6 | if(i == n - 1 && bits[i] == 0) 7 | return true; 8 | if(bits[i] == 1) 9 | ++i; 10 | } 11 | return false; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/724.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int pivotIndex(vector &nums) { 4 | int n = nums.size(); 5 | if (n == 0) 6 | return -1; 7 | double sum = 0.0; 8 | for (int i = 0; i < n; ++i) 9 | sum += nums[i]; 10 | if ((sum - (nums[0] * 1.0)) == 0) 11 | return 0; 12 | for (int i = 1; i < n; ++i) { 13 | if (nums[i - 1] * 1.0 == (sum - (nums[i] * 1.0)) / 2.0) 14 | return i; 15 | nums[i] += nums[i - 1]; 16 | } 17 | return -1; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/728.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector selfDividingNumbers(int left, int right) { 4 | vector ret; 5 | for (int &num = left; num <= right; ++num) { 6 | int i = num; 7 | while (i > 0) { 8 | int j = i % 10; 9 | if (j == 0 || num % j != 0) 10 | break; 11 | i /= 10; 12 | } 13 | if (i == 0) 14 | ret.push_back(num); 15 | } 16 | return ret; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/cpp/729.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendar { 2 | public: 3 | MyCalendar() { 4 | schedule = set>(); 5 | } 6 | 7 | bool book(int start, int end) { 8 | for (auto iter: schedule) { 9 | if (max(iter[0], start) < min(iter[1], end)) 10 | return false; 11 | } 12 | schedule.insert(vector{start, end}); 13 | return true; 14 | } 15 | 16 | private: 17 | set> schedule; 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/cpp/739.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector dailyTemperatures(vector &T) { 4 | int n = T.size(); 5 | stack s; 6 | vector ret(n, 0); 7 | for (int i = 0; i < n; ++i) { 8 | while (!s.empty() && T[s.top()] < T[i]) { 9 | ret[s.top()] = i - s.top(); 10 | s.pop(); 11 | } 12 | s.push(i); 13 | } 14 | return ret; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/cpp/74.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector> &matrix, int target) { 4 | int m = matrix.size(), n = m == 0 ? 0 : matrix[0].size(); 5 | if (m == 0) 6 | return false; 7 | int i = m - 1, j = 0; 8 | while (j < n && i >= 0) { 9 | if (matrix[i][j] > target) 10 | --i; 11 | else if (matrix[i][j] < target) 12 | ++j; 13 | else 14 | return true; 15 | } 16 | return false; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/744.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char nextGreatestLetter(vector &letters, char target) { 4 | int size = letters.size(); 5 | if (target >= letters.back()) 6 | return letters[0]; 7 | int i = 0, j = size - 1; 8 | while (i <= j) { 9 | int index = (j + i) / 2; 10 | if (letters[index] > target) 11 | j = index - 1; 12 | else if (letters[index] <= target) 13 | i = index + 1; 14 | } 15 | return letters[i]; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/746.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostClimbingStairs(vector &cost) { 4 | int size = cost.size(); 5 | if (size < 2) 6 | return 0; 7 | int second = cost[0], first = 0, current; 8 | for (int i = 1; i < size; ++i) { 9 | current = min(first, second) + cost[i]; 10 | first = second; 11 | second = current; 12 | } 13 | return min(first, second); 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/75.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector &nums) { 4 | int i = 0, j = 0, k = nums.size() - 1; 5 | while (j <= k) { 6 | if (nums[j] == 2) swap(nums[j], nums[k--]); 7 | else if (nums[j] == 0) swap(nums[i++], nums[j++]); 8 | else 9 | ++j; 10 | } 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/cpp/754.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int num; 3 | public: 4 | int reachNumber(int target) { 5 | int m = 0; 6 | target = abs(target); 7 | while (target > 0) 8 | target -= ++m; 9 | return target % 2 == 0 ? m : m + 1 + m % 2; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode/cpp/763.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector partitionLabels(string S) { 4 | int start = 0, end = 0, n = S.size(); 5 | vector res, pos(26); 6 | for (int i = 0; i < n; ++i) 7 | pos[S[i] - 'a'] = i; 8 | for (int i = 0; i < n; ++i) { 9 | end = max(end, pos[S[i] - 'a']); 10 | if (end == i) { 11 | res.push_back(end - start + 1); 12 | start = i + 1; 13 | end = start; 14 | } 15 | } 16 | return res; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/cpp/769.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxChunksToSorted(vector &arr) { 4 | int maxChunk = 0, til = 0; 5 | for (int i = 0; i < arr.size(); ++i) { 6 | til = max(til, arr[i]); 7 | if (til == i) 8 | ++maxChunk; 9 | } 10 | return maxChunk; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/cpp/771.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numJewelsInStones(string J, string S) { 4 | unordered_set jewel; 5 | int num = 0; 6 | for (auto j : J) 7 | jewel.insert(j); 8 | for (auto s: S) { 9 | if (jewel.find(s) != jewel.end()) 10 | ++num; 11 | } 12 | return num; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/775.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isIdealPermutation(vector &A) { 4 | int size = A.size(), m = INT_MIN; 5 | for (int i = 0; i < size - 2; ++i) { 6 | m = max(m, A[i]); 7 | if (m > A[i + 2]) 8 | return false; 9 | } 10 | return true; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/791.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string customSortString(string &S, string &T) { 4 | vector pos(26, 26); 5 | for (int i = 0; i < S.size(); ++i) 6 | pos[S[i] - 'a'] = i; 7 | sort(T.begin(), T.end(), [pos](const char &t1, const char &t2) { 8 | return pos[t1 - 'a'] < pos[t2 - 'a']; 9 | }); 10 | return T; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/cpp/80.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector &nums) { 4 | int n = nums.size(), i = 0, j = 0, k = 0; 5 | while (j < n) { 6 | nums[i] = nums[j]; 7 | k = j, ++i, ++j; 8 | if (j >= n) 9 | break; 10 | while (j < n && nums[j] == nums[j - 1]) 11 | ++j; 12 | if (j == k + 1) 13 | continue; 14 | nums[i] = nums[j - 1]; 15 | ++i; 16 | } 17 | return i; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/cpp/806.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector numberOfLines(vector &widths, string &S) { 4 | int line = 1, width = 0; 5 | for (auto s:S) { 6 | if (width + widths[s - 'a'] <= 100) 7 | width += widths[s - 'a']; 8 | else { 9 | width = widths[s - 'a']; 10 | ++line; 11 | } 12 | } 13 | return vector{line, width}; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/cpp/83.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *deleteDuplicates(ListNode *head) { 4 | ListNode *curr = head; 5 | while (curr && curr->next) { 6 | if (curr->val == curr->next->val) 7 | curr->next = curr->next->next; 8 | else 9 | curr = curr->next; 10 | } 11 | return head; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/cpp/830.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> largeGroupPositions(string S) { 4 | vector> ret; 5 | for (int i = 0; i < S.size(); ++i) { 6 | vector temp; 7 | char current = S[i]; 8 | int count = 1, j = i; 9 | while (j + 1 < S.size() && S[j + 1] == S[j]) { 10 | ++count; 11 | ++j; 12 | } 13 | if (count >= 3) { 14 | temp.push_back(i); 15 | temp.push_back(i + count - 1); 16 | ret.push_back(temp); 17 | } 18 | i += count - 1; 19 | cout << j << endl; 20 | } 21 | return ret; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/832.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> flipAndInvertImage(vector> &A) { 4 | for (int i = 0; i < A.size(); ++i) { 5 | int m = A[i].size(); 6 | for (int j = 0; j < m / 2; ++j) 7 | swap(A[i][j], A[i][m - j - 1]); 8 | for (int j = 0; j < m; ++j) 9 | A[i][j] ^= 1; 10 | } 11 | return A; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/844.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool backspaceCompare(string S, string T) { 4 | vector s, t; 5 | for (int i = 0; i < S.length(); ++i) { 6 | if (S[i] != '#') 7 | s.push_back(S[i]); 8 | else 9 | if(!s.empty()) 10 | s.pop_back(); 11 | } 12 | for (int j = 0; j < T.length(); ++j) { 13 | if (T[j] != '#') 14 | t.push_back(T[j]); 15 | else 16 | if (!t.empty()) 17 | t.pop_back(); 18 | } 19 | return s == t; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/cpp/852.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int peakIndexInMountainArray(vector &A) { 4 | int start = 0, end = A.size(), mid = 0; 5 | while (start < end) { 6 | mid = (start + end) / 2; 7 | if (A[mid] > A[mid - 1] && A[mid] > A[mid + 1]) 8 | break; 9 | else if (A[mid] < A[mid + 1]) 10 | start = mid + 1; 11 | else 12 | end = mid; 13 | } 14 | return mid; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/856.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int scoreOfParentheses(string S) { 4 | stack s; 5 | s.push(0); 6 | for (int i = 0; i < S.size(); ++i) { 7 | if (S[i] == '(') 8 | s.push(0); 9 | else { 10 | int j = s.top(); 11 | s.pop(); 12 | s.top() += max(2 * j, 1); 13 | } 14 | } 15 | return s.top(); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/cpp/859.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool buddyStrings(string A, string B) { 4 | int n = A.size(), m = B.size(); 5 | if (n != m) return false; 6 | for (int i = n - 1; i >= 0; --i) { 7 | for (int j = i - 1; j >= 0; --j) { 8 | swap(A[i], A[j]); 9 | if (A == B) 10 | return true; 11 | swap(A[i], A[j]); 12 | } 13 | } 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/867.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> transpose(vector> &A) { 4 | auto row = A.size(); 5 | if (row == 0) 6 | return {}; 7 | vector> ret; 8 | auto col = A[0].size(); 9 | for (int i = 0; i < col; ++i) { 10 | vector temp; 11 | for (int j = 0; j < row; ++j) { 12 | temp.push_back(A[j][i]); 13 | } 14 | ret.push_back(temp); 15 | } 16 | return ret; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/cpp/868.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int binaryGap(int N) { 4 | vector bin; 5 | while (N) { 6 | int temp = N % 2; 7 | bin.push_back(temp); 8 | N = N / 2; 9 | } 10 | int dis = 0, cur = 0; 11 | for (int i = bin.size() - 2; i >= 0; --i) { 12 | if (bin[i] == 1) { 13 | dis = max(dis, cur + 1); 14 | cur = 0; 15 | } else 16 | ++cur; 17 | } 18 | return dis; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/876.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *middleNode(ListNode *head) { 4 | if (!head) return nullptr; 5 | ListNode *first = head, *second = head; 6 | while (first && first->next) { 7 | first = first->next->next; 8 | second = second->next; 9 | } 10 | return second; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/877.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool stoneGame(vector &piles) { 4 | int size = piles.size(); 5 | vector> dp(size, vector(size, 0)); 6 | for (int i = 0; i < size; ++i) 7 | dp[i][i] = piles[i]; 8 | for (int i = 1; i < size; ++i) { 9 | for (int j = i - 1; j >= 0; --j) 10 | dp[j][i] = max(piles[j] - dp[j + 1][i], piles[i] - dp[j][i - 1]); 11 | } 12 | return dp[0][size - 1]; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode/cpp/88.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums1, int m, vector& nums2, int n) { 4 | if(m == 0) 5 | nums1 = nums2; 6 | int i = m - 1, j = n - 1, k = m + n - 1; 7 | while(i >= 0 && j >= 0) { 8 | if(nums1[i] <= nums2[j]) 9 | nums1[k--] = nums2[j--]; 10 | else 11 | nums1[k--] = nums1[i--]; 12 | } 13 | while (j >= 0) { 14 | nums1[k--] = nums2[j--]; 15 | } 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/881.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numRescueBoats(vector &people, int limit) { 4 | sort(people.begin(), people.end()); 5 | int i = 0, j = people.size() - 1, n = 0; 6 | while (i <= j) { 7 | int weight = people[j]; 8 | if (weight + people[i] <= limit && i <= j) { 9 | weight += people[i]; 10 | ++i; 11 | } 12 | --j; 13 | ++n; 14 | } 15 | return n; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/896.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMonotonic(vector &A) { 4 | auto size = A.size(); 5 | int &&inc = 0; 6 | for (int i = 0; i < size - 1; ++i) { 7 | if (A[i] < A[i + 1]) { 8 | if (inc == -1) 9 | return false; 10 | inc = 1; 11 | } else if (A[i] > A[i + 1]) { 12 | if (inc == 1) 13 | return false; 14 | inc = -1; 15 | } 16 | } 17 | return true; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/cpp/897.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode *increasingBST(TreeNode *root) { 4 | if (!root) 5 | return nullptr; 6 | root->right = increasingBST(root->right); 7 | if (root->left) { 8 | TreeNode *temp = increasingBST(root->left), *left = temp; 9 | while (left->right) 10 | left = left->right; 11 | left->right = root; 12 | root->left = nullptr; 13 | return temp; 14 | } 15 | return root; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/9.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | if (x < 0) 5 | return false; 6 | vector nums; 7 | while (x) { 8 | nums.push_back(x % 10); 9 | x /= 10; 10 | } 11 | int i = 0, j = nums.size() - 1; 12 | while (i < j) { 13 | if (nums[i] != nums[j]) 14 | return false; 15 | ++i, --j; 16 | } 17 | return true; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/cpp/900.cpp: -------------------------------------------------------------------------------- 1 | class RLEIterator { 2 | public: 3 | RLEIterator(vector A) { 4 | this->A = A; 5 | } 6 | 7 | int next(int n) { 8 | int i = 0; 9 | while (i < A.size()) { 10 | if (A[i] >= n) { 11 | A[i] -= n; 12 | return A[i + 1]; 13 | } else { 14 | n -= A[i]; 15 | A.erase(A.begin()); 16 | A.erase(A.begin()); 17 | } 18 | } 19 | return -1; 20 | } 21 | 22 | private: 23 | vector A; 24 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/905.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortArrayByParity(vector &A) { 4 | int i = 0, j = 0; 5 | while (j < A.size()) { 6 | if (A[j] % 2 == 0) { 7 | swap(A[i], A[j]); 8 | ++i; 9 | } 10 | ++j; 11 | } 12 | return A; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/908.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestRangeI(vector &A, int K) { 4 | int max_val = INT_MIN, min_val = INT_MAX; 5 | for (auto &a : A) { 6 | max_val = max(max_val, a); 7 | min_val = min(min_val, a); 8 | } 9 | return max(0, max_val - min_val - 2 * K); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode/cpp/921.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minAddToMakeValid(string S) { 4 | stack parentheses; 5 | for (auto c:S) { 6 | if (c == '(') 7 | parentheses.push(c); 8 | else { 9 | if (!parentheses.empty() and parentheses.top() == '(') 10 | parentheses.pop(); 11 | else 12 | parentheses.push(c); 13 | } 14 | } 15 | return parentheses.size(); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/cpp/925.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isLongPressedName(string name, string typed) { 4 | int i = 0, j = 0, name_size = name.size(), typed_size = typed.size(); 5 | while (i < name_size && j < typed_size) { 6 | if (name[i] == typed[j]) { 7 | ++i; 8 | ++j; 9 | } else 10 | ++j; 11 | } 12 | if (i == name_size) 13 | return true; 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/933.cpp: -------------------------------------------------------------------------------- 1 | class RecentCounter { 2 | public: 3 | vector time; 4 | int i, j; 5 | 6 | RecentCounter() { 7 | time = vector(); 8 | i = 0; 9 | j = 0; 10 | } 11 | 12 | int ping(int t) { 13 | ++j; 14 | time.push_back(t); 15 | while (i < j) { 16 | if (time[j - 1] - time[i] <= 3000) 17 | break; 18 | ++i; 19 | } 20 | return j - i; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/cpp/938.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int sum; 3 | public: 4 | int rangeSumBST(TreeNode *root, int &L, int &R) { 5 | sum = 0; 6 | DFS(root, L, R); 7 | return sum; 8 | } 9 | 10 | void DFS(TreeNode *node, int &L, int &R) { 11 | if (!node) 12 | return; 13 | if (node->val >= L && node->val <= R) 14 | sum += node->val; 15 | if (node->val < R) 16 | DFS(node->right, L, R); 17 | if (node->val > L) 18 | DFS(node->left, L, R); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode/cpp/94.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector inorderTraversal(TreeNode *root) { 4 | vector vec; 5 | stack s; 6 | while (root || !s.empty()) { 7 | if (root) { 8 | s.push(root); 9 | root = root->left; 10 | } else { 11 | root = s.top(); 12 | s.pop(); 13 | vec.push_back(root->val); 14 | root = root->right; 15 | } 16 | } 17 | return vec; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/941.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validMountainArray(vector &A) { 4 | auto size = A.size(); 5 | if (size < 3) 6 | return false; 7 | int i = 0; 8 | while (i + 1 < size && A[i] < A[i + 1]) 9 | ++i; 10 | if (i == 0 || i == size - 1) 11 | return false; 12 | while (i + 1 < size && A[i] > A[i + 1]) 13 | ++i; 14 | return i == size - 1; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/cpp/942.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector diStringMatch(string S) { 4 | S += 'E'; 5 | int n = S.size(), m = S.size() - 1, j = 0; 6 | vector ret(n, 0); 7 | for (int i = 0; i < n; ++i) 8 | ret[i] = S[i] == 'I' ? j++ : m--; 9 | return ret; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode/cpp/944.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDeletionSize(vector &A) { 4 | const int size = A.size(); 5 | if (size <= 1) return 0; 6 | const int len = A[0].size(); 7 | int sum = 0; 8 | for (int i = 0; i < len; ++i) { 9 | for (int j = 1; j < size; ++j) { 10 | if (A[j][i] < A[j - 1][i]) { 11 | ++sum; 12 | break; 13 | } 14 | } 15 | } 16 | return sum; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/946.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validateStackSequences(vector &pushed, vector &popped) { 4 | stack res; 5 | int i = 0, n = popped.size(); 6 | for (auto &p:pushed) { 7 | res.push(p); 8 | while (!res.empty() && i < n && res.top() == popped[i]) { 9 | ++i; 10 | res.pop(); 11 | } 12 | } 13 | return i == n; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/cpp/949.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string largestTimeFromDigits(vector &A) { 4 | sort(A.begin(), A.end()); 5 | string time; 6 | do { 7 | int h = A[0] * 10 + A[1], m = A[2] * 10 + A[3]; 8 | if (h < 24 && m < 60) 9 | time = to_string(A[0]) + to_string(A[1]) + ":" + to_string(A[2]) + to_string(A[3]); 10 | } while (next_permutation(A.begin(), A.end())); 11 | return time; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/951.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool flipEquiv(TreeNode *root1, TreeNode *root2) { 4 | if (!root1 && !root2) 5 | return true; 6 | else if (!root1 || !root2) 7 | return false; 8 | else if (root1->val != root2->val) 9 | return false; 10 | return (flipEquiv(root1->left, root2->left) && flipEquiv(root1->right, root2->right)) || 11 | (flipEquiv(root1->left, root2->right) && flipEquiv(root1->right, root2->left)); 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/96.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTrees(int n) { 4 | vector trees(n + 1, 0); 5 | trees[0] = 1; 6 | trees[1] = 1; 7 | for (int i = 2; i <= n; ++i) 8 | for (int j = 1; j <= i; ++j) 9 | trees[i] += trees[j - 1] * trees[i - j]; 10 | return trees[n]; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/961.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int repeatedNTimes(vector &A) { 4 | unordered_set s; 5 | for (auto a:A) { 6 | if (s.find(a) == s.end()) 7 | s.insert(a); 8 | else 9 | return a; 10 | } 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/cpp/962.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxWidthRamp(vector &A) { 4 | int size = A.size(); 5 | for (int i = size - 1; i >= 1; --i) { 6 | for (int j = 0; j + i < size; ++j) { 7 | if (A[j + i] >= A[j]) 8 | return i; 9 | } 10 | } 11 | return 0; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/965.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isUnivalTree(TreeNode *root) { 4 | if (!root) 5 | return true; 6 | return Traverse(root, root->val); 7 | } 8 | 9 | bool Traverse(TreeNode *node, const int &value) { 10 | if (!node) 11 | return true; 12 | if (node->val != value) 13 | return false; 14 | return Traverse(node->left, node->val) && Traverse(node->right, node->val); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/cpp/969.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector pancakeSort(vector A) { 4 | vector res; 5 | int x,i; 6 | for (x = A.size(); x > 0; --x) { 7 | for (i = 0; A[i] != x; ++i); 8 | reverse(A.begin(), A.begin() + i + 1); 9 | res.push_back(i + 1); 10 | reverse(A.begin(), A.begin() + x); 11 | res.push_back(x); 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/973.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> kClosest(vector> &points, int K) { 4 | vector> ret; 5 | sort(points.begin(), points.end(), [](vector &v1, vector v2) { 6 | return (pow(v1[0], 2) + pow(v1[1], 2)) < (pow(v2[0], 2) + pow(v2[1], 2)); 7 | }); 8 | for (int i = 0; i < K; ++i) 9 | ret.push_back(points[i]); 10 | return ret; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/cpp/974.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraysDivByK(vector &A, int K) { 4 | auto size = A.size(); 5 | unordered_map count; 6 | int ret = 0, prefix = 0; 7 | count[0] = 1; 8 | for (int a : A) { 9 | prefix = (prefix + a) % K; 10 | if (prefix < 0) prefix += K; 11 | ret += count[prefix]; 12 | ++count[prefix]; 13 | } 14 | return ret; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/976.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestPerimeter(vector &A) { 4 | auto size = A.size(); 5 | sort(A.begin(), A.end()); 6 | for (int i = size - 3; i >= 0; --i) { 7 | if (A[i] + A[i + 1] > A[i + 2]) 8 | return A[i] + A[i + 1] + A[i + 2]; 9 | } 10 | return 0; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/cpp/978.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxTurbulenceSize(vector &A) { 4 | int size = A.size(), maxSize = 1, curSize = 1; 5 | if (size <= 2) return size; 6 | for (int i = 1; i < size - 1; ++i) { 7 | if ((A[i] > A[i - 1] && A[i] > A[i + 1]) || (A[i] < A[i - 1] && A[i] < A[i + 1])) { 8 | ++curSize; 9 | maxSize = max(maxSize, curSize); 10 | } else 11 | curSize = 1; 12 | } 13 | return maxSize > 1 ? maxSize + 1 : 2; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/cpp/979.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int distributeCoins(TreeNode *root) { 4 | if (!root) 5 | return 0; 6 | int total = 0; 7 | CountCoins(root, total); 8 | return total; 9 | } 10 | 11 | int CountCoins(TreeNode *node, int &total) { 12 | if (!node) 13 | return 0; 14 | int &&value = node->val - 1 + CountCoins(node->left, total) + CountCoins(node->right, total); 15 | total += abs(value); 16 | return value; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/cpp/981.cpp: -------------------------------------------------------------------------------- 1 | class TimeMap { 2 | public: 3 | map> m; 4 | 5 | TimeMap() { 6 | m.clear(); 7 | } 8 | 9 | void set(string key, string value, int timestamp) { 10 | m[key][timestamp] = value; 11 | } 12 | 13 | string get(string key, int timestamp) { 14 | if (m.find(key) == m.end()) 15 | return ""; 16 | for (int i = timestamp; i >= 0; --i) { 17 | if (m[key].find(i) != m[key].end()) 18 | return m[key][i]; 19 | } 20 | return ""; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/991.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int brokenCalc(int X, int Y) { 4 | if (X >= Y) 5 | return X - Y; 6 | return Y % 2 ? brokenCalc(X, Y + 1) + 1 : brokenCalc(X, Y / 2) + 1; 7 | } 8 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/995.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minKBitFlips(vector &A, int K) { 4 | int k = 0, n = A.size(); 5 | if (n == 0) 6 | return 0; 7 | for (int i = 0; i < n; ++i) { 8 | if (A[i] == 1) 9 | continue; 10 | if (i + K - 1 >= n) 11 | return -1; 12 | ++k; 13 | for (int j = i; j < i + K; ++j) 14 | A[j] = A[j] ^ 1; 15 | } 16 | return k; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/997.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findJudge(int N, vector> &trust) { 4 | vector indegree(N + 1, 0), outdegree(N + 1, 0); 5 | for (auto &t:trust) { 6 | ++outdegree[t[0]]; 7 | ++indegree[t[1]]; 8 | } 9 | for (int i = 1; i <= N; ++i) { 10 | if (indegree[i] == N - 1 && outdegree[i] == 0) 11 | return i; 12 | } 13 | return -1; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/cpp/998.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode *insertIntoMaxTree(TreeNode *root, int val, TreeNode *p = nullptr) { 4 | if (!root || root->val < val) { 5 | auto node = new TreeNode(val); 6 | if (root) 7 | node->left = root; 8 | if (p) 9 | p->right = node; 10 | return node; 11 | } 12 | root->right = insertIntoMaxTree(root->right, val, root); 13 | return root; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/csharp/1025.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool DivisorGame(int N) { 3 | return N % 2 == 0 ? true : false; 4 | } 5 | } -------------------------------------------------------------------------------- /LeetCode/golang/1.go: -------------------------------------------------------------------------------- 1 | func twoSum(nums []int, target int) []int { 2 | u := make(map[int]int) 3 | for i,value := range nums { 4 | diff := target - value 5 | val, ok := u[diff] 6 | if ok { 7 | return []int{val, i} 8 | } 9 | u[value] = i 10 | } 11 | return []int{} 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode/golang/1025.go: -------------------------------------------------------------------------------- 1 | func divisorGame(N int) bool { 2 | if N % 2 == 0 { 3 | return true; 4 | } 5 | return false; 6 | } -------------------------------------------------------------------------------- /LeetCode/golang/551.go: -------------------------------------------------------------------------------- 1 | func checkRecord(s string) bool { 2 | a, l := 0, 0 3 | for i := 0; i < len(s); i++ { 4 | if s[i] == 'L' { 5 | l++ 6 | } else { 7 | l = 0 8 | } 9 | if s[i] == 'A' { 10 | a++ 11 | } 12 | if (a >= 2 || l >= 3) { 13 | return false 14 | } 15 | } 16 | return true 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode/golang/925.go: -------------------------------------------------------------------------------- 1 | func isLongPressedName(name string, typed string) bool { 2 | name_size, typed_size := len(name), len(typed) 3 | i, j := 0, 0 4 | for i < name_size && j < typed_size { 5 | if name[i] == typed[j] { 6 | i++ 7 | j++ 8 | } else { 9 | j++ 10 | } 11 | } 12 | if i == name_size { 13 | return true 14 | } 15 | return false 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/golang/985.go: -------------------------------------------------------------------------------- 1 | func sumEvenAfterQueries(A []int, queries [][]int) []int { 2 | sum, ret := 0, make([]int, 0) 3 | for i := 0; i < len(A); i++ { 4 | if A[i]%2 == 0 { 5 | sum += A[i] 6 | } 7 | } 8 | for i := 0; i < len(queries); i++ { 9 | if A[queries[i][1]]%2 == 0 { 10 | sum -= A[queries[i][1]] 11 | } 12 | A[queries[i][1]] += queries[i][0] 13 | if A[queries[i][1]]%2 == 0 { 14 | sum += A[queries[i][1]] 15 | } 16 | ret = append(ret, sum) 17 | } 18 | return ret 19 | } -------------------------------------------------------------------------------- /LeetCode/golang/991.go: -------------------------------------------------------------------------------- 1 | func brokenCalc(X int, Y int) int { 2 | if X >= Y { 3 | return X - Y 4 | } 5 | if Y % 2 == 1 { 6 | return brokenCalc(X, Y + 1) + 1 7 | } 8 | return brokenCalc(X, Y / 2) + 1 9 | } -------------------------------------------------------------------------------- /LeetCode/java/1025.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean divisorGame(int N) { 3 | return N % 2 == 0 ? true : false; 4 | } 5 | } -------------------------------------------------------------------------------- /LeetCode/java/1064.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int fixedPoint(int[] A) { 3 | for (int i = 0; i < A.length; ++i) 4 | if (A[i] == i) 5 | return i; 6 | return -1; 7 | } 8 | } -------------------------------------------------------------------------------- /LeetCode/java/122.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices) { 3 | int profit = 0; 4 | for (int i = 0; i < prices.length - 1; i++) { 5 | if (prices[i] < prices[i + 1]) 6 | profit += prices[i + 1] - prices[i]; 7 | } 8 | return profit; 9 | } 10 | } -------------------------------------------------------------------------------- /LeetCode/java/908.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int smallestRangeI(int[] A, int K) { 3 | int max_val = 0, min_val = 10000; 4 | for (int i = 0; i < A.length; ++i) { 5 | max_val = max_val > A[i] ? max_val : A[i]; 6 | min_val = min_val < A[i] ? min_val : A[i]; 7 | } 8 | int res = max_val - min_val - 2 * K; 9 | return 0 > res ? 0 : res; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/java/991.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int brokenCalc(int X, int Y) { 3 | if (X >= Y) 4 | return X - Y; 5 | return Y % 2 == 1 ? brokenCalc(X, Y + 1) + 1 : brokenCalc(X, Y / 2) + 1; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /LeetCode/javascript/1025.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} N 3 | * @return {boolean} 4 | */ 5 | var divisorGame = function(N) { 6 | return N % 2 == 0 ? true : false; 7 | }; -------------------------------------------------------------------------------- /LeetCode/mysql/175.sql: -------------------------------------------------------------------------------- 1 | SELECT FirstName, LastName, City, State 2 | FROM Person LEFT OUTER JOIN Address 3 | ON 4 | Person.PersonId = Address.PersonId; -------------------------------------------------------------------------------- /LeetCode/mysql/181.sql: -------------------------------------------------------------------------------- 1 | SELECT E1.Name AS 'Employee' 2 | FROM Employee AS E1 JOIN Employee AS E2 3 | ON E1.ManagerId = E2.Id 4 | WHERE E1.Salary > E2.Salary -------------------------------------------------------------------------------- /LeetCode/mysql/182.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | Email 3 | FROM 4 | Person 5 | GROUP BY 6 | Email 7 | HAVING 8 | COUNT(Email) >= 2 -------------------------------------------------------------------------------- /LeetCode/mysql/183.sql: -------------------------------------------------------------------------------- 1 | SELECT Name AS 'Customers' 2 | FROM Customers 3 | WHERE 4 | Id NOT IN ( 5 | SELECT CustomerId 6 | FROM Orders 7 | ); -------------------------------------------------------------------------------- /LeetCode/mysql/196.sql: -------------------------------------------------------------------------------- 1 | DELETE P1 2 | FROM Person AS P1, Person AS P2 3 | WHERE 4 | P1.Email = P2.Email AND 5 | P1.Id > P2.Id; -------------------------------------------------------------------------------- /LeetCode/mysql/197.sql: -------------------------------------------------------------------------------- 1 | SELECT w1.Id 2 | FROM Weather w1 JOIN Weather w2 3 | ON DATEDIFF(w1.RecordDate, w2.RecordDate) = 1 4 | AND w1.Temperature > w2.Temperature -------------------------------------------------------------------------------- /LeetCode/mysql/595.sql: -------------------------------------------------------------------------------- 1 | SELECT name, population, area 2 | FROM World 3 | WHERE area > 3000000 OR population > 25000000 -------------------------------------------------------------------------------- /LeetCode/mysql/596.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | class 3 | FROM 4 | courses 5 | GROUP BY 6 | class 7 | HAVING 8 | COUNT(DISTINCT STUDENT) >= 5 -------------------------------------------------------------------------------- /LeetCode/mysql/620.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | * 3 | FROM 4 | cinema 5 | WHERE 6 | id % 2 = 1 AND 7 | description != 'boring' 8 | ORDER BY 9 | rating DESC; -------------------------------------------------------------------------------- /LeetCode/mysql/627.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | UPDATE salary 3 | SET sex = CHAR(ASCII(sex) ^ ASCII('m') ^ ASCII('f')); -------------------------------------------------------------------------------- /LeetCode/python/1013.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def canThreePartsEqualSum(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: bool 6 | """ 7 | s = sum(A) 8 | if s % 3 != 0: 9 | return False 10 | s //= 3 11 | cum = 0 12 | total = 0 13 | for a in A: 14 | cum += a 15 | if cum == s: 16 | total += 1 17 | cum = 0 18 | if total == 3 and cum == 0: 19 | return True 20 | return False 21 | -------------------------------------------------------------------------------- /LeetCode/python/1014.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxScoreSightseeingPair(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: int 6 | """ 7 | prev, curr = A[0] + 0, -sys.maxsize 8 | for i in range(1, len(A)): 9 | curr = max(curr, A[i] - i + prev) 10 | prev = max(prev, A[i] + i) 11 | return curr 12 | -------------------------------------------------------------------------------- /LeetCode/python/1015.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def smallestRepunitDivByK(self, K): 3 | """ 4 | :type K: int 5 | :rtype: int 6 | """ 7 | n = 0 8 | for i in range(1, K + 1): 9 | n = n * 10 + 1 10 | n %= K 11 | if n == 0: 12 | return i 13 | return -1 14 | -------------------------------------------------------------------------------- /LeetCode/python/1020.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def canThreePartsEqualSum(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: bool 6 | """ 7 | s = sum(A) 8 | if s % 3 != 0: 9 | return False 10 | s //= 3 11 | cum = 0 12 | total = 0 13 | for a in A: 14 | cum += a 15 | if cum == s: 16 | total += 1 17 | cum = 0 18 | if total == 3 and cum == 0: 19 | return True 20 | return False 21 | -------------------------------------------------------------------------------- /LeetCode/python/1021.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxScoreSightseeingPair(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: int 6 | """ 7 | prev, curr = A[0] + 0, -sys.maxsize 8 | for i in range(1, len(A)): 9 | curr = max(curr, A[i] - i + prev) 10 | prev = max(prev, A[i] + i) 11 | return curr 12 | -------------------------------------------------------------------------------- /LeetCode/python/1022.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def smallestRepunitDivByK(self, K): 3 | """ 4 | :type K: int 5 | :rtype: int 6 | """ 7 | n = 0 8 | for i in range(1, K + 1): 9 | n = n * 10 + 1 10 | n %= K 11 | if n == 0: 12 | return i 13 | return -1 14 | -------------------------------------------------------------------------------- /LeetCode/python/1025.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def divisorGame(self, N): 3 | """ 4 | :type N: int 5 | :rtype: bool 6 | """ 7 | return True if N % 2 == 0 else False -------------------------------------------------------------------------------- /LeetCode/python/1046.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def lastStoneWeight(self, stones): 3 | """ 4 | :type stones: List[int] 5 | :rtype: int 6 | """ 7 | h = [-s for s in stones] 8 | heapq.heapify(h) 9 | while h: 10 | i = -heapq.heappop(h) 11 | if not h: 12 | return i 13 | i += heapq.heappop(h) 14 | if i != 0: 15 | heapq.heappush(h, -i) 16 | return -heapq.heappop(h) if h else 0 17 | -------------------------------------------------------------------------------- /LeetCode/python/1047.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def removeDuplicates(self, S): 3 | """ 4 | :type S: str 5 | :rtype: str 6 | """ 7 | stack = [] 8 | for s in S: 9 | if not stack or stack[-1] != s: 10 | stack.append(s) 11 | else: 12 | stack.pop() 13 | return ''.join(stack) 14 | -------------------------------------------------------------------------------- /LeetCode/python/1048.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def longestStrChain(self, words): 3 | """ 4 | :type words: List[str] 5 | :rtype: int 6 | """ 7 | dp = {} 8 | for word in sorted(words, key=len): 9 | dp[word] = 1 10 | for i in range(len(word)): 11 | temp = word[:i] + word[i + 1:] 12 | dp[word] = max(dp.get(word, 0), dp.get(temp, 0) + 1) 13 | return max(dp.values() or [1]) 14 | -------------------------------------------------------------------------------- /LeetCode/python/1051.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def heightChecker(self, heights): 3 | """ 4 | :type heights: List[int] 5 | :rtype: int 6 | """ 7 | res, h = 0, sorted(heights) 8 | for i in range(len(h)): 9 | if h[i] != heights[i]: 10 | res += 1 11 | return res -------------------------------------------------------------------------------- /LeetCode/python/1053.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def prevPermOpt1(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: List[int] 6 | """ 7 | n = len(A) 8 | for i in range(n - 2, -1, -1): 9 | k, num = 0, 0 10 | for j in range(i + 1, n): 11 | if A[j] < A[i] and num <= A[j]: 12 | num = A[j] 13 | k = j 14 | if k != 0: 15 | A[i], A[k] = A[k], A[i] 16 | return A 17 | return A 18 | -------------------------------------------------------------------------------- /LeetCode/python/1064.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def fixedPoint(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: int 6 | """ 7 | for i in range(len(A)): 8 | if A[i] == i: 9 | return i 10 | return -1 -------------------------------------------------------------------------------- /LeetCode/python/1065.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def indexPairs(self, text, words): 3 | """ 4 | :type text: str 5 | :type words: List[str] 6 | :rtype: List[List[int]] 7 | """ 8 | res = [] 9 | for word in words: 10 | i = 0 11 | j = text.find(word, i) 12 | while j != -1: 13 | res.append([j, j + len(word) - 1]) 14 | i = j + 1 15 | j = text.find(word, i) 16 | return sorted(res) -------------------------------------------------------------------------------- /LeetCode/python/136.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def singleNumber(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | x = 0 8 | for i in nums: 9 | x ^= i 10 | return x -------------------------------------------------------------------------------- /LeetCode/python/14.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def longestCommonPrefix(self, strs): 3 | """ 4 | :type strs: List[str] 5 | :rtype: str 6 | """ 7 | if len(strs) == 0: 8 | return "" 9 | pos, res = 0, "" 10 | for c in strs[0]: 11 | for i in range(1, len(strs)): 12 | if pos >= len(strs[i]) or strs[i][pos] != c: 13 | return res 14 | res += c 15 | pos += 1 16 | return res 17 | -------------------------------------------------------------------------------- /LeetCode/python/141.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def hasCycle(self, head): 3 | p, q = head, head 4 | while True: 5 | if p and p.next: 6 | p = p.next.next 7 | else: 8 | return False 9 | q = q.next 10 | if q == p: 11 | return True 12 | return False 13 | -------------------------------------------------------------------------------- /LeetCode/python/231.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isPowerOfTwo(self, n): 3 | """ 4 | :type n: int 5 | :rtype: bool 6 | """ 7 | if n <= 0: 8 | return False 9 | while n & 1 == 0: 10 | n >>= 1 11 | return n == 1 12 | -------------------------------------------------------------------------------- /LeetCode/python/258.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def addDigits(self, num): 3 | """ 4 | :type num: int 5 | :rtype: int 6 | """ 7 | while num > 9: 8 | temp, sum = num, 0 9 | while temp > 0: 10 | sum += temp % 10 11 | temp //= 10 12 | num = sum 13 | return num 14 | -------------------------------------------------------------------------------- /LeetCode/python/263.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isUgly(self, num): 3 | """ 4 | :type num: int 5 | :rtype: bool 6 | """ 7 | if num < 1: 8 | return False 9 | while num % 2 == 0: 10 | num //= 2 11 | while num % 3 == 0: 12 | num //= 3 13 | while num % 5 == 0: 14 | num //= 5 15 | return True if num == 1 else False -------------------------------------------------------------------------------- /LeetCode/python/283.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def moveZeroes(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: void Do not return anything, modify nums in-place instead. 6 | """ 7 | nonZero = 0 8 | for i in range(len(nums)): 9 | if nums[i] != 0: 10 | nums[nonZero] = nums[i] 11 | nonZero += 1 12 | for i in range(nonZero,len(nums)): 13 | nums[i] = 0 -------------------------------------------------------------------------------- /LeetCode/python/300.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def lengthOfLIS(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | n = len(nums) 8 | if n == 0: 9 | return 0 10 | dp = [1 for i in range(n)] 11 | LIS = 1 12 | for i in range(1, n): 13 | for j in range(i): 14 | if nums[i] > nums[j]: 15 | dp[i] = max(dp[i], dp[j] + 1) 16 | LIS = max(LIS, dp[i]) 17 | return LIS -------------------------------------------------------------------------------- /LeetCode/python/342.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isPowerOfFour(self, num): 3 | """ 4 | :type num: int 5 | :rtype: bool 6 | """ 7 | if num <= 0: 8 | return False 9 | while num > 1: 10 | if num & 1 or num & 2: 11 | return False 12 | num >>= 2 13 | return True -------------------------------------------------------------------------------- /LeetCode/python/448.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findDisappearedNumbers(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[int] 6 | """ 7 | return list(set(range(1,len(nums) + 1)) - set(nums)) -------------------------------------------------------------------------------- /LeetCode/python/461.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def hammingDistance(self, x, y): 3 | """ 4 | :type x: int 5 | :type y: int 6 | :rtype: int 7 | """ 8 | x = bin(x^y) 9 | l = len(x) 10 | sum = 0 11 | for i in range(l): 12 | if x[i] == '1': 13 | sum += 1 14 | return sum -------------------------------------------------------------------------------- /LeetCode/python/598.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxCount(self, m, n, ops): 3 | """ 4 | :type m: int 5 | :type n: int 6 | :type ops: List[List[int]] 7 | :rtype: int 8 | """ 9 | for o in ops: 10 | m = min(m, o[0]) 11 | n = min(n, o[1]) 12 | return m * n -------------------------------------------------------------------------------- /LeetCode/python/709.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def toLowerCase(self, str): 3 | """ 4 | :type str: str 5 | :rtype: str 6 | """ 7 | return str.lower() -------------------------------------------------------------------------------- /LeetCode/python/728.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def selfDividingNumbers(self, left, right): 3 | """ 4 | :type left: int 5 | :type right: int 6 | :rtype: List[int] 7 | """ 8 | ret = [] 9 | for x in range(left, right + 1): 10 | y = x 11 | while y > 0: 12 | z = y % 10 13 | if z == 0 or x % z != 0: 14 | break; 15 | y //= 10 16 | if y == 0: 17 | ret.append(x) 18 | return ret -------------------------------------------------------------------------------- /LeetCode/python/791.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def customSortString(self, S, T): 3 | """ 4 | :type S: str 5 | :type T: str 6 | :rtype: str 7 | """ 8 | counter = collections.Counter(T) 9 | res = "" 10 | for s in S: 11 | if s in counter: 12 | for i in range(counter[s]): 13 | res += s 14 | counter[s] = 0 15 | for c, v in counter.items(): 16 | for i in range(v): 17 | res += c 18 | return res -------------------------------------------------------------------------------- /LeetCode/python/806.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def numberOfLines(self, widths, S): 3 | """ 4 | :type widths: List[int] 5 | :type S: str 6 | :rtype: List[int] 7 | """ 8 | line, last_width = 1, 0 9 | for s in S: 10 | width = widths[ord(s) - ord('a')] 11 | if last_width + width <= 100: 12 | last_width += width 13 | else: 14 | last_width = width 15 | line += 1 16 | return [line, last_width] -------------------------------------------------------------------------------- /LeetCode/python/905.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def sortArrayByParity(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: List[int] 6 | """ 7 | j = 0 8 | for i in range(len(A)): 9 | if(A[i] % 2 == 0): 10 | A[i], A[j] = A[j], A[i] 11 | j += 1 12 | return A -------------------------------------------------------------------------------- /LeetCode/python/908.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def smallestRangeI(self, A, K): 3 | """ 4 | :type A: List[int] 5 | :type K: int 6 | :rtype: int 7 | """ 8 | return max(0, max(A) - min(A) - 2 * K) -------------------------------------------------------------------------------- /LeetCode/python/921.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def minAddToMakeValid(self, S): 3 | """ 4 | :type S: str 5 | :rtype: int 6 | """ 7 | parentheses = [] 8 | for c in S: 9 | if c == '(': 10 | parentheses.append(c) 11 | else: 12 | if len(parentheses) == 0 or parentheses[-1] != '(': 13 | parentheses.append(c) 14 | else: 15 | parentheses.pop() 16 | return len(parentheses) 17 | -------------------------------------------------------------------------------- /LeetCode/python/946.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def validateStackSequences(self, pushed, popped): 3 | """ 4 | :type pushed: List[int] 5 | :type popped: List[int] 6 | :rtype: bool 7 | """ 8 | res = [] 9 | i, n = 0, len(popped) 10 | for p in pushed: 11 | res.append(p) 12 | while res and i < n and res[-1] == popped[i]: 13 | i += 1 14 | res.pop() 15 | return i == n -------------------------------------------------------------------------------- /LeetCode/python/991.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def brokenCalc(self, X, Y): 3 | """ 4 | :type X: int 5 | :type Y: int 6 | :rtype: int 7 | """ 8 | if X >= Y: 9 | return X - Y 10 | return 1 + (self.brokenCalc(X, Y + 1) if Y % 2 == 1 else self.brokenCalc(X, Y // 2)) -------------------------------------------------------------------------------- /LeetCode/python3/1.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, nums, target): 3 | """ 4 | :type nums: List[int] 5 | :type target: int 6 | :rtype: List[int] 7 | """ 8 | d = {} 9 | for i in range(len(nums)): 10 | diff = target - nums[i] 11 | if diff in d: 12 | return [d[diff], i] 13 | d[nums[i]] = i 14 | return [] 15 | -------------------------------------------------------------------------------- /LeetCode/python3/100.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isSameTree(self, p: 'TreeNode', q: 'TreeNode') -> 'bool': 3 | if not p and not q: 4 | return True 5 | elif not p or not q: 6 | return False 7 | return q.val == p.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) -------------------------------------------------------------------------------- /LeetCode/python3/1003.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isValid(self, S: str) -> bool: 3 | while len(S) > 0: 4 | if S.find("abc") == -1: 5 | return False 6 | S = S.replace("abc", '') 7 | return True -------------------------------------------------------------------------------- /LeetCode/python3/1006.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def clumsy(self, N: int) -> int: 3 | if N <= 2: 4 | return N 5 | sum = N * (N - 1) // (N - 2) 6 | N -= 3 7 | while N > 0: 8 | sum += N 9 | temp = 0 10 | if N - 1 > 0: 11 | temp = N - 1 12 | if N - 2 > 0: 13 | temp *= N - 2 14 | if N - 3 > 0: 15 | temp //= N - 3 16 | sum -= temp 17 | N -= 4 18 | return sum 19 | -------------------------------------------------------------------------------- /LeetCode/python3/1009.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def bitwiseComplement(self, N: int) -> int: 3 | if N == 0: 4 | return 1 5 | m, res, = 1, 0 6 | while N: 7 | res += m * (N % 2 ^ 1) 8 | m <<= 1 9 | N >>= 1 10 | return res 11 | -------------------------------------------------------------------------------- /LeetCode/python3/1010.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numPairsDivisibleBy60(self, time: List[int]) -> int: 3 | count = collections.Counter() 4 | total = 0 5 | for t in time: 6 | total += count[(60 - t % 60) % 60] 7 | count[t % 60] += 1 8 | return total 9 | -------------------------------------------------------------------------------- /LeetCode/python3/1012.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def bitwiseComplement(self, N: int) -> int: 3 | if N == 0: 4 | return 1 5 | m, res, = 1, 0 6 | while N: 7 | res += m * (N % 2 ^ 1) 8 | m <<= 1 9 | N >>= 1 10 | return res 11 | -------------------------------------------------------------------------------- /LeetCode/python3/1013.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canThreePartsEqualSum(self, A: List[int]) -> bool: 3 | s = sum(A) 4 | if s % 3 != 0: 5 | return False 6 | s //= 3 7 | cum = 0 8 | total = 0 9 | for a in A: 10 | cum += a 11 | if cum == s: 12 | total += 1 13 | cum = 0 14 | if total == 3 and cum == 0: 15 | return True 16 | return False 17 | -------------------------------------------------------------------------------- /LeetCode/python3/1014.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxScoreSightseeingPair(self, A: List[int]) -> int: 3 | prev, curr = A[0] + 0, -sys.maxsize 4 | for i in range(1, len(A)): 5 | curr = max(curr, A[i] - i + prev) 6 | prev = max(prev, A[i] + i) 7 | return curr 8 | -------------------------------------------------------------------------------- /LeetCode/python3/1015.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestRepunitDivByK(self, K: int) -> int: 3 | n = 0 4 | for i in range(1, K + 1): 5 | n = n * 10 + 1 6 | n %= K 7 | if n == 0: 8 | return i 9 | return -1 10 | -------------------------------------------------------------------------------- /LeetCode/python3/1017.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def baseNeg2(self, N: int) -> str: 3 | res = [] 4 | while N: 5 | res.append(N & 1) 6 | N = -(N >> 1) 7 | return "".join(map(str, res[::-1] or [0])) 8 | -------------------------------------------------------------------------------- /LeetCode/python3/1018.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def prefixesDivBy5(self, A: List[int]) -> List[bool]: 3 | num = 0 4 | ret = [False for _ in range(len(A))] 5 | for i in range(len(A)): 6 | num = (num * 2 + A[i]) % 5 7 | ret[i] = True if num == 0 else False 8 | return ret 9 | -------------------------------------------------------------------------------- /LeetCode/python3/1021.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeOuterParentheses(self, S: str) -> str: 3 | empty, start, ret = 0, 0, "" 4 | for i in range(len(S)): 5 | if S[i] == '(': 6 | empty += 1 7 | elif S[i] == ')': 8 | empty -= 1 9 | if empty == 0: 10 | ret += S[start + 1:i] 11 | start = i + 1 12 | return ret 13 | -------------------------------------------------------------------------------- /LeetCode/python3/1024.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def videoStitching(self, clips: List[List[int]], T: int) -> int: 3 | clips.sort() 4 | res, start, end, i = 0, 0, 0, 0 5 | while end < T: 6 | while i < len(clips) and clips[i][0] <= start: 7 | end = max(end, clips[i][1]) 8 | i += 1 9 | if start == end: 10 | return -1 11 | res += 1 12 | start = end 13 | return res 14 | -------------------------------------------------------------------------------- /LeetCode/python3/1025.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def divisorGame(self, N: int) -> bool: 3 | return True if N % 2 == 0 else False -------------------------------------------------------------------------------- /LeetCode/python3/1029.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoCitySchedCost(self, costs: List[List[int]]) -> int: 3 | costs.sort(key=lambda x:x[0] - x[1]) 4 | sum, n = 0, len(costs) // 2 5 | for i in range(n * 2): 6 | sum += costs[i][0] if i < n else costs[i][1] 7 | return sum 8 | -------------------------------------------------------------------------------- /LeetCode/python3/1038.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def bstToGst(self, root: TreeNode) -> TreeNode: 3 | self.Traverse(root, 0) 4 | return root 5 | 6 | def Traverse(self, node: TreeNode, val: int) -> int: 7 | if not node: 8 | return val 9 | right = self.Traverse(node.right, val) 10 | node.val += right 11 | left = self.Traverse(node.left, node.val) 12 | return left -------------------------------------------------------------------------------- /LeetCode/python3/1046.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lastStoneWeight(self, stones: List[int]) -> int: 3 | h = [-s for s in stones] 4 | heapq.heapify(h) 5 | while h: 6 | i = -heapq.heappop(h) 7 | if not h: 8 | return i 9 | i += heapq.heappop(h) 10 | if i != 0: 11 | heapq.heappush(h, -i) 12 | return -heapq.heappop(h) if h else 0 13 | -------------------------------------------------------------------------------- /LeetCode/python3/1047.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeDuplicates(self, S: str) -> str: 3 | stack = [] 4 | for s in S: 5 | if not stack or stack[-1] != s: 6 | stack.append(s) 7 | else: 8 | stack.pop() 9 | return ''.join(stack) 10 | -------------------------------------------------------------------------------- /LeetCode/python3/1048.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestStrChain(self, words: List[str]) -> int: 3 | dp = {} 4 | for word in sorted(words, key=len): 5 | dp[word] = 1 6 | for i in range(len(word)): 7 | temp = word[:i] + word[i + 1:] 8 | dp[word] = max(dp.get(word, 0), dp.get(temp, 0) + 1) 9 | return max(dp.values() or [1]) 10 | -------------------------------------------------------------------------------- /LeetCode/python3/1051.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def heightChecker(self, heights: List[int]) -> int: 3 | res, h = 0, sorted(heights) 4 | for i in range(len(h)): 5 | if h[i] != heights[i]: 6 | res += 1 7 | return res -------------------------------------------------------------------------------- /LeetCode/python3/1053.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def prevPermOpt1(self, A: List[int]) -> List[int]: 3 | n = len(A) 4 | for i in range(n - 2, -1, -1): 5 | k, num = 0, 0 6 | for j in range(i + 1, n): 7 | if A[j] < A[i] and num <= A[j]: 8 | num = A[j] 9 | k = j 10 | if k != 0: 11 | A[i], A[k] = A[k], A[i] 12 | return A 13 | return A 14 | -------------------------------------------------------------------------------- /LeetCode/python3/1064.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fixedPoint(self, A: List[int]) -> int: 3 | for i in range(len(A)): 4 | if A[i] == i: 5 | return i 6 | return -1 -------------------------------------------------------------------------------- /LeetCode/python3/1065.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def indexPairs(self, text: str, words: List[str]) -> List[List[int]]: 3 | res = [] 4 | for word in words: 5 | i = 0 6 | j = text.find(word, i) 7 | while j != -1: 8 | res.append([j, j + len(word) - 1]) 9 | i = j + 1 10 | j = text.find(word, i) 11 | return sorted(res) -------------------------------------------------------------------------------- /LeetCode/python3/11.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxArea(self, height): 3 | """ 4 | :type height: List[int] 5 | :rtype: int 6 | """ 7 | i, j, maxAre = 0, len(height) - 1, 0 8 | while i < j: 9 | maxAre = max(maxAre, min(height[i], height[j]) * (j-i)) 10 | if height[i] < height[j]: 11 | i += 1 12 | else: 13 | j -= 1 14 | return maxAre 15 | 16 | -------------------------------------------------------------------------------- /LeetCode/python3/121.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProfit(self, prices: List[int]) -> int: 3 | lowest, profit = sys.maxsize, 0 4 | for p in prices: 5 | lowest = min(lowest, p) 6 | profit = max(profit, p - lowest) 7 | return profit 8 | -------------------------------------------------------------------------------- /LeetCode/python3/125.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, s: str) -> bool: 3 | s = s.lower() 4 | i, j = 0, len(s) - 1 5 | while i < j: 6 | if not s[i].islower() and not s[i].isdigit(): 7 | i += 1 8 | continue 9 | if not s[j].islower() and not s[j].isdigit(): 10 | j -= 1 11 | continue 12 | if s[i] == s[j]: 13 | i += 1 14 | j -= 1 15 | else: 16 | return False 17 | return True 18 | -------------------------------------------------------------------------------- /LeetCode/python3/136.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def singleNumber(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | k = 0 8 | for n in nums: 9 | k ^= n 10 | return k -------------------------------------------------------------------------------- /LeetCode/python3/14.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestCommonPrefix(self, strs: List[str]) -> str: 3 | if len(strs) == 0: 4 | return "" 5 | pos, res = 0, "" 6 | for c in strs[0]: 7 | for i in range(1, len(strs)): 8 | if pos >= len(strs[i]) or strs[i][pos] != c: 9 | return res 10 | res += c 11 | pos += 1 12 | return res -------------------------------------------------------------------------------- /LeetCode/python3/145.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def postorderTraversal(self, root): 3 | """ 4 | :type root: TreeNode 5 | :rtype: List[int] 6 | """ 7 | if not root: 8 | return [] 9 | ret = [] 10 | stack = [root] 11 | while stack: 12 | node = stack.pop() 13 | ret.append(node.val) 14 | if node.left: 15 | stack.append(node.left) 16 | if node.right: 17 | stack.append(node.right) 18 | return ret[::-1] 19 | -------------------------------------------------------------------------------- /LeetCode/python3/153.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMin(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | start, end = 0, len(nums) - 1 8 | while start < end - 1: 9 | mid = start + (end - start) // 2 10 | if nums[mid] < nums[start] and nums[mid] < nums[end]: 11 | end = mid 12 | elif nums[mid] > nums[start] and nums[mid] > nums[end]: 13 | start = mid 14 | else: 15 | return nums[0] 16 | return min(nums[start], nums[end]) -------------------------------------------------------------------------------- /LeetCode/python3/167.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, numbers: 'List[int]', target: 'int') -> 'List[int]': 3 | i, j = 0, len(numbers) - 1 4 | while i < j: 5 | s = numbers[i] + numbers[j] 6 | if s < target: 7 | i += 1 8 | elif s > target: 9 | j -= 1 10 | else: 11 | return [i + 1, j + 1] 12 | return [] -------------------------------------------------------------------------------- /LeetCode/python3/169.py: -------------------------------------------------------------------------------- 1 | import collections 2 | 3 | 4 | class Solution: 5 | def majorityElement(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | d = collections.Counter(nums) 11 | return max(d.keys(), key=d.get) 12 | -------------------------------------------------------------------------------- /LeetCode/python3/171.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def titleToNumber(self, s: str) -> int: 3 | res, base = 0, 1 4 | for i in range(len(s) - 1, -1, -1): 5 | res += (ord(s[i]) - ord('A') + 1) * base 6 | base *= 26 7 | return res -------------------------------------------------------------------------------- /LeetCode/python3/19.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: 3 | i = 0 4 | fast, slow = head,head 5 | while i < n: 6 | fast = fast.next 7 | i += 1 8 | if not fast: 9 | return slow.next 10 | while fast and fast.next: 11 | fast = fast.next 12 | slow = slow.next 13 | slow.next = slow.next.next if slow.next else None 14 | return head 15 | -------------------------------------------------------------------------------- /LeetCode/python3/198.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rob(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | if n == 0: 5 | return 0 6 | elif n == 1: 7 | return nums[0] 8 | elif n == 2: 9 | return max(nums[0], nums[1]) 10 | prev = nums[0] 11 | curr = max(nums[0], nums[1]) 12 | for i in range(2, n): 13 | temp = curr 14 | curr = max(curr, prev + nums[i]) 15 | prev = temp 16 | return curr 17 | -------------------------------------------------------------------------------- /LeetCode/python3/202.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isHappy(self, n): 3 | """ 4 | :type n: int 5 | :rtype: bool 6 | """ 7 | s = set() 8 | while n > 0: 9 | k = n 10 | sum = 0 11 | while (k > 0): 12 | sum += (k % 10) ** 2 13 | k //= 10 14 | n = sum 15 | if n == 1: 16 | return True 17 | if sum in s: 18 | return False 19 | s.add(sum) -------------------------------------------------------------------------------- /LeetCode/python3/205.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isIsomorphic(self, s: str, t: str) -> bool: 3 | dict1, dict2 = {}, {} 4 | for i in range(len(s)): 5 | dict1[s[i]] = t[i] 6 | for i in range(len(t)): 7 | dict2[t[i]] = s[i] 8 | res1 = "" 9 | for i in range(len(s)): 10 | res1 += dict1[s[i]] 11 | res2 = "" 12 | for i in range(len(t)): 13 | res2 += dict2[t[i]] 14 | return res1 == t and res2 == s 15 | -------------------------------------------------------------------------------- /LeetCode/python3/206.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseList(self, head): 3 | """ 4 | :type head: ListNode 5 | :rtype: ListNode 6 | """ 7 | prev = None 8 | while head: 9 | next = head.next 10 | head.next = prev 11 | prev = head 12 | head = next 13 | return prev -------------------------------------------------------------------------------- /LeetCode/python3/213.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def Rob(self, nums, m, n) -> int: 3 | prev, curr = nums[m], max(nums[m], nums[m + 1]) 4 | for i in range(m + 2, n): 5 | prev, curr = curr, max(prev + nums[i], curr) 6 | return curr 7 | 8 | def rob(self, nums: List[int]) -> int: 9 | if len(nums) == 0: 10 | return 0 11 | elif len(nums) <= 2: 12 | return max(nums) 13 | return max(self.Rob(nums, 0, len(nums) - 1), self.Rob(nums, 1, len(nums))) 14 | -------------------------------------------------------------------------------- /LeetCode/python3/215.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findKthLargest(self, nums: List[int], k: int) -> int: 3 | min_heap = [] 4 | for m in nums: 5 | if len(min_heap) < k or min_heap[0] < m: 6 | heapq.heappush(min_heap, m) 7 | if len(min_heap) > k: 8 | heapq.heappop(min_heap) 9 | return min_heap[0] -------------------------------------------------------------------------------- /LeetCode/python3/217.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def containsDuplicate(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: bool 6 | """ 7 | hash_table = set() 8 | for i in nums: 9 | if i in hash_table: 10 | return True 11 | else: 12 | hash_table.add(i) 13 | return False -------------------------------------------------------------------------------- /LeetCode/python3/231.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfTwo(self, n: int) -> bool: 3 | if n <= 0: 4 | return False 5 | while n & 1 == 0: 6 | n >>= 1 7 | return n == 1 8 | -------------------------------------------------------------------------------- /LeetCode/python3/238.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def productExceptSelf(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[int] 6 | """ 7 | temp = 1 8 | ret = [1] * len(nums) 9 | for i in range(len(nums) - 1): 10 | ret[i+1] = ret[i] * nums[i] 11 | for i in range(len(nums)-2, -1, -1): 12 | temp *= nums[i + 1] 13 | ret[i] *= temp 14 | return ret 15 | -------------------------------------------------------------------------------- /LeetCode/python3/24.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def swapPairs(self, head: ListNode) -> ListNode: 3 | if not head or not head.next: 4 | return head 5 | first, second = head, head.next if head else None 6 | head = second 7 | while first and second: 8 | cache = second.next 9 | second.next = first 10 | first.next = cache.next if cache and cache.next else cache 11 | first = cache 12 | second = first.next if first else None 13 | return head 14 | -------------------------------------------------------------------------------- /LeetCode/python3/240.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def searchMatrix(self, matrix, target) -> bool: 3 | m, n = len(matrix), len(matrix[0]) if len(matrix) > 0 else 0 4 | if m == 0: 5 | return False 6 | i, j = m - 1, 0 7 | while i >= 0 and j < n and matrix[i][j] != target: 8 | if matrix[i][j] > target: 9 | i -= 1 10 | else: 11 | j += 1 12 | if i < 0 or j >= n: 13 | return False 14 | return True 15 | -------------------------------------------------------------------------------- /LeetCode/python3/242.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isAnagram(self, s: str, t: str) -> bool: 3 | d1, d2 = {}, {} 4 | for ch in s: 5 | if ch not in d1: 6 | d1[ch] = 1 7 | else: 8 | d1[ch] += 1 9 | for ch in t: 10 | if ch not in d2: 11 | d2[ch] = 1 12 | else: 13 | d2[ch] += 1 14 | return d1 == d2 15 | -------------------------------------------------------------------------------- /LeetCode/python3/258.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addDigits(self, num: int) -> int: 3 | while num > 9: 4 | temp, sum = num, 0 5 | while temp > 0: 6 | sum += temp % 10 7 | temp //= 10 8 | num = sum 9 | return num 10 | -------------------------------------------------------------------------------- /LeetCode/python3/26.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeDuplicates(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | nums[:] = list(set(nums)) 8 | nums.sort() 9 | return len(nums) -------------------------------------------------------------------------------- /LeetCode/python3/260.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def singleNumber(self, nums: List[int]) -> List[int]: 3 | x = 0 4 | for m in nums: 5 | x ^= m 6 | s = 1 7 | while (s & x) == 0: 8 | s <<= 1 9 | ret = [0, 0] 10 | for m in nums: 11 | if (m & s) == 0: 12 | ret[0] ^= m 13 | else: 14 | ret[1] ^= m 15 | return ret 16 | -------------------------------------------------------------------------------- /LeetCode/python3/263.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isUgly(self, num: int) -> bool: 3 | if num < 1: 4 | return False 5 | while num % 2 == 0: 6 | num //= 2 7 | while num % 3 == 0: 8 | num //= 3 9 | while num % 5 == 0: 10 | num //= 5 11 | return True if num == 1 else False -------------------------------------------------------------------------------- /LeetCode/python3/264.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def nthUglyNumber(self, n: int) -> int: 3 | primes = [2, 3, 5] 4 | m = len(primes) 5 | factor, res = [0 for _ in range(m)], [1 for _ in range(n)] 6 | for i in range(1, n): 7 | res[i] = sys.maxsize 8 | for j in range(m): 9 | res[i] = min(res[i], primes[j] * res[factor[j]]) 10 | for j in range(m): 11 | if primes[j] * res[factor[j]] == res[i]: 12 | factor[j] += 1 13 | return res[-1] 14 | -------------------------------------------------------------------------------- /LeetCode/python3/279.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numSquares(self, n: int) -> int: 3 | dp = [sys.maxsize] * (n + 1) 4 | square = [i * i for i in range(1, int(math.sqrt(n)) + 1)] 5 | dp[0], dp[1] = 0, 1 6 | for i in range(2, n + 1): 7 | for j in square: 8 | if j > i: 9 | break 10 | dp[i] = min(dp[i], dp[i - j] + 1) 11 | return dp[n] 12 | -------------------------------------------------------------------------------- /LeetCode/python3/28.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def strStr(self, haystack: str, needle: str) -> int: 3 | n, m = len(haystack), len(needle) 4 | if m == 0: 5 | return 0 6 | for i in range(n): 7 | if i + m > n: 8 | return -1 9 | j = 0 10 | while i + j < n and j < m and haystack[i + j] == needle[j]: 11 | j += 1 12 | if j == m: 13 | return i 14 | return -1 -------------------------------------------------------------------------------- /LeetCode/python3/283.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def moveZeroes(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: void Do not return anything, modify nums in-place instead. 6 | """ 7 | index = 0 8 | for i in range(0,len(nums)): 9 | if nums[i] != 0: 10 | nums[index] = nums[i] 11 | index += 1 12 | nums[index:] = (len(nums) - index) * [0] -------------------------------------------------------------------------------- /LeetCode/python3/287.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDuplicate(self, nums: List[int]) -> int: 3 | p1, p2 = nums[0], nums[nums[0]] 4 | while nums[p1] != nums[p2]: 5 | p1 = nums[p1] 6 | p2 = nums[nums[p2]] 7 | p2 = 0 8 | while nums[p1] != nums[p2]: 9 | p1 = nums[p1] 10 | p2 = nums[p2] 11 | return nums[p1] 12 | -------------------------------------------------------------------------------- /LeetCode/python3/290.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def wordPattern(self, pattern: str, s: str) -> bool: 3 | match, rev = {}, {} 4 | s = s.split(' ') 5 | if len(s) != len(pattern): 6 | return False 7 | for i in range(len(s)): 8 | if (pattern[i] in match and match[pattern[i]] != s[i]) or (s[i] in rev and rev[s[i]] != pattern[i]): 9 | return False 10 | match[pattern[i]] = s[i] 11 | rev[s[i]] = pattern[i] 12 | return True 13 | -------------------------------------------------------------------------------- /LeetCode/python3/300.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLIS(self, nums: List[int]) -> int: 3 | dp = [] 4 | for i in range(len(nums)): 5 | for j in range(len(dp)): 6 | if nums[i] <= dp[j]: 7 | dp[j] = nums[i] 8 | break 9 | else: 10 | dp.append(nums[i]) 11 | return len(dp) -------------------------------------------------------------------------------- /LeetCode/python3/303.py: -------------------------------------------------------------------------------- 1 | class NumArray: 2 | def __init__(self, nums: List[int]): 3 | self.nums = nums 4 | self.sum = [] 5 | cul = 0 6 | for i in range(len(nums)): 7 | cul += nums[i] 8 | self.sum.append(cul) 9 | 10 | def sumRange(self, i: int, j: int) -> int: 11 | return self.sum[j] - self.sum[i] + self.nums[i] 12 | -------------------------------------------------------------------------------- /LeetCode/python3/313.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int: 3 | m = len(primes) 4 | factor, res = [0 for _ in range(m)], [1 for _ in range(n)] 5 | for i in range(1, n): 6 | res[i] = sys.maxsize 7 | for j in range(m): 8 | res[i] = min(res[i], primes[j] * res[factor[j]]) 9 | for j in range(m): 10 | if primes[j] * res[factor[j]] == res[i]: 11 | factor[j] += 1 12 | return res[-1] 13 | -------------------------------------------------------------------------------- /LeetCode/python3/326.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfThree(self, n: int) -> bool: 3 | if n <= 0: 4 | return False 5 | while n % 3 == 0: 6 | n //= 3 7 | return n == 1 8 | -------------------------------------------------------------------------------- /LeetCode/python3/342.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfFour(self, num: int) -> bool: 3 | if num <= 0: 4 | return False 5 | while num > 1: 6 | if num & 1 or num & 2: 7 | return False 8 | num >>= 2 9 | return True 10 | -------------------------------------------------------------------------------- /LeetCode/python3/343.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def integerBreak(self, n: int) -> int: 3 | dp = [0] * (n + 1) 4 | dp[1] = 1 5 | for i in range(2, n + 1): 6 | for d in range(i - 1, i // 2 - 1, -1): 7 | dp[i] = max(dp[i], max(d, dp[d]) * max(i - d, dp[i - d])) 8 | return dp[n] 9 | -------------------------------------------------------------------------------- /LeetCode/python3/345.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseVowels(self, s: str) -> str: 3 | vowel = set('aeiouAEIOU') 4 | i, j = 0, len(s) - 1 5 | while i < j: 6 | if s[i] in vowel and s[j] in vowel: 7 | s = s[:i] + s[j] + s[i + 1:j] + s[i] + s[j + 1:] 8 | i += 1 9 | j -= 1 10 | elif s[i] in vowel: 11 | j -= 1 12 | elif s[j] in vowel: 13 | i += 1 14 | else: 15 | i += 1 16 | j -= 1 17 | return s -------------------------------------------------------------------------------- /LeetCode/python3/347.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def topKFrequent(self, nums: List[int], k: int) -> List[int]: 3 | counter = collections.Counter(nums) 4 | min_heap = [] 5 | for key, value in counter.items(): 6 | if len(min_heap) < k or min_heap[0] < value: 7 | heapq.heappush(min_heap, value) 8 | if len(min_heap) > k: 9 | heapq.heappop(min_heap) 10 | ret = [] 11 | for key,value in counter.items(): 12 | if value >= min_heap[0]: 13 | ret.append(key) 14 | return ret -------------------------------------------------------------------------------- /LeetCode/python3/349.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intersection(self, nums1, nums2): 3 | """ 4 | :type nums1: List[int] 5 | :type nums2: List[int] 6 | :rtype: List[int] 7 | """ 8 | nums = [] 9 | s = set(nums1) 10 | for n in nums2: 11 | if n in s: 12 | nums.append(n) 13 | s.remove(n) 14 | return nums -------------------------------------------------------------------------------- /LeetCode/python3/376.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def wiggleMaxLength(self, nums: List[int]) -> int: 3 | if len(nums) < 2: 4 | return len(nums) 5 | inc, dec = 1, 1 6 | for i in range(1, len(nums)): 7 | if nums[i] > nums[i - 1]: 8 | inc = dec + 1 9 | elif nums[i] < nums[i - 1]: 10 | dec = inc + 1 11 | return max(inc, dec) 12 | -------------------------------------------------------------------------------- /LeetCode/python3/378.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kthSmallest(self, matrix: List[List[int]], k: int) -> int: 3 | heap = [] 4 | for i in range(len(matrix)): 5 | for j in range(len(matrix[i])): 6 | heapq.heappush(heap, matrix[i][j]) 7 | return heapq.nsmallest(k, heap)[-1] 8 | -------------------------------------------------------------------------------- /LeetCode/python3/387.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def firstUniqChar(self, s: str) -> int: 3 | res, pos = len(s), {} 4 | if len(s) == 0: 5 | return -1 6 | for i in range(len(s) - 1, -1, -1): 7 | if s[i] not in pos: 8 | pos[s[i]] = i 9 | else: 10 | pos[s[i]] = -1 11 | for _, val in pos.items(): 12 | if val != -1: 13 | res = min(res, val) 14 | return res if res != len(s) else -1 -------------------------------------------------------------------------------- /LeetCode/python3/398.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | 3 | def __init__(self, nums): 4 | self.nums = nums 5 | 6 | 7 | def pick(self, target): 8 | return random.choice([k for k, v in enumerate(self.nums) if v == target]) -------------------------------------------------------------------------------- /LeetCode/python3/412.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fizzBuzz(self, n): 3 | """ 4 | :type n: int 5 | :rtype: List[str] 6 | """ 7 | num = [] 8 | for i in range(1, n + 1): 9 | if i % 3 == 0 and i % 5 == 0: 10 | num.append("FizzBuzz") 11 | elif i % 3 == 0: 12 | num.append("Fizz") 13 | elif i % 5 == 0: 14 | num.append("Buzz") 15 | else: 16 | num.append(str(i)) 17 | return num -------------------------------------------------------------------------------- /LeetCode/python3/413.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfArithmeticSlices(self, A: List[int]) -> int: 3 | consecutive, curr, sum = 0, 0, 0 4 | cul = [0 for i in range(len(A))] 5 | for i in range(2, len(A)): 6 | if A[i] - A[i - 1] == A[i - 1] - A[i - 2]: 7 | consecutive += 1 8 | curr += consecutive 9 | else: 10 | consecutive = 0 11 | sum += curr 12 | curr = 0 13 | return sum if curr == 0 else sum + curr 14 | -------------------------------------------------------------------------------- /LeetCode/python3/435.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def eraseOverlapIntervals(self, intervals: List[Interval]) -> int: 3 | intervals.sort(key=lambda x: x.start) 4 | prev, count = 0, 0 5 | for i in range(1, len(intervals)): 6 | if intervals[i].start < intervals[prev].end: 7 | count += 1 8 | if intervals[i].end <= intervals[prev].end: 9 | prev = i 10 | else: 11 | prev = i 12 | return count -------------------------------------------------------------------------------- /LeetCode/python3/442.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDuplicates(self, nums: List[int]) -> List[int]: 3 | dup = [] 4 | for i in range(len(nums)): 5 | if nums[abs(nums[i]) - 1] < 0: 6 | dup.append(abs(nums[i])) 7 | else: 8 | nums[abs(nums[i]) - 1] *= -1 9 | return dup 10 | -------------------------------------------------------------------------------- /LeetCode/python3/448.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDisappearedNumbers(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[int] 6 | """ 7 | return list(set(range(1, len(nums) + 1)) - set(nums)) -------------------------------------------------------------------------------- /LeetCode/python3/455.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findContentChildren(self, g: List[int], s: List[int]) -> int: 3 | s.sort(reverse=True) 4 | g.sort(reverse=True) 5 | i, j = 0, 0 6 | while i < len(s) and j < len(g): 7 | if g[j] <= s[i]: 8 | i += 1 9 | j += 1 10 | return i -------------------------------------------------------------------------------- /LeetCode/python3/485.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMaxConsecutiveOnes(self, nums: List[int]) -> int: 3 | consecutive = 0 4 | ret = 0 5 | for i in range(len(nums) + 1): 6 | if i == len(nums) or nums[i] == 0: 7 | ret = max(ret, consecutive) 8 | consecutive = 0 9 | else: 10 | consecutive += 1 11 | return ret 12 | -------------------------------------------------------------------------------- /LeetCode/python3/492.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def constructRectangle(self, area: int) -> List[int]: 3 | ret = [-1e9, 1e9] 4 | low, high = 1, area 5 | while low <= high: 6 | high = area // low 7 | if area % low == 0 and abs(low - high) < abs(ret[0] - ret[1]): 8 | ret[0] = high; 9 | ret[1] = low; 10 | low += 1 11 | return ret 12 | -------------------------------------------------------------------------------- /LeetCode/python3/503.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def nextGreaterElements(self, nums: List[int]) -> List[int]: 3 | stack = nums[::-1] 4 | ret = [-1 for i in range(len(nums))] 5 | for i in range(len(nums) - 1, -1, -1): 6 | while stack and stack[-1] <= nums[i]: 7 | stack.pop() 8 | if stack: 9 | ret[i] = stack[-1] 10 | stack.append(nums[i]) 11 | return ret 12 | -------------------------------------------------------------------------------- /LeetCode/python3/509.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fib(self, N): 3 | """ 4 | :type N: int 5 | :rtype: int 6 | """ 7 | if N == 0: 8 | return 0 9 | a, b = 0, 1 10 | for i in range(1, N): 11 | a, b = b, a+b 12 | return b 13 | -------------------------------------------------------------------------------- /LeetCode/python3/521.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findLUSlength(self, a: str, b: str) -> int: 3 | return -1 if a == b else max(len(a), len(b)) -------------------------------------------------------------------------------- /LeetCode/python3/524.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findLongestWord(self, s: str, d: List[str]) -> str: 3 | res = "" 4 | for target in d: 5 | i, j = 0, 0 6 | while i < len(target) and j < len(s): 7 | if target[i] == s[j]: 8 | i += 1 9 | j += 1 10 | if i == len(target): 11 | if len(target) > len(res) or (len(target) == len(res) and target < res): 12 | res = target 13 | return res 14 | -------------------------------------------------------------------------------- /LeetCode/python3/528.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | 3 | def __init__(self, w: List[int]): 4 | self.nums = w 5 | for i in range(1, len(w)): 6 | self.nums[i] += self.nums[i - 1] 7 | 8 | def pickIndex(self) -> int: 9 | m = random.randint(1, self.nums[-1]) 10 | i, j, k = 0, len(self.nums) - 1, 0 11 | while i < j: 12 | k = i + (j - i) // 2 13 | if self.nums[k] < m: 14 | i = k + 1 15 | else: 16 | j = k 17 | return i 18 | -------------------------------------------------------------------------------- /LeetCode/python3/532.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findPairs(self, nums, k): 3 | """ 4 | :type nums: List[int] 5 | :type k: int 6 | :rtype: int 7 | """ 8 | counts = {} 9 | for n in nums: 10 | if n not in counts: 11 | counts[n] = 1 12 | else: 13 | counts[n] += 1 14 | ret = 0 15 | for c in counts: 16 | if k == 0 and counts[c] > 1: 17 | ret += 1 18 | elif k > 0 and c - k in counts: 19 | ret += 1 20 | return ret -------------------------------------------------------------------------------- /LeetCode/python3/55.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canJump(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: bool 6 | """ 7 | pos = 0 8 | for i in range(len(nums)-1): 9 | pos = max(pos, i + nums[i]) 10 | if pos <= i: 11 | return False 12 | return True 13 | -------------------------------------------------------------------------------- /LeetCode/python3/551.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkRecord(self, s): 3 | """ 4 | :type s: str 5 | :rtype: bool 6 | """ 7 | if s.count('A') > 1 or s.count('LLL') > 0: 8 | return False 9 | return True -------------------------------------------------------------------------------- /LeetCode/python3/56.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def merge(self, intervals): 3 | """ 4 | :type intervals: List[Interval] 5 | :rtype: List[Interval] 6 | """ 7 | intervals.sort(key=lambda x: x.start) 8 | ret = [] 9 | for interval in intervals: 10 | if not ret or interval.start > ret[-1].end: 11 | ret.append(interval) 12 | else: 13 | ret[-1].end = max(ret[-1].end, interval.end) 14 | return ret -------------------------------------------------------------------------------- /LeetCode/python3/560.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def subarraySum(self, nums: 'List[int]', k: 'int') -> 'int': 3 | sum, res, cul = 0, 0, {} 4 | cul[0] = 1 5 | for i in range(len(nums)): 6 | sum += nums[i] 7 | if sum - k in cul: 8 | res += cul[sum - k] 9 | if sum not in cul: 10 | cul[sum] = 0 11 | cul[sum] += 1 12 | return res -------------------------------------------------------------------------------- /LeetCode/python3/565.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arrayNesting(self, nums: List[int]) -> int: 3 | longest = 1 4 | for i in range(len(nums)): 5 | s = set() 6 | count = 0 7 | j = i 8 | while nums[j] >= 0 and nums[j] != j: 9 | s.add(nums[j]) 10 | nums[j] *= -1 11 | j = -nums[j] 12 | count += 1 13 | longest = max(longest, count) 14 | return longest 15 | -------------------------------------------------------------------------------- /LeetCode/python3/58.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLastWord(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | word = s.split() 8 | if not len(word): 9 | return 0 10 | return len(word[-1]) -------------------------------------------------------------------------------- /LeetCode/python3/598.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int: 3 | for o in ops: 4 | m = min(m, o[0]) 5 | n = min(n, o[1]) 6 | return m * n -------------------------------------------------------------------------------- /LeetCode/python3/62.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uniquePaths(self, m: int, n: int) -> int: 3 | if m == 0 or n == 0: 4 | return 0 5 | dp = [[0 for j in range(n)] for i in range(m)] 6 | dp[0][0] = 1 7 | for i in range(m): 8 | for j in range(n): 9 | dp[i][j] += (dp[i - 1][j] if i - 1 >= 0 else 0) + (dp[i][j - 1] if j - 1 >= 0 else 0) 10 | return dp[-1][-1] 11 | -------------------------------------------------------------------------------- /LeetCode/python3/621.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def leastInterval(self, tasks, n): 3 | """ 4 | :type tasks: List[str] 5 | :type n: int 6 | :rtype: int 7 | """ 8 | task = list(collections.Counter(tasks).values()) 9 | value = max(task) 10 | num = task.count(value) 11 | return max((value-1) * (n+1) + num, len(tasks)) -------------------------------------------------------------------------------- /LeetCode/python3/633.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def judgeSquareSum(self, c: int) -> bool: 3 | square = [] 4 | i = 0 5 | while i ** 2 <= c: 6 | square.append(i ** 2) 7 | i += 1 8 | i, j = 0, len(square) - 1 9 | while i <= j: 10 | if square[i] + square[j] == c: 11 | return True 12 | elif square[i] + square[j] < c: 13 | i += 1 14 | else: 15 | j -= 1 16 | return False 17 | -------------------------------------------------------------------------------- /LeetCode/python3/64.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minPathSum(self, grid: List[List[int]]) -> int: 3 | dp = [[sys.maxsize for j in range(len(grid[i]))] for i in range(len(grid))] 4 | dp[0][0] = grid[0][0] 5 | for i in range(len(grid)): 6 | for j in range(len(grid[i])): 7 | dp[i][j] = min(dp[i][j - 1] + grid[i][j] if j - 1 >= 0 else dp[i][j], 8 | dp[i - 1][j] + grid[i][j] if i - 1 >= 0 else dp[i][j]) 9 | return dp[-1][-1] 10 | -------------------------------------------------------------------------------- /LeetCode/python3/645.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findErrorNums(self, nums: List[int]) -> List[int]: 3 | return [sum(nums) - sum(set(nums)), sum(range(1, len(nums) + 1)) - sum(set(nums))] 4 | -------------------------------------------------------------------------------- /LeetCode/python3/646.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findLongestChain(self, pairs: List[List[int]]) -> int: 3 | dp = [1 for i in range(len(pairs))] 4 | max_len = 0 5 | pairs = sorted(pairs, key=lambda x: x[0]) 6 | for i in range(1, len(pairs)): 7 | for j in range(i): 8 | if pairs[i][0] > pairs[j][1]: 9 | dp[i] = max(dp[i], dp[j] + 1) 10 | max_len = max(max_len, dp[i]) 11 | return max_len 12 | -------------------------------------------------------------------------------- /LeetCode/python3/647.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSubstrings(self, s: str) -> int: 3 | total = len(s) 4 | dp = [[0 for i in range(len(s))] for j in range(len(s))] 5 | for i in range(len(s)): 6 | dp[i][i] = 1 7 | for i in range(len(s)): 8 | for j in range(i): 9 | if s[i] == s[j] and (i == j + 1 or dp[j + 1][i - 1] == 1): 10 | total += 1 11 | dp[j][i] = 1 12 | return total 13 | -------------------------------------------------------------------------------- /LeetCode/python3/650.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSteps(self, n: int) -> int: 3 | res, m = 0, 2 4 | while n > 1: 5 | while n % m == 0: 6 | res += m 7 | n //= m 8 | m += 1 9 | return res -------------------------------------------------------------------------------- /LeetCode/python3/66.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def plusOne(self, digits): 3 | """ 4 | :type digits: List[int] 5 | :rtype: List[int] 6 | """ 7 | extra = 1 8 | for i in range(len(digits)-1, -1, -1): 9 | digits[i] += extra 10 | extra = digits[i] // 10 11 | digits[i] %= 10 12 | if extra == 1: 13 | digits.insert(0, 1) 14 | return digits -------------------------------------------------------------------------------- /LeetCode/python3/665.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkPossibility(self, nums: List[int]) -> bool: 3 | pos = 0 4 | for i in range(1, len(nums)): 5 | if nums[i] < nums[i - 1]: 6 | if pos != 0: 7 | return False 8 | pos = i 9 | return pos == 0 or pos == 1 or pos == len(nums) - 1 or nums[pos - 1] <= nums[pos + 1] or nums[pos - 2] <= nums[pos] 10 | -------------------------------------------------------------------------------- /LeetCode/python3/667.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def constructArray(self, n, k): 3 | ans = list(range(1, n - k)) 4 | for i in range(k + 1): 5 | if i % 2 == 0: 6 | ans.append(n - k + i // 2) 7 | else: 8 | ans.append(n - i // 2) 9 | return ans 10 | -------------------------------------------------------------------------------- /LeetCode/python3/70.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def climbStairs(self, n: int) -> int: 3 | if n == 1 or n == 0: 4 | return 1 5 | prev, curr = 1, 1 6 | for i in range(2, n + 1): 7 | temp = curr 8 | curr += prev 9 | prev = temp 10 | return curr 11 | -------------------------------------------------------------------------------- /LeetCode/python3/703.py: -------------------------------------------------------------------------------- 1 | class KthLargest: 2 | 3 | def __init__(self, k: 'int', nums: 'List[int]'): 4 | self.k = k 5 | self.h = nums 6 | heapq.heapify(self.h) 7 | while len(self.h) > k: 8 | heapq.heappop(self.h) 9 | 10 | def add(self, val: 'int') -> 'int': 11 | heapq.heappush(self.h, val) 12 | if len(self.h) > self.k: 13 | heapq.heappop(self.h) 14 | return self.h[0] -------------------------------------------------------------------------------- /LeetCode/python3/704.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def search(self, nums, target): 3 | """ 4 | :type nums: List[int] 5 | :type target: int 6 | :rtype: int 7 | """ 8 | start, end = 0, len(nums)- 1 9 | while start <= end: 10 | mid = start + (end - start) // 2 11 | if nums[mid] < target: 12 | start = mid + 1 13 | elif nums[mid] > target: 14 | end = mid - 1 15 | else: 16 | return mid 17 | return -1 -------------------------------------------------------------------------------- /LeetCode/python3/728.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def selfDividingNumbers(self, left: int, right: int) -> List[int]: 3 | ret = [] 4 | for x in range(left, right + 1): 5 | y = x 6 | while y > 0: 7 | z = y % 10 8 | if z == 0 or x % z != 0: 9 | break; 10 | y //= 10 11 | if y == 0: 12 | ret.append(x) 13 | return ret -------------------------------------------------------------------------------- /LeetCode/python3/739.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def dailyTemperatures(self, T: List[int]) -> List[int]: 3 | ret = [0 for i in range(len(T))] 4 | stack = [] 5 | for i in range(len(T) - 1, -1, -1): 6 | while stack and T[i] >= T[stack[-1]]: 7 | stack.pop() 8 | if stack: 9 | ret[i] = stack[-1] - i 10 | stack.append(i) 11 | return ret 12 | -------------------------------------------------------------------------------- /LeetCode/python3/763.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def partitionLabels(self, S: str) -> List[int]: 3 | start, end, n, pos, res = 0, 0, len(S), [0 for _ in range(26)], [] 4 | for i in range(n): 5 | pos[ord(S[i]) - ord('a')] = i 6 | for i in range(n): 7 | end = max(end, pos[ord(S[i]) - ord('a')]) 8 | if end == i: 9 | res.append(end - start + 1) 10 | start = end + 1 11 | end = start 12 | return res -------------------------------------------------------------------------------- /LeetCode/python3/766.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool: 3 | dict = {} 4 | for i in range(len(matrix)): 5 | for j in range(len(matrix[i])): 6 | if i - j not in dict: 7 | dict[i - j] = matrix[i][j] 8 | elif dict[i - j] != matrix[i][j]: 9 | return False 10 | return True 11 | -------------------------------------------------------------------------------- /LeetCode/python3/769.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxChunksToSorted(self, arr: List[int]) -> int: 3 | j, count = 0, 0 4 | for i in range(len(arr)): 5 | j = max(j, arr[i]) 6 | if j == i: 7 | count += 1 8 | return count -------------------------------------------------------------------------------- /LeetCode/python3/771.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numJewelsInStones(self, J, S): 3 | """ 4 | :type J: str 5 | :type S: str 6 | :rtype: int 7 | """ 8 | num = 0 9 | jew = set(list(J)) 10 | for s in S: 11 | if s in jew: 12 | num += 1 13 | return num -------------------------------------------------------------------------------- /LeetCode/python3/775.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isIdealPermutation(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: bool 6 | """ 7 | size, m = len(A), 0 8 | for i in range(size - 2): 9 | m = max(m, A[i]) 10 | if m > A[i + 2]: 11 | return False 12 | return True 13 | -------------------------------------------------------------------------------- /LeetCode/python3/78.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def subsets(self, nums: List[int]) -> List[List[int]]: 3 | ret = [] 4 | self.Backtrack(nums, [], ret, 0) 5 | return ret 6 | 7 | def Backtrack(self, nums, curr, ret, k): 8 | ret.append([c for c in curr]) 9 | if k == len(nums): 10 | return curr 11 | for i in range(k, len(nums)): 12 | curr.append(nums[i]) 13 | curr = self.Backtrack(nums, curr, ret, i + 1) 14 | curr = curr[:-1] 15 | return curr 16 | -------------------------------------------------------------------------------- /LeetCode/python3/791.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def customSortString(self, S: str, T: str) -> str: 3 | counter = collections.Counter(T) 4 | res = "" 5 | for s in S: 6 | if s in counter: 7 | for i in range(counter[s]): 8 | res += s 9 | counter[s] = 0 10 | for c, v in counter.items(): 11 | for i in range(v): 12 | res += c 13 | return res -------------------------------------------------------------------------------- /LeetCode/python3/795.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numSubarrayBoundedMax(self, A: 'List[int]', L: 'int', R: 'int') -> 'int': 3 | i, m, j, res = 0, -1, 0, 0 4 | while j < len(A): 5 | if A[j] >= L and A[j] <= R: 6 | m = j 7 | res += m - i + 1 8 | elif A[j] < L: 9 | res += m - i + 1 if m != -1 else 0 10 | else: 11 | i = j + 1 12 | m = -1 13 | j += 1 14 | return res -------------------------------------------------------------------------------- /LeetCode/python3/80.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeDuplicates(self, nums: List[int]) -> int: 3 | i, j, n = 0, 0, len(nums) 4 | while j < n: 5 | k = j 6 | nums[i] = nums[j] 7 | i += 1 8 | j += 1 9 | if j >= n: 10 | break 11 | while j < n and nums[j] == nums[j - 1]: 12 | j += 1 13 | if j == k + 1: 14 | continue 15 | nums[i] = nums[j - 1] 16 | i += 1 17 | return i 18 | -------------------------------------------------------------------------------- /LeetCode/python3/806.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfLines(self, widths: List[int], S: str) -> List[int]: 3 | line, last_width = 1, 0 4 | for s in S: 5 | width = widths[ord(s) - ord('a')] 6 | if last_width + width <= 100: 7 | last_width += width 8 | else: 9 | last_width = width 10 | line += 1 11 | return [line, last_width] 12 | -------------------------------------------------------------------------------- /LeetCode/python3/821.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def shortestToChar(self, S: str, C: str) -> List[int]: 3 | n, l = len(S), -len(S) 4 | dis = [] 5 | for i in range(n): 6 | if S[i] == C: 7 | l = i 8 | dis.append(i - l) 9 | l = 0 10 | for i in range(n - 1, -1, -1): 11 | if S[i] == C: 12 | l = i 13 | dis[i] = min(dis[i], abs(l - i)) 14 | return dis 15 | -------------------------------------------------------------------------------- /LeetCode/python3/83.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def deleteDuplicates(self, head: ListNode) -> ListNode: 3 | node = head 4 | while node and node.next: 5 | if node.val == node.next.val: 6 | node.next = node.next.next 7 | else: 8 | node = node.next 9 | return head 10 | -------------------------------------------------------------------------------- /LeetCode/python3/867.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def transpose(self, A): 3 | """ 4 | :type A: List[List[int]] 5 | :rtype: List[List[int]] 6 | """ 7 | row, col = len(A), len(A[0]) 8 | res = [[0 for i in range(row)] for j in range(col)] 9 | for i in range(col): 10 | for j in range(row): 11 | res[i][j] = A[j][i] 12 | return res -------------------------------------------------------------------------------- /LeetCode/python3/870.py: -------------------------------------------------------------------------------- 1 | import collections 2 | class Solution: 3 | def advantageCount(self, A, B): 4 | """ 5 | :type A: List[int] 6 | :type B: List[int] 7 | :rtype: List[int] 8 | """ 9 | A.sort() 10 | d = collections.defaultdict(list) 11 | for b in sorted(B)[::-1]: 12 | if b < A[-1]: 13 | d[b].append(A.pop()) 14 | else: 15 | d[b].append(A[0]) 16 | A.remove(A[0]) 17 | return [d[b].pop() for b in B] -------------------------------------------------------------------------------- /LeetCode/python3/873.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lenLongestFibSubseq(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: int 6 | """ 7 | maxLen = 0 8 | S = set(A) 9 | for i in range(0, len(A)): 10 | for j in range(i + 1, len(A)): 11 | a, b = A[j], A[i] + A[j] 12 | cuurentLen = 2 13 | while b in S: 14 | a, b = b, a + b 15 | cuurentLen += 1 16 | maxLen = max(maxLen, cuurentLen) 17 | return maxLen if maxLen >= 3 else 0 -------------------------------------------------------------------------------- /LeetCode/python3/896.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isMonotonic(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: bool 6 | """ 7 | inc = dec = False 8 | for i in range(len(A) - 1): 9 | if A[i] < A[i+1]: 10 | inc = True 11 | elif A[i] > A[i+1]: 12 | dec = True 13 | return not (inc and dec) 14 | -------------------------------------------------------------------------------- /LeetCode/python3/9.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, x: int) -> bool: 3 | if x < 0 or (x != 0 and x % 10 == 0): 4 | return False 5 | a, b = x, 0 6 | while a > b: 7 | b = b * 10 + a % 10 8 | a //= 10 9 | return a == b or a == b // 10 10 | -------------------------------------------------------------------------------- /LeetCode/python3/908.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestRangeI(self, A: List[int], K: int) -> int: 3 | return max(0, max(A) - min(A) - 2 * K) -------------------------------------------------------------------------------- /LeetCode/python3/914.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hasGroupsSizeX(self, deck): 3 | """ 4 | :type deck: List[int] 5 | :rtype: bool 6 | """ 7 | from fractions import gcd 8 | from collections import Counter 9 | from functools import reduce 10 | vals = Counter(deck).values() 11 | g = reduce(gcd, vals) 12 | return g >= 2 13 | -------------------------------------------------------------------------------- /LeetCode/python3/921.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minAddToMakeValid(self, S: str) -> int: 3 | parentheses = [] 4 | for c in S: 5 | if c == '(': 6 | parentheses.append(c) 7 | else: 8 | if len(parentheses) == 0 or parentheses[-1] != '(': 9 | parentheses.append(c) 10 | else: 11 | parentheses.pop() 12 | return len(parentheses) 13 | -------------------------------------------------------------------------------- /LeetCode/python3/925.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isLongPressedName(self, name, typed): 3 | """ 4 | :type name: str 5 | :type typed: str 6 | :rtype: bool 7 | """ 8 | i, j, n, t = 0, 0, len(name), len(typed) 9 | while i < n and j < t: 10 | if name[i] == typed[j]: 11 | i += 1 12 | j += 1 13 | else: 14 | j += 1 15 | if i == n: 16 | return True 17 | return False -------------------------------------------------------------------------------- /LeetCode/python3/929.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numUniqueEmails(self, emails): 3 | """ 4 | :type emails: List[str] 5 | :rtype: int 6 | """ 7 | email_set = set() 8 | for email in emails: 9 | local, domain = email.split('@') 10 | local = local.replace('.', '') 11 | if '+' in local: 12 | local = local[:local.index('+')] 13 | email_set.add(local + domain) 14 | return len(email_set) -------------------------------------------------------------------------------- /LeetCode/python3/933.py: -------------------------------------------------------------------------------- 1 | class RecentCounter: 2 | 3 | def __init__(self): 4 | self.time = [] 5 | self.i, self.j = 0, 0 6 | 7 | def ping(self, t): 8 | """ 9 | :type t: int 10 | :rtype: int 11 | """ 12 | self.j += 1 13 | self.time.append(t) 14 | while self.i < self.j: 15 | if self.time[self.j - 1 ] - self.time[self.i] <= 3000: 16 | break 17 | self.i += 1 18 | return self.j-self.i 19 | -------------------------------------------------------------------------------- /LeetCode/python3/938.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int: 3 | if not root: 4 | return 0 5 | sum = 0 6 | if root.val <= R and root.val >= L: 7 | sum += root.val 8 | if root.val < R: 9 | sum += self.rangeSumBST(root.right, L, R) 10 | if root.val > L: 11 | sum += self.rangeSumBST(root.left, L, R) 12 | return sum 13 | -------------------------------------------------------------------------------- /LeetCode/python3/941.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def validMountainArray(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: bool 6 | """ 7 | size = len(A) 8 | if size < 3: 9 | return False 10 | i = 0 11 | while i + 1 < size and A[i] < A[i + 1]: 12 | i += 1 13 | if i == 0 or i == size - 1: 14 | return False 15 | while i + 1 < size and A[i] > A[i + 1]: 16 | i += 1 17 | return i == size - 1 18 | -------------------------------------------------------------------------------- /LeetCode/python3/942.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def diStringMatch(self, S: str) -> List[int]: 3 | S += 'E' 4 | l, h, n = 0, len(S) - 1, len(S) 5 | res = [0 for _ in range(n)] 6 | for i in range(n): 7 | if S[i] == 'I': 8 | res[i] = l 9 | l += 1 10 | else: 11 | res[i] = h 12 | h -= 1 13 | return res 14 | -------------------------------------------------------------------------------- /LeetCode/python3/946.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: 3 | res = [] 4 | i, n = 0, len(popped) 5 | for p in pushed: 6 | res.append(p) 7 | while res and i < n and res[-1] == popped[i]: 8 | i += 1 9 | res.pop() 10 | return i == n -------------------------------------------------------------------------------- /LeetCode/python3/96.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numTrees(self, n: int) -> int: 3 | trees = [0 for _ in range(n + 1)] 4 | trees[0], trees[1] = 1, 1, 5 | for i in range(2, n + 1): 6 | for j in range(1, i + 1): 7 | trees[i] += trees[j - 1] * trees[i - j] 8 | return trees[n] 9 | -------------------------------------------------------------------------------- /LeetCode/python3/965.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | value = 0 3 | 4 | def isUnivalTree(self, root): 5 | """ 6 | :type root: TreeNode 7 | :rtype: bool 8 | """ 9 | if not root: 10 | return True 11 | return self.CompareValue(root, root.val) 12 | 13 | def CompareValue(self, root, value): 14 | if not root: 15 | return True 16 | if root.val != value: 17 | return False 18 | return self.CompareValue(root.left, value) and self.CompareValue(root.right, value) -------------------------------------------------------------------------------- /LeetCode/python3/973.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kClosest(self, points, K): 3 | """ 4 | :type points: List[List[int]] 5 | :type K: int 6 | :rtype: List[List[int]] 7 | """ 8 | points.sort(key=lambda p: pow(p[0], 2) + pow(p[1], 2)) 9 | return points[:K] 10 | -------------------------------------------------------------------------------- /LeetCode/python3/974.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def subarraysDivByK(self, A, K): 3 | """ 4 | :type A: List[int] 5 | :type K: int 6 | :rtype: int 7 | """ 8 | count = {0: 1} 9 | prefix, ret = 0, 0 10 | for a in A: 11 | prefix = (prefix + a) % K 12 | if prefix < 0: 13 | prefix += K 14 | if prefix in count: 15 | ret += count[prefix] 16 | else: 17 | count[prefix] = 0 18 | count[prefix] += 1 19 | return ret -------------------------------------------------------------------------------- /LeetCode/python3/976.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestPerimeter(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: int 6 | """ 7 | A.sort() 8 | for i in range(len(A) - 3, -1, -1): 9 | if A[i] + A[i + 1] > A[i + 2]: 10 | return A[i] + A[i + 1] + A[i + 2] 11 | return 0 -------------------------------------------------------------------------------- /LeetCode/python3/977.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortedSquares(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: List[int] 6 | """ 7 | return sorted([i*i for i in A]) -------------------------------------------------------------------------------- /LeetCode/python3/978.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxTurbulenceSize(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: int 6 | """ 7 | size, maxLen, curLen = len(A), 1, 1 8 | if size <= 2: 9 | return size 10 | for i in range(1, size - 1): 11 | if (A[i] > A[i-1] and A[i] > A[i + 1]) or (A[i] < A[i-1] and A[i] < A[i + 1]): 12 | curLen += 1 13 | maxLen = max(maxLen, curLen) 14 | else: 15 | curLen = 1 16 | return 2 if maxLen == 1 else maxLen + 1 -------------------------------------------------------------------------------- /LeetCode/python3/979.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | total = 0 3 | 4 | def distributeCoins(self, root): 5 | """ 6 | :type root: TreeNode 7 | :rtype: int 8 | """ 9 | if not root: 10 | return 0 11 | self.total = 0 12 | value = self.CountCoins(root) 13 | return self.total 14 | 15 | def CountCoins(self, node): 16 | if not node: 17 | return 0 18 | value = node.val - 1 + self.CountCoins(node.left) + self.CountCoins(node.right) 19 | self.total += abs(value) 20 | return value 21 | -------------------------------------------------------------------------------- /LeetCode/python3/985.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumEvenAfterQueries(self, A: 'List[int]', queries: 'List[List[int]]') -> 'List[int]': 3 | m = sum(a for a in A if a % 2 == 0) 4 | ret = [] 5 | for q in queries: 6 | m -= A[q[1]] if A[q[1]] % 2 == 0 else 0 7 | A[q[1]] += q[0] 8 | m += A[q[1]] if A[q[1]] % 2 == 0 else 0 9 | ret.append(m) 10 | return ret 11 | -------------------------------------------------------------------------------- /LeetCode/python3/989.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addToArrayForm(self, A: 'List[int]', K: 'int') -> 'List[int]': 3 | rem = 0 4 | for i in range(len(A) - 1, -1, -1): 5 | rem += A[i] + K % 10 6 | A[i] = rem % 10 7 | rem //= 10 8 | K //= 10 9 | while K > 0: 10 | rem += K % 10 11 | A = [rem % 10] + A 12 | rem //= 10 13 | K //= 10 14 | if rem: 15 | A = [1] + A 16 | return A -------------------------------------------------------------------------------- /LeetCode/python3/991.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def brokenCalc(self, X: 'int', Y: 'int') -> 'int': 3 | if X >= Y: 4 | return X - Y 5 | return 1 + (self.brokenCalc(X, Y + 1) if Y % 2 == 1 else self.brokenCalc(X, Y // 2)) -------------------------------------------------------------------------------- /LeetCode_Archiver/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chr1sc2y/LeetCode_Archiver/de23e16ead29336b5ee7aa1898a392a5d6463d27/LeetCode_Archiver/__init__.py -------------------------------------------------------------------------------- /LeetCode_Archiver/spiders/__init__.py: -------------------------------------------------------------------------------- 1 | # This package will contain the spiders of your Scrapy project 2 | # 3 | # Please refer to the documentation for information on how to create and manage 4 | # your spiders. 5 | -------------------------------------------------------------------------------- /config_template.json: -------------------------------------------------------------------------------- 1 | { 2 | "username": "xxxx", 3 | "password": "xxxx" 4 | } -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import os 2 | os.system('sh ./requirements.sh') 3 | 4 | import time 5 | start = time.time() 6 | 7 | from scrapy import cmdline 8 | if os.path.exists("./.log"): 9 | os.remove("./.log") 10 | cmdline.execute("scrapy crawl QuestionSetSpider".split()) 11 | 12 | end = time.time() 13 | print("Total time: " + str(end - start)) -------------------------------------------------------------------------------- /requirements.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | pip3 install scrapy 4 | pip3 install requests 5 | pip3 install numpy 6 | pip3 install matplotlib -------------------------------------------------------------------------------- /scrapy.cfg: -------------------------------------------------------------------------------- 1 | # Automatically created by: scrapy startproject 2 | # 3 | # For more information about the [deploy] section see: 4 | # https://scrapyd.readthedocs.io/en/latest/deploy.html 5 | 6 | [settings] 7 | default = LeetCode_Archiver.settings 8 | 9 | [deploy] 10 | #url = http://localhost:6800/ 11 | project = LeetCode_Archiver 12 | --------------------------------------------------------------------------------