diff --git a/.idea/leetcode/editor.xml b/.idea/leetcode/editor.xml index 27dc992..63b52e6 100644 --- a/.idea/leetcode/editor.xml +++ b/.idea/leetcode/editor.xml @@ -51,6 +51,14 @@ + + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml index 1268fe5..9dde46f 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -226,7 +226,9 @@ + + @@ -240,10 +242,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f0fc058..4b16f3e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,31 +2,16 @@ - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + - + @@ -70,79 +57,99 @@ + + + + + + + + - + + - + - + + + + + + + + + + - - - + + + - - + + - - + + - - + + - - + + @@ -153,19 +160,19 @@ - - - - - + + + + + - - - - - + + + + + @@ -221,7 +228,22 @@ - + + + + + + + + + + + + + + + + 1560552440693 @@ -577,99 +599,182 @@ - + - + + + + + + + + + + - + - - + + + + + + + + + + - + - - + + + + + + + + + + - + - - + + + + + + + + + + - + - - + + + + + + + + + + - + - - + + + + + + + - - + + + + + + + - - + + + + + + + - - + + + + + + + - + - - + + + - - + + - + + + - + - - + + + - + - - + + + + - - + + + + + + + + + + + + - - + + + + + + + + @@ -795,7 +900,7 @@ file://$PROJECT_DIR$/[0429][N-ary Tree Level Order Traversal]/src/Main.java - 38 + 37 @@ -845,12 +950,12 @@ file://$PROJECT_DIR$/[0241][Different Ways to Add Parentheses]/src/Solution.java - 75 + 76 file://$PROJECT_DIR$/[0241][Different Ways to Add Parentheses]/src/Solution.java - 69 + 70 @@ -861,7 +966,7 @@ file://$PROJECT_DIR$/[0207][Course Schedule]/src/Solution.java - 64 + 60 @@ -871,7 +976,7 @@ file://$PROJECT_DIR$/[0475][Heaters]/src/Solution.java - 46 + 47 @@ -891,12 +996,12 @@ file://$PROJECT_DIR$/[0392][Is Subsequence]/src/Solution.java - 66 + 65 file://$PROJECT_DIR$/[0441][Arranging Coins]/src/Solution.java - 67 + 66 @@ -904,6 +1009,87 @@ 57 + + file://$PROJECT_DIR$/[0447][Number of Boomerangs]/src/Solution.java + 47 + + + file://$PROJECT_DIR$/[0447][Number of Boomerangs]/src/Solution.java + 65 + + + file://$PROJECT_DIR$/[0507][Perfect Number]/src/Solution.java + 13 + + + file://$PROJECT_DIR$/[0523][K Diff Pairs In An Array]/src/Solution.java + 19 + + + file://$PROJECT_DIR$/[0551][Student Attendance Record I]/src/Solution.java + 12 + + + file://$PROJECT_DIR$/[0566][Reshape the Matrix]/src/SolutionTest.java + 24 + + + file://$PROJECT_DIR$/[0572][Subtree of Another Tree]/src/Solution.java + 23 + + + + file://$PROJECT_DIR$/[0572][Subtree of Another Tree]/src/Solution.java + 32 + + + file://$PROJECT_DIR$/[0572][Subtree of Another Tree]/src/Solution.java + 42 + + + file://$PROJECT_DIR$/[0599][Minimum Index Sum of Two Lists]/src/Solution.java + 23 + + + file://$PROJECT_DIR$/[0605][Can Place Flowers]/src/Solution.java + 18 + + + file://$PROJECT_DIR$/[0643][Maximum Average Subarray I]/src/Solution.java + 19 + + + file://$PROJECT_DIR$/[0653][Two Sum IV - Input is a BST]/src/Solution.java + 49 + + + file://$PROJECT_DIR$/[0661][Image Smoother]/src/SolutionTest.java + 23 + + + file://$PROJECT_DIR$/[0661][Image Smoother]/src/Solution.java + 25 + + + file://$PROJECT_DIR$/[0671][Second Minimum Node In a Binary Tree]/src/Solution.java + 60 + file://$PROJECT_DIR$/[081][Search In Rotated Sorted Array II]/src/Solution2.java 17 @@ -923,167 +1109,7 @@ - - - - course == 1 - JAVA - EXPRESSION - - - i == 3 - JAVA - EXPRESSION - - - index >= s.length() - JAVA - EXPRESSION - - - path.size()==4 - JAVA - EXPRESSION - - - path.size() == 3 - JAVA - EXPRESSION - - - idx >= nums.length - JAVA - EXPRESSION - - - start2 + k - 1 >= nums2.length - JAVA - EXPRESSION - - - - - t.charAt(i) - JAVA - EXPRESSION - - - (2 << h1) - 1 - JAVA - EXPRESSION - - - dependency[1] - JAVA - EXPRESSION - - - operator.subList(i + 1, operator.size()) - JAVA - EXPRESSION - - - operand.subList(i + 1, operand.size()) - JAVA - EXPRESSION - - - new ArrayList<>(operator.subList(0, i)) - JAVA - EXPRESSION - - - operand.subList(0, i + 1) - JAVA - EXPRESSION - - - nums[i - 1] - 1 - JAVA - EXPRESSION - - - result.first() - JAVA - EXPRESSION - - - - - - - - - - - - - - - - No facets are configured - - - - - - - - Bundled Protobuf Distribution - - - - - - - - 1.7 - - - - - - - - [0419][Battleships in a Board] - - - - - - - - R User Library - - - - - - + + \ No newline at end of file diff --git a/[0000][common]/src/MaxSubArray.java b/[0000][common]/src/MaxSubArray.java index e19a385..9a12bb0 100644 --- a/[0000][common]/src/MaxSubArray.java +++ b/[0000][common]/src/MaxSubArray.java @@ -7,7 +7,7 @@ * @time: 2019-06-16 11:16 **/ public class MaxSubArray { - public int[] solve(int[] array) { + public int[] solve(int[] array) { if (array == null || array.length < 1) { return null; } diff --git a/[0001][Two Sum]/src/Main.java b/[0001][Two Sum]/src/Main.java index 7c82290..da4a59c 100644 --- a/[0001][Two Sum]/src/Main.java +++ b/[0001][Two Sum]/src/Main.java @@ -25,9 +25,10 @@ public void test2() { int[] result = new Solution().twoSum(new int[]{3, 2, 4}, 6); System.out.println("[" + result[0] + " ," + result[1] + "]"); } + @Test public void test3() { - int[] nums = {876,879,155,291,431,296,592,965,502,173,869,504,258,342,192,478,270,341,811,794,472,625,229,829,122,858,738,481,102,946,305,399,216,752,413,352,271,193,534,508,152,989,154,456,168,510,391,28,317,409,609,532,784,160,696,105,245,231,20,17,81,781,79,816,918,838,123,602,338,997,192,947,388,515,510,441,175,539,708,980,207,336,524,610,3,427,282,84,953,855,117,737,288,371,623,484,738,874,426,202,481,132,499,500,89,786,276,221,857,398,242,639,771,149,758,775,39,836,70,903,193,959,169,851,798,815,755,498,308,70,217,765,504,498,56,547,578,977,882,909,9,874,223,39,429,982,129,712,77,996,43,613,800,810,73,993,763,978,912,255,468,937,987,701,155,347,980,147,698,41,353,178,396,241,71,482,40,593,993,959,193,544,376,752,804,194,800,837,673,261,348,963,918,217,945,271,493,538,203,54,850,753,954,312,584,399,504,62,124,790,542,239,662,410,12,362,798,726,798,780,785,737,280,931,452,643,362,190,975,520,219,330,290,451,22,756,837,787,758,661,75,697,419,485,290,84,401,447,400,311,121,216,574,724,733,496,680,831,736,43,578,201,109,197,125,66,739,339,925,148,381,513,152,305,603,516,979,133,993,430,167,826,526,290,562,559,947,448,903,289,259,221,915,71,879,639,390,588,496,430,778,722,421,821,436,621,959,728,81,117,202,17,408,829,438,970,93,738,838,902,248,128,903,800,567,829,3,407,306,773,71,323,492,305,301,28,220,455,320,478,873,483,521,260,460,342,846,577,874,530,588,965,985,606,410,443,662,81,667,27,912,602,957,822,164,489,942,414,549,991,747,680,498,831,805,89,846,467,909,7,651,250,534,984,587,348,150,329,194,20,519,250,232,224,378,539,83,177,872,130,419,387,654,917,259,447,979,184,965,51,349,422,983,682,172,177,177,484,652,930,495,65,511,318,621,297,803,476,370,826,328,150,354,393,900,340,73,781,70,260,293,862,335,395,51,326,363,78,968,446,565,683,654,767,719,324,2,617,451,56,789,464,119,53,269,369,137,612,54,217,719,823,601,663,310,594,301,636,22,333,351,126,810,812,827,634,441,534,434,967,637,795,335,965,876,778,987,217,451,264,341,566,656,612,413,682,429,161,801,167,309,846,754,541,9,711,707,848,989,580,20,431,163,252,200,54,56,666,425,592,513,230,894,20,260,282,297,129,414,326,577,184,698,620,138,131,236,848,995,879,354,107,67,92,260,531,757,640,305,848,959,416,109,513,769,131,501,197,225,358,67,663,761,742,83,648,230,59,873,231,228,470,503,615,245,258,84,832,132,156,324,27,583,766,676,130,978,306,387,733,592,763,592,487,504,493,139,897,290,432,976,946,24,586,104,648,333,2,359,166,968,990,39,353,376,839,9,75,874,203,762,489,21,14,888,570,449,539,772,919,697,883,278,18,151,113,148,330,158,772,852,93,288,213,299,338,297,862,371,708,815,108,326,115,923,541,144,521,441,99,773,950,519,948,258,328,624,936,681,935,328,70,826,110,153,236,191,222,340,653,918,976,857,184,193,397,39,190,147,763,760,95,917,559,529,680,376,389,215,705,586,205,653,324,960,33,404,888,680,95,263,860,150,683,930,588,9,690,919,745,815,331,425,879,648,398,2,997,865,429,399,264,704,699,333,126,753,565,529,35,520,94,401,552,592,543,864,23,764,763,51,631,348,198,255,73,281,996,371,23,581,84,367,469,604,716,393,942,764,239,502,501,973,438,760,398,158,853,178,348,659,1000,739,296,444,587,528,355,867,615,847,885,160,357,618,959,330,82,182,59,224,355,250,270,447,534,97,590,284,909,406,954,419,909,158,626,818,350,994,609,540,957,152,827,830,386,380,318,580,853,440,789,432,710,955,381,241,930,880,632,750,876,189,662,127,434,38,144,20,424,27,466,538,158,416,508,990,650,698,990,970,663,121,9,713,489,977,530,694,141,930,169,695,305,567,368,777,442,668,746,618,86,592,185,328,772,213,644,440,178,243,774,467,991,455,404,919,197,830,568,661,826,841,695,52,982,515,47,47,198,9,272,425,975,472,9,302,338,470,542,247,492,367,180,708,521,592,58,572,887,670,314,191,280,256,845,971,157,725,862,452,76,200,538,44,324,992,459,196,18,64,147,423,187,191,246,305,973,802,832,436,444,242,979,351,733,459,825,833,691,372,861,617,618,190,57,848,527,56,378,533,308,430,473,701,401,871,790,459,216,983,305,61,391,251,447,661,951,150,28,572,206,299,477,703,301,227,960,866,450,335,337,852,906,956,873,893,867,196,131,456,608,688,840,569,91,922,606,961,906,836,168,838,91,607,186,754,708,477,248,138,211,458,17,509,645,629,816,47,185,661,856,508,984,320,763,297,9,446,970,472,12,386,476,686,940,387,721,546,206,110,349,88,781,150,308,136,809,670,291,767,889,926,999,832,462,706,13,9,753,458,309,984,404,801,366,56,611,38,691,174,670,306,229,12,151,697,415,180,655,418,975,781,40,448,625,775,722,350,163,397,634,102,961,322,354,836,652,877,997,397,957,640,70,467,976,901,792,173,869,248,829,919,89,324,9,639,560,744,890,846,452,197,558,756,988,771,573,494,64,423,348,296,587,327,909,371,24,369,174,132,197,412,142,257,790,770,171,875,724,608,329,256,626,868,22,311,499,933,173,78,631,931,191,132,970,194,778,33,832,75,76,63,271,905,164,970,716,216,828,56,131,898,565,791,47,634,205,118,280,605,896,433,38,39,303,242,746,673,541,759,588,990,586,244,152,586,371,666,361,691,815,658,537,371,482,656,117,316,327,368,657,848,557,761,221,147,673,945,914,976,579,804,405,182,89,429,133,485,939,586,452,20,292,108,747,188,899,293,125,976,573,162,592,880,241,685,191,539,361,430,84,791,903,475,96,388,485,416,583,944,939,987,939,545,474,272,494,664,543,480,812,212,400,728,28,379,410,127,607,59,614,883,509,695,765,533,665,754,848,268,159,678,807,325,125,92,208,216,337,697,778,466,861,22,950,74,804,925,617,159,73,676,712,558,487,711,774,383,817,737,555,811,304,743,27,67,535,426,766,615,102,437,765,291,718,641,951,255,375,442,204,108,455,592,364,457,758,486,593,780,277,789,323,404,473,258,953,318,898,555,390,727,510,783,427,806,92,33,474,858,851,783,12,752,356,942,307,235,397,915,502,939}; + int[] nums = {876, 879, 155, 291, 431, 296, 592, 965, 502, 173, 869, 504, 258, 342, 192, 478, 270, 341, 811, 794, 472, 625, 229, 829, 122, 858, 738, 481, 102, 946, 305, 399, 216, 752, 413, 352, 271, 193, 534, 508, 152, 989, 154, 456, 168, 510, 391, 28, 317, 409, 609, 532, 784, 160, 696, 105, 245, 231, 20, 17, 81, 781, 79, 816, 918, 838, 123, 602, 338, 997, 192, 947, 388, 515, 510, 441, 175, 539, 708, 980, 207, 336, 524, 610, 3, 427, 282, 84, 953, 855, 117, 737, 288, 371, 623, 484, 738, 874, 426, 202, 481, 132, 499, 500, 89, 786, 276, 221, 857, 398, 242, 639, 771, 149, 758, 775, 39, 836, 70, 903, 193, 959, 169, 851, 798, 815, 755, 498, 308, 70, 217, 765, 504, 498, 56, 547, 578, 977, 882, 909, 9, 874, 223, 39, 429, 982, 129, 712, 77, 996, 43, 613, 800, 810, 73, 993, 763, 978, 912, 255, 468, 937, 987, 701, 155, 347, 980, 147, 698, 41, 353, 178, 396, 241, 71, 482, 40, 593, 993, 959, 193, 544, 376, 752, 804, 194, 800, 837, 673, 261, 348, 963, 918, 217, 945, 271, 493, 538, 203, 54, 850, 753, 954, 312, 584, 399, 504, 62, 124, 790, 542, 239, 662, 410, 12, 362, 798, 726, 798, 780, 785, 737, 280, 931, 452, 643, 362, 190, 975, 520, 219, 330, 290, 451, 22, 756, 837, 787, 758, 661, 75, 697, 419, 485, 290, 84, 401, 447, 400, 311, 121, 216, 574, 724, 733, 496, 680, 831, 736, 43, 578, 201, 109, 197, 125, 66, 739, 339, 925, 148, 381, 513, 152, 305, 603, 516, 979, 133, 993, 430, 167, 826, 526, 290, 562, 559, 947, 448, 903, 289, 259, 221, 915, 71, 879, 639, 390, 588, 496, 430, 778, 722, 421, 821, 436, 621, 959, 728, 81, 117, 202, 17, 408, 829, 438, 970, 93, 738, 838, 902, 248, 128, 903, 800, 567, 829, 3, 407, 306, 773, 71, 323, 492, 305, 301, 28, 220, 455, 320, 478, 873, 483, 521, 260, 460, 342, 846, 577, 874, 530, 588, 965, 985, 606, 410, 443, 662, 81, 667, 27, 912, 602, 957, 822, 164, 489, 942, 414, 549, 991, 747, 680, 498, 831, 805, 89, 846, 467, 909, 7, 651, 250, 534, 984, 587, 348, 150, 329, 194, 20, 519, 250, 232, 224, 378, 539, 83, 177, 872, 130, 419, 387, 654, 917, 259, 447, 979, 184, 965, 51, 349, 422, 983, 682, 172, 177, 177, 484, 652, 930, 495, 65, 511, 318, 621, 297, 803, 476, 370, 826, 328, 150, 354, 393, 900, 340, 73, 781, 70, 260, 293, 862, 335, 395, 51, 326, 363, 78, 968, 446, 565, 683, 654, 767, 719, 324, 2, 617, 451, 56, 789, 464, 119, 53, 269, 369, 137, 612, 54, 217, 719, 823, 601, 663, 310, 594, 301, 636, 22, 333, 351, 126, 810, 812, 827, 634, 441, 534, 434, 967, 637, 795, 335, 965, 876, 778, 987, 217, 451, 264, 341, 566, 656, 612, 413, 682, 429, 161, 801, 167, 309, 846, 754, 541, 9, 711, 707, 848, 989, 580, 20, 431, 163, 252, 200, 54, 56, 666, 425, 592, 513, 230, 894, 20, 260, 282, 297, 129, 414, 326, 577, 184, 698, 620, 138, 131, 236, 848, 995, 879, 354, 107, 67, 92, 260, 531, 757, 640, 305, 848, 959, 416, 109, 513, 769, 131, 501, 197, 225, 358, 67, 663, 761, 742, 83, 648, 230, 59, 873, 231, 228, 470, 503, 615, 245, 258, 84, 832, 132, 156, 324, 27, 583, 766, 676, 130, 978, 306, 387, 733, 592, 763, 592, 487, 504, 493, 139, 897, 290, 432, 976, 946, 24, 586, 104, 648, 333, 2, 359, 166, 968, 990, 39, 353, 376, 839, 9, 75, 874, 203, 762, 489, 21, 14, 888, 570, 449, 539, 772, 919, 697, 883, 278, 18, 151, 113, 148, 330, 158, 772, 852, 93, 288, 213, 299, 338, 297, 862, 371, 708, 815, 108, 326, 115, 923, 541, 144, 521, 441, 99, 773, 950, 519, 948, 258, 328, 624, 936, 681, 935, 328, 70, 826, 110, 153, 236, 191, 222, 340, 653, 918, 976, 857, 184, 193, 397, 39, 190, 147, 763, 760, 95, 917, 559, 529, 680, 376, 389, 215, 705, 586, 205, 653, 324, 960, 33, 404, 888, 680, 95, 263, 860, 150, 683, 930, 588, 9, 690, 919, 745, 815, 331, 425, 879, 648, 398, 2, 997, 865, 429, 399, 264, 704, 699, 333, 126, 753, 565, 529, 35, 520, 94, 401, 552, 592, 543, 864, 23, 764, 763, 51, 631, 348, 198, 255, 73, 281, 996, 371, 23, 581, 84, 367, 469, 604, 716, 393, 942, 764, 239, 502, 501, 973, 438, 760, 398, 158, 853, 178, 348, 659, 1000, 739, 296, 444, 587, 528, 355, 867, 615, 847, 885, 160, 357, 618, 959, 330, 82, 182, 59, 224, 355, 250, 270, 447, 534, 97, 590, 284, 909, 406, 954, 419, 909, 158, 626, 818, 350, 994, 609, 540, 957, 152, 827, 830, 386, 380, 318, 580, 853, 440, 789, 432, 710, 955, 381, 241, 930, 880, 632, 750, 876, 189, 662, 127, 434, 38, 144, 20, 424, 27, 466, 538, 158, 416, 508, 990, 650, 698, 990, 970, 663, 121, 9, 713, 489, 977, 530, 694, 141, 930, 169, 695, 305, 567, 368, 777, 442, 668, 746, 618, 86, 592, 185, 328, 772, 213, 644, 440, 178, 243, 774, 467, 991, 455, 404, 919, 197, 830, 568, 661, 826, 841, 695, 52, 982, 515, 47, 47, 198, 9, 272, 425, 975, 472, 9, 302, 338, 470, 542, 247, 492, 367, 180, 708, 521, 592, 58, 572, 887, 670, 314, 191, 280, 256, 845, 971, 157, 725, 862, 452, 76, 200, 538, 44, 324, 992, 459, 196, 18, 64, 147, 423, 187, 191, 246, 305, 973, 802, 832, 436, 444, 242, 979, 351, 733, 459, 825, 833, 691, 372, 861, 617, 618, 190, 57, 848, 527, 56, 378, 533, 308, 430, 473, 701, 401, 871, 790, 459, 216, 983, 305, 61, 391, 251, 447, 661, 951, 150, 28, 572, 206, 299, 477, 703, 301, 227, 960, 866, 450, 335, 337, 852, 906, 956, 873, 893, 867, 196, 131, 456, 608, 688, 840, 569, 91, 922, 606, 961, 906, 836, 168, 838, 91, 607, 186, 754, 708, 477, 248, 138, 211, 458, 17, 509, 645, 629, 816, 47, 185, 661, 856, 508, 984, 320, 763, 297, 9, 446, 970, 472, 12, 386, 476, 686, 940, 387, 721, 546, 206, 110, 349, 88, 781, 150, 308, 136, 809, 670, 291, 767, 889, 926, 999, 832, 462, 706, 13, 9, 753, 458, 309, 984, 404, 801, 366, 56, 611, 38, 691, 174, 670, 306, 229, 12, 151, 697, 415, 180, 655, 418, 975, 781, 40, 448, 625, 775, 722, 350, 163, 397, 634, 102, 961, 322, 354, 836, 652, 877, 997, 397, 957, 640, 70, 467, 976, 901, 792, 173, 869, 248, 829, 919, 89, 324, 9, 639, 560, 744, 890, 846, 452, 197, 558, 756, 988, 771, 573, 494, 64, 423, 348, 296, 587, 327, 909, 371, 24, 369, 174, 132, 197, 412, 142, 257, 790, 770, 171, 875, 724, 608, 329, 256, 626, 868, 22, 311, 499, 933, 173, 78, 631, 931, 191, 132, 970, 194, 778, 33, 832, 75, 76, 63, 271, 905, 164, 970, 716, 216, 828, 56, 131, 898, 565, 791, 47, 634, 205, 118, 280, 605, 896, 433, 38, 39, 303, 242, 746, 673, 541, 759, 588, 990, 586, 244, 152, 586, 371, 666, 361, 691, 815, 658, 537, 371, 482, 656, 117, 316, 327, 368, 657, 848, 557, 761, 221, 147, 673, 945, 914, 976, 579, 804, 405, 182, 89, 429, 133, 485, 939, 586, 452, 20, 292, 108, 747, 188, 899, 293, 125, 976, 573, 162, 592, 880, 241, 685, 191, 539, 361, 430, 84, 791, 903, 475, 96, 388, 485, 416, 583, 944, 939, 987, 939, 545, 474, 272, 494, 664, 543, 480, 812, 212, 400, 728, 28, 379, 410, 127, 607, 59, 614, 883, 509, 695, 765, 533, 665, 754, 848, 268, 159, 678, 807, 325, 125, 92, 208, 216, 337, 697, 778, 466, 861, 22, 950, 74, 804, 925, 617, 159, 73, 676, 712, 558, 487, 711, 774, 383, 817, 737, 555, 811, 304, 743, 27, 67, 535, 426, 766, 615, 102, 437, 765, 291, 718, 641, 951, 255, 375, 442, 204, 108, 455, 592, 364, 457, 758, 486, 593, 780, 277, 789, 323, 404, 473, 258, 953, 318, 898, 555, 390, 727, 510, 783, 427, 806, 92, 33, 474, 858, 851, 783, 12, 752, 356, 942, 307, 235, 397, 915, 502, 939}; int target = 28; int[] result = new Solution().twoSum(nums, target); System.out.println("[" + result[0] + " ," + result[1] + "]"); diff --git a/[0001][Two Sum]/src/Solution.java b/[0001][Two Sum]/src/Solution.java index 5380a2f..707bcc7 100644 --- a/[0001][Two Sum]/src/Solution.java +++ b/[0001][Two Sum]/src/Solution.java @@ -62,7 +62,7 @@ public int[] twoSum(int[] nums, int target) { List list = map.get(num); result[0] = Math.min(list.get(0), list.get(1)); result[1] = Math.max(list.get(0), list.get(1)); - } else if (gap != num){ + } else if (gap != num) { result[0] = Math.min(map.get(num).get(0), map.get(gap).get(0)); result[1] = Math.max(map.get(num).get(0), map.get(gap).get(0)); } diff --git a/[0010][Regular Expression Matching]/src/Solution.java b/[0010][Regular Expression Matching]/src/Solution.java index b462e2f..8116492 100644 --- a/[0010][Regular Expression Matching]/src/Solution.java +++ b/[0010][Regular Expression Matching]/src/Solution.java @@ -44,7 +44,7 @@ public boolean isMatch(String s, String p) { match[s.length()] = true; for (int i = p.length() - 1; i >= 0; i--) { if (p.charAt(i) == '*') { - for (int j = s.length() - 1; j >= 0; j--) { + for (int j = s.length() - 1; j >= 0; j--) { match[j] = match[j] || match[j + 1] && (p.charAt(i - 1) == '.' || s.charAt(j) == p.charAt(i - 1)); } diff --git a/[0013][Roman To Integer]/src/Solution.java b/[0013][Roman To Integer]/src/Solution.java index f47e222..eee8be3 100644 --- a/[0013][Roman To Integer]/src/Solution.java +++ b/[0013][Roman To Integer]/src/Solution.java @@ -34,22 +34,31 @@ public int romanToInt(String s) { // 要减去两倍之前前值才能回到真实值 result += charToInt(s.charAt(i)) - 2 * charToInt(s.charAt(i - 1)); } else { - result += charToInt(s.charAt(i)); + result += charToInt(s.charAt(i)); } } return result; } + private int charToInt(char c) { switch (c) { - case 'I': return 1; - case 'V': return 5; - case 'X': return 10; - case 'L': return 50; - case 'C': return 100; - case 'D': return 500; - case 'M': return 1000; - default: return 0; + case 'I': + return 1; + case 'V': + return 5; + case 'X': + return 10; + case 'L': + return 50; + case 'C': + return 100; + case 'D': + return 500; + case 'M': + return 1000; + default: + return 0; } } } diff --git a/[0014][Longest Common Prefix]/src/Main.java b/[0014][Longest Common Prefix]/src/Main.java index 5a40b00..8b1b6f7 100644 --- a/[0014][Longest Common Prefix]/src/Main.java +++ b/[0014][Longest Common Prefix]/src/Main.java @@ -15,6 +15,6 @@ public void test1() { @Test public void test2() { Solution solution = new Solution(); - Assert.assertEquals("", solution.longestCommonPrefix(new String[]{"dog","racecar","car"})); + Assert.assertEquals("", solution.longestCommonPrefix(new String[]{"dog", "racecar", "car"})); } } diff --git a/[0014][Longest Common Prefix]/src/Solution.java b/[0014][Longest Common Prefix]/src/Solution.java index c7fd1ef..05c3523 100644 --- a/[0014][Longest Common Prefix]/src/Solution.java +++ b/[0014][Longest Common Prefix]/src/Solution.java @@ -46,7 +46,7 @@ public String longestCommonPrefix(String[] strs) { } } - for (String s: strs) { + for (String s : strs) { for (int i = 0; i < result.length(); i++) { if (result.charAt(i) != s.charAt(i)) { result = result.substring(0, i); diff --git a/[0015][3 Sum]/src/Main.java b/[0015][3 Sum]/src/Main.java index fc64650..1b6cc33 100644 --- a/[0015][3 Sum]/src/Main.java +++ b/[0015][3 Sum]/src/Main.java @@ -41,7 +41,7 @@ public void test4() { @Test public void test5() { Solution solution = new Solution(); - System.out.println(solution.threeSum(new int[]{82597,-9243,62390,83030,-97960,-26521,-61011,83390,-38677,12333,75987,46091,83794,19355,-71037,-6242,-28801,324,1202,-90885,-2989,-95597,-34333,35528,5680,89093,-90606,50360,-29393,-27012,53313,65213,99818,-82405,-41661,-3333,-51952,72135,-1523,26377,74685,96992,92263,15929,5467,-99555,-43348,-41689,-60383,-3990,32165,65265,-72973,-58372,12741,-48568,-46596,72419,-1859,34153,62937,81310,-61823,-96770,-54944,8845,-91184,24208,-29078,31495,65258,14198,85395,70506,-40908,56740,-12228,-40072,32429,93001,68445,-73927,25731,-91859,-24150,10093,-60271,-81683,-18126,51055,48189,-6468,25057,81194,-58628,74042,66158,-14452,-49851,-43667,11092,39189,-17025,-79173,13606,83172,92647,-59741,19343,-26644,-57607,82908,-20655,1637,80060,98994,39331,-31274,-61523,91225,-72953,13211,-75116,-98421,-41571,-69074,99587,39345,42151,-2460,98236,15690,-52507,-95803,-48935,-46492,-45606,-79254,-99851,52533,73486,39948,-7240,71815,-585,-96252,90990,-93815,93340,-71848,58733,-14859,-83082,-75794,-82082,-24871,-15206,91207,-56469,-93618,67131,-8682,75719,87429,-98757,-7535,-24890,-94160,85003,33928,75538,97456,-66424,-60074,-8527,-28697,-22308,2246,-70134,-82319,-10184,87081,-34949,-28645,-47352,-83966,-60418,-15293,-53067,-25921,55172,75064,95859,48049,34311,-86931,-38586,33686,-36714,96922,76713,-22165,-80585,-34503,-44516,39217,-28457,47227,-94036,43457,24626,-87359,26898,-70819,30528,-32397,-69486,84912,-1187,-98986,-32958,4280,-79129,-65604,9344,58964,50584,71128,-55480,24986,15086,-62360,-42977,-49482,-77256,-36895,-74818,20,3063,-49426,28152,-97329,6086,86035,-88743,35241,44249,19927,-10660,89404,24179,-26621,-6511,57745,-28750,96340,-97160,-97822,-49979,52307,79462,94273,-24808,77104,9255,-83057,77655,21361,55956,-9096,48599,-40490,-55107,2689,29608,20497,66834,-34678,23553,-81400,-66630,-96321,-34499,-12957,-20564,25610,-4322,-58462,20801,53700,71527,24669,-54534,57879,-3221,33636,3900,97832,-27688,-98715,5992,24520,-55401,-57613,-69926,57377,-77610,20123,52174,860,60429,-91994,-62403,-6218,-90610,-37263,-15052,62069,-96465,44254,89892,-3406,19121,-41842,-87783,-64125,-56120,73904,-22797,-58118,-4866,5356,75318,46119,21276,-19246,-9241,-97425,57333,-15802,93149,25689,-5532,95716,39209,-87672,-29470,-16324,-15331,27632,-39454,56530,-16000,29853,46475,78242,-46602,83192,-73440,-15816,50964,-36601,89758,38375,-40007,-36675,-94030,67576,46811,-64919,45595,76530,40398,35845,41791,67697,-30439,-82944,63115,33447,-36046,-50122,-34789,43003,-78947,-38763,-89210,32756,-20389,-31358,-90526,-81607,88741,86643,98422,47389,-75189,13091,95993,-15501,94260,-25584,-1483,-67261,-70753,25160,89614,-90620,-48542,83889,-12388,-9642,-37043,-67663,28794,-8801,13621,12241,55379,84290,21692,-95906,-85617,-17341,-63767,80183,-4942,-51478,30997,-13658,8838,17452,-82869,-39897,68449,31964,98158,-49489,62283,-62209,-92792,-59342,55146,-38533,20496,62667,62593,36095,-12470,5453,-50451,74716,-17902,3302,-16760,-71642,-34819,96459,-72860,21638,47342,-69897,-40180,44466,76496,84659,13848,-91600,-90887,-63742,-2156,-84981,-99280,94326,-33854,92029,-50811,98711,-36459,-75555,79110,-88164,-97397,-84217,97457,64387,30513,-53190,-83215,252,2344,-27177,-92945,-89010,82662,-11670,86069,53417,42702,97082,3695,-14530,-46334,17910,77999,28009,-12374,15498,-46941,97088,-35030,95040,92095,-59469,-24761,46491,67357,-66658,37446,-65130,-50416,99197,30925,27308,54122,-44719,12582,-99525,-38446,-69050,-22352,94757,-56062,33684,-40199,-46399,96842,-50881,-22380,-65021,40582,53623,-76034,77018,-97074,-84838,-22953,-74205,79715,-33920,-35794,-91369,73421,-82492,63680,-14915,-33295,37145,76852,-69442,60125,-74166,74308,-1900,-30195,-16267,-60781,-27760,5852,38917,25742,-3765,49097,-63541,98612,-92865,-30248,9612,-8798,53262,95781,-42278,-36529,7252,-27394,-5021,59178,80934,-48480,-75131,-54439,-19145,-48140,98457,-6601,-51616,-89730,78028,32083,-48904,16822,-81153,-8832,48720,-80728,-45133,-86647,-4259,-40453,2590,28613,50523,-4105,-27790,-74579,-17223,63721,33489,-47921,97628,-97691,-14782,-65644,18008,-93651,-71266,80990,-76732,-47104,35368,28632,59818,-86269,-89753,34557,-92230,-5933,-3487,-73557,-13174,-43981,-43630,-55171,30254,-83710,-99583,-13500,71787,5017,-25117,-78586,86941,-3251,-23867,-36315,75973,86272,-45575,77462,-98836,-10859,70168,-32971,-38739,-12761,93410,14014,-30706,-77356,-85965,-62316,63918,-59914,-64088,1591,-10957,38004,15129,-83602,-51791,34381,-89382,-26056,8942,5465,71458,-73805,-87445,-19921,-80784,69150,-34168,28301,-68955,18041,6059,82342,9947,39795,44047,-57313,48569,81936,-2863,-80932,32976,-86454,-84207,33033,32867,9104,-16580,-25727,80157,-70169,53741,86522,84651,68480,84018,61932,7332,-61322,-69663,76370,41206,12326,-34689,17016,82975,-23386,39417,72793,44774,-96259,3213,79952,29265,-61492,-49337,14162,65886,3342,-41622,-62659,-90402,-24751,88511,54739,-21383,-40161,-96610,-24944,-602,-76842,-21856,69964,43994,-15121,-85530,12718,13170,-13547,69222,62417,-75305,-81446,-38786,-52075,-23110,97681,-82800,-53178,11474,35857,94197,-58148,-23689,32506,92154,-64536,-73930,-77138,97446,-83459,70963,22452,68472,-3728,-25059,-49405,95129,-6167,12808,99918,30113,-12641,-26665,86362,-33505,50661,26714,33701,89012,-91540,40517,-12716,-57185,-87230,29914,-59560,13200,-72723,58272,23913,-45586,-96593,-26265,-2141,31087,81399,92511,-34049,20577,2803,26003,8940,42117,40887,-82715,38269,40969,-50022,72088,21291,-67280,-16523,90535,18669,94342,-39568,-88080,-99486,-20716,23108,-28037,63342,36863,-29420,-44016,75135,73415,16059,-4899,86893,43136,-7041,33483,-67612,25327,40830,6184,61805,4247,81119,-22854,-26104,-63466,63093,-63685,60369,51023,51644,-16350,74438,-83514,99083,10079,-58451,-79621,48471,67131,-86940,99093,11855,-22272,-67683,-44371,9541,18123,37766,-70922,80385,-57513,-76021,-47890,36154,72935,84387,-92681,-88303,-7810,59902,-90,-64704,-28396,-66403,8860,13343,33882,85680,7228,28160,-14003,54369,-58893,92606,-63492,-10101,64714,58486,29948,-44679,-22763,10151,-56695,4031,-18242,-36232,86168,-14263,9883,47124,47271,92761,-24958,-73263,-79661,-69147,-18874,29546,-92588,-85771,26451,-86650,-43306,-59094,-47492,-34821,-91763,-47670,33537,22843,67417,-759,92159,63075,94065,-26988,55276,65903,30414,-67129,-99508,-83092,-91493,-50426,14349,-83216,-76090,32742,-5306,-93310,-60750,-60620,-45484,-21108,-58341,-28048,-52803,69735,78906,81649,32565,-86804,-83202,-65688,-1760,89707,93322,-72750,84134,71900,-37720,19450,-78018,22001,-23604,26276,-21498,65892,-72117,-89834,-23867,55817,-77963,42518,93123,-83916,63260,-2243,-97108,85442,-36775,17984,-58810,99664,-19082,93075,-69329,87061,79713,16296,70996,13483,-74582,49900,-27669,-40562,1209,-20572,34660,83193,75579,7344,64925,88361,60969,3114,44611,-27445,53049,-16085,-92851,-53306,13859,-33532,86622,-75666,-18159,-98256,51875,-42251,-27977,-18080,23772,38160,41779,9147,94175,99905,-85755,62535,-88412,-52038,-68171,93255,-44684,-11242,-104,31796,62346,-54931,-55790,-70032,46221,56541,-91947,90592,93503,4071,20646,4856,-63598,15396,-50708,32138,-85164,38528,-89959,53852,57915,-42421,-88916,-75072,67030,-29066,49542,-71591,61708,-53985,-43051,28483,46991,-83216,80991,-46254,-48716,39356,-8270,-47763,-34410,874,-1186,-7049,28846,11276,21960,-13304,-11433,-4913,55754,79616,70423,-27523,64803,49277,14906,-97401,-92390,91075,70736,21971,-3303,55333,-93996,76538,54603,-75899,98801,46887,35041,48302,-52318,55439,24574,14079,-24889,83440,14961,34312,-89260,-22293,-81271,-2586,-71059,-10640,-93095,-5453,-70041,66543,74012,-11662,-52477,-37597,-70919,92971,-17452,-67306,-80418,7225,-89296,24296,86547,37154,-10696,74436,-63959,58860,33590,-88925,-97814,-83664,85484,-8385,-50879,57729,-74728,-87852,-15524,-91120,22062,28134,80917,32026,49707,-54252,-44319,-35139,13777,44660,85274,25043,58781,-89035,-76274,6364,-63625,72855,43242,-35033,12820,-27460,77372,-47578,-61162,-70758,-1343,-4159,64935,56024,-2151,43770,19758,-30186,-86040,24666,-62332,-67542,73180,-25821,-27826,-45504,-36858,-12041,20017,-24066,-56625,-52097,-47239,-90694,8959,7712,-14258,-5860,55349,61808,-4423,-93703,64681,-98641,-25222,46999,-83831,-54714,19997,-68477,66073,51801,-66491,52061,-52866,79907,-39736,-68331,68937,91464,98892,910,93501,31295,-85873,27036,-57340,50412,21,-2445,29471,71317,82093,-94823,-54458,-97410,39560,-7628,66452,39701,54029,37906,46773,58296,60370,-61090,85501,-86874,71443,-72702,-72047,14848,34102,77975,-66294,-36576,31349,52493,-70833,-80287,94435,39745,-98291,84524,-18942,10236,93448,50846,94023,-6939,47999,14740,30165,81048,84935,-19177,-13594,32289,62628,-90612,-542,-66627,64255,71199,-83841,-82943,-73885,8623,-67214,-9474,-35249,62254,-14087,-90969,21515,-83303,94377,-91619,19956,-98810,96727,-91939,29119,-85473,-82153,-69008,44850,74299,-76459,-86464,8315,-49912,-28665,59052,-69708,76024,-92738,50098,18683,-91438,18096,-19335,35659,91826,15779,-73070,67873,-12458,-71440,-46721,54856,97212,-81875,35805,36952,68498,81627,-34231,81712,27100,-9741,-82612,18766,-36392,2759,41728,69743,26825,48355,-17790,17165,56558,3295,-24375,55669,-16109,24079,73414,48990,-11931,-78214,90745,19878,35673,-15317,-89086,94675,-92513,88410,-93248,-19475,-74041,-19165,32329,-26266,-46828,-18747,45328,8990,-78219,-25874,-74801,-44956,-54577,-29756,-99822,-35731,-18348,-68915,-83518,-53451,95471,-2954,-13706,-8763,-21642,-37210,16814,-60070,-42743,27697,-36333,-42362,11576,85742,-82536,68767,-56103,-63012,71396,-78464,-68101,-15917,-11113,-3596,77626,-60191,-30585,-73584,6214,-84303,18403,23618,-15619,-89755,-59515,-59103,-74308,-63725,-29364,-52376,-96130,70894,-12609,50845,-2314,42264,-70825,64481,55752,4460,-68603,-88701,4713,-50441,-51333,-77907,97412,-66616,-49430,60489,-85262,-97621,-18980,44727,-69321,-57730,66287,-92566,-64427,-14270,11515,-92612,-87645,61557,24197,-81923,-39831,-10301,-23640,-76219,-68025,92761,-76493,68554,-77734,-95620,-11753,-51700,98234,-68544,-61838,29467,46603,-18221,-35441,74537,40327,-58293,75755,-57301,-7532,-94163,18179,-14388,-22258,-46417,-48285,18242,-77551,82620,250,-20060,-79568,-77259,82052,-98897,-75464,48773,-79040,-11293,45941,-67876,-69204,-46477,-46107,792,60546,-34573,-12879,-94562,20356,-48004,-62429,96242,40594,2099,99494,25724,-39394,-2388,-18563,-56510,-83570,-29214,3015,74454,74197,76678,-46597,60630,-76093,37578,-82045,-24077,62082,-87787,-74936,58687,12200,-98952,70155,-77370,21710,-84625,-60556,-84128,925,65474,-15741,-94619,88377,89334,44749,22002,-45750,-93081,-14600,-83447,46691,85040,-66447,-80085,56308,44310,24979,-29694,57991,4675,-71273,-44508,13615,-54710,23552,-78253,-34637,50497,68706,81543,-88408,-21405,6001,-33834,-21570,-46692,-25344,20310,71258,-97680,11721,59977,59247,-48949,98955,-50276,-80844,-27935,-76102,55858,-33492,40680,66691,-33188,8284,64893,-7528,6019,-85523,8434,-64366,-56663,26862,30008,-7611,-12179,-70076,21426,-11261,-36864,-61937,-59677,929,-21052,3848,-20888,-16065,98995,-32293,-86121,-54564,77831,68602,74977,31658,40699,29755,98424,80358,-69337,26339,13213,-46016,-18331,64713,-46883,-58451,-70024,-92393,-4088,70628,-51185,71164,-75791,-1636,-29102,-16929,-87650,-84589,-24229,-42137,-15653,94825,13042,88499,-47100,-90358,-7180,29754,-65727,-42659,-85560,-9037,-52459,20997,-47425,17318,21122,20472,-23037,65216,-63625,-7877,-91907,24100,-72516,22903,-85247,-8938,73878,54953,87480,-31466,-99524,35369,-78376,89984,-15982,94045,-7269,23319,-80456,-37653,-76756,2909,81936,54958,-12393,60560,-84664,-82413,66941,-26573,-97532,64460,18593,-85789,-38820,-92575,-43663,-89435,83272,-50585,13616,-71541,-53156,727,-27644,16538,34049,57745,34348,35009,16634,-18791,23271,-63844,95817,21781,16590,59669,15966,-6864,48050,-36143,97427,-59390,96931,78939,-1958,50777,43338,-51149,39235,-27054,-43492,67457,-83616,37179,10390,85818,2391,73635,87579,-49127,-81264,-79023,-81590,53554,-74972,-83940,-13726,-39095,29174,78072,76104,47778,25797,-29515,-6493,-92793,22481,-36197,-65560,42342,15750,97556,99634,-56048,-35688,13501,63969,-74291,50911,39225,93702,-3490,-59461,-30105,-46761,-80113,92906,-68487,50742,36152,-90240,-83631,24597,-50566,-15477,18470,77038,40223,-80364,-98676,70957,-63647,99537,13041,31679,86631,37633,-16866,13686,-71565,21652,-46053,-80578,-61382,68487,-6417,4656,20811,67013,-30868,-11219,46,74944,14627,56965,42275,-52480,52162,-84883,-52579,-90331,92792,42184,-73422,-58440,65308,-25069,5475,-57996,59557,-17561,2826,-56939,14996,-94855,-53707,99159,43645,-67719,-1331,21412,41704,31612,32622,1919,-69333,-69828,22422,-78842,57896,-17363,27979,-76897,35008,46482,-75289,65799,20057,7170,41326,-76069,90840,-81253,-50749,3649,-42315,45238,-33924,62101,96906,58884,-7617,-28689,-66578,62458,50876,-57553,6739,41014,-64040,-34916,37940,13048,-97478,-11318,-89440,-31933,-40357,-59737,-76718,-14104,-31774,28001,4103,41702,-25120,-31654,63085,-3642,84870,-83896,-76422,-61520,12900,88678,85547,33132,-88627,52820,63915,-27472,78867,-51439,33005,-23447,-3271,-39308,39726,-74260,-31874,-36893,93656,910,-98362,60450,-88048,99308,13947,83996,-90415,-35117,70858,-55332,-31721,97528,82982,-86218,6822,25227,36946,97077,-4257,-41526,56795,89870,75860,-70802,21779,14184,-16511,-89156,-31422,71470,69600,-78498,74079,-19410,40311,28501,26397,-67574,-32518,68510,38615,19355,-6088,-97159,-29255,-92523,3023,-42536,-88681,64255,41206,44119,52208,39522,-52108,91276,-70514,83436,63289,-79741,9623,99559,12642,85950,83735,-21156,-67208,98088,-7341,-27763,-30048,-44099,-14866,-45504,-91704,19369,13700,10481,-49344,-85686,33994,19672,36028,60842,66564,-24919,33950,-93616,-47430,-35391,-28279,56806,74690,39284,-96683,-7642,-75232,37657,-14531,-86870,-9274,-26173,98640,88652,64257,46457,37814,-19370,9337,-22556,-41525,39105,-28719,51611,-93252,98044,-90996,21710,-47605,-64259,-32727,53611,-31918,-3555,33316,-66472,21274,-37731,-2919,15016,48779,-88868,1897,41728,46344,-89667,37848,68092,-44011,85354,-43776,38739,-31423,-66330,65167,-22016,59405,34328,-60042,87660,-67698,-59174,-1408,-46809,-43485,-88807,-60489,13974,22319,55836,-62995,-37375,-4185,32687,-36551,-75237,58280,26942,-73756,71756,78775,-40573,14367,-71622,-77338,24112,23414,-7679,-51721,87492,85066,-21612,57045,10673,-96836,52461,-62218,-9310,65862,-22748,89906,-96987,-98698,26956,-43428,46141,47456,28095,55952,67323,-36455,-60202,-43302,-82932,42020,77036,10142,60406,70331,63836,58850,-66752,52109,21395,-10238,-98647,-41962,27778,69060,98535,-28680,-52263,-56679,66103,-42426,27203,80021,10153,58678,36398,63112,34911,20515,62082,-15659,-40785,27054,43767,-20289,65838,-6954,-60228,-72226,52236,-35464,25209,-15462,-79617,-41668,-84083,62404,-69062,18913,46545,20757,13805,24717,-18461,-47009,-25779,68834,64824,34473,39576,31570,14861,-15114,-41233,95509,68232,67846,84902,-83060,17642,-18422,73688,77671,-26930,64484,-99637,73875,6428,21034,-73471,19664,-68031,15922,-27028,48137,54955,-82793,-41144,-10218,-24921,-28299,-2288,68518,-54452,15686,-41814,66165,-72207,-61986,80020,50544,-99500,16244,78998,40989,14525,-56061,-24692,-94790,21111,37296,-90794,72100,70550,-31757,17708,-74290,61910,78039,-78629,-25033,73172,-91953,10052,64502,99585,-1741,90324,-73723,68942,28149,30218,24422,16659,10710,-62594,94249,96588,46192,34251,73500,-65995,-81168,41412,-98724,-63710,-54696,-52407,19746,45869,27821,-94866,-76705,-13417,-61995,-71560,43450,67384,-8838,-80293,-28937,23330,-89694,-40586,46918,80429,-5475,78013,25309,-34162,37236,-77577,86744,26281,-29033,-91813,35347,13033,-13631,-24459,3325,-71078,-75359,81311,19700,47678,-74680,-84113,45192,35502,37675,19553,76522,-51098,-18211,89717,4508,-82946,27749,85995,89912,-53678,-64727,-14778,32075,-63412,-40524,86440,-2707,-36821,63850,-30883,67294,-99468,-23708,34932,34386,98899,29239,-23385,5897,54882,98660,49098,70275,17718,88533,52161,63340,50061,-89457,19491,-99156,24873,-17008,64610,-55543,50495,17056,-10400,-56678,-29073,-42960,-76418,98562,-88104,-96255,10159,-90724,54011,12052,45871,-90933,-69420,67039,37202,78051,-52197,-40278,-58425,65414,-23394,-1415,6912,-53447,7352,17307,-78147,63727,98905,55412,-57658,-32884,-44878,22755,39730,3638,35111,39777,74193,38736,-11829,-61188,-92757,55946,-71232,-63032,-83947,39147,-96684,-99233,25131,-32197,24406,-55428,-61941,25874,-69453,64483,-19644,-68441,12783,87338,-48676,66451,-447,-61590,50932,-11270,29035,65698,-63544,10029,80499,-9461,86368,91365,-81810,-71914,-52056,-13782,44240,-30093,-2437,24007,67581,-17365,-69164,-8420,-69289,-29370,48010,90439,13141,69243,50668,39328,61731,78266,-81313,17921,-38196,55261,9948,-24970,75712,-72106,28696,7461,31621,61047,51476,56512,11839,-96916,-82739,28924,-99927,58449,37280,69357,11219,-32119,-62050,-48745,-83486,-52376,42668,82659,68882,38773,46269,-96005,97630,25009,-2951,-67811,99801,81587,-79793,-18547,-83086,69512,33127,-92145,-88497,47703,59527,1909,88785,-88882,69188,-46131,-5589,-15086,36255,-53238,-33009,82664,53901,35939,-42946,-25571,33298,69291,53199,74746,-40127,-39050,91033,51717,-98048,87240,36172,65453,-94425,-63694,-30027,59004,88660,3649,-20267,-52565,-67321,34037,4320,91515,-56753,60115,27134,68617,-61395,-26503,-98929,-8849,-63318,10709,-16151,61905,-95785,5262,23670,-25277,90206,-19391,45735,37208,-31992,-92450,18516,-90452,-58870,-58602,93383,14333,17994,82411,-54126,-32576,35440,-60526,-78764,-25069,-9022,-394,92186,-38057,55328,-61569,67780,77169,19546,-92664,-94948,44484,-13439,83529,27518,-48333,72998,38342,-90553,-98578,-76906,81515,-16464,78439,92529,35225,-39968,-10130,-7845,-32245,-74955,-74996,67731,-13897,-82493,33407,93619,59560,-24404,-57553,19486,-45341,34098,-24978,-33612,79058,71847,76713,-95422,6421,-96075,-59130,-28976,-16922,-62203,69970,68331,21874,40551,89650,51908,58181,66480,-68177,34323,-3046,-49656,-59758,43564,-10960,-30796,15473,-20216,46085,-85355,41515,-30669,-87498,57711,56067,63199,-83805,62042,91213,-14606,4394,-562,74913,10406,96810,-61595,32564,31640,-9732,42058,98052,-7908,-72330,1558,-80301,34878,32900,3939,-8824,88316,20937,21566,-3218,-66080,-31620,86859,54289,90476,-42889,-15016,-18838,75456,30159,-67101,42328,-92703,85850,-5475,23470,-80806,68206,17764,88235,46421,-41578,74005,-81142,80545,20868,-1560,64017,83784,68863,-97516,-13016,-72223,79630,-55692,82255,88467,28007,-34686,-69049,-41677,88535,-8217,68060,-51280,28971,49088,49235,26905,-81117,-44888,40623,74337,-24662,97476,79542,-72082,-35093,98175,-61761,-68169,59697,-62542,-72965,59883,-64026,-37656,-92392,-12113,-73495,98258,68379,-21545,64607,-70957,-92254,-97460,-63436,-8853,-19357,-51965,-76582,12687,-49712,45413,-60043,33496,31539,-57347,41837,67280,-68813,52088,-13155,-86430,-15239,-45030,96041,18749,-23992,46048,35243,-79450,85425,-58524,88781,-39454,53073,-48864,-82289,39086,82540,-11555,25014,-5431,-39585,-89526,2705,31953,-81611,36985,-56022,68684,-27101,11422,64655,-26965,-63081,-13840,-91003,-78147,-8966,41488,1988,99021,-61575,-47060,65260,-23844,-21781,-91865,-19607,44808,2890,63692,-88663,-58272,15970,-65195,-45416,-48444,-78226,-65332,-24568,42833,-1806,-71595,80002,-52250,30952,48452,-90106,31015,-22073,62339,63318,78391,28699,77900,-4026,-76870,-45943,33665,9174,-84360,-22684,-16832,-67949,-38077,-38987,-32847,51443,-53580,-13505,9344,-92337,26585,70458,-52764,-67471,-68411,-1119,-2072,-93476,67981,40887,-89304,-12235,41488,1454,5355,-34855,-72080,24514,-58305,3340,34331,8731,77451,-64983,-57876,82874,62481,-32754,-39902,22451,-79095,-23904,78409,-7418,77916})); + System.out.println(solution.threeSum(new int[]{82597, -9243, 62390, 83030, -97960, -26521, -61011, 83390, -38677, 12333, 75987, 46091, 83794, 19355, -71037, -6242, -28801, 324, 1202, -90885, -2989, -95597, -34333, 35528, 5680, 89093, -90606, 50360, -29393, -27012, 53313, 65213, 99818, -82405, -41661, -3333, -51952, 72135, -1523, 26377, 74685, 96992, 92263, 15929, 5467, -99555, -43348, -41689, -60383, -3990, 32165, 65265, -72973, -58372, 12741, -48568, -46596, 72419, -1859, 34153, 62937, 81310, -61823, -96770, -54944, 8845, -91184, 24208, -29078, 31495, 65258, 14198, 85395, 70506, -40908, 56740, -12228, -40072, 32429, 93001, 68445, -73927, 25731, -91859, -24150, 10093, -60271, -81683, -18126, 51055, 48189, -6468, 25057, 81194, -58628, 74042, 66158, -14452, -49851, -43667, 11092, 39189, -17025, -79173, 13606, 83172, 92647, -59741, 19343, -26644, -57607, 82908, -20655, 1637, 80060, 98994, 39331, -31274, -61523, 91225, -72953, 13211, -75116, -98421, -41571, -69074, 99587, 39345, 42151, -2460, 98236, 15690, -52507, -95803, -48935, -46492, -45606, -79254, -99851, 52533, 73486, 39948, -7240, 71815, -585, -96252, 90990, -93815, 93340, -71848, 58733, -14859, -83082, -75794, -82082, -24871, -15206, 91207, -56469, -93618, 67131, -8682, 75719, 87429, -98757, -7535, -24890, -94160, 85003, 33928, 75538, 97456, -66424, -60074, -8527, -28697, -22308, 2246, -70134, -82319, -10184, 87081, -34949, -28645, -47352, -83966, -60418, -15293, -53067, -25921, 55172, 75064, 95859, 48049, 34311, -86931, -38586, 33686, -36714, 96922, 76713, -22165, -80585, -34503, -44516, 39217, -28457, 47227, -94036, 43457, 24626, -87359, 26898, -70819, 30528, -32397, -69486, 84912, -1187, -98986, -32958, 4280, -79129, -65604, 9344, 58964, 50584, 71128, -55480, 24986, 15086, -62360, -42977, -49482, -77256, -36895, -74818, 20, 3063, -49426, 28152, -97329, 6086, 86035, -88743, 35241, 44249, 19927, -10660, 89404, 24179, -26621, -6511, 57745, -28750, 96340, -97160, -97822, -49979, 52307, 79462, 94273, -24808, 77104, 9255, -83057, 77655, 21361, 55956, -9096, 48599, -40490, -55107, 2689, 29608, 20497, 66834, -34678, 23553, -81400, -66630, -96321, -34499, -12957, -20564, 25610, -4322, -58462, 20801, 53700, 71527, 24669, -54534, 57879, -3221, 33636, 3900, 97832, -27688, -98715, 5992, 24520, -55401, -57613, -69926, 57377, -77610, 20123, 52174, 860, 60429, -91994, -62403, -6218, -90610, -37263, -15052, 62069, -96465, 44254, 89892, -3406, 19121, -41842, -87783, -64125, -56120, 73904, -22797, -58118, -4866, 5356, 75318, 46119, 21276, -19246, -9241, -97425, 57333, -15802, 93149, 25689, -5532, 95716, 39209, -87672, -29470, -16324, -15331, 27632, -39454, 56530, -16000, 29853, 46475, 78242, -46602, 83192, -73440, -15816, 50964, -36601, 89758, 38375, -40007, -36675, -94030, 67576, 46811, -64919, 45595, 76530, 40398, 35845, 41791, 67697, -30439, -82944, 63115, 33447, -36046, -50122, -34789, 43003, -78947, -38763, -89210, 32756, -20389, -31358, -90526, -81607, 88741, 86643, 98422, 47389, -75189, 13091, 95993, -15501, 94260, -25584, -1483, -67261, -70753, 25160, 89614, -90620, -48542, 83889, -12388, -9642, -37043, -67663, 28794, -8801, 13621, 12241, 55379, 84290, 21692, -95906, -85617, -17341, -63767, 80183, -4942, -51478, 30997, -13658, 8838, 17452, -82869, -39897, 68449, 31964, 98158, -49489, 62283, -62209, -92792, -59342, 55146, -38533, 20496, 62667, 62593, 36095, -12470, 5453, -50451, 74716, -17902, 3302, -16760, -71642, -34819, 96459, -72860, 21638, 47342, -69897, -40180, 44466, 76496, 84659, 13848, -91600, -90887, -63742, -2156, -84981, -99280, 94326, -33854, 92029, -50811, 98711, -36459, -75555, 79110, -88164, -97397, -84217, 97457, 64387, 30513, -53190, -83215, 252, 2344, -27177, -92945, -89010, 82662, -11670, 86069, 53417, 42702, 97082, 3695, -14530, -46334, 17910, 77999, 28009, -12374, 15498, -46941, 97088, -35030, 95040, 92095, -59469, -24761, 46491, 67357, -66658, 37446, -65130, -50416, 99197, 30925, 27308, 54122, -44719, 12582, -99525, -38446, -69050, -22352, 94757, -56062, 33684, -40199, -46399, 96842, -50881, -22380, -65021, 40582, 53623, -76034, 77018, -97074, -84838, -22953, -74205, 79715, -33920, -35794, -91369, 73421, -82492, 63680, -14915, -33295, 37145, 76852, -69442, 60125, -74166, 74308, -1900, -30195, -16267, -60781, -27760, 5852, 38917, 25742, -3765, 49097, -63541, 98612, -92865, -30248, 9612, -8798, 53262, 95781, -42278, -36529, 7252, -27394, -5021, 59178, 80934, -48480, -75131, -54439, -19145, -48140, 98457, -6601, -51616, -89730, 78028, 32083, -48904, 16822, -81153, -8832, 48720, -80728, -45133, -86647, -4259, -40453, 2590, 28613, 50523, -4105, -27790, -74579, -17223, 63721, 33489, -47921, 97628, -97691, -14782, -65644, 18008, -93651, -71266, 80990, -76732, -47104, 35368, 28632, 59818, -86269, -89753, 34557, -92230, -5933, -3487, -73557, -13174, -43981, -43630, -55171, 30254, -83710, -99583, -13500, 71787, 5017, -25117, -78586, 86941, -3251, -23867, -36315, 75973, 86272, -45575, 77462, -98836, -10859, 70168, -32971, -38739, -12761, 93410, 14014, -30706, -77356, -85965, -62316, 63918, -59914, -64088, 1591, -10957, 38004, 15129, -83602, -51791, 34381, -89382, -26056, 8942, 5465, 71458, -73805, -87445, -19921, -80784, 69150, -34168, 28301, -68955, 18041, 6059, 82342, 9947, 39795, 44047, -57313, 48569, 81936, -2863, -80932, 32976, -86454, -84207, 33033, 32867, 9104, -16580, -25727, 80157, -70169, 53741, 86522, 84651, 68480, 84018, 61932, 7332, -61322, -69663, 76370, 41206, 12326, -34689, 17016, 82975, -23386, 39417, 72793, 44774, -96259, 3213, 79952, 29265, -61492, -49337, 14162, 65886, 3342, -41622, -62659, -90402, -24751, 88511, 54739, -21383, -40161, -96610, -24944, -602, -76842, -21856, 69964, 43994, -15121, -85530, 12718, 13170, -13547, 69222, 62417, -75305, -81446, -38786, -52075, -23110, 97681, -82800, -53178, 11474, 35857, 94197, -58148, -23689, 32506, 92154, -64536, -73930, -77138, 97446, -83459, 70963, 22452, 68472, -3728, -25059, -49405, 95129, -6167, 12808, 99918, 30113, -12641, -26665, 86362, -33505, 50661, 26714, 33701, 89012, -91540, 40517, -12716, -57185, -87230, 29914, -59560, 13200, -72723, 58272, 23913, -45586, -96593, -26265, -2141, 31087, 81399, 92511, -34049, 20577, 2803, 26003, 8940, 42117, 40887, -82715, 38269, 40969, -50022, 72088, 21291, -67280, -16523, 90535, 18669, 94342, -39568, -88080, -99486, -20716, 23108, -28037, 63342, 36863, -29420, -44016, 75135, 73415, 16059, -4899, 86893, 43136, -7041, 33483, -67612, 25327, 40830, 6184, 61805, 4247, 81119, -22854, -26104, -63466, 63093, -63685, 60369, 51023, 51644, -16350, 74438, -83514, 99083, 10079, -58451, -79621, 48471, 67131, -86940, 99093, 11855, -22272, -67683, -44371, 9541, 18123, 37766, -70922, 80385, -57513, -76021, -47890, 36154, 72935, 84387, -92681, -88303, -7810, 59902, -90, -64704, -28396, -66403, 8860, 13343, 33882, 85680, 7228, 28160, -14003, 54369, -58893, 92606, -63492, -10101, 64714, 58486, 29948, -44679, -22763, 10151, -56695, 4031, -18242, -36232, 86168, -14263, 9883, 47124, 47271, 92761, -24958, -73263, -79661, -69147, -18874, 29546, -92588, -85771, 26451, -86650, -43306, -59094, -47492, -34821, -91763, -47670, 33537, 22843, 67417, -759, 92159, 63075, 94065, -26988, 55276, 65903, 30414, -67129, -99508, -83092, -91493, -50426, 14349, -83216, -76090, 32742, -5306, -93310, -60750, -60620, -45484, -21108, -58341, -28048, -52803, 69735, 78906, 81649, 32565, -86804, -83202, -65688, -1760, 89707, 93322, -72750, 84134, 71900, -37720, 19450, -78018, 22001, -23604, 26276, -21498, 65892, -72117, -89834, -23867, 55817, -77963, 42518, 93123, -83916, 63260, -2243, -97108, 85442, -36775, 17984, -58810, 99664, -19082, 93075, -69329, 87061, 79713, 16296, 70996, 13483, -74582, 49900, -27669, -40562, 1209, -20572, 34660, 83193, 75579, 7344, 64925, 88361, 60969, 3114, 44611, -27445, 53049, -16085, -92851, -53306, 13859, -33532, 86622, -75666, -18159, -98256, 51875, -42251, -27977, -18080, 23772, 38160, 41779, 9147, 94175, 99905, -85755, 62535, -88412, -52038, -68171, 93255, -44684, -11242, -104, 31796, 62346, -54931, -55790, -70032, 46221, 56541, -91947, 90592, 93503, 4071, 20646, 4856, -63598, 15396, -50708, 32138, -85164, 38528, -89959, 53852, 57915, -42421, -88916, -75072, 67030, -29066, 49542, -71591, 61708, -53985, -43051, 28483, 46991, -83216, 80991, -46254, -48716, 39356, -8270, -47763, -34410, 874, -1186, -7049, 28846, 11276, 21960, -13304, -11433, -4913, 55754, 79616, 70423, -27523, 64803, 49277, 14906, -97401, -92390, 91075, 70736, 21971, -3303, 55333, -93996, 76538, 54603, -75899, 98801, 46887, 35041, 48302, -52318, 55439, 24574, 14079, -24889, 83440, 14961, 34312, -89260, -22293, -81271, -2586, -71059, -10640, -93095, -5453, -70041, 66543, 74012, -11662, -52477, -37597, -70919, 92971, -17452, -67306, -80418, 7225, -89296, 24296, 86547, 37154, -10696, 74436, -63959, 58860, 33590, -88925, -97814, -83664, 85484, -8385, -50879, 57729, -74728, -87852, -15524, -91120, 22062, 28134, 80917, 32026, 49707, -54252, -44319, -35139, 13777, 44660, 85274, 25043, 58781, -89035, -76274, 6364, -63625, 72855, 43242, -35033, 12820, -27460, 77372, -47578, -61162, -70758, -1343, -4159, 64935, 56024, -2151, 43770, 19758, -30186, -86040, 24666, -62332, -67542, 73180, -25821, -27826, -45504, -36858, -12041, 20017, -24066, -56625, -52097, -47239, -90694, 8959, 7712, -14258, -5860, 55349, 61808, -4423, -93703, 64681, -98641, -25222, 46999, -83831, -54714, 19997, -68477, 66073, 51801, -66491, 52061, -52866, 79907, -39736, -68331, 68937, 91464, 98892, 910, 93501, 31295, -85873, 27036, -57340, 50412, 21, -2445, 29471, 71317, 82093, -94823, -54458, -97410, 39560, -7628, 66452, 39701, 54029, 37906, 46773, 58296, 60370, -61090, 85501, -86874, 71443, -72702, -72047, 14848, 34102, 77975, -66294, -36576, 31349, 52493, -70833, -80287, 94435, 39745, -98291, 84524, -18942, 10236, 93448, 50846, 94023, -6939, 47999, 14740, 30165, 81048, 84935, -19177, -13594, 32289, 62628, -90612, -542, -66627, 64255, 71199, -83841, -82943, -73885, 8623, -67214, -9474, -35249, 62254, -14087, -90969, 21515, -83303, 94377, -91619, 19956, -98810, 96727, -91939, 29119, -85473, -82153, -69008, 44850, 74299, -76459, -86464, 8315, -49912, -28665, 59052, -69708, 76024, -92738, 50098, 18683, -91438, 18096, -19335, 35659, 91826, 15779, -73070, 67873, -12458, -71440, -46721, 54856, 97212, -81875, 35805, 36952, 68498, 81627, -34231, 81712, 27100, -9741, -82612, 18766, -36392, 2759, 41728, 69743, 26825, 48355, -17790, 17165, 56558, 3295, -24375, 55669, -16109, 24079, 73414, 48990, -11931, -78214, 90745, 19878, 35673, -15317, -89086, 94675, -92513, 88410, -93248, -19475, -74041, -19165, 32329, -26266, -46828, -18747, 45328, 8990, -78219, -25874, -74801, -44956, -54577, -29756, -99822, -35731, -18348, -68915, -83518, -53451, 95471, -2954, -13706, -8763, -21642, -37210, 16814, -60070, -42743, 27697, -36333, -42362, 11576, 85742, -82536, 68767, -56103, -63012, 71396, -78464, -68101, -15917, -11113, -3596, 77626, -60191, -30585, -73584, 6214, -84303, 18403, 23618, -15619, -89755, -59515, -59103, -74308, -63725, -29364, -52376, -96130, 70894, -12609, 50845, -2314, 42264, -70825, 64481, 55752, 4460, -68603, -88701, 4713, -50441, -51333, -77907, 97412, -66616, -49430, 60489, -85262, -97621, -18980, 44727, -69321, -57730, 66287, -92566, -64427, -14270, 11515, -92612, -87645, 61557, 24197, -81923, -39831, -10301, -23640, -76219, -68025, 92761, -76493, 68554, -77734, -95620, -11753, -51700, 98234, -68544, -61838, 29467, 46603, -18221, -35441, 74537, 40327, -58293, 75755, -57301, -7532, -94163, 18179, -14388, -22258, -46417, -48285, 18242, -77551, 82620, 250, -20060, -79568, -77259, 82052, -98897, -75464, 48773, -79040, -11293, 45941, -67876, -69204, -46477, -46107, 792, 60546, -34573, -12879, -94562, 20356, -48004, -62429, 96242, 40594, 2099, 99494, 25724, -39394, -2388, -18563, -56510, -83570, -29214, 3015, 74454, 74197, 76678, -46597, 60630, -76093, 37578, -82045, -24077, 62082, -87787, -74936, 58687, 12200, -98952, 70155, -77370, 21710, -84625, -60556, -84128, 925, 65474, -15741, -94619, 88377, 89334, 44749, 22002, -45750, -93081, -14600, -83447, 46691, 85040, -66447, -80085, 56308, 44310, 24979, -29694, 57991, 4675, -71273, -44508, 13615, -54710, 23552, -78253, -34637, 50497, 68706, 81543, -88408, -21405, 6001, -33834, -21570, -46692, -25344, 20310, 71258, -97680, 11721, 59977, 59247, -48949, 98955, -50276, -80844, -27935, -76102, 55858, -33492, 40680, 66691, -33188, 8284, 64893, -7528, 6019, -85523, 8434, -64366, -56663, 26862, 30008, -7611, -12179, -70076, 21426, -11261, -36864, -61937, -59677, 929, -21052, 3848, -20888, -16065, 98995, -32293, -86121, -54564, 77831, 68602, 74977, 31658, 40699, 29755, 98424, 80358, -69337, 26339, 13213, -46016, -18331, 64713, -46883, -58451, -70024, -92393, -4088, 70628, -51185, 71164, -75791, -1636, -29102, -16929, -87650, -84589, -24229, -42137, -15653, 94825, 13042, 88499, -47100, -90358, -7180, 29754, -65727, -42659, -85560, -9037, -52459, 20997, -47425, 17318, 21122, 20472, -23037, 65216, -63625, -7877, -91907, 24100, -72516, 22903, -85247, -8938, 73878, 54953, 87480, -31466, -99524, 35369, -78376, 89984, -15982, 94045, -7269, 23319, -80456, -37653, -76756, 2909, 81936, 54958, -12393, 60560, -84664, -82413, 66941, -26573, -97532, 64460, 18593, -85789, -38820, -92575, -43663, -89435, 83272, -50585, 13616, -71541, -53156, 727, -27644, 16538, 34049, 57745, 34348, 35009, 16634, -18791, 23271, -63844, 95817, 21781, 16590, 59669, 15966, -6864, 48050, -36143, 97427, -59390, 96931, 78939, -1958, 50777, 43338, -51149, 39235, -27054, -43492, 67457, -83616, 37179, 10390, 85818, 2391, 73635, 87579, -49127, -81264, -79023, -81590, 53554, -74972, -83940, -13726, -39095, 29174, 78072, 76104, 47778, 25797, -29515, -6493, -92793, 22481, -36197, -65560, 42342, 15750, 97556, 99634, -56048, -35688, 13501, 63969, -74291, 50911, 39225, 93702, -3490, -59461, -30105, -46761, -80113, 92906, -68487, 50742, 36152, -90240, -83631, 24597, -50566, -15477, 18470, 77038, 40223, -80364, -98676, 70957, -63647, 99537, 13041, 31679, 86631, 37633, -16866, 13686, -71565, 21652, -46053, -80578, -61382, 68487, -6417, 4656, 20811, 67013, -30868, -11219, 46, 74944, 14627, 56965, 42275, -52480, 52162, -84883, -52579, -90331, 92792, 42184, -73422, -58440, 65308, -25069, 5475, -57996, 59557, -17561, 2826, -56939, 14996, -94855, -53707, 99159, 43645, -67719, -1331, 21412, 41704, 31612, 32622, 1919, -69333, -69828, 22422, -78842, 57896, -17363, 27979, -76897, 35008, 46482, -75289, 65799, 20057, 7170, 41326, -76069, 90840, -81253, -50749, 3649, -42315, 45238, -33924, 62101, 96906, 58884, -7617, -28689, -66578, 62458, 50876, -57553, 6739, 41014, -64040, -34916, 37940, 13048, -97478, -11318, -89440, -31933, -40357, -59737, -76718, -14104, -31774, 28001, 4103, 41702, -25120, -31654, 63085, -3642, 84870, -83896, -76422, -61520, 12900, 88678, 85547, 33132, -88627, 52820, 63915, -27472, 78867, -51439, 33005, -23447, -3271, -39308, 39726, -74260, -31874, -36893, 93656, 910, -98362, 60450, -88048, 99308, 13947, 83996, -90415, -35117, 70858, -55332, -31721, 97528, 82982, -86218, 6822, 25227, 36946, 97077, -4257, -41526, 56795, 89870, 75860, -70802, 21779, 14184, -16511, -89156, -31422, 71470, 69600, -78498, 74079, -19410, 40311, 28501, 26397, -67574, -32518, 68510, 38615, 19355, -6088, -97159, -29255, -92523, 3023, -42536, -88681, 64255, 41206, 44119, 52208, 39522, -52108, 91276, -70514, 83436, 63289, -79741, 9623, 99559, 12642, 85950, 83735, -21156, -67208, 98088, -7341, -27763, -30048, -44099, -14866, -45504, -91704, 19369, 13700, 10481, -49344, -85686, 33994, 19672, 36028, 60842, 66564, -24919, 33950, -93616, -47430, -35391, -28279, 56806, 74690, 39284, -96683, -7642, -75232, 37657, -14531, -86870, -9274, -26173, 98640, 88652, 64257, 46457, 37814, -19370, 9337, -22556, -41525, 39105, -28719, 51611, -93252, 98044, -90996, 21710, -47605, -64259, -32727, 53611, -31918, -3555, 33316, -66472, 21274, -37731, -2919, 15016, 48779, -88868, 1897, 41728, 46344, -89667, 37848, 68092, -44011, 85354, -43776, 38739, -31423, -66330, 65167, -22016, 59405, 34328, -60042, 87660, -67698, -59174, -1408, -46809, -43485, -88807, -60489, 13974, 22319, 55836, -62995, -37375, -4185, 32687, -36551, -75237, 58280, 26942, -73756, 71756, 78775, -40573, 14367, -71622, -77338, 24112, 23414, -7679, -51721, 87492, 85066, -21612, 57045, 10673, -96836, 52461, -62218, -9310, 65862, -22748, 89906, -96987, -98698, 26956, -43428, 46141, 47456, 28095, 55952, 67323, -36455, -60202, -43302, -82932, 42020, 77036, 10142, 60406, 70331, 63836, 58850, -66752, 52109, 21395, -10238, -98647, -41962, 27778, 69060, 98535, -28680, -52263, -56679, 66103, -42426, 27203, 80021, 10153, 58678, 36398, 63112, 34911, 20515, 62082, -15659, -40785, 27054, 43767, -20289, 65838, -6954, -60228, -72226, 52236, -35464, 25209, -15462, -79617, -41668, -84083, 62404, -69062, 18913, 46545, 20757, 13805, 24717, -18461, -47009, -25779, 68834, 64824, 34473, 39576, 31570, 14861, -15114, -41233, 95509, 68232, 67846, 84902, -83060, 17642, -18422, 73688, 77671, -26930, 64484, -99637, 73875, 6428, 21034, -73471, 19664, -68031, 15922, -27028, 48137, 54955, -82793, -41144, -10218, -24921, -28299, -2288, 68518, -54452, 15686, -41814, 66165, -72207, -61986, 80020, 50544, -99500, 16244, 78998, 40989, 14525, -56061, -24692, -94790, 21111, 37296, -90794, 72100, 70550, -31757, 17708, -74290, 61910, 78039, -78629, -25033, 73172, -91953, 10052, 64502, 99585, -1741, 90324, -73723, 68942, 28149, 30218, 24422, 16659, 10710, -62594, 94249, 96588, 46192, 34251, 73500, -65995, -81168, 41412, -98724, -63710, -54696, -52407, 19746, 45869, 27821, -94866, -76705, -13417, -61995, -71560, 43450, 67384, -8838, -80293, -28937, 23330, -89694, -40586, 46918, 80429, -5475, 78013, 25309, -34162, 37236, -77577, 86744, 26281, -29033, -91813, 35347, 13033, -13631, -24459, 3325, -71078, -75359, 81311, 19700, 47678, -74680, -84113, 45192, 35502, 37675, 19553, 76522, -51098, -18211, 89717, 4508, -82946, 27749, 85995, 89912, -53678, -64727, -14778, 32075, -63412, -40524, 86440, -2707, -36821, 63850, -30883, 67294, -99468, -23708, 34932, 34386, 98899, 29239, -23385, 5897, 54882, 98660, 49098, 70275, 17718, 88533, 52161, 63340, 50061, -89457, 19491, -99156, 24873, -17008, 64610, -55543, 50495, 17056, -10400, -56678, -29073, -42960, -76418, 98562, -88104, -96255, 10159, -90724, 54011, 12052, 45871, -90933, -69420, 67039, 37202, 78051, -52197, -40278, -58425, 65414, -23394, -1415, 6912, -53447, 7352, 17307, -78147, 63727, 98905, 55412, -57658, -32884, -44878, 22755, 39730, 3638, 35111, 39777, 74193, 38736, -11829, -61188, -92757, 55946, -71232, -63032, -83947, 39147, -96684, -99233, 25131, -32197, 24406, -55428, -61941, 25874, -69453, 64483, -19644, -68441, 12783, 87338, -48676, 66451, -447, -61590, 50932, -11270, 29035, 65698, -63544, 10029, 80499, -9461, 86368, 91365, -81810, -71914, -52056, -13782, 44240, -30093, -2437, 24007, 67581, -17365, -69164, -8420, -69289, -29370, 48010, 90439, 13141, 69243, 50668, 39328, 61731, 78266, -81313, 17921, -38196, 55261, 9948, -24970, 75712, -72106, 28696, 7461, 31621, 61047, 51476, 56512, 11839, -96916, -82739, 28924, -99927, 58449, 37280, 69357, 11219, -32119, -62050, -48745, -83486, -52376, 42668, 82659, 68882, 38773, 46269, -96005, 97630, 25009, -2951, -67811, 99801, 81587, -79793, -18547, -83086, 69512, 33127, -92145, -88497, 47703, 59527, 1909, 88785, -88882, 69188, -46131, -5589, -15086, 36255, -53238, -33009, 82664, 53901, 35939, -42946, -25571, 33298, 69291, 53199, 74746, -40127, -39050, 91033, 51717, -98048, 87240, 36172, 65453, -94425, -63694, -30027, 59004, 88660, 3649, -20267, -52565, -67321, 34037, 4320, 91515, -56753, 60115, 27134, 68617, -61395, -26503, -98929, -8849, -63318, 10709, -16151, 61905, -95785, 5262, 23670, -25277, 90206, -19391, 45735, 37208, -31992, -92450, 18516, -90452, -58870, -58602, 93383, 14333, 17994, 82411, -54126, -32576, 35440, -60526, -78764, -25069, -9022, -394, 92186, -38057, 55328, -61569, 67780, 77169, 19546, -92664, -94948, 44484, -13439, 83529, 27518, -48333, 72998, 38342, -90553, -98578, -76906, 81515, -16464, 78439, 92529, 35225, -39968, -10130, -7845, -32245, -74955, -74996, 67731, -13897, -82493, 33407, 93619, 59560, -24404, -57553, 19486, -45341, 34098, -24978, -33612, 79058, 71847, 76713, -95422, 6421, -96075, -59130, -28976, -16922, -62203, 69970, 68331, 21874, 40551, 89650, 51908, 58181, 66480, -68177, 34323, -3046, -49656, -59758, 43564, -10960, -30796, 15473, -20216, 46085, -85355, 41515, -30669, -87498, 57711, 56067, 63199, -83805, 62042, 91213, -14606, 4394, -562, 74913, 10406, 96810, -61595, 32564, 31640, -9732, 42058, 98052, -7908, -72330, 1558, -80301, 34878, 32900, 3939, -8824, 88316, 20937, 21566, -3218, -66080, -31620, 86859, 54289, 90476, -42889, -15016, -18838, 75456, 30159, -67101, 42328, -92703, 85850, -5475, 23470, -80806, 68206, 17764, 88235, 46421, -41578, 74005, -81142, 80545, 20868, -1560, 64017, 83784, 68863, -97516, -13016, -72223, 79630, -55692, 82255, 88467, 28007, -34686, -69049, -41677, 88535, -8217, 68060, -51280, 28971, 49088, 49235, 26905, -81117, -44888, 40623, 74337, -24662, 97476, 79542, -72082, -35093, 98175, -61761, -68169, 59697, -62542, -72965, 59883, -64026, -37656, -92392, -12113, -73495, 98258, 68379, -21545, 64607, -70957, -92254, -97460, -63436, -8853, -19357, -51965, -76582, 12687, -49712, 45413, -60043, 33496, 31539, -57347, 41837, 67280, -68813, 52088, -13155, -86430, -15239, -45030, 96041, 18749, -23992, 46048, 35243, -79450, 85425, -58524, 88781, -39454, 53073, -48864, -82289, 39086, 82540, -11555, 25014, -5431, -39585, -89526, 2705, 31953, -81611, 36985, -56022, 68684, -27101, 11422, 64655, -26965, -63081, -13840, -91003, -78147, -8966, 41488, 1988, 99021, -61575, -47060, 65260, -23844, -21781, -91865, -19607, 44808, 2890, 63692, -88663, -58272, 15970, -65195, -45416, -48444, -78226, -65332, -24568, 42833, -1806, -71595, 80002, -52250, 30952, 48452, -90106, 31015, -22073, 62339, 63318, 78391, 28699, 77900, -4026, -76870, -45943, 33665, 9174, -84360, -22684, -16832, -67949, -38077, -38987, -32847, 51443, -53580, -13505, 9344, -92337, 26585, 70458, -52764, -67471, -68411, -1119, -2072, -93476, 67981, 40887, -89304, -12235, 41488, 1454, 5355, -34855, -72080, 24514, -58305, 3340, 34331, 8731, 77451, -64983, -57876, 82874, 62481, -32754, -39902, 22451, -79095, -23904, 78409, -7418, 77916})); } diff --git a/[0015][3 Sum]/src/Solution.java b/[0015][3 Sum]/src/Solution.java index 4d788fd..5595514 100644 --- a/[0015][3 Sum]/src/Solution.java +++ b/[0015][3 Sum]/src/Solution.java @@ -73,7 +73,7 @@ public List> threeSum(int[] nums) { // nums[a-1]Ѿѡˣѡ do { ++a; - }while (a < nums.length - 2 && nums[a - 1] == nums[a]); + } while (a < nums.length - 2 && nums[a - 1] == nums[a]); } return result; } diff --git a/[0016][3 Sum Closest]/src/Main.java b/[0016][3 Sum Closest]/src/Main.java index ecb9b20..86ada0f 100644 --- a/[0016][3 Sum Closest]/src/Main.java +++ b/[0016][3 Sum Closest]/src/Main.java @@ -7,6 +7,6 @@ public class Main { public static void main(String[] args) { Solution solution = new Solution(); - System.out.println(solution.threeSumClosest(new int[]{0, 1 , 2}, 3)); + System.out.println(solution.threeSumClosest(new int[]{0, 1, 2}, 3)); } } diff --git a/[0016][3 Sum Closest]/src/Solution.java b/[0016][3 Sum Closest]/src/Solution.java index 84df103..42019e7 100644 --- a/[0016][3 Sum Closest]/src/Solution.java +++ b/[0016][3 Sum Closest]/src/Solution.java @@ -1,6 +1,4 @@ -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; /** * Author: @@ -47,7 +45,7 @@ public int threeSumClosest(int[] nums, int target) { // iʾȡiΪ - for (int i = 0; i < nums.length - 2;) { + for (int i = 0; i < nums.length - 2; ) { // ڶܵʼλ int j = i + 1; // ǽλ @@ -76,21 +74,21 @@ public int threeSumClosest(int[] nums, int target) { // ʹtarget if (sum > target) { - do { - k--; // ҵֵͬ - }while (j < k && nums[k]==nums[k + 1]); + do { + k--; // ҵֵͬ + } while (j < k && nums[k] == nums[k + 1]); } // Сtarget else { do { j++; - }while (j < k && nums[j - 1] == nums[j]); + } while (j < k && nums[j - 1] == nums[j]); } } do { i++; - }while (i < nums.length - 2 && nums[i - 1] == nums[i]); + } while (i < nums.length - 2 && nums[i - 1] == nums[i]); } return (int) result; diff --git a/[0018][4 Sum]/src/Solution.java b/[0018][4 Sum]/src/Solution.java index f4e4785..6d14161 100644 --- a/[0018][4 Sum]/src/Solution.java +++ b/[0018][4 Sum]/src/Solution.java @@ -49,7 +49,7 @@ public List> fourSum(int[] num, int target) { Arrays.sort(num); // һ - for (int i = 0; i < num.length - 3;) { + for (int i = 0; i < num.length - 3; ) { // ĸ for (int j = num.length - 1; j > i + 2; j--) { // ĸʹòظ @@ -98,7 +98,7 @@ public List> fourSum(int[] num, int target) { // ͬΪֻʹһ do { i++; - }while (i < num.length - 2 && num[i - 1] == num[i]); + } while (i < num.length - 2 && num[i - 1] == num[i]); } return result; } diff --git a/[0022][Generate Parentheses]/src/Solution.java b/[0022][Generate Parentheses]/src/Solution.java index 75a6eb1..ff49de9 100644 --- a/[0022][Generate Parentheses]/src/Solution.java +++ b/[0022][Generate Parentheses]/src/Solution.java @@ -1,5 +1,4 @@ import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; /** diff --git a/[0023][Merge K Sorted Lists]/src/Solution.java b/[0023][Merge K Sorted Lists]/src/Solution.java index f5c8571..ef06cbb 100644 --- a/[0023][Merge K Sorted Lists]/src/Solution.java +++ b/[0023][Merge K Sorted Lists]/src/Solution.java @@ -1,6 +1,4 @@ -import java.util.ArrayList; import java.util.Comparator; -import java.util.List; import java.util.PriorityQueue; /** diff --git a/[0032][Longest Valid Parentheses]/src/Solution.java b/[0032][Longest Valid Parentheses]/src/Solution.java index 2e4f5d1..ba8947c 100644 --- a/[0032][Longest Valid Parentheses]/src/Solution.java +++ b/[0032][Longest Valid Parentheses]/src/Solution.java @@ -1,5 +1,3 @@ -import java.util.Deque; -import java.util.LinkedList; import java.util.Stack; /** diff --git a/[0033][Search In Rotated Sorted Array]/src/Main.java b/[0033][Search In Rotated Sorted Array]/src/Main.java index 7665c43..d522524 100644 --- a/[0033][Search In Rotated Sorted Array]/src/Main.java +++ b/[0033][Search In Rotated Sorted Array]/src/Main.java @@ -4,9 +4,8 @@ **/ public class Main { public static void main(String[] args) { - int[] nums = {4,5,6,7,0,1,2} - ; - Solution2 solution = new Solution2(); + int[] nums = {4, 5, 6, 7, 0, 1, 2}; + Solution2 solution = new Solution2(); System.out.println(solution.search(nums, 4)); System.out.println(solution.search(nums, 3)); diff --git a/[0037][Sudoku Solver]/src/Solution.java b/[0037][Sudoku Solver]/src/Solution.java index 299431d..8485787 100644 --- a/[0037][Sudoku Solver]/src/Solution.java +++ b/[0037][Sudoku Solver]/src/Solution.java @@ -4,26 +4,26 @@ **/ public class Solution { public void solveSudoku(char[][] board) { - if(board == null || board.length == 0) { + if (board == null || board.length == 0) { return; } solve(board); } - public boolean solve(char[][] board){ - for(int i = 0; i < board.length; i++){ - for(int j = 0; j < board[0].length; j++){ - if(board[i][j] == '.'){ + public boolean solve(char[][] board) { + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + if (board[i][j] == '.') { // 尝试1到9的数字 - for(char c = '1'; c <= '9'; c++){ - if(isValid(board, i, j, c)){ + for (char c = '1'; c <= '9'; c++) { + if (isValid(board, i, j, c)) { // 将字符放入单元格 board[i][j] = c; // 如果字符可以放入单元格返回true - if(solve(board)){ + if (solve(board)) { return true; - }else { // 如果字符不能放入单元格就是还原成点号 + } else { // 如果字符不能放入单元格就是还原成点号 board[i][j] = '.'; } } @@ -35,18 +35,18 @@ public boolean solve(char[][] board){ return true; } - private boolean isValid(char[][] board, int row, int col, char c){ - for(int i = 0; i < 9; i++) { + private boolean isValid(char[][] board, int row, int col, char c) { + for (int i = 0; i < 9; i++) { // 检查行 - if(board[i][col] != '.' && board[i][col] == c) { + if (board[i][col] != '.' && board[i][col] == c) { return false; } // 检查列 - if(board[row][i] != '.' && board[row][i] == c) { + if (board[row][i] != '.' && board[row][i] == c) { return false; } // 检查3*3的单元格 - if(board[3 * (row / 3) + i / 3][ 3 * (col / 3) + i % 3] != '.' + if (board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] != '.' && board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) { return false; } diff --git a/[0039][Combination Sum]/src/Solution.java b/[0039][Combination Sum]/src/Solution.java index 05d51b7..48c86b6 100644 --- a/[0039][Combination Sum]/src/Solution.java +++ b/[0039][Combination Sum]/src/Solution.java @@ -22,15 +22,15 @@ public List> combinationSum(int[] nums, int target) { // nums:输入的数组 // remain:剩下的值 // start:可用的值在数组中的起始位置 - private void backtrack(List> list, List tempList, int [] nums, int remain, int start){ + private void backtrack(List> list, List tempList, int[] nums, int remain, int start) { // 不够减 - if(remain < 0) { + if (remain < 0) { return; - } else if(remain == 0) { // 刚好够减 + } else if (remain == 0) { // 刚好够减 list.add(new LinkedList<>(tempList)); } else { // 从剩下的数中一个一个尝试 - for(int i = start; i < nums.length; i++){ + for (int i = start; i < nums.length; i++) { // 假设这个值是临时结果中的一个值 tempList.add(nums[i]); // 处理下一步 diff --git a/[0041][First Missing Positive]/src/Solution.java b/[0041][First Missing Positive]/src/Solution.java index b551643..d8f7546 100644 --- a/[0041][First Missing Positive]/src/Solution.java +++ b/[0041][First Missing Positive]/src/Solution.java @@ -23,6 +23,7 @@ public class Solution { * 本质上是桶排序(bucket sort),每当A[i]!= i+1 的时候,将A[i] 与A[A[i]-1] 交换, * 直到无法交换为止,终止条件是A[i]== A[A[i]-1]。 * + * * @param nums * @return */ diff --git a/[0042][Trapping Rain Water]/src/Solution.java b/[0042][Trapping Rain Water]/src/Solution.java index 810a76c..597d513 100644 --- a/[0042][Trapping Rain Water]/src/Solution.java +++ b/[0042][Trapping Rain Water]/src/Solution.java @@ -79,6 +79,7 @@ int trap2(int[] height) { * ============= * 0123456789012 * + * * @param height * @return */ diff --git a/[0046][Permutations]/src/Solution.java b/[0046][Permutations]/src/Solution.java index b08b4bb..4f8f1ad 100644 --- a/[0046][Permutations]/src/Solution.java +++ b/[0046][Permutations]/src/Solution.java @@ -1,5 +1,4 @@ import java.util.ArrayList; -import java.util.Collections; import java.util.LinkedList; import java.util.List; diff --git a/[0047][Permutations II]/src/Solution.java b/[0047][Permutations II]/src/Solution.java index 859454a..8775cef 100644 --- a/[0047][Permutations II]/src/Solution.java +++ b/[0047][Permutations II]/src/Solution.java @@ -11,7 +11,7 @@ public class Solution { public List> permuteUnique(int[] nums) { List> res = new ArrayList<>(); - if (nums == null || nums.length == 0){ + if (nums == null || nums.length == 0) { return res; } @@ -26,6 +26,7 @@ public List> permuteUnique(int[] nums) { /** * 时间复杂度:n! + * * @param nums * @param used * @param list diff --git a/[0049][Group Anagrams]/src/Solution.java b/[0049][Group Anagrams]/src/Solution.java index 3ff45ad..ef5973e 100644 --- a/[0049][Group Anagrams]/src/Solution.java +++ b/[0049][Group Anagrams]/src/Solution.java @@ -1,8 +1,4 @@ -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * @author: wangjunchao(王俊超) diff --git a/[0055][Jump Game]/src/Solution.java b/[0055][Jump Game]/src/Solution.java index 09109fc..73b2d82 100644 --- a/[0055][Jump Game]/src/Solution.java +++ b/[0055][Jump Game]/src/Solution.java @@ -30,6 +30,7 @@ public class Solution { public boolean canJump(int[] nums) { return canJump2(nums); } + public boolean canJump1(int[] nums) { if (nums == null || nums.length < 2) { return true; @@ -42,7 +43,7 @@ public boolean canJump1(int[] nums) { for (int i = 0; i < nums.length; i++) { // i不能比lastPos大,否则说明不能走到i,走不到i也就不能走到最后 // 如果在i位置可以移动的距离比已经记录到的最大距离还要大,那么更新最大的移动距离 - if (i <= lastPos && i + nums[i] > lastPos ) { + if (i <= lastPos && i + nums[i] > lastPos) { lastPos = i + nums[i]; } else if (i > lastPos) { return false; @@ -55,6 +56,7 @@ public boolean canJump1(int[] nums) { /** * 逆向,从最高层下楼梯,一层一层下降,看最后能不能下降到第0 层。 + * * @param nums * @return */ diff --git a/[0056][Merge Intervals]/src/Solution.java b/[0056][Merge Intervals]/src/Solution.java index 312eac5..7f0df6a 100644 --- a/[0056][Merge Intervals]/src/Solution.java +++ b/[0056][Merge Intervals]/src/Solution.java @@ -1,4 +1,7 @@ -import java.util.*; +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedList; +import java.util.List; /** * Author: diff --git a/[0057][Insert Interval]/src/Solution.java b/[0057][Insert Interval]/src/Solution.java index d158b09..7486cdb 100644 --- a/[0057][Insert Interval]/src/Solution.java +++ b/[0057][Insert Interval]/src/Solution.java @@ -1,5 +1,3 @@ -import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; - import java.util.LinkedList; import java.util.List; diff --git a/[0070][Climbing Stairs]/src/Solution.java b/[0070][Climbing Stairs]/src/Solution.java index 570efe8..a475bb0 100644 --- a/[0070][Climbing Stairs]/src/Solution.java +++ b/[0070][Climbing Stairs]/src/Solution.java @@ -12,6 +12,7 @@ public class Solution { * - ӵn - 1 ǰ2 * ˣf(n) = f(n - 1) + f(n - 2) * + * * @param n * @return */ diff --git a/[0073][Set Matrix Zeroes]/src/Solution.java b/[0073][Set Matrix Zeroes]/src/Solution.java index 1d25618..e0d6ab2 100644 --- a/[0073][Set Matrix Zeroes]/src/Solution.java +++ b/[0073][Set Matrix Zeroes]/src/Solution.java @@ -20,7 +20,7 @@ public void setZeroes(int[][] matrix) { } // ǵһҪ - if (j == 0){ + if (j == 0) { colFlag = true; } diff --git a/[0079][Word Search]/src/Main.java b/[0079][Word Search]/src/Main.java index 2491d90..52d523d 100644 --- a/[0079][Word Search]/src/Main.java +++ b/[0079][Word Search]/src/Main.java @@ -1,5 +1,5 @@ /** - * Author: + * Author: ������ * Date: 2015-06-20 * Time: 08:02 * Declaration: All Rights Reserved !!! @@ -8,7 +8,7 @@ public class Main { public static void main(String[] args) { Solution solution = new Solution(); System.out.println(solution.exist(new char[][]{ - "abc".toCharArray(),"aed".toCharArray(),"afg".toCharArray() + "abc".toCharArray(), "aed".toCharArray(), "afg".toCharArray() }, "abcdefg")); } } diff --git a/[0079][Word Search]/src/Solution.java b/[0079][Word Search]/src/Solution.java index e510517..ad373b1 100644 --- a/[0079][Word Search]/src/Solution.java +++ b/[0079][Word Search]/src/Solution.java @@ -76,7 +76,7 @@ private boolean search(char[][] board, boolean[][] visited, int row, int col, St visited[row][col] = true; idx[0]++; // ϣң£ĸ - hasPath = search(board, visited, row - 1, col, word, idx ) // + hasPath = search(board, visited, row - 1, col, word, idx) // || search(board, visited, row, col + 1, word, idx) // || search(board, visited, row + 1, col, word, idx) // || search(board, visited, row, col - 1, word, idx); // diff --git a/[0080][Remove Duplicates from Sorted Array II]/src/Main.java b/[0080][Remove Duplicates from Sorted Array II]/src/Main.java index 83f9e74..db6c257 100644 --- a/[0080][Remove Duplicates from Sorted Array II]/src/Main.java +++ b/[0080][Remove Duplicates from Sorted Array II]/src/Main.java @@ -9,7 +9,7 @@ public static void main(String[] args) { Solution solution = new Solution(); int[] nums = {1, 1, 1, 2, 2, 3}; int i = solution.removeDuplicates(nums); - System.out.println(i + ": "+ Arrays.toString(nums)); + System.out.println(i + ": " + Arrays.toString(nums)); } } diff --git a/[0080][Remove Duplicates from Sorted Array II]/src/Solution.java b/[0080][Remove Duplicates from Sorted Array II]/src/Solution.java index 9109816..ecd3151 100644 --- a/[0080][Remove Duplicates from Sorted Array II]/src/Solution.java +++ b/[0080][Remove Duplicates from Sorted Array II]/src/Solution.java @@ -13,7 +13,7 @@ public int removeDuplicates(int[] nums) { int index = 2; for (int i = 2; i < nums.length; i++) { - if (nums[index - 2] < nums[i] ) { + if (nums[index - 2] < nums[i]) { nums[index] = nums[i]; index++; } diff --git a/[0080][Remove Duplicates from Sorted Array II]/src/Solution2.java b/[0080][Remove Duplicates from Sorted Array II]/src/Solution2.java index c3e4f83..449987a 100644 --- a/[0080][Remove Duplicates from Sorted Array II]/src/Solution2.java +++ b/[0080][Remove Duplicates from Sorted Array II]/src/Solution2.java @@ -2,6 +2,7 @@ * @author: wangjunchao(王俊超) * @time: 2019-06-12 22:53 **/ + /** * 删除重复出现的数字,使之最多出现两次 */ diff --git a/[0081][Search In Rotated Sorted Array II]/src/Solution.java b/[0081][Search In Rotated Sorted Array II]/src/Solution.java index 3a86418..1c71ba1 100644 --- a/[0081][Search In Rotated Sorted Array II]/src/Solution.java +++ b/[0081][Search In Rotated Sorted Array II]/src/Solution.java @@ -16,6 +16,7 @@ public class Solution { * "תֵ"ĺеֵظ * дһȷһֵǷ * + * * @param nums * @param target * @return diff --git a/[0086][Partition List]/src/ListNode.java b/[0086][Partition List]/src/ListNode.java index b0946e7..63ee591 100644 --- a/[0086][Partition List]/src/ListNode.java +++ b/[0086][Partition List]/src/ListNode.java @@ -7,5 +7,8 @@ public class ListNode { int val; ListNode next; - ListNode(int x) { val = x; } + + ListNode(int x) { + val = x; + } } diff --git a/[0089][Gray Code]/src/Main.java b/[0089][Gray Code]/src/Main.java index ba43581..6349221 100644 --- a/[0089][Gray Code]/src/Main.java +++ b/[0089][Gray Code]/src/Main.java @@ -11,7 +11,7 @@ public static void main(String[] args) { Solution solution = new Solution(); List result = solution.grayCode(2); - for (Integer i: result) { + for (Integer i : result) { System.out.printf("%s\n", Integer.toBinaryString(i)); } } diff --git a/[0090][Subsets II]/src/Main.java b/[0090][Subsets II]/src/Main.java index c656a4c..4af3b37 100644 --- a/[0090][Subsets II]/src/Main.java +++ b/[0090][Subsets II]/src/Main.java @@ -1,7 +1,5 @@ import org.junit.Test; -import java.util.Arrays; - /** * @author: wangjunchao(王俊超) * @time: 2019-06-19 17:33 diff --git a/[0092][Reverse Linked List II]/src/ListNode.java b/[0092][Reverse Linked List II]/src/ListNode.java index 69ec703..8f1581b 100644 --- a/[0092][Reverse Linked List II]/src/ListNode.java +++ b/[0092][Reverse Linked List II]/src/ListNode.java @@ -7,5 +7,8 @@ public class ListNode { int val; ListNode next; - ListNode(int x) { val = x; } + + ListNode(int x) { + val = x; + } } diff --git a/[0095][Unique Binary Search Trees II]/src/Main.java b/[0095][Unique Binary Search Trees II]/src/Main.java index 97ab220..de39bd8 100644 --- a/[0095][Unique Binary Search Trees II]/src/Main.java +++ b/[0095][Unique Binary Search Trees II]/src/Main.java @@ -15,7 +15,7 @@ public void test1() { } private void print(List generateTrees) { - for (TreeNode node:generateTrees) { + for (TreeNode node : generateTrees) { print(node); System.out.println(); } @@ -23,7 +23,7 @@ private void print(List generateTrees) { private void print(TreeNode node) { if (node != null) { - System.out.print(node +", "); + System.out.print(node + ", "); if (node.left == null && node.right == null) { return; diff --git a/[0095][Unique Binary Search Trees II]/src/Solution.java b/[0095][Unique Binary Search Trees II]/src/Solution.java index 07c646e..d3ca6f6 100644 --- a/[0095][Unique Binary Search Trees II]/src/Solution.java +++ b/[0095][Unique Binary Search Trees II]/src/Solution.java @@ -31,8 +31,8 @@ private List trees(int start, int end) { List left = trees(start, i - 1); List right = trees(i + 1, end); - for(TreeNode l : left) { - for (TreeNode r: right) { + for (TreeNode l : left) { + for (TreeNode r : right) { TreeNode root = new TreeNode(i); root.left = l; root.right = r; diff --git a/[0095][Unique Binary Search Trees II]/src/TreeNode.java b/[0095][Unique Binary Search Trees II]/src/TreeNode.java index 8c356eb..2878b91 100644 --- a/[0095][Unique Binary Search Trees II]/src/TreeNode.java +++ b/[0095][Unique Binary Search Trees II]/src/TreeNode.java @@ -3,7 +3,7 @@ * @time: 2019-06-28 16:14 **/ public class TreeNode { - int val; + int val; TreeNode left; TreeNode right; diff --git a/[0096][Unique Binary Search Trees]/src/Solution2.java b/[0096][Unique Binary Search Trees]/src/Solution2.java index 753d48c..31624d1 100644 --- a/[0096][Unique Binary Search Trees]/src/Solution2.java +++ b/[0096][Unique Binary Search Trees]/src/Solution2.java @@ -7,18 +7,18 @@ public class Solution2 { /** * Given n, how many structurally unique BST's (binary search trees) that store values 1...n? - * + *

* For example, * Given n = 3, there are a total of 5 unique BST's. - * 1 3 3 2 1 - * \ / / / \ \ - * 3 2 1 1 3 2 - * / / \ \ + * 1 3 3 2 1 + * \ / / / \ \ + * 3 2 1 1 3 2 + * / / \ \ * 2 1 2 3 - * + *

* ƹʽ * f(k)*f(n-1-k)f(k)ʾk㣬е״f(k)f(n-1-k)ʾn-1-k - * + *

* f(n) = 2*f(n-1) + f(1)*f(n-2) + f(2)f(n-3) + f(3)f(n-4) + ... +f(n-2)*f(1) * * @param n @@ -41,8 +41,8 @@ public int numTrees(int n) { for (int i = 2; i <= n; i++) { // f(i) result[i] = 2 * result[i - 1]; - for (int j = 1; j <= i - 1 ; j++) { - result[i] += result[j]*result[i - 1 -j]; + for (int j = 1; j <= i - 1; j++) { + result[i] += result[j] * result[i - 1 - j]; } } diff --git a/[0100][Same Tree]/src/TreeNode.java b/[0100][Same Tree]/src/TreeNode.java index 58ea6f4..a4b81b6 100644 --- a/[0100][Same Tree]/src/TreeNode.java +++ b/[0100][Same Tree]/src/TreeNode.java @@ -1,5 +1,5 @@ /** - * Author: + * Author: ������ * Date: 2015-08-21 * Time: 18:40 * Declaration: All Rights Reserved !!! @@ -8,5 +8,8 @@ public class TreeNode { int val; TreeNode left; TreeNode right; - TreeNode(int x) { val = x; } + + TreeNode(int x) { + val = x; + } } diff --git a/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Main.java b/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Main.java index 232e3c8..c3569f8 100644 --- a/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Main.java +++ b/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Main.java @@ -1,5 +1,5 @@ /** - * Author: + * Author: ������ * Date: 2015-06-23 * Time: 10:32 * Declaration: All Rights Reserved !!! @@ -7,8 +7,8 @@ public class Main { public static void main(String[] args) { Solution solution = new Solution(); - int[] inorder = {1,2,3,4}; - int[] postorder = {3,2,4,1}; + int[] inorder = {1, 2, 3, 4}; + int[] postorder = {3, 2, 4, 1}; TreeNode root = solution.buildTree(inorder, postorder); print(root); diff --git a/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Solution.java b/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Solution.java index 83cccad..d6415c7 100644 --- a/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Solution.java +++ b/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Solution.java @@ -1,4 +1,3 @@ - /** * Author: * Date: 2015-06-23 diff --git a/[0114][Flatten Binary Tree To Linked List]/src/TreeNode.java b/[0114][Flatten Binary Tree To Linked List]/src/TreeNode.java index c988e88..463b6fe 100644 --- a/[0114][Flatten Binary Tree To Linked List]/src/TreeNode.java +++ b/[0114][Flatten Binary Tree To Linked List]/src/TreeNode.java @@ -1,5 +1,5 @@ /** - * Author: + * Author: ������ * Date: 2015-08-21 * Time: 19:02 * Declaration: All Rights Reserved !!! @@ -8,5 +8,8 @@ public class TreeNode { int val; TreeNode left; TreeNode right; - TreeNode(int x) { val = x; } + + TreeNode(int x) { + val = x; + } } diff --git a/[0126][Word Ladder II]/src/Main.java b/[0126][Word Ladder II]/src/Main.java index 356b93f..87f620f 100644 --- a/[0126][Word Ladder II]/src/Main.java +++ b/[0126][Word Ladder II]/src/Main.java @@ -1,7 +1,5 @@ -import org.junit.Assert; import org.junit.Test; -import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -25,7 +23,7 @@ public void test2() { Solution solution = new Solution(); String beginWord = "hot"; String endWord = "dog"; - List wordList = new LinkedList<>(Arrays.asList("hot","dog","dot")); + List wordList = new LinkedList<>(Arrays.asList("hot", "dog", "dot")); System.out.println(solution.findLadders(beginWord, endWord, wordList)); } @@ -34,7 +32,7 @@ public void test3() { Solution solution = new Solution(); String beginWord = "cet"; String endWord = "ism"; - String[] words = {"kid","tag","pup","ail","tun","woo","erg","luz","brr","gay","sip","kay","per","val","mes","ohs","now","boa","cet","pal","bar","die","war","hay","eco","pub","lob","rue","fry","lit","rex","jan","cot","bid","ali","pay","col","gum","ger","row","won","dan","rum","fad","tut","sag","yip","sui","ark","has","zip","fez","own","ump","dis","ads","max","jaw","out","btu","ana","gap","cry","led","abe","box","ore","pig","fie","toy","fat","cal","lie","noh","sew","ono","tam","flu","mgm","ply","awe","pry","tit","tie","yet","too","tax","jim","san","pan","map","ski","ova","wed","non","wac","nut","why","bye","lye","oct","old","fin","feb","chi","sap","owl","log","tod","dot","bow","fob","for","joe","ivy","fan","age","fax","hip","jib","mel","hus","sob","ifs","tab","ara","dab","jag","jar","arm","lot","tom","sax","tex","yum","pei","wen","wry","ire","irk","far","mew","wit","doe","gas","rte","ian","pot","ask","wag","hag","amy","nag","ron","soy","gin","don","tug","fay","vic","boo","nam","ave","buy","sop","but","orb","fen","paw","his","sub","bob","yea","oft","inn","rod","yam","pew","web","hod","hun","gyp","wei","wis","rob","gad","pie","mon","dog","bib","rub","ere","dig","era","cat","fox","bee","mod","day","apr","vie","nev","jam","pam","new","aye","ani","and","ibm","yap","can","pyx","tar","kin","fog","hum","pip","cup","dye","lyx","jog","nun","par","wan","fey","bus","oak","bad","ats","set","qom","vat","eat","pus","rev","axe","ion","six","ila","lao","mom","mas","pro","few","opt","poe","art","ash","oar","cap","lop","may","shy","rid","bat","sum","rim","fee","bmw","sky","maj","hue","thy","ava","rap","den","fla","auk","cox","ibo","hey","saw","vim","sec","ltd","you","its","tat","dew","eva","tog","ram","let","see","zit","maw","nix","ate","gig","rep","owe","ind","hog","eve","sam","zoo","any","dow","cod","bed","vet","ham","sis","hex","via","fir","nod","mao","aug","mum","hoe","bah","hal","keg","hew","zed","tow","gog","ass","dem","who","bet","gos","son","ear","spy","kit","boy","due","sen","oaf","mix","hep","fur","ada","bin","nil","mia","ewe","hit","fix","sad","rib","eye","hop","haw","wax","mid","tad","ken","wad","rye","pap","bog","gut","ito","woe","our","ado","sin","mad","ray","hon","roy","dip","hen","iva","lug","asp","hui","yak","bay","poi","yep","bun","try","lad","elm","nat","wyo","gym","dug","toe","dee","wig","sly","rip","geo","cog","pas","zen","odd","nan","lay","pod","fit","hem","joy","bum","rio","yon","dec","leg","put","sue","dim","pet","yaw","nub","bit","bur","sid","sun","oil","red","doc","moe","caw","eel","dix","cub","end","gem","off","yew","hug","pop","tub","sgt","lid","pun","ton","sol","din","yup","jab","pea","bug","gag","mil","jig","hub","low","did","tin","get","gte","sox","lei","mig","fig","lon","use","ban","flo","nov","jut","bag","mir","sty","lap","two","ins","con","ant","net","tux","ode","stu","mug","cad","nap","gun","fop","tot","sow","sal","sic","ted","wot","del","imp","cob","way","ann","tan","mci","job","wet","ism","err","him","all","pad","hah","hie","aim","ike","jed","ego","mac","baa","min","com","ill","was","cab","ago","ina","big","ilk","gal","tap","duh","ola","ran","lab","top","gob","hot","ora","tia","kip","han","met","hut","she","sac","fed","goo","tee","ell","not","act","gil","rut","ala","ape","rig","cid","god","duo","lin","aid","gel","awl","lag","elf","liz","ref","aha","fib","oho","tho","her","nor","ace","adz","fun","ned","coo","win","tao","coy","van","man","pit","guy","foe","hid","mai","sup","jay","hob","mow","jot","are","pol","arc","lax","aft","alb","len","air","pug","pox","vow","got","meg","zoe","amp","ale","bud","gee","pin","dun","pat","ten","mob"}; + String[] words = {"kid", "tag", "pup", "ail", "tun", "woo", "erg", "luz", "brr", "gay", "sip", "kay", "per", "val", "mes", "ohs", "now", "boa", "cet", "pal", "bar", "die", "war", "hay", "eco", "pub", "lob", "rue", "fry", "lit", "rex", "jan", "cot", "bid", "ali", "pay", "col", "gum", "ger", "row", "won", "dan", "rum", "fad", "tut", "sag", "yip", "sui", "ark", "has", "zip", "fez", "own", "ump", "dis", "ads", "max", "jaw", "out", "btu", "ana", "gap", "cry", "led", "abe", "box", "ore", "pig", "fie", "toy", "fat", "cal", "lie", "noh", "sew", "ono", "tam", "flu", "mgm", "ply", "awe", "pry", "tit", "tie", "yet", "too", "tax", "jim", "san", "pan", "map", "ski", "ova", "wed", "non", "wac", "nut", "why", "bye", "lye", "oct", "old", "fin", "feb", "chi", "sap", "owl", "log", "tod", "dot", "bow", "fob", "for", "joe", "ivy", "fan", "age", "fax", "hip", "jib", "mel", "hus", "sob", "ifs", "tab", "ara", "dab", "jag", "jar", "arm", "lot", "tom", "sax", "tex", "yum", "pei", "wen", "wry", "ire", "irk", "far", "mew", "wit", "doe", "gas", "rte", "ian", "pot", "ask", "wag", "hag", "amy", "nag", "ron", "soy", "gin", "don", "tug", "fay", "vic", "boo", "nam", "ave", "buy", "sop", "but", "orb", "fen", "paw", "his", "sub", "bob", "yea", "oft", "inn", "rod", "yam", "pew", "web", "hod", "hun", "gyp", "wei", "wis", "rob", "gad", "pie", "mon", "dog", "bib", "rub", "ere", "dig", "era", "cat", "fox", "bee", "mod", "day", "apr", "vie", "nev", "jam", "pam", "new", "aye", "ani", "and", "ibm", "yap", "can", "pyx", "tar", "kin", "fog", "hum", "pip", "cup", "dye", "lyx", "jog", "nun", "par", "wan", "fey", "bus", "oak", "bad", "ats", "set", "qom", "vat", "eat", "pus", "rev", "axe", "ion", "six", "ila", "lao", "mom", "mas", "pro", "few", "opt", "poe", "art", "ash", "oar", "cap", "lop", "may", "shy", "rid", "bat", "sum", "rim", "fee", "bmw", "sky", "maj", "hue", "thy", "ava", "rap", "den", "fla", "auk", "cox", "ibo", "hey", "saw", "vim", "sec", "ltd", "you", "its", "tat", "dew", "eva", "tog", "ram", "let", "see", "zit", "maw", "nix", "ate", "gig", "rep", "owe", "ind", "hog", "eve", "sam", "zoo", "any", "dow", "cod", "bed", "vet", "ham", "sis", "hex", "via", "fir", "nod", "mao", "aug", "mum", "hoe", "bah", "hal", "keg", "hew", "zed", "tow", "gog", "ass", "dem", "who", "bet", "gos", "son", "ear", "spy", "kit", "boy", "due", "sen", "oaf", "mix", "hep", "fur", "ada", "bin", "nil", "mia", "ewe", "hit", "fix", "sad", "rib", "eye", "hop", "haw", "wax", "mid", "tad", "ken", "wad", "rye", "pap", "bog", "gut", "ito", "woe", "our", "ado", "sin", "mad", "ray", "hon", "roy", "dip", "hen", "iva", "lug", "asp", "hui", "yak", "bay", "poi", "yep", "bun", "try", "lad", "elm", "nat", "wyo", "gym", "dug", "toe", "dee", "wig", "sly", "rip", "geo", "cog", "pas", "zen", "odd", "nan", "lay", "pod", "fit", "hem", "joy", "bum", "rio", "yon", "dec", "leg", "put", "sue", "dim", "pet", "yaw", "nub", "bit", "bur", "sid", "sun", "oil", "red", "doc", "moe", "caw", "eel", "dix", "cub", "end", "gem", "off", "yew", "hug", "pop", "tub", "sgt", "lid", "pun", "ton", "sol", "din", "yup", "jab", "pea", "bug", "gag", "mil", "jig", "hub", "low", "did", "tin", "get", "gte", "sox", "lei", "mig", "fig", "lon", "use", "ban", "flo", "nov", "jut", "bag", "mir", "sty", "lap", "two", "ins", "con", "ant", "net", "tux", "ode", "stu", "mug", "cad", "nap", "gun", "fop", "tot", "sow", "sal", "sic", "ted", "wot", "del", "imp", "cob", "way", "ann", "tan", "mci", "job", "wet", "ism", "err", "him", "all", "pad", "hah", "hie", "aim", "ike", "jed", "ego", "mac", "baa", "min", "com", "ill", "was", "cab", "ago", "ina", "big", "ilk", "gal", "tap", "duh", "ola", "ran", "lab", "top", "gob", "hot", "ora", "tia", "kip", "han", "met", "hut", "she", "sac", "fed", "goo", "tee", "ell", "not", "act", "gil", "rut", "ala", "ape", "rig", "cid", "god", "duo", "lin", "aid", "gel", "awl", "lag", "elf", "liz", "ref", "aha", "fib", "oho", "tho", "her", "nor", "ace", "adz", "fun", "ned", "coo", "win", "tao", "coy", "van", "man", "pit", "guy", "foe", "hid", "mai", "sup", "jay", "hob", "mow", "jot", "are", "pol", "arc", "lax", "aft", "alb", "len", "air", "pug", "pox", "vow", "got", "meg", "zoe", "amp", "ale", "bud", "gee", "pin", "dun", "pat", "ten", "mob"}; // List wordList = new ArrayList<>(); // wordList.addAll(Arrays.asList(words)); System.out.println(solution.findLadders(beginWord, endWord, Arrays.asList(words))); diff --git a/[0126][Word Ladder II]/src/Solution.java b/[0126][Word Ladder II]/src/Solution.java index bc6ce38..14edd09 100644 --- a/[0126][Word Ladder II]/src/Solution.java +++ b/[0126][Word Ladder II]/src/Solution.java @@ -1,10 +1,4 @@ -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; -import java.util.Set; +import java.util.*; /** * @author: wangjunchao(王俊超) diff --git a/[0126][Word Ladder II]/src/Solution2.java b/[0126][Word Ladder II]/src/Solution2.java index 7315d8d..206bddb 100644 --- a/[0126][Word Ladder II]/src/Solution2.java +++ b/[0126][Word Ladder II]/src/Solution2.java @@ -1,8 +1,4 @@ -import java.util.Deque; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; +import java.util.*; /** * @author: wangjunchao(王俊超) @@ -58,7 +54,7 @@ public List> findLadders(String beginWord, String endWord, List> result = new LinkedList<>(); Deque current = new LinkedList<>(); findLadders(beginWord, endWord, wordDict, current, result); - for(List r: result) { + for (List r : result) { r.add(0, beginWord); } return result; @@ -95,7 +91,7 @@ else if (result.get(0).size() > current.size()) { chars[i] = ch; String s = new String(chars); - if (wordDict.contains(s) ) { + if (wordDict.contains(s)) { wordDict.remove(s); current.add(s); diff --git a/[0126][Word Ladder II]/src/Solution3.java b/[0126][Word Ladder II]/src/Solution3.java index 906bc48..ddb5639 100644 --- a/[0126][Word Ladder II]/src/Solution3.java +++ b/[0126][Word Ladder II]/src/Solution3.java @@ -1,8 +1,4 @@ -import java.util.Deque; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; +import java.util.*; /** * @author: wangjunchao(王俊超) diff --git a/[0127][Word Ladder]/src/Solution.java b/[0127][Word Ladder]/src/Solution.java index 5366850..4cb9ab8 100644 --- a/[0127][Word Ladder]/src/Solution.java +++ b/[0127][Word Ladder]/src/Solution.java @@ -1,8 +1,4 @@ -import java.util.Deque; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; +import java.util.*; /** * Author: diff --git a/[0130][Surrounded Regions]/src/Solution.java b/[0130][Surrounded Regions]/src/Solution.java index 636f0a0..4f7cf47 100644 --- a/[0130][Surrounded Regions]/src/Solution.java +++ b/[0130][Surrounded Regions]/src/Solution.java @@ -50,7 +50,6 @@ void solve(char[][] board) { } - for (int i = 0; i < board.length; ++i) { for (int j = 0; j < board[i].length; ++j) { if (board[i][j] == 'O') { diff --git a/[0131][Palindrome Partitioning]/src/Main.java b/[0131][Palindrome Partitioning]/src/Main.java index 8266432..d295707 100644 --- a/[0131][Palindrome Partitioning]/src/Main.java +++ b/[0131][Palindrome Partitioning]/src/Main.java @@ -1,7 +1,5 @@ import org.junit.Test; -import java.util.Arrays; - /** * @author: wangjunchao(王俊超) * @time: 2019-06-21 18:36 diff --git a/[0136][Single Number]/src/Solution.java b/[0136][Single Number]/src/Solution.java index 1ec5d37..b32b8a7 100644 --- a/[0136][Single Number]/src/Solution.java +++ b/[0136][Single Number]/src/Solution.java @@ -8,13 +8,13 @@ public class Solution { /** * Given an array of integers, every element appears twice except for one. * Find that single one. - * + *

* Note: Your algorithm should have a linear runtime complexity. * Could you implement it without using extra memory? - * + *

* һ飬ÿԪض2γеһҳֻһε * ע⣺㷨ʱ临ӶȣԲʹöռʵ - * + *

* ˼·ʹ * * @param nums @@ -27,7 +27,7 @@ public int singleNumber(int[] nums) { } - for (int i = 1; i< nums.length; i++) { + for (int i = 1; i < nums.length; i++) { nums[0] ^= nums[i]; } return nums[0]; diff --git a/[0138][Copy List With Random Pointer]/src/Main.java b/[0138][Copy List With Random Pointer]/src/Main.java index 85972fb..92f5099 100644 --- a/[0138][Copy List With Random Pointer]/src/Main.java +++ b/[0138][Copy List With Random Pointer]/src/Main.java @@ -1,5 +1,5 @@ /** - * Author: + * Author: ������ * Date: 2015-06-24 * Time: 09:18 * Declaration: All Rights Reserved !!! @@ -21,7 +21,7 @@ public static void main(String[] args) { } - public static void print(RandomListNode head) { + public static void print(RandomListNode head) { while (head != null) { System.out.print(head.label + "->"); head = head.next; diff --git a/[0138][Copy List With Random Pointer]/src/Solution.java b/[0138][Copy List With Random Pointer]/src/Solution.java index 5501ab5..f392526 100644 --- a/[0138][Copy List With Random Pointer]/src/Solution.java +++ b/[0138][Copy List With Random Pointer]/src/Solution.java @@ -87,7 +87,7 @@ public RandomListNode splitList(RandomListNode head) { // ǰƵĽ RandomListNode copy; - while (node != null){ + while (node != null) { // ָƽ copy = node.next; diff --git a/[0141][Linked List Cycle]/src/ListNode.java b/[0141][Linked List Cycle]/src/ListNode.java index a9b5f6a..8892796 100644 --- a/[0141][Linked List Cycle]/src/ListNode.java +++ b/[0141][Linked List Cycle]/src/ListNode.java @@ -1,5 +1,5 @@ /** - * Author: + * Author: ������ * Date: 2015-08-21 * Time: 19:17 * Declaration: All Rights Reserved !!! @@ -7,6 +7,7 @@ public class ListNode { int val; ListNode next; + ListNode(int x) { val = x; next = null; diff --git a/[0142][Linked List Cycle II]/src/ListNode.java b/[0142][Linked List Cycle II]/src/ListNode.java index fba59f9..c9da456 100644 --- a/[0142][Linked List Cycle II]/src/ListNode.java +++ b/[0142][Linked List Cycle II]/src/ListNode.java @@ -1,5 +1,5 @@ /** - * Author: + * Author: ������ * Date: 2015-08-21 * Time: 19:19 * Declaration: All Rights Reserved !!! @@ -7,6 +7,7 @@ public class ListNode { int val; ListNode next; + ListNode(int x) { val = x; next = null; diff --git a/[0143][Reorder List]/src/ListNode.java b/[0143][Reorder List]/src/ListNode.java index 1f6cb21..dc97875 100644 --- a/[0143][Reorder List]/src/ListNode.java +++ b/[0143][Reorder List]/src/ListNode.java @@ -5,7 +5,7 @@ * Declaration: All Rights Reserved !!! */ public class ListNode { - int val; + int val; ListNode next; ListNode(int x) { diff --git a/[0143][Reorder List]/src/Solution.java b/[0143][Reorder List]/src/Solution.java index 8c489cf..c4f61a9 100644 --- a/[0143][Reorder List]/src/Solution.java +++ b/[0143][Reorder List]/src/Solution.java @@ -24,6 +24,7 @@ public class Solution { * 2㣬ͽм֮в֣ * * + * * @param head */ public void reorderList(ListNode head) { diff --git a/[0150][Evaluate Reverse Polish Notation]/src/Main.java b/[0150][Evaluate Reverse Polish Notation]/src/Main.java index f6672ab..ce7f89f 100644 --- a/[0150][Evaluate Reverse Polish Notation]/src/Main.java +++ b/[0150][Evaluate Reverse Polish Notation]/src/Main.java @@ -1,5 +1,5 @@ /** - * Author: + * Author: ������ * Date: 2015-06-20 * Time: 07:14 * Declaration: All Rights Reserved !!! @@ -7,6 +7,6 @@ public class Main { public static void main(String[] args) { Solution solution = new Solution(); - System.out.println(solution.evalRPN(new String[]{"0","3","/"})); + System.out.println(solution.evalRPN(new String[]{"0", "3", "/"})); } } diff --git a/[0150][Evaluate Reverse Polish Notation]/src/Solution.java b/[0150][Evaluate Reverse Polish Notation]/src/Solution.java index 499adc9..17993a7 100644 --- a/[0150][Evaluate Reverse Polish Notation]/src/Solution.java +++ b/[0150][Evaluate Reverse Polish Notation]/src/Solution.java @@ -1,4 +1,3 @@ -import java.util.Queue; import java.util.Stack; /** diff --git a/[0160][Intersection of Two Linked Lists]/src/Solution.java b/[0160][Intersection of Two Linked Lists]/src/Solution.java index 9872669..72f8e1a 100644 --- a/[0160][Intersection of Two Linked Lists]/src/Solution.java +++ b/[0160][Intersection of Two Linked Lists]/src/Solution.java @@ -63,15 +63,16 @@ //leetcode submit region begin(Prohibit modification and deletion) + /** * Definition for singly-linked list. * public class ListNode { - * int val; - * ListNode next; - * ListNode(int x) { - * val = x; - * next = null; - * } + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } * } */ public class Solution { @@ -96,7 +97,7 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) { tailA.next = headB; // 定义快慢指针 - ListNode fast = headA; + ListNode fast = headA; ListNode slow = headA; while (fast.next != null && fast.next.next != null) { diff --git a/[0167][Two Sum II - Input array is sorted]/src/Solution.java b/[0167][Two Sum II - Input array is sorted]/src/Solution.java index 74f81cc..fd34284 100644 --- a/[0167][Two Sum II - Input array is sorted]/src/Solution.java +++ b/[0167][Two Sum II - Input array is sorted]/src/Solution.java @@ -5,6 +5,7 @@ public class Solution { /** * 如果数组很大,target很小,可以考虑先用二分查找法找到不大于target元素的下标,再作处理 + * * @param numbers * @param target * @return diff --git a/[0173][Binary Search Tree Iterator]/src/BSTIterator.java b/[0173][Binary Search Tree Iterator]/src/BSTIterator.java index a1dddf3..8465ef7 100644 --- a/[0173][Binary Search Tree Iterator]/src/BSTIterator.java +++ b/[0173][Binary Search Tree Iterator]/src/BSTIterator.java @@ -7,8 +7,8 @@ * @time: 2019-06-28 16:54 **/ public class BSTIterator { - private TreeNode root; - private TreeNode prev = null; + private TreeNode root; + private TreeNode prev = null; // 最后一个元素表示next元素的值 private Deque deque = new LinkedList<>(); @@ -33,9 +33,9 @@ public int next() { TreeNode temp = deque.removeLast(); - if (temp.right!= null) { + if (temp.right != null) { TreeNode n = temp.right; - while (n!=null) { + while (n != null) { deque.addLast(n); n = n.left; } diff --git a/[0173][Binary Search Tree Iterator]/src/Solution.java b/[0173][Binary Search Tree Iterator]/src/Solution.java index f65bfd9..6fb2141 100644 --- a/[0173][Binary Search Tree Iterator]/src/Solution.java +++ b/[0173][Binary Search Tree Iterator]/src/Solution.java @@ -8,40 +8,40 @@ public class Solution { @Test public void test1() { - TreeNode root = new TreeNode(7); + TreeNode root = new TreeNode(7); root.left = new TreeNode(3); root.right = new TreeNode(15); root.right.left = new TreeNode(9); root.right.right = new TreeNode(20); BSTIterator iterator = new BSTIterator(root); - Assert.assertEquals(3, iterator.next() ); // return 3 - Assert.assertEquals(7, iterator.next() ); // return 7 + Assert.assertEquals(3, iterator.next()); // return 3 + Assert.assertEquals(7, iterator.next()); // return 7 Assert.assertEquals(true, iterator.hasNext()); // return true - Assert.assertEquals(9, iterator.next() ); // return 9 + Assert.assertEquals(9, iterator.next()); // return 9 Assert.assertEquals(true, iterator.hasNext()); // return true - Assert.assertEquals(15, iterator.next() ); // return 15 + Assert.assertEquals(15, iterator.next()); // return 15 Assert.assertEquals(true, iterator.hasNext()); // return true - Assert.assertEquals(20, iterator.next() ); // return 20 + Assert.assertEquals(20, iterator.next()); // return 20 Assert.assertEquals(false, iterator.hasNext()); // return false } @Test public void test2() { - TreeNode root = new TreeNode(7); + TreeNode root = new TreeNode(7); BSTIterator iterator = new BSTIterator(root); - Assert.assertEquals(7, iterator.next() ); // return 3 + Assert.assertEquals(7, iterator.next()); // return 3 Assert.assertEquals(false, iterator.hasNext()); // return false Assert.assertEquals(false, iterator.hasNext()); // return false } @Test public void test3() { - TreeNode root = new TreeNode(1); + TreeNode root = new TreeNode(1); root.right = new TreeNode(2); BSTIterator iterator = new BSTIterator(root); - Assert.assertEquals(true, iterator.hasNext() ); // return 3 - Assert.assertEquals(1, iterator.next() ); // return 3 + Assert.assertEquals(true, iterator.hasNext()); // return 3 + Assert.assertEquals(1, iterator.next()); // return 3 Assert.assertEquals(true, iterator.hasNext()); // return false Assert.assertEquals(2, iterator.next()); // return false Assert.assertEquals(false, iterator.hasNext()); // return false diff --git a/[0191][Number Of 1 Bits]/src/Solution.java b/[0191][Number Of 1 Bits]/src/Solution.java index 5b782f6..3e63a99 100644 --- a/[0191][Number Of 1 Bits]/src/Solution.java +++ b/[0191][Number Of 1 Bits]/src/Solution.java @@ -1,18 +1,18 @@ /** - * Author: + * Author: ������ * Date: 2015-06-17 * Time: 21:51 * Declaration: All Rights Reserved !!! */ public class Solution { /** - * Write a function that takes an unsigned integer and returns the number of 1' bits it has + * Write a function that takes an unsigned integer and returns the number of ��1' bits it has * (also known as the Hamming weight). - * - * For example, the 32-bit integer 11' has binary representation 00000000000000000000000000001011, + *

+ * For example, the 32-bit integer ��11' has binary representation 00000000000000000000000000001011, * so the function should return 3. - * - * еλ + *

+ * ���������е�λ�� * * @param n * @return diff --git a/[0200][Number Of Islands]/src/Solution.java b/[0200][Number Of Islands]/src/Solution.java index 47991f4..ff8698a 100644 --- a/[0200][Number Of Islands]/src/Solution.java +++ b/[0200][Number Of Islands]/src/Solution.java @@ -1,5 +1,5 @@ /** - * Author: + * Author: ������ * Date: 2015-06-20 * Time: 11:00 * Declaration: All Rights Reserved !!! @@ -25,12 +25,12 @@ public class Solution { * 00011 * Answer: 3 * - * Ŀ⣺ - * һάĵͼ'1'½أ0ˮ滷ˮ - * ڵ½ˮƽֱӶγɵġԼĸ߶ˮΧ + * ��Ŀ���⣺ + * ������һ����ά����ĵ�ͼ��'1'��½�أ���0��ˮ�����������������������������滷ˮ�� + * �������ڵ�½��ˮƽ��ֱ���Ӷ��γɵġ�����Լ��������������ĸ��߶���ˮ��Χ�� * - * ˼· - * һһӦķʱǾ󣬶ÿԪʱйȱȱͳƵĿ + * ����˼·�� + * һ��������һ����Ӧ�ķ��ʱ�Ǿ��󣬶�����ÿ��Ԫ��ʱ�й�����ȱ���������������ȱ�������ͳ�Ƶ�����Ŀ * * * @@ -38,18 +38,18 @@ public class Solution { * @return */ public int numIslands(char[][] grid) { - // У + // ����У�� if (grid == null || grid.length == 0 || grid[0].length == 0) { return 0; } - // ԪĬֵfalse + // Ԫ��Ĭ��ֵ��false boolean[][] visited = new boolean[grid.length][grid[0].length]; int result = 0; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { - // λûбʹҴλǵ¹ȱ + // �����λ��û�б����ʹ������Ҵ�λ���ǵ�������¹�����ȱ��� if (!visited[i][j] && grid[i][j] == '1') { result++; bfs(grid, visited, i, j); @@ -60,29 +60,30 @@ public int numIslands(char[][] grid) { } /** - * - * @param grid - * @param visited ʱǾ - * @param row - * @param col + * ����������� + * + * @param grid ���� + * @param visited ���ʱ�Ǿ��� + * @param row ������ + * @param col ������ */ private void bfs(char[][] grid, boolean[][] visited, int row, int col) { - if (row >= 0 && row < grid.length // кϷ - && col >= 0 && col < grid[0].length // кϷ - && !visited[row][col] // ûзʹ - && grid[row][col] == '1') { // ǵ½ + if (row >= 0 && row < grid.length // �кϷ� + && col >= 0 && col < grid[0].length // �кϷ� + && !visited[row][col] // û�з��ʹ� + && grid[row][col] == '1') { // �ǵ���½�� - // ǴλѾʹ + // ��Ǵ�λ���Ѿ����ʹ��� visited[row][col] = true; - // + // �� bfs(grid, visited, row - 1, col); - // + // �� bfs(grid, visited, row, col + 1); - // + // �� bfs(grid, visited, row + 1, col); - // + // �� bfs(grid, visited, row, col - 1); } } diff --git a/[0202][Happy Number ]/src/Solution.java b/[0202][Happy Number ]/src/Solution.java index 5df793f..46f562d 100644 --- a/[0202][Happy Number ]/src/Solution.java +++ b/[0202][Happy Number ]/src/Solution.java @@ -1,7 +1,7 @@ import java.util.HashSet; /** - * Author: + * Author: ������ * Date: 2015-06-17 * Time: 21:36 * Declaration: All Rights Reserved !!! @@ -13,9 +13,9 @@ public class Solution { * replace the number by the sum of the squares of its digits, and repeat the process until the number * equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those * numbers for which this process ends in 1 are happy numbers. - * - * ĿҪһϸλֵƽͣΪ1Ϊhappy number - * ܴijʼѭ + *

+ * ��ĿҪ�������һ�������������ϸ�����λ�����ֵ�ƽ���ͣ�����������Ϊ1���������Ϊhappy number�� + * ���������ܴ�ij������ʼ����ѭ���� * * @param n * @return @@ -26,13 +26,13 @@ public boolean isHappy(int n) { return false; } - // ڱмֵĽ + // ���ڱ����м���ֵĽ�� HashSet set = new HashSet<>(32); int tmp; int newN; - // nΪ1nֵظ֣ѭ + // n��Ϊ1������n��ֵ�����ظ����֣��������ѭ�� while (n != 1 && !set.contains(n)) { set.add(n); newN = 0; diff --git a/[0206][Reverse Linked List]/src/Solution.java b/[0206][Reverse Linked List]/src/Solution.java index 3799055..360bebd 100644 --- a/[0206][Reverse Linked List]/src/Solution.java +++ b/[0206][Reverse Linked List]/src/Solution.java @@ -32,6 +32,7 @@ public ListNode reverseList(ListNode head) { /** * TODO ʹõݹⷨ + * * @param head * @return */ diff --git a/[0207][Course Schedule]/src/Main.java b/[0207][Course Schedule]/src/Main.java index ac84c6d..a94abab 100644 --- a/[0207][Course Schedule]/src/Main.java +++ b/[0207][Course Schedule]/src/Main.java @@ -39,7 +39,7 @@ public void test4() { @Test public void test5() { Solution solution = new Solution(); - int[][] arr = {{1,0},{2,6},{1,7},{6,4},{7,0},{0,5}}; + int[][] arr = {{1, 0}, {2, 6}, {1, 7}, {6, 4}, {7, 0}, {0, 5}}; Assert.assertEquals(true, solution.canFinish(8, arr)); } } diff --git a/[0207][Course Schedule]/src/Solution.java b/[0207][Course Schedule]/src/Solution.java index a929c91..208c49e 100644 --- a/[0207][Course Schedule]/src/Solution.java +++ b/[0207][Course Schedule]/src/Solution.java @@ -1,8 +1,4 @@ -import java.util.Deque; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; +import java.util.*; /** * @author: wangjunchao(王俊超) @@ -93,7 +89,7 @@ private boolean dfs(int course, Map> dependencies, boolea path.addLast(course); - for(int i: dependency) { + for (int i : dependency) { boolean result = dfs(i, dependencies, visit, path); if (!result) { return false; diff --git a/[0215][Kth Largest Element In An Array]/src/Main.java b/[0215][Kth Largest Element In An Array]/src/Main.java index cd77742..ec1a203 100644 --- a/[0215][Kth Largest Element In An Array]/src/Main.java +++ b/[0215][Kth Largest Element In An Array]/src/Main.java @@ -1,5 +1,5 @@ /** - * Author: + * Author: ������ * Date: 2015-06-19 * Time: 20:01 * Declaration: All Rights Reserved !!! @@ -7,6 +7,6 @@ public class Main { public static void main(String[] args) { Solution solution = new Solution(); - System.out.println(solution.findKthLargest(new int[]{2 , 1}, 2)); + System.out.println(solution.findKthLargest(new int[]{2, 1}, 2)); } } diff --git a/[0215][Kth Largest Element In An Array]/src/Solution.java b/[0215][Kth Largest Element In An Array]/src/Solution.java index 8119edf..7648127 100644 --- a/[0215][Kth Largest Element In An Array]/src/Solution.java +++ b/[0215][Kth Largest Element In An Array]/src/Solution.java @@ -1,7 +1,5 @@ -import java.util.Collections; - /** - * Author: + * Author: ������ * Date: 2015-06-19 * Time: 19:36 * Declaration: All Rights Reserved !!! @@ -16,14 +14,14 @@ public class Solution { * Given [3,2,1,5,6,4] and k = 2, return 5. * * Note: - * You may assume k is always valid, 1 k array's length. + * You may assume k is always valid, 1 �� k �� array's length. * - * Ŀ⣺ - * һδҳkԪءע֮ĵk󣬶ǵkظԪ - * ԼkһЧģ 1 k 鳤 + * ��Ŀ���⣺ + * ��һ��δ��������������ҳ���k���Ԫ�ء�ע��������֮��ĵ�k�󣬶��ǵ�k�����ظ���Ԫ�� + * ���Լ���kһ������Ч�ģ� 1 �� k �� ���鳤�� * - * ˼· - * O(n)ⷨѡQuickSelect㷨 + * ����˼·�� + * O(n)�ⷨ������ѡ��QuickSelect���㷨 * * * @param nums @@ -41,19 +39,19 @@ public int findKthLargest(int[] nums, int k) { public int findKthLargest(int[] nums, int start, int end, int k) { - // ֵ + // ����ֵ int pivot = nums[start]; int lo = start; int hi = end; while (lo < hi) { - // Сֵƶ + // ��С������ֵ�����ƶ���������� while (lo < hi && nums[hi] >= pivot) { hi--; } nums[lo] = nums[hi]; - // ֵƶұ + // ����������ֵ�����ƶ��������ұ� while (lo < hi && nums[lo] <= pivot) { lo++; } @@ -62,19 +60,19 @@ public int findKthLargest(int[] nums, int start, int end, int k) { nums[lo] = pivot; - // Ѿҵ + // ����Ѿ��ҵ��� if (end - lo + 1 == k) { return pivot; } - // kloλõұ - else if (end - lo + 1 > k){ + // ��k�������loλ�õ��ұ� + else if (end - lo + 1 > k) { return findKthLargest(nums, lo + 1, end, k); } - // kloλõ + // ��k�������loλ�õ���� else { // k-(end-lo+1) - // (end-lo+1)ʾloλÿʼendλõԪظҰ벿 - // ԭĵkk-(end-lo+1) + // (end-lo+1)����ʾ��loλ�ÿ�ʼ��endλ�õ�Ԫ�ظ�������������Ұ벿�� + // ԭ���ĵ�k����k-(end-lo+1)�� return findKthLargest(nums, start, lo - 1, k - (end - lo + 1)); } } diff --git a/[0216][Combination Sum III]/src/Solution.java b/[0216][Combination Sum III]/src/Solution.java index e2c8e6f..563a5f7 100644 --- a/[0216][Combination Sum III]/src/Solution.java +++ b/[0216][Combination Sum III]/src/Solution.java @@ -1,9 +1,8 @@ -import java.util.Collections; import java.util.LinkedList; import java.util.List; /** - * Author: + * Author: ������ * Date: 2015-06-21 * Time: 21:31 * Declaration: All Rights Reserved !!! @@ -27,12 +26,12 @@ public class Solution { * Output: * [[1,2,6], [1,3,5], [2,3,4]] * - * Ŀ⣺ - * Ѱk֮͵nϣֻʹ1-9ÿһеӦΨһġ - * ȷеԵ˳С + * ��Ŀ���⣺ + * Ѱ����������k����֮�͵���n����ϣ�ֻ����ʹ������1-9������ÿһ������е�����Ӧ����Ψһ�ġ� + * ȷ������е������Ե���˳�����С� * - * ˼· - * ݷ + * ����˼·�� + * ���ݷ� * * * @param k @@ -40,57 +39,57 @@ public class Solution { * @return */ public List> combinationSum3(int k, int n) { - // ڱн + // ���ڱ������н�� List> result = new LinkedList<>(); - // ڱм + // ���ڱ����м��� List list = new LinkedList<>(); - // ͽн + // ��������ͽ��н������ if (k > 0 && k <= 9) { - solve(k, 1, n, 0, list, result); + solve(k, 1, n, 0, list, result); } - // ؽ + // ���ؽ�� return result; } /** - * ⷽ + * ��ⷽ�� * - * @param k ÿԪظ - * @param cur ǰkԪ - * @param remainder k - cur + 1Ԫصĺ - * @param prevVal cur-1Ԫصȡֵ - * @param list Ԫصļ - * @param result н + * @param k ÿ�����Ԫ�ظ��� + * @param cur ��ǰ�����k��Ԫ�� + * @param remainder k - cur + 1��Ԫ�صĺ� + * @param prevVal ��cur-1��Ԫ�ص�ȡֵ + * @param list �����Ԫ�صļ����� + * @param result �������н�������� */ public void solve(int k, int cur, int remainder, int prevVal, List list, List> result) { - // һԪ + // �������һ��Ԫ�� if (cur == k) { - // remainderΪһԪصֵǰһԪصֵҲܳ9 + // remainderΪ���һ����Ԫ�ص�ֵ�����������ǰһ����Ԫ�ص�ֵ�����Ҳ��ܳ���9 if (remainder > prevVal && remainder <= 9) { - // Ԫֵ + // ���Ԫ��ֵ list.add(remainder); - // µĶ + // ����������µĶ����� List one = new LinkedList<>(); for (Integer i : list) { one.add(i); } - // + // ������ result.add(one); - // ɾһԪأֳԭ + // ɾ�����һ��Ԫ�أ������ֳ���ԭ list.remove(list.size() - 1); } } - // һԪ - else if (cur < k){ + // �������һ��Ԫ�� + else if (cur < k) { for (int i = prevVal + 1; i <= 9 - (k - cur); i++) { - // Ԫ + // ���Ԫ�� list.add(i); - // ݹ + // �ݹ���� solve(k, cur + 1, remainder - i, i, list, result); - // ֳԭ + // �ֳ���ԭ list.remove(list.size() - 1); } diff --git a/[0217][Contains Duplicate ]/src/Solution.java b/[0217][Contains Duplicate ]/src/Solution.java index 67fdbb9..d144b96 100644 --- a/[0217][Contains Duplicate ]/src/Solution.java +++ b/[0217][Contains Duplicate ]/src/Solution.java @@ -2,7 +2,7 @@ import java.util.Set; /** - * Author: + * Author: ������ * Date: 2015-06-18 * Time: 09:40 * Declaration: All Rights Reserved !!! @@ -15,26 +15,26 @@ public class Solution { * Your function should return true if any value appears at least twice in the * array, and it should return false if every element is distinct. *

- * Ŀ⣺һ飬жǷظԪءһֳΣ - * ĺӦ÷trueÿһԪضΨһģfalse - * - * ˼·setݽṹ + * ��Ŀ���⣺����һ���������飬�ж��������Ƿ�����ظ�Ԫ�ء��������������һ�����ֳ������������Σ� + * ��ĺ���Ӧ�÷���true�����ÿһ��Ԫ�ض���Ψһ�ģ�����false�� + *

+ * ����˼·����set���ݽṹ * * @param nums * @return */ public boolean containsDuplicate(int[] nums) { - // Ԫظ1ŽIJ + // Ԫ�ظ�������1�Ž�������IJ��� if (nums != null && nums.length > 1) { - //һhashSet + //����һ��hashSet Set set = new HashSet<>(nums.length); - for(int i : nums) { - // ԪѾھͷtrue + for (int i : nums) { + // ���Ԫ���Ѿ����ھͷ���true if (set.contains(i)) { return true; } - // ûоӵԪؼ + // û�о���ӵ�Ԫ�ؼ����� else { set.add(i); } diff --git a/[0219][Contains Duplicate II]/src/Solution.java b/[0219][Contains Duplicate II]/src/Solution.java index 2fa0545..3c0f97e 100644 --- a/[0219][Contains Duplicate II]/src/Solution.java +++ b/[0219][Contains Duplicate II]/src/Solution.java @@ -2,7 +2,7 @@ import java.util.Map; /** - * Author: + * Author: ������ * Date: 2015-06-19 * Time: 14:10 * Declaration: All Rights Reserved !!! @@ -13,22 +13,22 @@ public class Solution { * Given an array of integers and an integer k, find out whether there there are * two distinct indices i and j in the array such that nums[i] = nums[j] and the * difference between i and j is at most k. - * - * Ŀ⣺ - * һnumsһkҽͬ±ijnums[i] = nums[j] - * |i-j|<=kʱtrue򷵻false - * - * ˼· - * nums[0...n-1]һmapУ(muns[i], i)nums[k]Ѿڣ - * Ƚ֮ǰ±ڵ±IJֵֵk˵ֵ - * ʹµ±Ϊֵ + *

+ * ��Ŀ���⣺ + * ����һ����������nums��һ������k�����ҽ�������������ͬ���±�i��j����nums[i] = nums[j] + * ����|i-j|<=kʱ����true�����򷵻�false�� + *

+ * ����˼·�� + * ��nums[0...n-1]������һ��map�У�(muns[i], i)�������nums[k]�Ѿ����ڣ� + * ��Ƚ�֮ǰ���±�����ڵ��±�IJ�ֵ�������ֵ������k��˵��������������������ֵ�� + * ����ʹ���µ��±���Ϊֵ * * @param nums * @param k * @return */ public boolean containsNearbyDuplicate(int[] nums, int k) { - // ж + // ���������ж� if (nums == null || nums.length < 2 || k < 1) { return false; } @@ -36,13 +36,13 @@ public boolean containsNearbyDuplicate(int[] nums, int k) { Map map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { - // ûжӦkeyӽȥ + // ���û�ж�Ӧ��key��ӽ�ȥ if (!map.containsKey(nums[i])) { map.put(nums[i], i); } - // ѾжӦkey-value + // �Ѿ��ж�Ӧ��key-value�� else { - // ԭֵӦ±꣬һСڵ± + // ԭ�������ֵ��Ӧ���±꣬��һ��С�����ڵ��±� int value = map.get(nums[i]); if (i - value <= k) { return true; diff --git a/[0223][Rectangle Area]/src/Solution.java b/[0223][Rectangle Area]/src/Solution.java index f2ea311..b8d05d5 100644 --- a/[0223][Rectangle Area]/src/Solution.java +++ b/[0223][Rectangle Area]/src/Solution.java @@ -1,7 +1,5 @@ -import java.util.*; - /** - * Author: + * Author: ������ * Date: 2015-06-19 * Time: 15:32 * Declaration: All Rights Reserved !!! @@ -28,7 +26,7 @@ public class Solution { * ***************************** * * (A,B):(-3,0)* * * * * - * * (E,F):(0-1) * + * * (E,F):(0��-1) * * ************************************* * * *************************** *************************** @@ -43,11 +41,11 @@ public class Solution { * * * - * Ŀ⣺ - * ܹǵ + * ��Ŀ���⣺ + * �������������ܹ����ǵ���� *

- * ˼· - * 㷽Ϊ1+2-ιͬĸ + * ����˼·�� + * ���㷽��Ϊ����1�����+����2�����-�������ι�ͬ�ĸ�������� * * @param A * @param B @@ -60,18 +58,18 @@ public class Solution { * @return */ public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { - long area = (long) (C - A) * (D - B) + (long)(G - E) * (H - F); - // Math.min(C, G) ʾұ߱ߵСı - // Math.max(A, E) ʾ߱ߵı - // ļԽ飬ҪݷΧ - long width = Math.max((long)Math.min(C, G) - (long)Math.max(A, E), 0); - // Math.min(D, H) ʾ߱ߵСı - // Math.max(B, F) ʾױ߱ߵı - long height = Math.max((long)Math.min(D, H) - (long)Math.max(B, F), 0); + long area = (long) (C - A) * (D - B) + (long) (G - E) * (H - F); + // Math.min(C, G) ��ʾ�ұ߱ߵ���С�ı� + // Math.max(A, E) ��ʾ��߱ߵ����ı� + // ����ļ������������Խ�飬Ҫ�������ݷ�Χ + long width = Math.max((long) Math.min(C, G) - (long) Math.max(A, E), 0); + // Math.min(D, H) ��ʾ���߱ߵ���С�ı� + // Math.max(B, F) ��ʾ�ױ߱ߵ����ı� + long height = Math.max((long) Math.min(D, H) - (long) Math.max(B, F), 0); - System.out.println(width +":" + height + "=" + width*height); + System.out.println(width + ":" + height + "=" + width * height); - return (int)(area - width * height); + return (int) (area - width * height); } diff --git a/[0232][Implement Queue using Stacks]/src/MyQueue.java b/[0232][Implement Queue using Stacks]/src/MyQueue.java index a4cdc0c..25f2f3c 100644 --- a/[0232][Implement Queue using Stacks]/src/MyQueue.java +++ b/[0232][Implement Queue using Stacks]/src/MyQueue.java @@ -1,6 +1,5 @@ import java.util.LinkedList; import java.util.List; -import java.util.Queue; /** * @author: wangjunchao(王俊超) @@ -8,7 +7,7 @@ **/ class MyQueue { - private List list; + private List list; /** * Initialize your data structure here. diff --git a/[0234][Palindrome Linked List]/src/ListNode.java b/[0234][Palindrome Linked List]/src/ListNode.java index 0304f36..4d4fcb5 100644 --- a/[0234][Palindrome Linked List]/src/ListNode.java +++ b/[0234][Palindrome Linked List]/src/ListNode.java @@ -3,7 +3,7 @@ * @time: 2018-10-09 11:03 **/ public class ListNode { - int val; + int val; ListNode next; ListNode(int x) { diff --git a/[0234][Palindrome Linked List]/src/Solution.java b/[0234][Palindrome Linked List]/src/Solution.java index 9ace1e4..65ba7ea 100644 --- a/[0234][Palindrome Linked List]/src/Solution.java +++ b/[0234][Palindrome Linked List]/src/Solution.java @@ -27,7 +27,7 @@ public boolean isPalindrome(ListNode head) { // 找反向链的起始位置 count = (count + 1) / 2; node = head; - while (count >0) { + while (count > 0) { count--; node = node.next; } diff --git a/[0234][Palindrome Linked List]/src/Test.java b/[0234][Palindrome Linked List]/src/Test.java index 97b7f35..28f5ec5 100644 --- a/[0234][Palindrome Linked List]/src/Test.java +++ b/[0234][Palindrome Linked List]/src/Test.java @@ -21,11 +21,11 @@ public static void test2() { ListNode head = new ListNode(1); head.next = new ListNode(2); - head.next.next= new ListNode(2); - head.next.next.next= new ListNode(1); - head.next.next.next.next= new ListNode(2); - head.next.next.next.next.next= new ListNode(2); - head.next.next.next.next.next.next= new ListNode(1); + head.next.next = new ListNode(2); + head.next.next.next = new ListNode(1); + head.next.next.next.next = new ListNode(2); + head.next.next.next.next.next = new ListNode(2); + head.next.next.next.next.next.next = new ListNode(1); System.out.println(solution.isPalindrome(head)); } @@ -34,8 +34,8 @@ public static void test3() { ListNode head = new ListNode(1); head.next = new ListNode(2); - head.next.next= new ListNode(2); - head.next.next.next= new ListNode(1); + head.next.next = new ListNode(2); + head.next.next.next = new ListNode(1); System.out.println(solution.isPalindrome(head)); } diff --git a/[0235][Lowest Common Ancestor of a Binary Search Tree]/src/TreeNode.java b/[0235][Lowest Common Ancestor of a Binary Search Tree]/src/TreeNode.java index cb9448f..141d430 100644 --- a/[0235][Lowest Common Ancestor of a Binary Search Tree]/src/TreeNode.java +++ b/[0235][Lowest Common Ancestor of a Binary Search Tree]/src/TreeNode.java @@ -3,7 +3,7 @@ * @time: 2018-10-09 11:33 **/ public class TreeNode { - int val; + int val; TreeNode left; TreeNode right; diff --git a/[0237][Delete Node in a Linked List]/src/ListNode.java b/[0237][Delete Node in a Linked List]/src/ListNode.java index 50f6a11..9e0f1da 100644 --- a/[0237][Delete Node in a Linked List]/src/ListNode.java +++ b/[0237][Delete Node in a Linked List]/src/ListNode.java @@ -3,7 +3,7 @@ * @time: 2018-10-10 09:58 **/ public class ListNode { - int val; + int val; ListNode next; ListNode(int x) { diff --git a/[0237][Delete Node in a Linked List]/src/Solution.java b/[0237][Delete Node in a Linked List]/src/Solution.java index b440a61..6c60753 100644 --- a/[0237][Delete Node in a Linked List]/src/Solution.java +++ b/[0237][Delete Node in a Linked List]/src/Solution.java @@ -7,6 +7,7 @@ * ListNode(int x) { val = x; } * } * + * * @author: wangjunchao(王俊超) * @time: 2018-10-10 09:57 **/ diff --git a/[0237][Delete Node in a Linked List]/src/Test.java b/[0237][Delete Node in a Linked List]/src/Test.java index 8863287..a6ee290 100644 --- a/[0237][Delete Node in a Linked List]/src/Test.java +++ b/[0237][Delete Node in a Linked List]/src/Test.java @@ -19,7 +19,7 @@ private static void print(ListNode node) { do { if (node == null) { System.out.println("null"); - } else if (node.next != null){ + } else if (node.next != null) { System.out.print(node.val + "->"); node = node.next; } else { @@ -27,6 +27,6 @@ private static void print(ListNode node) { node = node.next; } - }while (node != null); + } while (node != null); } } diff --git a/[0238][Product of Array Except Self]/src/Solution.java b/[0238][Product of Array Except Self]/src/Solution.java index 0c25aa7..e0c6b32 100644 --- a/[0238][Product of Array Except Self]/src/Solution.java +++ b/[0238][Product of Array Except Self]/src/Solution.java @@ -22,6 +22,7 @@ public class Solution { * Could you solve it with constant space complexity? (The output array does not count as * extra space for the purpose of space complexity analysis.) * + * * @param nums * @return */ @@ -33,7 +34,7 @@ public int[] productExceptSelf(int[] nums) { long product = 1; - for (int i = 0; i < nums.length; i++) { + for (int i = 0; i < nums.length; i++) { product *= nums[i]; if (nums[i] == 0) { zero.add(i); @@ -43,13 +44,13 @@ public int[] productExceptSelf(int[] nums) { // 0的个数大于1个 if (zero.size() > 1) { return result; - } else if (zero.size() == 1){ + } else if (zero.size() == 1) { int firstZeroIndex = 0; - for (Integer i: zero) { + for (Integer i : zero) { firstZeroIndex = i; } - result[firstZeroIndex] = 1; + result[firstZeroIndex] = 1; for (int i = 0; i < nums.length; i++) { if (i != firstZeroIndex) { result[firstZeroIndex] *= nums[i]; diff --git a/[0240][Search a 2D Matrix II]/src/Solution.java b/[0240][Search a 2D Matrix II]/src/Solution.java index 0119f00..4b780fe 100644 --- a/[0240][Search a 2D Matrix II]/src/Solution.java +++ b/[0240][Search a 2D Matrix II]/src/Solution.java @@ -32,7 +32,7 @@ public class Solution { */ public boolean searchMatrix(int[][] matrix, int target) { - if (matrix == null ||matrix.length == 0) { + if (matrix == null || matrix.length == 0) { return false; } diff --git a/[0241][Different Ways to Add Parentheses]/src/Solution.java b/[0241][Different Ways to Add Parentheses]/src/Solution.java index 4a5c8ad..d996d4c 100644 --- a/[0241][Different Ways to Add Parentheses]/src/Solution.java +++ b/[0241][Different Ways to Add Parentheses]/src/Solution.java @@ -30,6 +30,7 @@ public class Solution { * (2*((3-4)*5)) = -10 * (((2*3)-4)*5) = 10 * + * * @param input * @return */ diff --git a/[0278][First Bad Version]/src/VersionControl.java b/[0278][First Bad Version]/src/VersionControl.java index d124b97..327bc14 100644 --- a/[0278][First Bad Version]/src/VersionControl.java +++ b/[0278][First Bad Version]/src/VersionControl.java @@ -6,7 +6,7 @@ public class VersionControl { private boolean[] badVersion; - public boolean isBadVersion(int version){ + public boolean isBadVersion(int version) { return badVersion[version - 1]; } diff --git a/[0292][Nim Game]/src/Solution.java b/[0292][Nim Game]/src/Solution.java index 2193932..a6fa330 100644 --- a/[0292][Nim Game]/src/Solution.java +++ b/[0292][Nim Game]/src/Solution.java @@ -1,5 +1,6 @@ /** * https://leetcode.com/problems/nim-game/ + * * @author: wangjunchao(王俊超) * @time: 2019-07-08 18:39 **/ @@ -26,6 +27,7 @@ public class Solution { * 轮完后都有4的倍数个石头被取走。这样,如果我们先走的话,先把n除4的余数个石头拿走,这样不管怎样,到最 * 后都会留4个下来,对方取1个你就取3个,对方取2个你就取2个,就必赢了。 * + * * @param n * @return */ @@ -34,6 +36,6 @@ public boolean canWinNim(int n) { } public boolean canWinNim2(int n) { - return n %4 != 0; + return n % 4 != 0; } } diff --git a/[0303][Range Sum Query - Immutable]/src/NumArray.java b/[0303][Range Sum Query - Immutable]/src/NumArray.java index fb70aae..c64d89d 100644 --- a/[0303][Range Sum Query - Immutable]/src/NumArray.java +++ b/[0303][Range Sum Query - Immutable]/src/NumArray.java @@ -12,11 +12,11 @@ public NumArray(int[] nums) { public int sumRange(int i, int j) { if (i > j) { - throw new IllegalArgumentException("i = " + i + ", j = " + j +", i must not greater than j"); + throw new IllegalArgumentException("i = " + i + ", j = " + j + ", i must not greater than j"); } if (j < 0) { - throw new IllegalArgumentException("i = " + i + ", j = " + j +", i must not less than 0"); + throw new IllegalArgumentException("i = " + i + ", j = " + j + ", i must not less than 0"); } i = i < 0 ? 0 : i; diff --git a/[0326][Power of Three]/src/Solution.java b/[0326][Power of Three]/src/Solution.java index 8ab7b8d..71d743c 100644 --- a/[0326][Power of Three]/src/Solution.java +++ b/[0326][Power of Three]/src/Solution.java @@ -6,7 +6,7 @@ public class Solution { public boolean isPowerOfThree(int n) { double rst = (Math.log10(n) / Math.log10(3)); - return n > 0 &&Double.doubleToLongBits((int)rst) == Double.doubleToLongBits(rst); + return n > 0 && Double.doubleToLongBits((int) rst) == Double.doubleToLongBits(rst); } public boolean isPowerOfThree2(int n) { diff --git a/[0326][Power of Three]/src/Test.java b/[0326][Power of Three]/src/Test.java index c99e19b..9cb8806 100644 --- a/[0326][Power of Three]/src/Test.java +++ b/[0326][Power of Three]/src/Test.java @@ -4,10 +4,10 @@ **/ public class Test { public static void main(String[] args) { - System.out.println(Double.doubleToLongBits((int)1.00000000001) + System.out.println(Double.doubleToLongBits((int) 1.00000000001) == Double.doubleToLongBits(1.00000000001)); - System.out.println(Double.doubleToLongBits((int)1.00000000000) + System.out.println(Double.doubleToLongBits((int) 1.00000000000) == Double.doubleToLongBits(1.00000000000)); } } diff --git a/[0338][Counting Bits ]/src/Solution.java b/[0338][Counting Bits ]/src/Solution.java index 68effd5..901792b 100644 --- a/[0338][Counting Bits ]/src/Solution.java +++ b/[0338][Counting Bits ]/src/Solution.java @@ -31,6 +31,7 @@ public class Solution { * i为偶数 bit(i) = bit(i/2) * i为奇数 bit(i) = bit(i/2) + 1 * + * * @param num * @return */ diff --git a/[0344][Reverse String]/src/Solution.java b/[0344][Reverse String]/src/Solution.java index 955ed6b..b7bc9c5 100644 --- a/[0344][Reverse String]/src/Solution.java +++ b/[0344][Reverse String]/src/Solution.java @@ -10,7 +10,7 @@ public String reverseString(String s) { char[] chars = s.toCharArray(); - for (int i = 0, j = chars.length - 1; i + * * @param n * @return */ diff --git a/[0387][First Unique Character in a String]/src/Solution.java b/[0387][First Unique Character in a String]/src/Solution.java index 349a460..ea4eea0 100644 --- a/[0387][First Unique Character in a String]/src/Solution.java +++ b/[0387][First Unique Character in a String]/src/Solution.java @@ -20,6 +20,7 @@ public class Solution { * s = "loveleetcode", * return 2. * + * * @param s * @return */ @@ -43,7 +44,7 @@ public int firstUniqChar(String s) { Integer result = null; - for(Integer n: map.values()) { + for (Integer n : map.values()) { if (n >= 0) { if (result == null) { result = n; diff --git a/[0389][Find the Difference]/src/Solution.java b/[0389][Find the Difference]/src/Solution.java index b2fcd9a..a22ff15 100644 --- a/[0389][Find the Difference]/src/Solution.java +++ b/[0389][Find the Difference]/src/Solution.java @@ -26,6 +26,7 @@ public class Solution { * Explanation: * 'e' is the letter that was added. * + * * @param s * @param t * @return diff --git a/[0392][Is Subsequence]/src/Solution.java b/[0392][Is Subsequence]/src/Solution.java index dc4bc70..fd4ddb2 100644 --- a/[0392][Is Subsequence]/src/Solution.java +++ b/[0392][Is Subsequence]/src/Solution.java @@ -1,4 +1,3 @@ - //Given a string s and a string t, check if s is subsequence of t. // // A subsequence of a string is a new string which is formed from the original s @@ -50,11 +49,11 @@ public boolean isSubsequence(String s, String t) { return true; } - if (s == null || t == null ) { + if (s == null || t == null) { return false; } - if (s.length() == 0){ + if (s.length() == 0) { return true; } diff --git a/[0400][Nth Digit]/src/Solution2.java b/[0400][Nth Digit]/src/Solution2.java index b4fc5cb..4a5b2c9 100644 --- a/[0400][Nth Digit]/src/Solution2.java +++ b/[0400][Nth Digit]/src/Solution2.java @@ -1,5 +1,6 @@ /** * https://leetcode.com/problems/nth-digit/ + * * @author: wangjunchao(王俊超) * @time: 2019-07-09 15:56 **/ @@ -31,6 +32,7 @@ public class Solution2 { * * https://www.cnblogs.com/grandyang/p/5891871.html * + * * @param n * @return */ diff --git a/[0401][Binary Watch]/src/Main.java b/[0401][Binary Watch]/src/Main.java index 7369853..efea389 100644 --- a/[0401][Binary Watch]/src/Main.java +++ b/[0401][Binary Watch]/src/Main.java @@ -1,4 +1,3 @@ -import org.junit.Assert; import org.junit.Test; /** diff --git a/[0404][Sum of Left Leaves]/src/Main.java b/[0404][Sum of Left Leaves]/src/Main.java index b80e566..cf253ab 100644 --- a/[0404][Sum of Left Leaves]/src/Main.java +++ b/[0404][Sum of Left Leaves]/src/Main.java @@ -9,7 +9,7 @@ public class Main { @Test public void test1() { TreeNode root = new TreeNode(3); - root.left =new TreeNode(9); + root.left = new TreeNode(9); root.right = new TreeNode(20); root.right.left = new TreeNode(15); root.right.right = new TreeNode(7); diff --git a/[0404][Sum of Left Leaves]/src/Solution.java b/[0404][Sum of Left Leaves]/src/Solution.java index fb3f2d6..d5a8ad8 100644 --- a/[0404][Sum of Left Leaves]/src/Solution.java +++ b/[0404][Sum of Left Leaves]/src/Solution.java @@ -1,4 +1,3 @@ - /** * @author: wangjunchao(王俊超) * @time: 2019-06-30 14:11 @@ -30,6 +29,7 @@ public int sumOfLeftLeaves(TreeNode root) { /** * 计算所有左叶子的和 + * * @param root * @param result */ @@ -39,7 +39,7 @@ private void sumOfLeftLeaves(TreeNode root, boolean isLeftLeaf, int[] result) { return; } - if (root.left == null && root.right==null && isLeftLeaf) { + if (root.left == null && root.right == null && isLeftLeaf) { result[0] += root.val; return; } diff --git a/[0405][Convert a Number to Hexadecimal]/src/Main.java b/[0405][Convert a Number to Hexadecimal]/src/Main.java index 343277e..0e56b0e 100644 --- a/[0405][Convert a Number to Hexadecimal]/src/Main.java +++ b/[0405][Convert a Number to Hexadecimal]/src/Main.java @@ -7,19 +7,19 @@ **/ public class Main { @Test - public void test1(){ + public void test1() { Solution solution = new Solution(); Assert.assertEquals("1a", solution.toHex(26)); } @Test - public void test2(){ + public void test2() { Solution solution = new Solution(); Assert.assertEquals("ffffffff", solution.toHex(-1)); } @Test - public void test3(){ + public void test3() { Solution solution = new Solution(); Assert.assertEquals("10", solution.toHex(16)); } diff --git a/[0409][Longest Palindrome]/src/Solution2.java b/[0409][Longest Palindrome]/src/Solution2.java index 4546ba5..ad898f8 100644 --- a/[0409][Longest Palindrome]/src/Solution2.java +++ b/[0409][Longest Palindrome]/src/Solution2.java @@ -1,5 +1,6 @@ /** * https://leetcode.com/problems/longest-palindrome/ + * * @author: wangjunchao(王俊超) * @time: 2019-07-03 04:08 **/ diff --git a/[0412][Fizz Buzz]/src/Solution.java b/[0412][Fizz Buzz]/src/Solution.java index 9689a9c..12a2ebe 100644 --- a/[0412][Fizz Buzz]/src/Solution.java +++ b/[0412][Fizz Buzz]/src/Solution.java @@ -39,6 +39,7 @@ public class Solution { * "FizzBuzz" * ] * + * * @param n * @return */ diff --git a/[0415][Add Strings]/src/Solution.java b/[0415][Add Strings]/src/Solution.java index 9ce5f26..6c1cdab 100644 --- a/[0415][Add Strings]/src/Solution.java +++ b/[0415][Add Strings]/src/Solution.java @@ -1,67 +1,67 @@ +/** + * https://leetcode.com/problems/add-strings/ + * + * @author: wangjunchao(王俊超) + * @time: 2019-07-03 04:26 + **/ +public class Solution { /** - * https://leetcode.com/problems/add-strings/ + *

+     * Given two non-negative integers num1 and num2 represented as string, return the sum of
+     * num1 and num2.
      *
-     * @author: wangjunchao(王俊超)
-     * @time: 2019-07-03 04:26
-     **/
-    public class Solution {
-        /**
-         * 
-         * Given two non-negative integers num1 and num2 represented as string, return the sum of
-         * num1 and num2.
-         *
-         * Note:
-         *
-         * The length of both num1 and num2 is < 5100.
-         * Both num1 and num2 contains only digits 0-9.
-         * Both num1 and num2 does not contain any leading zero.
-         * You must not use any built-in BigInteger library or convert the inputs to integer directly.
-         * 
- * - * @param num1 - * @param num2 - * @return - */ - public String addStrings(String num1, String num2) { - if (num1.length() > num2.length()) { - // 保证第两个字符串不短于第一个 - return addStrings(num2, num1); - } + * Note: + * + * The length of both num1 and num2 is < 5100. + * Both num1 and num2 contains only digits 0-9. + * Both num1 and num2 does not contain any leading zero. + * You must not use any built-in BigInteger library or convert the inputs to integer directly. + *
+ * + * @param num1 + * @param num2 + * @return + */ + public String addStrings(String num1, String num2) { + if (num1.length() > num2.length()) { + // 保证第两个字符串不短于第一个 + return addStrings(num2, num1); + } - int[] result = new int[num2.length()]; + int[] result = new int[num2.length()]; - int idx1 = num1.length() - 1; - int idx2 = num2.length() - 1; - int idx3 = result.length - 1; - int carry = 0; - while (idx1 >= 0) { - int a = num1.charAt(idx1) - '0'; - int b = num2.charAt(idx2) - '0'; - result[idx3] = (a + b + carry) % 10; - carry = (a + b + carry) / 10; - idx1--; - idx2--; - idx3--; - } + int idx1 = num1.length() - 1; + int idx2 = num2.length() - 1; + int idx3 = result.length - 1; + int carry = 0; + while (idx1 >= 0) { + int a = num1.charAt(idx1) - '0'; + int b = num2.charAt(idx2) - '0'; + result[idx3] = (a + b + carry) % 10; + carry = (a + b + carry) / 10; + idx1--; + idx2--; + idx3--; + } - // num2还未处理完 - while (idx2 >= 0) { - int a = num2.charAt(idx2) - '0'; - result[idx3] = (a + carry) % 10; - carry = (a + carry) / 10; - idx2--; - idx3--; - } + // num2还未处理完 + while (idx2 >= 0) { + int a = num2.charAt(idx2) - '0'; + result[idx3] = (a + carry) % 10; + carry = (a + carry) / 10; + idx2--; + idx3--; + } - StringBuilder builder = new StringBuilder(); - if (carry > 0) { - builder.append(carry); - } + StringBuilder builder = new StringBuilder(); + if (carry > 0) { + builder.append(carry); + } - for(int i : result) { - builder.append(i); - } - return builder.toString(); + for (int i : result) { + builder.append(i); } + return builder.toString(); } +} diff --git a/[0419][Battleships in a Board]/src/Main.java b/[0419][Battleships in a Board]/src/Main.java index 2ef0da0..1b46aca 100644 --- a/[0419][Battleships in a Board]/src/Main.java +++ b/[0419][Battleships in a Board]/src/Main.java @@ -9,9 +9,9 @@ public class Main { @Test public void test1() { char[][] board = { - {'X','.', ',', 'X'}, - {'.','.', ',', 'X'}, - {'.','.', ',', 'X'} + {'X', '.', ',', 'X'}, + {'.', '.', ',', 'X'}, + {'.', '.', ',', 'X'} }; Solution solution = new Solution(); diff --git a/[0419][Battleships in a Board]/src/Solution.java b/[0419][Battleships in a Board]/src/Solution.java index 65b0a27..0452809 100644 --- a/[0419][Battleships in a Board]/src/Solution.java +++ b/[0419][Battleships in a Board]/src/Solution.java @@ -16,11 +16,11 @@ public int countBattleships(char[][] board) { if (board[i][j] == 'X') { // 如果左边一个并且上面一个不是X,说明是战舰的起点 // 考虑边界情况 - if (i > 0 && j > 0 && board[i -1][j] != board[i][j] && board[i][j - 1] != board[i][j]) { + if (i > 0 && j > 0 && board[i - 1][j] != board[i][j] && board[i][j - 1] != board[i][j]) { result++; - } else if (i > 0 && j == 0 && board[i -1][j] != board[i][j]) { + } else if (i > 0 && j == 0 && board[i - 1][j] != board[i][j]) { result++; - } else if (i == 0 && j > 0 && board[i][j - 1] != board[i][j] ) { + } else if (i == 0 && j > 0 && board[i][j - 1] != board[i][j]) { result++; } else if (i == 0 && j == 0) { result++; diff --git a/[0427][Construct Quad Tree]/src/Node.java b/[0427][Construct Quad Tree]/src/Node.java index 3123e2a..6dbf8a7 100644 --- a/[0427][Construct Quad Tree]/src/Node.java +++ b/[0427][Construct Quad Tree]/src/Node.java @@ -5,10 +5,10 @@ public class Node { public boolean val; public boolean isLeaf; - public Node topLeft; - public Node topRight; - public Node bottomLeft; - public Node bottomRight; + public Node topLeft; + public Node topRight; + public Node bottomLeft; + public Node bottomRight; public Node() { } diff --git a/[0427][Construct Quad Tree]/src/Solution.java b/[0427][Construct Quad Tree]/src/Solution.java index a46f286..7489596 100644 --- a/[0427][Construct Quad Tree]/src/Solution.java +++ b/[0427][Construct Quad Tree]/src/Solution.java @@ -38,7 +38,7 @@ private void construct(Node node, int[][] grid, int x, int y, int size) { construct(topLeft, grid, x, y, half); construct(topRight, grid, x, y + half, half); construct(bottomLeft, grid, x + half, y, half); - construct(bottomRight, grid, x+ half, y + half, half); + construct(bottomRight, grid, x + half, y + half, half); // 四个孩子都是叶子节点,并且值都相等 if (topLeft.isLeaf && topRight.isLeaf && bottomLeft.isLeaf && bottomRight.isLeaf) { diff --git a/[0429][N-ary Tree Level Order Traversal]/src/Main.java b/[0429][N-ary Tree Level Order Traversal]/src/Main.java index aa6fc47..45d4d16 100644 --- a/[0429][N-ary Tree Level Order Traversal]/src/Main.java +++ b/[0429][N-ary Tree Level Order Traversal]/src/Main.java @@ -1,4 +1,3 @@ -import com.sun.tools.corba.se.idl.constExpr.Not; import org.junit.Test; import java.util.LinkedList; diff --git a/[0429][N-ary Tree Level Order Traversal]/src/Node.java b/[0429][N-ary Tree Level Order Traversal]/src/Node.java index 2eee60a..4a21647 100644 --- a/[0429][N-ary Tree Level Order Traversal]/src/Node.java +++ b/[0429][N-ary Tree Level Order Traversal]/src/Node.java @@ -5,7 +5,7 @@ * @time: 2019-07-01 14:44 **/ public class Node { - public int val; + public int val; public List children; public Node() { diff --git a/[0434][Number of Segments in a String]/src/Solution.java b/[0434][Number of Segments in a String]/src/Solution.java index 1bcf1ee..8fd15cf 100644 --- a/[0434][Number of Segments in a String]/src/Solution.java +++ b/[0434][Number of Segments in a String]/src/Solution.java @@ -15,6 +15,7 @@ public class Solution { * Input: "Hello, my name is John" * Output: 5 * + * * @param s * @return */ diff --git a/[0437][Path Sum III]/src/Main.java b/[0437][Path Sum III]/src/Main.java index bde432a..30b8b7d 100644 --- a/[0437][Path Sum III]/src/Main.java +++ b/[0437][Path Sum III]/src/Main.java @@ -13,8 +13,8 @@ public void test1() { root.right = new TreeNode(-3); root.left.left = new TreeNode(3); root.left.right = new TreeNode(2); - root.left.left.left =new TreeNode(3); - root.left.left.right =new TreeNode(-2); + root.left.left.left = new TreeNode(3); + root.left.left.right = new TreeNode(-2); root.left.right.right = new TreeNode(1); root.right.right = new TreeNode(11); @@ -29,8 +29,8 @@ public void test2() { root.right = new TreeNode(-3); root.left.left = new TreeNode(3); root.left.right = new TreeNode(2); - root.left.left.left =new TreeNode(3); - root.left.left.right =new TreeNode(-2); + root.left.left.left = new TreeNode(3); + root.left.left.right = new TreeNode(-2); root.left.right.right = new TreeNode(1); root.right.right = new TreeNode(11); @@ -45,8 +45,8 @@ public void test3() { root.right = new TreeNode(-3); root.left.left = new TreeNode(3); root.left.right = new TreeNode(2); - root.left.left.left =new TreeNode(3); - root.left.left.right =new TreeNode(-2); + root.left.left.left = new TreeNode(3); + root.left.left.right = new TreeNode(-2); root.left.right.right = new TreeNode(1); root.right.right = new TreeNode(11); @@ -61,8 +61,8 @@ public void test4() { root.right = new TreeNode(-3); root.left.left = new TreeNode(3); root.left.right = new TreeNode(2); - root.left.left.left =new TreeNode(3); - root.left.left.right =new TreeNode(-2); + root.left.left.left = new TreeNode(3); + root.left.left.right = new TreeNode(-2); root.left.right.right = new TreeNode(1); root.right.right = new TreeNode(11); diff --git a/[0437][Path Sum III]/src/TreeNode.java b/[0437][Path Sum III]/src/TreeNode.java index a09e411..23d0f71 100644 --- a/[0437][Path Sum III]/src/TreeNode.java +++ b/[0437][Path Sum III]/src/TreeNode.java @@ -4,7 +4,7 @@ **/ public class TreeNode { - int val; + int val; TreeNode left; TreeNode right; diff --git a/[0438][Find All Anagrams in a String]/src/Solution.java b/[0438][Find All Anagrams in a String]/src/Solution.java index 766fa5c..8e32c89 100644 --- a/[0438][Find All Anagrams in a String]/src/Solution.java +++ b/[0438][Find All Anagrams in a String]/src/Solution.java @@ -1,9 +1,6 @@ import java.util.Arrays; -import java.util.HashMap; import java.util.LinkedList; import java.util.List; -import java.util.Map; -import java.util.Objects; /** * @author: wangjunchao(王俊超) @@ -71,7 +68,7 @@ public List findAnagrams(String s, String p) { sArr[s.charAt(i + p.length() - 1) - 'a']++; } - if (Arrays.equals(sArr, pArr) ) { + if (Arrays.equals(sArr, pArr)) { result.add(i); } } diff --git a/[0438][Find All Anagrams in a String]/src/Solution2.java b/[0438][Find All Anagrams in a String]/src/Solution2.java index f72d6e4..1f805b7 100644 --- a/[0438][Find All Anagrams in a String]/src/Solution2.java +++ b/[0438][Find All Anagrams in a String]/src/Solution2.java @@ -1,8 +1,4 @@ -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; /** * @author: wangjunchao(王俊超) diff --git a/[0441][Arranging Coins]/src/Solution.java b/[0441][Arranging Coins]/src/Solution.java index 13f4edd..f1c8291 100644 --- a/[0441][Arranging Coins]/src/Solution.java +++ b/[0441][Arranging Coins]/src/Solution.java @@ -1,4 +1,3 @@ - //You have a total of n coins that you want to form in a staircase shape, where //every k-th row must have exactly k coins. // @@ -65,8 +64,8 @@ public int arrangeCoins(int n) { long x = (long) Math.sqrt(t); - while ( true ) { - if (x * x + 3*x < t) { + while (true) { + if (x * x + 3 * x < t) { x++; continue; } diff --git a/[0441][Arranging Coins]/src/SolutionTest.java b/[0441][Arranging Coins]/src/SolutionTest.java index 86237cf..378df37 100644 --- a/[0441][Arranging Coins]/src/SolutionTest.java +++ b/[0441][Arranging Coins]/src/SolutionTest.java @@ -28,7 +28,7 @@ public void test1() { {15, 5}, }; - for(int[] d: data) { + for (int[] d : data) { Assert.assertEquals(s.arrangeCoins(d[0]), d[1]); } } diff --git a/[0442][Find All Duplicates in an Array]/src/Solution.java b/[0442][Find All Duplicates in an Array]/src/Solution.java index 5582095..15e0aea 100644 --- a/[0442][Find All Duplicates in an Array]/src/Solution.java +++ b/[0442][Find All Duplicates in an Array]/src/Solution.java @@ -1,9 +1,4 @@ -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; +import java.util.*; /** * @author: wangjunchao(王俊超) @@ -19,7 +14,7 @@ public List findDuplicates(int[] nums) { Set set = new HashSet<>(); - for(int i: nums) { + for (int i : nums) { if (set.contains(i)) { result.add(i); } else { diff --git a/[0442][Find All Duplicates in an Array]/src/Solution2.java b/[0442][Find All Duplicates in an Array]/src/Solution2.java index 5616c28..3a26109 100644 --- a/[0442][Find All Duplicates in an Array]/src/Solution2.java +++ b/[0442][Find All Duplicates in an Array]/src/Solution2.java @@ -31,7 +31,7 @@ public List findDuplicates(int[] nums) { result.add(nums[i - 1]); } } - + Collections.sort(result); return result; } diff --git a/[0443][String Compression]/src/SolutionTest.java b/[0443][String Compression]/src/SolutionTest.java index 86170a5..278614c 100644 --- a/[0443][String Compression]/src/SolutionTest.java +++ b/[0443][String Compression]/src/SolutionTest.java @@ -21,7 +21,7 @@ public void test1() { // {"abcdefg","abcdefg", 7}, // {"aaab","a3b", 3}, // {"aaabcccc","a3bc4", 5}, - {"aaabbbccc","a3b3c3", 6}, + {"aaabbbccc", "a3b3c3", 6}, }; for (Object[] d : data) { diff --git a/[0447][Number of Boomerangs]/[0447][Number of Boomerangs].iml b/[0447][Number of Boomerangs]/[0447][Number of Boomerangs].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0447][Number of Boomerangs]/[0447][Number of Boomerangs].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0447][Number of Boomerangs]/src/Solution.java b/[0447][Number of Boomerangs]/src/Solution.java new file mode 100644 index 0000000..102a1df --- /dev/null +++ b/[0447][Number of Boomerangs]/src/Solution.java @@ -0,0 +1,69 @@ +import java.util.HashMap; +import java.util.Map; + +/** + * Author: 王俊超 + * Time: 2020-06-26 16:47 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int numberOfBoomerangs(int[][] points) { + int count = 0; + if (points == null || points.length < 2) { + return count; + } + Map map = new HashMap<>(); + + for (int i = 0; i < points.length; i++) { + for (int j = 0; j < points.length; j++) { + + if (i == j) { + continue; + } + int square = square(points[i], points[j]); + if (!map.containsKey(square)) { + map.put(square, 1); + } else { + count += map.get(square) * 2; // A + int oldCount = map.get(square); + map.put(square, oldCount + 1); + } + } + + // 如果不在A处理,那么对于大于1的value,取组合值 + // C(2, n) * 2 = 1*2*...*(n-2)*2 + // 放在A处理进行了分散处理 + + map.clear(); + } + + return count; + + } + + public int numberOfBoomerangsSlow(int[][] points) { + + int answer = 0; + for (int i = 0; i < points.length; i++) { + for (int j = i + 1; j < points.length; j++) { + for (int k = 0; k < points.length; k++) { + if (k != i && k != j && square(points[k], points[i]) == square(points[k], points[j])) { + answer += 2; + } + } + } + } + + return answer; + } + + public int square(int[] a, int[] b) { + int x = a[0] - b[0]; + int y = a[1] - b[1]; + + return x * x + y * y; + } + +} diff --git a/[0447][Number of Boomerangs]/src/SolutionTest.java b/[0447][Number of Boomerangs]/src/SolutionTest.java new file mode 100644 index 0000000..b961285 --- /dev/null +++ b/[0447][Number of Boomerangs]/src/SolutionTest.java @@ -0,0 +1,26 @@ +import org.junit.Assert; +import org.junit.Test; + +/** + * Author: 王俊超 + * Time: 2020-06-26 16:55 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + @Test + public void test1() { + Solution s = new Solution(); + Object[][] data = { +// {new int[][]{{0, 0}, {1, 0}, {2, 0}}, 2}, +// {new int[][]{{1, 1}, {2, 2}, {3, 3}}, 2}, + {new int[][]{{0, 0}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}}, 20} + }; + + for (Object[] d : data) { + Assert.assertEquals(d[1], s.numberOfBoomerangs((int[][]) d[0])); + } + + } +} diff --git a/[0453][Minimum Moves to Equal Array Elements]/[0453][Minimum Moves to Equal Array Elements].iml b/[0453][Minimum Moves to Equal Array Elements]/[0453][Minimum Moves to Equal Array Elements].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0453][Minimum Moves to Equal Array Elements]/[0453][Minimum Moves to Equal Array Elements].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java b/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java new file mode 100644 index 0000000..395fde3 --- /dev/null +++ b/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java @@ -0,0 +1,40 @@ +/** + * Author: 王俊超 + * Time: 2020-06-28 21:38 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 解题思路: + * 假设数组最小的值是xmin,一共加了k次,加完后的值是W,元素个数是n + * 则:W <= xmin + k + * k(n-1) + sum(nums) = W*n + * ==> + * k(n-1) + sum(nums) <= (xmin + k)n + * ==> + * sum(nums) - n*xmin <= k + * + * @param nums + * @return + */ + public int minMoves(int[] nums) { + + if (nums == null) { + return 0; + } + + int sum = 0; + int min = Integer.MAX_VALUE; + + for (int i : nums) { + sum += i; + if (i < min) { + min = i; + } + } + + return sum - min * nums.length; + } +} diff --git a/[0453][Minimum Moves to Equal Array Elements]/src/SolutionTest.java b/[0453][Minimum Moves to Equal Array Elements]/src/SolutionTest.java new file mode 100644 index 0000000..1b8b239 --- /dev/null +++ b/[0453][Minimum Moves to Equal Array Elements]/src/SolutionTest.java @@ -0,0 +1,27 @@ +import org.junit.Assert; +import org.junit.Test; + +import java.lang.annotation.Target; + +/** + * Author: 王俊超 + * Time: 2020-06-28 22:09 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + @Test + public void test1() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{1, 2, 3}, 3}, + {new int[]{1, 1, 1}, 0}, + {new int[]{1, 1, 2}, 1}, + }; + + for (Object[] d : data) { + Assert.assertEquals(s.minMoves((int[]) d[0]), d[1]); + } + } +} diff --git a/[0455][Assign Cookies]/src/Solution.java b/[0455][Assign Cookies]/src/Solution.java index 9d3bcaf..58f164e 100644 --- a/[0455][Assign Cookies]/src/Solution.java +++ b/[0455][Assign Cookies]/src/Solution.java @@ -2,6 +2,7 @@ /** * https://leetcode.com/problems/assign-cookies/ + * * @author: wangjunchao(王俊超) * @time: 2019-07-10 08:30 **/ @@ -41,6 +42,7 @@ public class Solution { * 思路: * 先将饼干尺寸和小孩需求都排个序,然后从小到大去遍历地给。 * + * * @param g * @param s * @return diff --git a/[0461][Hamming Distance]/src/Solution.java b/[0461][Hamming Distance]/src/Solution.java index 78490f0..7204175 100644 --- a/[0461][Hamming Distance]/src/Solution.java +++ b/[0461][Hamming Distance]/src/Solution.java @@ -26,6 +26,7 @@ public class Solution { * * The above arrows point to positions where the corresponding bits are different. * + * * @param x * @param y * @return diff --git a/[0463][Island Perimeter]/src/Solution2.java b/[0463][Island Perimeter]/src/Solution2.java index 498b571..bde8363 100644 --- a/[0463][Island Perimeter]/src/Solution2.java +++ b/[0463][Island Perimeter]/src/Solution2.java @@ -29,6 +29,7 @@ public class Solution2 { * * Explanation: The perimeter is the 16 yellow stripes in the image below: * + * * @param grid * @return */ diff --git a/[0475][Heaters]/src/Solution.java b/[0475][Heaters]/src/Solution.java index 39b8c0b..e353380 100644 --- a/[0475][Heaters]/src/Solution.java +++ b/[0475][Heaters]/src/Solution.java @@ -2,6 +2,7 @@ /** * https://leetcode.com/problems/heaters/ + * * @author: wangjunchao(王俊超) * @time: 2019-07-10 08:58 **/ diff --git a/[0476][Number Complement]/src/Solution.java b/[0476][Number Complement]/src/Solution.java index c509d85..a992057 100644 --- a/[0476][Number Complement]/src/Solution.java +++ b/[0476][Number Complement]/src/Solution.java @@ -22,6 +22,7 @@ public class Solution { * Explanation: The binary representation of 1 is 1 (no leading zero bits), and its * complement is 0. So you need to output 0. * + * * @param num * @return */ diff --git a/[0482][License Key Formatting]/src/Solution.java b/[0482][License Key Formatting]/src/Solution.java index 73ec646..d9e9120 100644 --- a/[0482][License Key Formatting]/src/Solution.java +++ b/[0482][License Key Formatting]/src/Solution.java @@ -38,6 +38,7 @@ public class Solution { * 思路: * 从后向前处理,每k个分成一个组 * + * * @param s * @param k * @return @@ -74,6 +75,6 @@ public String licenseKeyFormatting(String s, int k) { return ""; } - return result.charAt(0) == '-' ? result.substring(1) : result; + return result.charAt(0) == '-' ? result.substring(1) : result; } } diff --git a/[0492][Construct the Rectangle]/src/Solution.java b/[0492][Construct the Rectangle]/src/Solution.java index d929935..834f7ce 100644 --- a/[0492][Construct the Rectangle]/src/Solution.java +++ b/[0492][Construct the Rectangle]/src/Solution.java @@ -28,6 +28,7 @@ public class Solution { * The given area won't exceed 10,000,000 and is a positive integer * The web page's width and length you designed must be positive integers. * + * * @param area * @return */ diff --git a/[0500][Keyboard Row]/src/Solution.java b/[0500][Keyboard Row]/src/Solution.java index 39dbb1b..d0cf17b 100644 --- a/[0500][Keyboard Row]/src/Solution.java +++ b/[0500][Keyboard Row]/src/Solution.java @@ -1,9 +1,9 @@ - import java.util.LinkedList; import java.util.List; /** * https://leetcode.com/problems/keyboard-row/ + * * @author: wangjunchao(王俊超) * @time: 2019-07-11 10:25 **/ diff --git a/[0501][Find Mode in Binary Search Tree]/src/Solution.java b/[0501][Find Mode in Binary Search Tree]/src/Solution.java index af9abc6..5b104f5 100644 --- a/[0501][Find Mode in Binary Search Tree]/src/Solution.java +++ b/[0501][Find Mode in Binary Search Tree]/src/Solution.java @@ -1,8 +1,4 @@ -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.SortedMap; -import java.util.TreeMap; +import java.util.*; /** * @author: wangjunchao(王俊超) @@ -15,14 +11,14 @@ public int[] findMode(TreeNode root) { findMode(root, map); int max = 0; - for (int v: map.values()) { + for (int v : map.values()) { if (max < v) { max = v; } } List list = new LinkedList<>(); - for(Map.Entry e: map.entrySet()) { + for (Map.Entry e : map.entrySet()) { if (e.getValue() == max) { list.add(e.getKey()); } @@ -30,7 +26,7 @@ public int[] findMode(TreeNode root) { int[] result = new int[list.size()]; int i = 0; - for (int v: list) { + for (int v : list) { result[i] = v; i++; } diff --git a/[0501][Find Mode in Binary Search Tree]/src/TreeNode.java b/[0501][Find Mode in Binary Search Tree]/src/TreeNode.java index 8e2131e..8b88e24 100644 --- a/[0501][Find Mode in Binary Search Tree]/src/TreeNode.java +++ b/[0501][Find Mode in Binary Search Tree]/src/TreeNode.java @@ -1,4 +1,3 @@ - /** * Author: 王俊超 * Date: 2015-08-21 diff --git a/[0504][Base 7]/src/Solution.java b/[0504][Base 7]/src/Solution.java index 8eba5e2..62947d6 100644 --- a/[0504][Base 7]/src/Solution.java +++ b/[0504][Base 7]/src/Solution.java @@ -15,6 +15,7 @@ public class Solution { * Output: "-10" * Note: The input will be in range of [-1e7, 1e7]. * + * * @param num * @return */ diff --git a/[0506][Relative Ranks]/[0506][Relative Ranks].iml b/[0506][Relative Ranks]/[0506][Relative Ranks].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0506][Relative Ranks]/[0506][Relative Ranks].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0506][Relative Ranks]/src/Solution.java b/[0506][Relative Ranks]/src/Solution.java new file mode 100644 index 0000000..3cc25f6 --- /dev/null +++ b/[0506][Relative Ranks]/src/Solution.java @@ -0,0 +1,139 @@ +import java.util.*; + +/** + * Author: 王俊超 + * Time: 2020-06-29 07:34 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + + class Rank { + Rank(int s, int i) { + this.Score = s; + this.Index = i; + } + int Score; + int Index; + } + + /** + * 题解思路: + * 1、记录分数对应的下标; + * 2、对分数进行排序,求行排名 + * 3、再根据分数将原始数组的下标和排名对应起来 + * + * @param nums + * @return + */ + public String[] findRelativeRanks(int[] nums) { + if (nums == null) { + return null; + } + + Rank[] ranks = new Rank[nums.length]; + for (int i = 0; i < nums.length; i++) { + ranks[i] = new Rank(nums[i], i); + } + + Arrays.sort(ranks, new Comparator() { + @Override + public int compare(Rank o1, Rank o2) { + return -(o1.Score - o2.Score); + } + }); + + String[] result = new String[nums.length]; + for (int i = 0; i < ranks.length; i++) { + switch (i) { + case 0: + result[ranks[i].Index] = "Gold Medal"; + break; + case 1: + result[ranks[i].Index] = "Silver Medal"; + break; + + case 2: + result[ranks[i].Index] = "Bronze Medal"; + break; + default: + result[ranks[i].Index] = "" + (i + 1); + } + } + return result; + } + + public String[] findRelativeRanks3(int[] nums) { + if (nums == null) { + return null; + } + + Map map = new HashMap<>(); + Queue queue = new PriorityQueue<>(nums.length + 1); // 使用优先堆,加1是为了防止参数为0 + + for (int i = 0; i < nums.length; i++) { + map.put(nums[i], i); + queue.add(nums[i]); // 创建小顶堆 + } + + + String[] result = new String[nums.length]; + int i = nums.length + 1; + while (!queue.isEmpty()) { + int score = queue.poll(); + i--; + switch (i) { + case 1: + result[map.get(score)] = "Gold Medal"; + break; + case 2: + result[map.get(score)] = "Silver Medal"; + break; + case 3: + result[map.get(score)] = "Bronze Medal"; + break; + default: + result[map.get(score)] = "" + i; + } + + } + + return result; + } + + public String[] findRelativeRanks2(int[] nums) { + if (nums == null) { + return null; + } + + Map map = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + map.put(nums[i], i); + } + + String[] result = new String[nums.length]; + Arrays.sort(nums); + + for (int i = 0; i < nums.length; i++) { + int score = nums[nums.length - 1 - i]; + switch (i) { + case 0: + result[map.get(score)] = "Gold Medal"; + break; + case 1: + result[map.get(score)] = "Silver Medal"; + break; + + case 2: + result[map.get(score)] = "Bronze Medal"; + break; + default: + result[map.get(score)] = "" + (i + 1); + } + } + + return result; + } +} diff --git a/[0506][Relative Ranks]/src/SolutionTest.java b/[0506][Relative Ranks]/src/SolutionTest.java new file mode 100644 index 0000000..6af869f --- /dev/null +++ b/[0506][Relative Ranks]/src/SolutionTest.java @@ -0,0 +1,25 @@ +import org.junit.Assert; +import org.junit.Test; + +/** + * Author: 王俊超 + * Time: 2020-06-29 07:46 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @Test + public void test1() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{5, 4, 3, 2, 1}, new String[]{"Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"}}, + {new int[]{1, 2, 3, 4, 5}, new String[]{"5", "4", "Bronze Medal", "Silver Medal", "Gold Medal"}}, + }; + + for (Object[] d: data) { + Assert.assertArrayEquals(s.findRelativeRanks((int[]) d[0]), (Object[]) d[1]); + } + } +} diff --git a/[0507][Perfect Number]/[0507][Perfect Number].iml b/[0507][Perfect Number]/[0507][Perfect Number].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0507][Perfect Number]/[0507][Perfect Number].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0507][Perfect Number]/src/Solution.java b/[0507][Perfect Number]/src/Solution.java new file mode 100644 index 0000000..036581e --- /dev/null +++ b/[0507][Perfect Number]/src/Solution.java @@ -0,0 +1,26 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 08:33 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public boolean checkPerfectNumber(int num) { + if (num < 6) { + return false; + } + + int sum = 1; + int sqrt = (int) Math.sqrt(num); + + for (int i = 2; i <= sqrt; i++) { + if (num % i == 0) { + sum += i; + sum += num / i; + } + } + + return sum == num; + } +} diff --git a/[0507][Perfect Number]/src/SolutionTest.java b/[0507][Perfect Number]/src/SolutionTest.java new file mode 100644 index 0000000..287f91d --- /dev/null +++ b/[0507][Perfect Number]/src/SolutionTest.java @@ -0,0 +1,29 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-06-29 08:36 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void checkPerfectNumber() { + Solution s = new Solution(); + Object[][] data = { + {28, true}, + {-1, false}, + {0, false}, + {1, false}, + {6, true}, + {7, false}, + {8, false}, + }; + + for (Object[] d: data) { + Assert.assertEquals(s.checkPerfectNumber((Integer) d[0]), d[1]); + } + } +} \ No newline at end of file diff --git a/[0509][Fibonacci Number]/[0509][Fibonacci Number].iml b/[0509][Fibonacci Number]/[0509][Fibonacci Number].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0509][Fibonacci Number]/[0509][Fibonacci Number].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0509][Fibonacci Number]/src/Solution.java b/[0509][Fibonacci Number]/src/Solution.java new file mode 100644 index 0000000..4e5fa95 --- /dev/null +++ b/[0509][Fibonacci Number]/src/Solution.java @@ -0,0 +1,27 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 08:44 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int fib(int N) { + if (N <= 0) { + return 0; + } else if (N == 1) { + return 1; + } + + int n2 = 0; + int n1 = 1; + int n0; + for (int i = 2; i <= N; i++) { + n0 = n1 + n2; + n2 = n1; + n1 = n0; + } + + return n1; + } +} diff --git a/[0509][Fibonacci Number]/src/SolutionTest.java b/[0509][Fibonacci Number]/src/SolutionTest.java new file mode 100644 index 0000000..c959dd8 --- /dev/null +++ b/[0509][Fibonacci Number]/src/SolutionTest.java @@ -0,0 +1,26 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-06-29 08:48 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void fib() { + Solution s = new Solution(); + Object[][] data = { + {0, 0}, + {1, 1}, + {2, 1}, + {3, 2}, + }; + + for (Object[] d : data) { + Assert.assertEquals(s.fib((Integer) d[0]), d[1]); + } + } +} \ No newline at end of file diff --git a/[0520][Detect Capital]/[0520][Detect Capital].iml b/[0520][Detect Capital]/[0520][Detect Capital].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0520][Detect Capital]/[0520][Detect Capital].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0520][Detect Capital]/src/Solution.java b/[0520][Detect Capital]/src/Solution.java new file mode 100644 index 0000000..c8c6c13 --- /dev/null +++ b/[0520][Detect Capital]/src/Solution.java @@ -0,0 +1,73 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 08:42 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + *
+     * Given a word, you need to judge whether the usage of capitals in it is right or not.
+     *
+     * We define the usage of capitals in a word to be right when one of the following
+     * cases holds:
+     *
+     * All letters in this word are capitals, like "USA".
+     * All letters in this word are not capitals, like "leetcode".
+     * Only the first letter in this word is capital, like "Google".
+     * Otherwise, we define that this word doesn't use capitals in a right way.
+     *
+     *
+     * Example 1:
+     *
+     * Input: "USA"
+     * Output: True
+     *
+     *
+     * Example 2:
+     *
+     * Input: "FlaG"
+     * Output: False
+     *
+     *
+     * Note: The input will be a non-empty word consisting of uppercase
+     * and lowercase latin letters.
+     * 
+ * + * @param word + * @return + */ + public boolean detectCapitalUse(String word) { + + if (Character.isUpperCase(word.charAt(0))) { + return isAllUpperCase(word.substring(1)) || isAllLowerCase(word.substring(1)); + } else if (Character.isLowerCase(word.charAt(0))) { + return isAllLowerCase(word.substring(1)); + } + + return false; + } + + private boolean isAllLowerCase(String s) { + for (int i = 0; i < s.length(); i++) { + if (!Character.isLowerCase(s.charAt(i))) { + return false; + } + } + + return true; + } + + private boolean isAllUpperCase(String s) { + + for (int i = 0; i < s.length(); i++) { + if (!Character.isUpperCase(s.charAt(i))) { + return false; + } + } + + return true; + } +} + diff --git a/[0521][Longest Uncommon Subsequence I]/[0521][Longest Uncommon Subsequence I].iml b/[0521][Longest Uncommon Subsequence I]/[0521][Longest Uncommon Subsequence I].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0521][Longest Uncommon Subsequence I]/[0521][Longest Uncommon Subsequence I].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0521][Longest Uncommon Subsequence I]/src/Solution.java b/[0521][Longest Uncommon Subsequence I]/src/Solution.java new file mode 100644 index 0000000..7f99841 --- /dev/null +++ b/[0521][Longest Uncommon Subsequence I]/src/Solution.java @@ -0,0 +1,20 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 08:55 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * @param a + * @param b + * @return + */ + public int findLUSlength(String a, String b) { + if (a.equals(b)) { + return -1; + } + return Math.max(a.length(), b.length()); + } +} diff --git a/[0523][K Diff Pairs In An Array]/[0523][K Diff Pairs In An Array].iml b/[0523][K Diff Pairs In An Array]/[0523][K Diff Pairs In An Array].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0523][K Diff Pairs In An Array]/[0523][K Diff Pairs In An Array].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0523][K Diff Pairs In An Array]/src/Solution.java b/[0523][K Diff Pairs In An Array]/src/Solution.java new file mode 100644 index 0000000..c461f31 --- /dev/null +++ b/[0523][K Diff Pairs In An Array]/src/Solution.java @@ -0,0 +1,40 @@ +import java.util.Arrays; + +/** + * Author: 王俊超 + * Time: 2020-06-30 08:52 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int findPairs(int[] nums, int k) { + if (nums == null || nums.length < 2 || k < 0) { + return 0; + } + + int result = 0; + + Arrays.sort(nums); + + for (int i = 0; i < nums.length - 1;i++) { + + for (int j = i + 1; j < nums.length && nums[j] - nums[i] <= k; j++) { + while (j + 1 < nums.length && nums[j] == nums[j + 1]) { + j++; + } + + if (nums[j] - nums[i] == k) { + result++; + } + } + + while (i + 1 < nums.length && nums[i] == nums[i + 1]) { + i++; + } + + } + + return result; + } +} diff --git a/[0523][K Diff Pairs In An Array]/src/SolutionTest.java b/[0523][K Diff Pairs In An Array]/src/SolutionTest.java new file mode 100644 index 0000000..2e368fa --- /dev/null +++ b/[0523][K Diff Pairs In An Array]/src/SolutionTest.java @@ -0,0 +1,27 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-06-30 08:56 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findPairs() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{3, 1, 4, 1, 5}, 2, 2}, + {new int[]{1, 2, 3, 4, 5}, 1, 4}, + {new int[]{1, 3, 1, 5, 4}, 0, 1}, + {new int[]{1, 1, 1, 1, 1}, 0, 1}, + {new int[]{1, 1, 2, 2, 1}, 0, 2}, + }; + + for (Object[] d: data) { + Assert.assertEquals(d[2], s.findPairs((int[]) d[0], (Integer) d[1])); + } + } +} \ No newline at end of file diff --git a/[0530][Minimum Absolute Difference in BST]/[0530][Minimum Absolute Difference in BST].iml b/[0530][Minimum Absolute Difference in BST]/[0530][Minimum Absolute Difference in BST].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0530][Minimum Absolute Difference in BST]/[0530][Minimum Absolute Difference in BST].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0530][Minimum Absolute Difference in BST]/src/Solution.java b/[0530][Minimum Absolute Difference in BST]/src/Solution.java new file mode 100644 index 0000000..603e965 --- /dev/null +++ b/[0530][Minimum Absolute Difference in BST]/src/Solution.java @@ -0,0 +1,92 @@ +import java.util.*; + +/** + * Author: 王俊超 + * Time: 2020-06-29 10:09 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 解题思路 + * root提个二叉搜索树 + * 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: + * 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; + * 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; + * 它的左、右子树也分别为二叉排序树。 + * + * @param root + * @return + */ + public int getMinimumDifference(TreeNode root) { + + if (root == null) { + return 0; + } + Deque s = new LinkedList<>(); + int min = Integer.MAX_VALUE; + int prev = 0; + boolean first = true; + while (root != null || !s.isEmpty()) { + while (root != null) { + s.push(root);//先访问再入栈 + root = root.left; + } + root = s.pop(); + + if (first) { + first = false; + } else { + min = Math.min(min, root.val - prev); + } + + prev = root.val; + root = root.right;//如果是null,出栈并处理右子树 + } + + return min; + } + + public int getMinimumDifference2(TreeNode root) { + + if (root == null) { + return 0; + } + + List list = new ArrayList<>(); + + Deque queue = new LinkedList<>(); + queue.add(root); + while (!queue.isEmpty()) { + TreeNode node = queue.poll(); + list.add(node.val); + if (node.right != null) { + queue.addFirst(node.right); + } + if (node.left != null) { + queue.addFirst(node.left); + } + } + + + if (list.size() == 1) { + return list.get(0); + } + + list.sort(new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return o1 - o2; + } + }); + + int min = Integer.MAX_VALUE; + + for (int i = 1; i < list.size(); i++) { + min = Math.min(min, Math.abs(list.get(i - 1) - list.get(i))); + } + + return min; + } +} diff --git a/[0530][Minimum Absolute Difference in BST]/src/SolutionTest.java b/[0530][Minimum Absolute Difference in BST]/src/SolutionTest.java new file mode 100644 index 0000000..b0523af --- /dev/null +++ b/[0530][Minimum Absolute Difference in BST]/src/SolutionTest.java @@ -0,0 +1,35 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-06-29 11:33 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void getMinimumDifference() { + TreeNode root = new TreeNode(1); + root.right = new TreeNode(3); + root.right.left = new TreeNode(2); + + Solution s = new Solution(); + Assert.assertEquals(s.getMinimumDifference(root), 1); + } + +// [236,104,701,null,227,null,911] + + @org.junit.Test + public void getMinimumDifference2() { + TreeNode root = new TreeNode(236); + root.left = new TreeNode(104); + root.left.right = new TreeNode(227); + root.right = new TreeNode(701); + root.right.right = new TreeNode(911); + + Solution s = new Solution(); + Assert.assertEquals(s.getMinimumDifference(root), 9); + } +} \ No newline at end of file diff --git a/[0530][Minimum Absolute Difference in BST]/src/TreeNode.java b/[0530][Minimum Absolute Difference in BST]/src/TreeNode.java new file mode 100644 index 0000000..bff99a6 --- /dev/null +++ b/[0530][Minimum Absolute Difference in BST]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/[0538][Convert BST to Greater Tree]/[0538][Convert BST to Greater Tree].iml b/[0538][Convert BST to Greater Tree]/[0538][Convert BST to Greater Tree].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0538][Convert BST to Greater Tree]/[0538][Convert BST to Greater Tree].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0538][Convert BST to Greater Tree]/src/Solution.java b/[0538][Convert BST to Greater Tree]/src/Solution.java new file mode 100644 index 0000000..0e52f13 --- /dev/null +++ b/[0538][Convert BST to Greater Tree]/src/Solution.java @@ -0,0 +1,52 @@ +import java.util.Deque; +import java.util.LinkedList; + +/** + * Author: 王俊超 + * Time: 2020-06-30 10:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 解题思路 + * root提个二叉搜索树 + * 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: + * 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; + * 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; + * 它的左、右子树也分别为二叉排序树。 + * 先处理右子树,再处理自己,再处理左子树 + * + * @param root + * @return + */ + public TreeNode convertBST(TreeNode root) { + + if (root == null) { + return null; + } + Deque s = new LinkedList<>(); + TreeNode prev = null; + TreeNode curr = root; + + while (curr != null || !s.isEmpty()) { + while (curr != null) { + s.push(curr);//先访问再入栈 + curr = curr.right; + } + curr = s.pop(); + + if (prev == null) { + prev = curr; + } else { + curr.val += prev.val; + } + + prev = curr; + curr = curr.left;//如果是null,出栈并处理左子树 + } + + return root; + } +} diff --git a/[0538][Convert BST to Greater Tree]/src/SolutionTest.java b/[0538][Convert BST to Greater Tree]/src/SolutionTest.java new file mode 100644 index 0000000..5b181bf --- /dev/null +++ b/[0538][Convert BST to Greater Tree]/src/SolutionTest.java @@ -0,0 +1,39 @@ +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-06-30 10:14 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void convertBST() { + Solution s = new Solution(); + + TreeNode root = new TreeNode(4); + root.left = new TreeNode(2); + root.left.left = new TreeNode(1); + root.left.right = new TreeNode(3); + root.right =new TreeNode(6); + root.right.left = new TreeNode(5); + root.right.right = new TreeNode(7); + + s.convertBST(root); + + print(root); + } + + private void print(TreeNode root) { + if (root == null) { + return; + } + + print(root.left); + System.out.println(root.val); + print(root.right); + + } +} \ No newline at end of file diff --git a/[0538][Convert BST to Greater Tree]/src/TreeNode.java b/[0538][Convert BST to Greater Tree]/src/TreeNode.java new file mode 100644 index 0000000..bff99a6 --- /dev/null +++ b/[0538][Convert BST to Greater Tree]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/[0541][Reverse String II]/[0541][Reverse String II].iml b/[0541][Reverse String II]/[0541][Reverse String II].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0541][Reverse String II]/[0541][Reverse String II].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0541][Reverse String II]/src/Solution.java b/[0541][Reverse String II]/src/Solution.java new file mode 100644 index 0000000..df9a379 --- /dev/null +++ b/[0541][Reverse String II]/src/Solution.java @@ -0,0 +1,32 @@ +/** + * Author: 王俊超 + * Time: 2020-06-30 12:50 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public String reverseStr(String s, int k) { + if (s == null || s.length() < 2 || k < 1) { + return s; + } + char[] chars = s.toCharArray(); + int index = 0; + while (index < chars.length) { + swap(chars, index, Math.min(chars.length - 1, index + k - 1)); + index += 2 * k; + } + + return new String(chars); + } + + public void swap(char[] chars, int i, int j) { + while (i < j) { + char ch = chars[i]; + chars[i] = chars[j]; + chars[j] = ch; + i++; + j--; + } + } +} diff --git a/[0541][Reverse String II]/src/SolutionTest.java b/[0541][Reverse String II]/src/SolutionTest.java new file mode 100644 index 0000000..373a1d1 --- /dev/null +++ b/[0541][Reverse String II]/src/SolutionTest.java @@ -0,0 +1,24 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-06-30 12:54 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void reverseStr() { + Solution s = new Solution(); + Object[][] data = { + {"abcdefg", 2, "bacdfeg"}, + {"abcdefgh", 2, "bacdfegh"}, + }; + + for (Object[] d : data) { + Assert.assertEquals(d[2], s.reverseStr((String) d[0], (Integer) d[1])); + } + } +} \ No newline at end of file diff --git a/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml b/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0543][Diameter of Binary Tree]/src/Solution.java b/[0543][Diameter of Binary Tree]/src/Solution.java new file mode 100644 index 0000000..5bca04a --- /dev/null +++ b/[0543][Diameter of Binary Tree]/src/Solution.java @@ -0,0 +1,59 @@ +/** + * Author: 王俊超 + * Time: 2020-07-01 08:53 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int diameterOfBinaryTree(TreeNode root) { + return layerAndDiameter(root)[1]; + } + + /** + * 解题思路: + * 最大直径的三种情况: + * 1、在左子树中 + * 2、在右子树中 + * 3、对过root节点,如果经过root节点,那么直径就是 Height(root.left) + Height(root.right) + 2 + * 对每个节点都是同样的。所以可以使用递归求解,先求左右子树的高度和最大直径,再求通过root的高度和直径,再找出最大的直径 + * + * + * 第一个反回参数表示root最大有多少层, + * 第二个返回参数表示root的最大直径 + * + * @param root + * @return + */ + public int[] layerAndDiameter(TreeNode root) { + if (root == null) { // 边界条件 + return new int[]{0, 0}; + } + + if (root.left == null && root.right == null) { // 边界条件 + return new int[]{0, 0}; + } else if (root.left != null && root.right == null) { + int[] result = layerAndDiameter(root.left); + result[0] += 1; // 层升高1 + result[1] = Math.max(result[0], result[1]); // 因为没有右子树,最大直径要么经过root,要么不经过 + + return result; + } else if (root.left == null && root.right != null) { + int[] result = layerAndDiameter(root.right); + result[0] += 1; // 层升高1 + result[1] = Math.max(result[0], result[1]); // 因为没有左子树,最大直径要么经过root,要么不经过 + return result; + } + + int[] left = layerAndDiameter(root.left); + int[] right = layerAndDiameter(root.right); + + int[] result = {0, 0}; + result[0] = 1 + Math.max(left[0], right[0]); // 层升高1 + + result[1] = Math.max(left[1], right[1]); // 最大直径可能在左子树或者右子树中 + result[1] = Math.max(result[1], 2 + left[0] + right[0]); // 最大直径可能经过root + + return result; + } +} diff --git a/[0543][Diameter of Binary Tree]/src/SolutionTest.java b/[0543][Diameter of Binary Tree]/src/SolutionTest.java new file mode 100644 index 0000000..c3cab02 --- /dev/null +++ b/[0543][Diameter of Binary Tree]/src/SolutionTest.java @@ -0,0 +1,25 @@ +import org.junit.Assert; + +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-07-01 08:57 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void diameterOfBinaryTree() { + Solution s = new Solution(); + TreeNode root = new TreeNode(1); + root.left = new TreeNode(2); + root.right = new TreeNode(3); + root.left.left = new TreeNode(4); + root.left.right = new TreeNode(5); + + Assert.assertEquals(3, s.diameterOfBinaryTree(root)); + } +} \ No newline at end of file diff --git a/[0543][Diameter of Binary Tree]/src/TreeNode.java b/[0543][Diameter of Binary Tree]/src/TreeNode.java new file mode 100644 index 0000000..bff99a6 --- /dev/null +++ b/[0543][Diameter of Binary Tree]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/[0551][Student Attendance Record I]/[0551][Student Attendance Record I].iml b/[0551][Student Attendance Record I]/[0551][Student Attendance Record I].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0551][Student Attendance Record I]/[0551][Student Attendance Record I].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0551][Student Attendance Record I]/src/Solution.java b/[0551][Student Attendance Record I]/src/Solution.java new file mode 100644 index 0000000..3ca6969 --- /dev/null +++ b/[0551][Student Attendance Record I]/src/Solution.java @@ -0,0 +1,41 @@ +/** + * Author: 王俊超 + * Time: 2020-07-02 08:05 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public boolean checkRecord(String s) { + int a = 0; + int l = 0; + int preLIndex = -1; + if (s != null) { + for (int i = 0; i < s.length(); i++) { + switch (s.charAt(i)) { + case 'A': + a++; + break; + case 'L': + if (l < 3) { // 连续两个L后面步不用记录了 + if (preLIndex == -1) { // 刚开始记录 + preLIndex = i; + l = 1; + } else if (preLIndex + 1 == i) { // 当前和前一个都是L + l++; + preLIndex = i; + } else { // 前一个不是L,重新记数 + preLIndex = i; + l = 1; + } + } + break; + default: + // do nothing + } + } + } + + return a <= 1 && l < 3; + } +} diff --git a/[0551][Student Attendance Record I]/src/SolutionTest.java b/[0551][Student Attendance Record I]/src/SolutionTest.java new file mode 100644 index 0000000..4c102f4 --- /dev/null +++ b/[0551][Student Attendance Record I]/src/SolutionTest.java @@ -0,0 +1,27 @@ +import org.junit.Assert; +import org.junit.Test; + + +/** + * Author: 王俊超 + * Time: 2020-07-02 08:12 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @Test + public void checkRecord() { + Solution s = new Solution(); + Object[][] data = { + {"PPALLP", true}, + {"PPALLL", false}, + }; + + for (Object[] d : data) { + Assert.assertEquals(d[1], s.checkRecord((String) d[0])); + } + } + +} \ No newline at end of file diff --git a/[0559][Maximum Depth of N-ary Tree]/[0559][Maximum Depth of N-ary Tree].iml b/[0559][Maximum Depth of N-ary Tree]/[0559][Maximum Depth of N-ary Tree].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0559][Maximum Depth of N-ary Tree]/[0559][Maximum Depth of N-ary Tree].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0559][Maximum Depth of N-ary Tree]/src/Node.java b/[0559][Maximum Depth of N-ary Tree]/src/Node.java new file mode 100644 index 0000000..bf5b87a --- /dev/null +++ b/[0559][Maximum Depth of N-ary Tree]/src/Node.java @@ -0,0 +1,25 @@ +import java.util.List; + +/** + * Author: 王俊超 + * Time: 2020-07-02 07:58 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Node { + public int val; + public List children; + + public Node() { + } + + public Node(int val) { + this.val = val; + } + + public Node(int val, List children) { + this.val = val; + this.children = children; + } +} diff --git a/[0559][Maximum Depth of N-ary Tree]/src/Solution.java b/[0559][Maximum Depth of N-ary Tree]/src/Solution.java new file mode 100644 index 0000000..d43fa08 --- /dev/null +++ b/[0559][Maximum Depth of N-ary Tree]/src/Solution.java @@ -0,0 +1,27 @@ +/** + * Author: 王俊超 + * Time: 2020-07-02 07:58 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int maxDepth(Node root) { + if (root == null) { + return 0; + } + + if (root.children == null || root.children.isEmpty()) { + return 1; + } + + int max = 0; + + for (Node child : root.children) { + max = Math.max(max, maxDepth(child)); + } + + return 1 + max; + } +} + diff --git a/[0559][Maximum Depth of N-ary Tree]/src/SolutionTest.java b/[0559][Maximum Depth of N-ary Tree]/src/SolutionTest.java new file mode 100644 index 0000000..4aebf27 --- /dev/null +++ b/[0559][Maximum Depth of N-ary Tree]/src/SolutionTest.java @@ -0,0 +1,31 @@ +import org.junit.Assert; + +import java.util.ArrayList; + +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-07-02 08:00 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void maxDepth() { + Node root = new Node(1); + root.children = new ArrayList<>(); + root.children.add(new Node(3)); + root.children.add(new Node(2)); + root.children.add(new Node(4)); + root.children.get(0).children = new ArrayList<>(); + root.children.get(0).children.add(new Node(5)); + root.children.get(0).children.add(new Node(6)); + + Solution s =new Solution(); + + Assert.assertEquals(3, s.maxDepth(root)); + } +} \ No newline at end of file diff --git a/[0561][Array Partition I]/src/Main.java b/[0561][Array Partition I]/src/Main.java index 92bec11..9deca22 100644 --- a/[0561][Array Partition I]/src/Main.java +++ b/[0561][Array Partition I]/src/Main.java @@ -7,9 +7,9 @@ **/ public class Main { @Test - public void test1(){ + public void test1() { Solution solution = new Solution(); - int[] arr = {1,4,3,2}; + int[] arr = {1, 4, 3, 2}; Assert.assertEquals(4, solution.arrayPairSum(arr)); } } diff --git a/[0561][Array Partition I]/src/Solution.java b/[0561][Array Partition I]/src/Solution.java index 4c41f06..7169b92 100644 --- a/[0561][Array Partition I]/src/Solution.java +++ b/[0561][Array Partition I]/src/Solution.java @@ -2,6 +2,7 @@ /** * https://leetcode.com/problems/array-partition-i/ + * * @author: wangjunchao(王俊超) * @time: 2019-07-04 08:46 **/ @@ -25,6 +26,7 @@ public class Solution { * 先将数据组排序,取偶数位置的数相加,即为结果 * 证明略 * + * * @param nums * @return */ @@ -32,7 +34,7 @@ public int arrayPairSum(int[] nums) { Arrays.sort(nums); int result = 0; - for (int i = 0; i < nums.length; i+=2) { + for (int i = 0; i < nums.length; i += 2) { result += nums[i]; } diff --git a/[0563][Binary Tree Tilt]/[0563][Binary Tree Tilt].iml b/[0563][Binary Tree Tilt]/[0563][Binary Tree Tilt].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0563][Binary Tree Tilt]/[0563][Binary Tree Tilt].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0563][Binary Tree Tilt]/src/Solution.java b/[0563][Binary Tree Tilt]/src/Solution.java new file mode 100644 index 0000000..a3ae597 --- /dev/null +++ b/[0563][Binary Tree Tilt]/src/Solution.java @@ -0,0 +1,47 @@ +/** + * Author: 王俊超 + * Time: 2020-07-02 08:26 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 解题思路 + * 一个树的坡度等于=左子树的坡度+右子树的坡度+根节点的坡度 + * 子树同样适应 + * + * @param root + * @return + */ + public int findTilt(TreeNode root) { + return findTiltHelp(root)[0]; + } + + /** + * 第一个返回值表示root的坡度 + * 第二个返回值表示root的为根的树的节点和 + * + * @param root + * @return + */ + public int[] findTiltHelp(TreeNode root) { + // 空结点或者左右孩子为都空的结点 + if (root == null) { + return new int[]{0, 0}; + } else if (root.left == null && root.right == null) { + return new int[]{0, root.val}; + } + + + int[] left = findTiltHelp(root.left); + int[] right = findTiltHelp(root.right); + + int[] result = {0, 0}; + + result[0] = left[0] + right[0] + Math.abs(left[1] - right[1]); + result[1] = left[1] + right[1] + root.val; + + return result; + } +} diff --git a/[0563][Binary Tree Tilt]/src/SolutionTest.java b/[0563][Binary Tree Tilt]/src/SolutionTest.java new file mode 100644 index 0000000..358cd67 --- /dev/null +++ b/[0563][Binary Tree Tilt]/src/SolutionTest.java @@ -0,0 +1,22 @@ +import org.junit.Assert; + +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-07-02 08:38 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findTilt() { + Solution s = new Solution(); + TreeNode root = new TreeNode(1); + root.left = new TreeNode(2); + root.right = new TreeNode(3); + Assert.assertEquals(1, s.findTilt(root)); + } +} \ No newline at end of file diff --git a/[0563][Binary Tree Tilt]/src/TreeNode.java b/[0563][Binary Tree Tilt]/src/TreeNode.java new file mode 100644 index 0000000..8b88e24 --- /dev/null +++ b/[0563][Binary Tree Tilt]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Date: 2015-08-21 + * Time: 18:45 + * Declaration: All Rights Reserved !!! + */ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + diff --git a/[0566][Reshape the Matrix]/[0566][Reshape the Matrix].iml b/[0566][Reshape the Matrix]/[0566][Reshape the Matrix].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0566][Reshape the Matrix]/[0566][Reshape the Matrix].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0566][Reshape the Matrix]/src/Solution.java b/[0566][Reshape the Matrix]/src/Solution.java new file mode 100644 index 0000000..f4516d8 --- /dev/null +++ b/[0566][Reshape the Matrix]/src/Solution.java @@ -0,0 +1,29 @@ +/** + * Author: 王俊超 + * Time: 2020-07-02 08:43 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int[][] matrixReshape(int[][] nums, int r, int c) { + int row = nums.length; + int col = nums[0].length; + if (row * col != r * c) { + return nums; + } + + int[][] result = new int[r][c]; + for (int i = 0; i < r; i++) { + result[i] = new int[c]; + } + + int num = row * col; + + for (int i = 0; i < num; i++) { + result[i / c][i % c] = nums[i / col][i % col]; + } + + return result; + } +} diff --git a/[0566][Reshape the Matrix]/src/SolutionTest.java b/[0566][Reshape the Matrix]/src/SolutionTest.java new file mode 100644 index 0000000..d21e1f9 --- /dev/null +++ b/[0566][Reshape the Matrix]/src/SolutionTest.java @@ -0,0 +1,32 @@ +import org.junit.Assert; + +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-07-02 08:46 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void matrixReshape() { + Solution s = new Solution(); + Object[][] data = { + {new int[][]{{1, 2}, {3, 4}}, 1, 4, new int[][]{{1, 2, 3, 4}}}, + {new int[][]{{1, 2}, {3, 4}}, 2, 4, new int[][]{{1, 2}, {3, 4}}}, + }; + + for (Object[] d : data) { + int[][] result = s.matrixReshape((int[][]) d[0], (Integer) d[1], (Integer) d[2]); + int[][] actual = (int[][]) d[3]; + Assert.assertEquals(result.length, actual.length); + + for (int i = 0; i < result.length; i++) { + Assert.assertArrayEquals(actual[i], result[i]); + } + } + } +} \ No newline at end of file diff --git a/[0572][Subtree of Another Tree]/[0572][Subtree of Another Tree].iml b/[0572][Subtree of Another Tree]/[0572][Subtree of Another Tree].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0572][Subtree of Another Tree]/[0572][Subtree of Another Tree].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0572][Subtree of Another Tree]/src/Solution.java b/[0572][Subtree of Another Tree]/src/Solution.java new file mode 100644 index 0000000..f14f241 --- /dev/null +++ b/[0572][Subtree of Another Tree]/src/Solution.java @@ -0,0 +1,56 @@ +import java.util.Deque; +import java.util.LinkedList; + +/** + * Author: 王俊超 + * Time: 2020-07-02 09:02 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public boolean isSubtree(TreeNode s, TreeNode t) { + + if (s == t || t == null) { + return true; + } + + if (s == null) { + return false; + } + + Deque queue = new LinkedList<>(); + queue.addFirst(s); + while (!queue.isEmpty()) { + TreeNode root = queue.removeFirst(); + if (root.right != null) { + queue.addFirst(root.right); + } + if (root.left != null) { + queue.addFirst(root.left); + } + + if(isSame(root, t)) { + return true; + } + + } + + return false; + } + + public boolean isSame(TreeNode s, TreeNode t) { + if (s == t) { + return true; + } + + if (s == null || t == null) { + return false; + } + + boolean leftSame = isSame(s.left, t.left); + boolean rightSame = isSame(s.right, t.right); + + return s.val == t.val && leftSame && rightSame; + } +} diff --git a/[0572][Subtree of Another Tree]/src/SolutionTest.java b/[0572][Subtree of Another Tree]/src/SolutionTest.java new file mode 100644 index 0000000..15b8f13 --- /dev/null +++ b/[0572][Subtree of Another Tree]/src/SolutionTest.java @@ -0,0 +1,46 @@ +import org.junit.Assert; + +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-07-02 09:11 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void isSubtree() { + Solution s = new Solution(); + TreeNode root = new TreeNode(3); + root.left = new TreeNode(4); + root.right = new TreeNode(5); + root.left.left = new TreeNode(1); + root.left.right = new TreeNode(2); + + TreeNode t = new TreeNode(4); + t.left = new TreeNode(1); + t.right = new TreeNode(2); + + Assert.assertEquals(true, s.isSubtree(root, t)); + } + + @org.junit.Test + public void isSubtree2() { + Solution s = new Solution(); + TreeNode root = new TreeNode(3); + root.left = new TreeNode(4); + root.right = new TreeNode(5); + root.left.left = new TreeNode(1); + root.left.right = new TreeNode(2); + root.left.right.left = new TreeNode(0); + + TreeNode t = new TreeNode(4); + t.left = new TreeNode(1); + t.right = new TreeNode(2); + + Assert.assertEquals(false, s.isSubtree(root, t)); + } +} \ No newline at end of file diff --git a/[0572][Subtree of Another Tree]/src/TreeNode.java b/[0572][Subtree of Another Tree]/src/TreeNode.java new file mode 100644 index 0000000..ac7b907 --- /dev/null +++ b/[0572][Subtree of Another Tree]/src/TreeNode.java @@ -0,0 +1,22 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} diff --git a/[0575][Distribute Candies]/[0575][Distribute Candies].iml b/[0575][Distribute Candies]/[0575][Distribute Candies].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0575][Distribute Candies]/[0575][Distribute Candies].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0575][Distribute Candies]/src/Solution.java b/[0575][Distribute Candies]/src/Solution.java new file mode 100644 index 0000000..660aaac --- /dev/null +++ b/[0575][Distribute Candies]/src/Solution.java @@ -0,0 +1,32 @@ +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +/** + * Author: 王俊超 + * Time: 2020-07-03 10:03 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 找出共有多少种糖果,如果种类大于糖果数目的一半就取糖果数目的一半,否则就取糖果的种类数 + * + * @param candies + * @return + */ + public int distributeCandies(int[] candies) { + if (candies == null || candies.length % 2 != 0) { + throw new IllegalArgumentException("illegal candies: " + Arrays.toString(candies)); + } + + Set set = new HashSet<>(candies.length); + for (int c : candies) { + set.add(c); + } + + return Math.min(set.size(), candies.length / 2); + } + +} diff --git a/[0575][Distribute Candies]/src/SolutionTest.java b/[0575][Distribute Candies]/src/SolutionTest.java new file mode 100644 index 0000000..a066444 --- /dev/null +++ b/[0575][Distribute Candies]/src/SolutionTest.java @@ -0,0 +1,27 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-03 10:08 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void distributeCandies() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{1, 1, 2, 2, 3, 3}, 3}, + {new int[]{1, 1, 2, 3}, 2}, + {new int[]{1, 2, 3, 4}, 2}, + }; + + for (Object[] d : data) { + int result = s.distributeCandies((int[]) d[0]); + Assert.assertEquals(d[1], result); + } + + } +} \ No newline at end of file diff --git a/[0581][Shortest Unsorted Continuous Subarray]/[0581][Shortest Unsorted Continuous Subarray].iml b/[0581][Shortest Unsorted Continuous Subarray]/[0581][Shortest Unsorted Continuous Subarray].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0581][Shortest Unsorted Continuous Subarray]/[0581][Shortest Unsorted Continuous Subarray].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0581][Shortest Unsorted Continuous Subarray]/src/Solution.java b/[0581][Shortest Unsorted Continuous Subarray]/src/Solution.java new file mode 100644 index 0000000..bace39f --- /dev/null +++ b/[0581][Shortest Unsorted Continuous Subarray]/src/Solution.java @@ -0,0 +1,39 @@ +import java.util.Arrays; + +/** + * Author: 王俊超 + * Time: 2020-07-04 19:24 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int findUnsortedSubarray(int[] nums) { + if (nums == null || nums.length < 2) { + return 0; + } + + int[] sorted = Arrays.copyOf(nums, nums.length); + Arrays.sort(sorted); + + + int diffStart = 0; // 从开始找第一个位置变化的下标 + while (diffStart < nums.length && nums[diffStart] == sorted[diffStart]) { + diffStart++; + } + + if (diffStart >= nums.length) { + return 0; + } + + + int diffEnd = nums.length - 1; // 从最后找第一个位置变化的下标 + while (diffEnd >= 0 && nums[diffEnd] == sorted[diffEnd]) { + diffEnd--; + } + + + + return diffEnd - diffStart + 1; + } +} diff --git a/[0581][Shortest Unsorted Continuous Subarray]/src/SolutionTest.java b/[0581][Shortest Unsorted Continuous Subarray]/src/SolutionTest.java new file mode 100644 index 0000000..dcda38c --- /dev/null +++ b/[0581][Shortest Unsorted Continuous Subarray]/src/SolutionTest.java @@ -0,0 +1,26 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-04 19:32 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findUnsortedSubarray() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{2, 6, 4, 8, 10, 9, 15}, 5}, + {new int[]{1, 2, 3, 4, 5, 6, 7}, 0}, + {new int[]{7, 6, 5, 4, 3, 2, 1}, 7}, + }; + + for (Object[] d : data) { + Object result = s.findUnsortedSubarray((int[]) d[0]); + Assert.assertEquals(d[1], result); + } + } +} \ No newline at end of file diff --git a/[0589][N-ary Tree Preorder Traversal]/[0589][N-ary Tree Preorder Traversal].iml b/[0589][N-ary Tree Preorder Traversal]/[0589][N-ary Tree Preorder Traversal].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0589][N-ary Tree Preorder Traversal]/[0589][N-ary Tree Preorder Traversal].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0589][N-ary Tree Preorder Traversal]/src/Node.java b/[0589][N-ary Tree Preorder Traversal]/src/Node.java new file mode 100644 index 0000000..6df4c7f --- /dev/null +++ b/[0589][N-ary Tree Preorder Traversal]/src/Node.java @@ -0,0 +1,24 @@ +import java.util.List; + +/** + * Author: 王俊超 + * Time: 2020-07-03 10:20 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +class Node { + public int val; + public List children; + + public Node() {} + + public Node(int _val) { + this.val = _val; + } + + public Node(int val, List children) { + this.val = val; + this.children = children; + } +} diff --git a/[0589][N-ary Tree Preorder Traversal]/src/Solution.java b/[0589][N-ary Tree Preorder Traversal]/src/Solution.java new file mode 100644 index 0000000..f4cc00d --- /dev/null +++ b/[0589][N-ary Tree Preorder Traversal]/src/Solution.java @@ -0,0 +1,56 @@ +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +/** + * Author: 王俊超 + * Time: 2020-07-03 10:20 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public List preorder(Node root) { + List result = new LinkedList<>(); +// preorderReserve(root, result); + preorderIterator(root, result); + return result; + } + + public void preorderIterator(Node root, List result) { + if (root == null) { + return; + } + + Deque deque = new LinkedList<>(); + deque.addFirst(root); + + while (!deque.isEmpty()) { + Node n = deque.removeFirst(); + result.add(n.val); + if (n.children != null) { + int size = n.children.size(); + for (int i = size - 1; i >= 0; i--) { + Node child = n.children.get(i); + if (child != null) { + deque.addFirst(child); + } + } + } + } + } + + public void preorderReserve(Node root, List result) { + + if (root == null) { + return; + } + + result.add(root.val); + if (root.children != null) { + for (Node n : root.children) { + preorderReserve(n, result); + } + } + } +} diff --git a/[0590][N-ary Tree postorder Traversal]/[0590][N-ary Tree postorder Traversal].iml b/[0590][N-ary Tree postorder Traversal]/[0590][N-ary Tree postorder Traversal].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0590][N-ary Tree postorder Traversal]/[0590][N-ary Tree postorder Traversal].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0590][N-ary Tree postorder Traversal]/src/Node.java b/[0590][N-ary Tree postorder Traversal]/src/Node.java new file mode 100644 index 0000000..a6c2e09 --- /dev/null +++ b/[0590][N-ary Tree postorder Traversal]/src/Node.java @@ -0,0 +1,24 @@ +import java.util.List; + +/** + * Author: 王俊超 + * Time: 2020-07-03 10:20 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +class Node { + public int val; + public List children; + + public Node() {} + + public Node(int val) { + this.val = val; + } + + public Node(int val, List children) { + this.val = val; + this.children = children; + } +} diff --git a/[0590][N-ary Tree postorder Traversal]/src/Solution.java b/[0590][N-ary Tree postorder Traversal]/src/Solution.java new file mode 100644 index 0000000..0dcb602 --- /dev/null +++ b/[0590][N-ary Tree postorder Traversal]/src/Solution.java @@ -0,0 +1,63 @@ +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +/** + * Author: 王俊超 + * Time: 2020-07-03 10:20 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public List postorder(Node root) { + List result = new LinkedList<>(); +// postorderReserve(root, result); + postorderIterator(root, result); + return result; + } + + public void postorderIterator(Node root, List result) { + if (root == null) { + return; + } + + Deque deque = new LinkedList<>(); + deque.addFirst(root); + Node curr; + Node prev = null; + while (!deque.isEmpty()) { + curr = deque.getFirst(); + if (curr.children == null || curr.children.isEmpty() // 没有子孩子 + ||(prev != null && prev == curr.children.get(curr.children.size() - 1))) { // 前一个遍历的元素是最后一个子孩子,说明子树已经遍历完了 + result.add(curr.val); + prev = curr; + deque.removeFirst(); + } else { + // 有孩子先处理孩子 + int size = curr.children.size(); + for (int i = size - 1; i >= 0; i--) { + Node child = curr.children.get(i); + if (child != null) { + deque.addFirst(child); + } + } + } + } + } + + public void postorderReserve(Node root, List result) { + + if (root == null) { + return; + } + + if (root.children != null) { + for (Node n : root.children) { + postorderReserve(n, result); + } + } + + result.add(root.val); + } +} diff --git a/[0590][N-ary Tree postorder Traversal]/src/SolutionTest.java b/[0590][N-ary Tree postorder Traversal]/src/SolutionTest.java new file mode 100644 index 0000000..36f1259 --- /dev/null +++ b/[0590][N-ary Tree postorder Traversal]/src/SolutionTest.java @@ -0,0 +1,35 @@ +import org.junit.Assert; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-07-03 10:47 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void postorder() { + Solution s = new Solution(); + + Node root = new Node(1); + root.children = new ArrayList<>(); + root.children.add(new Node(3)); + root.children.add(new Node(2)); + root.children.add(new Node(4)); + root.children.get(0).children = new ArrayList<>(); + root.children.get(0).children.add(new Node(5)); + root.children.get(0).children.add(new Node(6)); + + List result = Arrays.asList(5,6,3,2,4,1); + + Assert.assertEquals(result, s.postorder(root)); + } +} \ No newline at end of file diff --git a/[0594][Longest Harmonious Subsequence]/[0594][Longest Harmonious Subsequence].iml b/[0594][Longest Harmonious Subsequence]/[0594][Longest Harmonious Subsequence].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0594][Longest Harmonious Subsequence]/[0594][Longest Harmonious Subsequence].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0594][Longest Harmonious Subsequence]/src/Solution.java b/[0594][Longest Harmonious Subsequence]/src/Solution.java new file mode 100644 index 0000000..d3ec9aa --- /dev/null +++ b/[0594][Longest Harmonious Subsequence]/src/Solution.java @@ -0,0 +1,78 @@ +import java.util.Arrays; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; + +/** + * Author: 王俊超 + * Time: 2020-07-04 15:09 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + + /** + * 求每个元素的个数,再找相邻元素之和最大的 + * 相邻指元素之差为1 + * + * @param nums + * @return + */ + public int findLHS(int[] nums) { + if (nums == null || nums.length < 2) { + return 0; + } + + int max = 0; + Arrays.sort(nums); + + int prevNum = Integer.MIN_VALUE; + int prevCnt = 0; + for (int i = 0; i < nums.length; ) { + int next = i + 1; + while (next < nums.length && nums[next] == nums[i]) { + next++; + } + if (prevNum + 1 == nums[i]) { + max = Math.max(max, prevCnt + next - i); + } + + prevNum = nums[i]; + prevCnt = next - i; + i = next; + + } + + return max; + } + + /** + * 求每个元素的个数,再找相邻元素之和最大的 + * 相邻指元素之差为1 + * + * @param nums + * @return + */ + public int findLHS2(int[] nums) { + if (nums == null || nums.length < 2) { + return 0; + } + + SortedMap map = new TreeMap<>(); + for (int n : nums) { + map.put(n, map.getOrDefault(n, 0) + 1); + } + + int max = 0; + Map.Entry prev = null; // 前一个遍历的元素 + for (Map.Entry entry : map.entrySet()) { + if (prev != null && prev.getKey() + 1 == entry.getKey()) { // 相邻元素 + max = Math.max(max, prev.getValue() + entry.getValue()); // 取较大的 + } + prev = entry; + } + + return max; + } +} diff --git a/[0594][Longest Harmonious Subsequence]/src/SolutionTest.java b/[0594][Longest Harmonious Subsequence]/src/SolutionTest.java new file mode 100644 index 0000000..f51d032 --- /dev/null +++ b/[0594][Longest Harmonious Subsequence]/src/SolutionTest.java @@ -0,0 +1,26 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-04 15:20 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findLHS() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{1, 3, 2, 2, 5, 2, 3, 7}, 5}, + {new int[]{-2, 0, 2, 2, 5, 5, 5, 5}, 0}, + {new int[]{1, 1, 1, 1}, 0}, + }; + + for (Object[] d : data) { + int result = s.findLHS((int[]) d[0]); + Assert.assertEquals(d[1], result); + } + } +} \ No newline at end of file diff --git a/[0598][Range Addition II]/[0598][Range Addition II].iml b/[0598][Range Addition II]/[0598][Range Addition II].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0598][Range Addition II]/[0598][Range Addition II].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0598][Range Addition II]/src/Solution.java b/[0598][Range Addition II]/src/Solution.java new file mode 100644 index 0000000..49747ce --- /dev/null +++ b/[0598][Range Addition II]/src/Solution.java @@ -0,0 +1,29 @@ +/** + * Author: 王俊超 + * Time: 2020-07-04 15:50 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 数组的第一元素一定是最大的,要找最大的元素个数,只要找ops中min(ops[0])*min(ops[1]) + * + * @param m + * @param n + * @param ops + * @return + */ + public int maxCount(int m, int n, int[][] ops) { + + + int row = m; + int col = n; + for (int[] op : ops) { + row = Math.min(op[0], row); + col = Math.min(op[1], col); + } + + return row * col; + } +} diff --git a/[0598][Range Addition II]/src/SolutionTest.java b/[0598][Range Addition II]/src/SolutionTest.java new file mode 100644 index 0000000..8550371 --- /dev/null +++ b/[0598][Range Addition II]/src/SolutionTest.java @@ -0,0 +1,26 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-04 16:04 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void maxCount() { + Solution s = new Solution(); + Object[][] data = { + {3, 3, new int[][]{{2, 2}, {3, 3}}, 4}, + {3, 3, new int[][]{{2, 3}, {3, 2}}, 4}, + {3, 3, new int[][]{}, 9}, + }; + + for (Object[] d : data) { + Object result = s.maxCount((int) d[0], (int)d[1], (int[][]) d[2]); + Assert.assertEquals(d[3], result); + } + } +} \ No newline at end of file diff --git a/[0599][Minimum Index Sum of Two Lists]/[0599][Minimum Index Sum of Two Lists].iml b/[0599][Minimum Index Sum of Two Lists]/[0599][Minimum Index Sum of Two Lists].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0599][Minimum Index Sum of Two Lists]/[0599][Minimum Index Sum of Two Lists].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0599][Minimum Index Sum of Two Lists]/src/Solution.java b/[0599][Minimum Index Sum of Two Lists]/src/Solution.java new file mode 100644 index 0000000..238a202 --- /dev/null +++ b/[0599][Minimum Index Sum of Two Lists]/src/Solution.java @@ -0,0 +1,39 @@ +import java.util.*; + +/** + * Author: 王俊超 + * Time: 2020-07-04 16:13 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public String[] findRestaurant(String[] list1, String[] list2) { + if (list1 == null || list2 == null){ + return null; + } + + Map map = new HashMap<>(); + for (int i = 0; i < list1.length; i++) { + map.put(list1[i], i); + } + List result = new ArrayList<>(list1.length); + + int min = Integer.MAX_VALUE; + + for (int i = 0; i < list2.length; i++) { + if (map.containsKey(list2[i])) { + if (map.get(list2[i]) + i < min) { + result.clear(); + min = map.get(list2[i]) + i; + result.add(list2[i]); + } else if (map.get(list2[i]) + i == min) { + result.add(list2[i]); + } + + } + } + + return result.toArray(new String[0]); + } +} diff --git a/[0599][Minimum Index Sum of Two Lists]/src/SolutionTest.java b/[0599][Minimum Index Sum of Two Lists]/src/SolutionTest.java new file mode 100644 index 0000000..671b81b --- /dev/null +++ b/[0599][Minimum Index Sum of Two Lists]/src/SolutionTest.java @@ -0,0 +1,34 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-04 16:23 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findRestaurant() { + Solution s = new Solution(); + Object[][] data = { + { + new String[]{"Shogun", "Tapioca Express", "Burger King", "KFC"}, + new String[]{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"}, + new String[]{"Shogun"}, + }, + { + new String[]{"Shogun","Tapioca Express","Burger King","KFC"}, + new String[]{"KFC","Shogun","Burger King"}, + new String[]{"Shogun"}, + } + + }; + + for (Object[] d : data) { + String[] result = s.findRestaurant((String[]) d[0], (String[]) d[1]); + Assert.assertArrayEquals((String[]) d[2], result); + } + } +} \ No newline at end of file diff --git a/[0605][Can Place Flowers]/[0605][Can Place Flowers].iml b/[0605][Can Place Flowers]/[0605][Can Place Flowers].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0605][Can Place Flowers]/[0605][Can Place Flowers].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0605][Can Place Flowers]/src/Solution.java b/[0605][Can Place Flowers]/src/Solution.java new file mode 100644 index 0000000..d6aed3c --- /dev/null +++ b/[0605][Can Place Flowers]/src/Solution.java @@ -0,0 +1,54 @@ +/** + * Author: 王俊超 + * Time: 2020-07-04 19:42 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public boolean canPlaceFlowers(int[] flowerbed, int n) { + if (flowerbed == null || flowerbed.length < 1) { + return false; + } + + if (n < 1) { + return true; + } + + // 找第一个1的位置 + int firstOneIdx = 0; + while (firstOneIdx < flowerbed.length && flowerbed[firstOneIdx] == 0) { + firstOneIdx++; + } + + // [0,0,0] => 2 + // [0,0] => 1 + // [0] => 0 + int num = firstOneIdx / 2 + firstOneIdx % 2; + + if (firstOneIdx >= flowerbed.length) { + return num >= n; + } else { + // [0,0,0,1] => 1 + // [0,0,1] => 1 + // [0,1] => 0 + num -= firstOneIdx % 2; + } + + int prev = flowerbed[firstOneIdx]; + for (int i = firstOneIdx + 1; i < flowerbed.length; i++) { + if (prev == 0 && flowerbed[i] == 0) { + // i是最后一个,或者下一个位置也没有种花 + if (i + 1 >= flowerbed.length || flowerbed[i + 1] == 0) { + num++; + flowerbed[i] = 1; + } + } + + prev = flowerbed[i]; + } + + + return num >= n; + } +} diff --git a/[0605][Can Place Flowers]/src/SolutionTest.java b/[0605][Can Place Flowers]/src/SolutionTest.java new file mode 100644 index 0000000..b37b2f1 --- /dev/null +++ b/[0605][Can Place Flowers]/src/SolutionTest.java @@ -0,0 +1,31 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-04 19:52 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void canPlaceFlowers() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{1, 0, 0, 0, 1}, 1, true}, + {new int[]{1, 0, 0, 0, 1}, 2, false}, + {new int[]{1, 0, 1, 0, 1}, 1, false}, + {new int[]{1, 0, 1, 0, 0}, 2, false}, + {new int[]{0, 0, 0, 0, 0}, 3, true}, + {new int[]{1, 0, 0, 0, 0, 1}, 2, false}, + {new int[]{1, 0, 0, 0, 0, 1}, 2, false}, + {new int[]{ 0, 0, 1, 0, 1}, 1, true}, + }; + + for (Object[] d : data) { + Object result = s.canPlaceFlowers((int[]) d[0], (Integer) d[1]); + Assert.assertEquals(d[2], result); + } + } +} \ No newline at end of file diff --git a/[0606][Construct String from Binary Tree]/[0606][Construct String from Binary Tree].iml b/[0606][Construct String from Binary Tree]/[0606][Construct String from Binary Tree].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0606][Construct String from Binary Tree]/[0606][Construct String from Binary Tree].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0606][Construct String from Binary Tree]/src/Solution.java b/[0606][Construct String from Binary Tree]/src/Solution.java new file mode 100644 index 0000000..36c2245 --- /dev/null +++ b/[0606][Construct String from Binary Tree]/src/Solution.java @@ -0,0 +1,41 @@ +/** + * Author: 王俊超 + * Time: 2020-07-04 20:09 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public String tree2str(TreeNode t) { + StringBuilder builder = new StringBuilder(100); + tree2str(t, builder); + return builder.toString(); + } + + private void tree2str(TreeNode root, StringBuilder builder) { + if (root == null) { + return; + } + + builder.append(root.val); + + if (root.left == null && root.right == null) { + return; + } + + if (root.left != null && root.right == null) { + builder.append("("); + tree2str(root.left, builder); + builder.append(")"); + } + + if (root.right != null) { + builder.append("("); + tree2str(root.left, builder); + builder.append(")"); + builder.append("("); + tree2str(root.right, builder); + builder.append(")"); + } + } +} diff --git a/[0606][Construct String from Binary Tree]/src/SolutionTest.java b/[0606][Construct String from Binary Tree]/src/SolutionTest.java new file mode 100644 index 0000000..08f47cc --- /dev/null +++ b/[0606][Construct String from Binary Tree]/src/SolutionTest.java @@ -0,0 +1,33 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-04 20:12 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void tree2str() { + TreeNode root = new TreeNode(1); + root.left = new TreeNode(2); + root.right = new TreeNode(3); + root.left.left = new TreeNode(4); + + Solution s = new Solution(); + Assert.assertEquals("1(2(4))(3)", s.tree2str(root)); + } + + @org.junit.Test + public void tree2str2() { + TreeNode root = new TreeNode(1); + root.left = new TreeNode(2); + root.right = new TreeNode(3); + root.left.right = new TreeNode(4); + + Solution s = new Solution(); + Assert.assertEquals("1(2()(4))(3)", s.tree2str(root)); + } +} \ No newline at end of file diff --git a/[0606][Construct String from Binary Tree]/src/TreeNode.java b/[0606][Construct String from Binary Tree]/src/TreeNode.java new file mode 100644 index 0000000..8b88e24 --- /dev/null +++ b/[0606][Construct String from Binary Tree]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Date: 2015-08-21 + * Time: 18:45 + * Declaration: All Rights Reserved !!! + */ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + diff --git a/[0617][Merge Two Binary Trees]/src/Main.java b/[0617][Merge Two Binary Trees]/src/Main.java index 64abb99..f0f2e17 100644 --- a/[0617][Merge Two Binary Trees]/src/Main.java +++ b/[0617][Merge Two Binary Trees]/src/Main.java @@ -7,7 +7,7 @@ public class Main { @Test public void test1() { - TreeNode t1 = new TreeNode(1); + TreeNode t1 = new TreeNode(1); t1.left = new TreeNode(3); t1.left.left = new TreeNode(5); t1.right = new TreeNode(2); @@ -24,6 +24,7 @@ public void test1() { print(node); } + private void print(TreeNode node) { if (node != null) { System.out.print(node.val + ", "); diff --git a/[0617][Merge Two Binary Trees]/src/Solution.java b/[0617][Merge Two Binary Trees]/src/Solution.java index c9aa871..3123ffe 100644 --- a/[0617][Merge Two Binary Trees]/src/Solution.java +++ b/[0617][Merge Two Binary Trees]/src/Solution.java @@ -1,4 +1,3 @@ - /** * @author: wangjunchao(王俊超) * @time: 2019-07-04 08:57 diff --git a/[0617][Merge Two Binary Trees]/src/TreeNode.java b/[0617][Merge Two Binary Trees]/src/TreeNode.java index 8e2131e..8b88e24 100644 --- a/[0617][Merge Two Binary Trees]/src/TreeNode.java +++ b/[0617][Merge Two Binary Trees]/src/TreeNode.java @@ -1,4 +1,3 @@ - /** * Author: 王俊超 * Date: 2015-08-21 diff --git a/[0628][Maximum Product of Three Numbers]/[0628][Maximum Product of Three Numbers].iml b/[0628][Maximum Product of Three Numbers]/[0628][Maximum Product of Three Numbers].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0628][Maximum Product of Three Numbers]/[0628][Maximum Product of Three Numbers].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0628][Maximum Product of Three Numbers]/src/Solution.java b/[0628][Maximum Product of Three Numbers]/src/Solution.java new file mode 100644 index 0000000..a39a8e8 --- /dev/null +++ b/[0628][Maximum Product of Three Numbers]/src/Solution.java @@ -0,0 +1,29 @@ +import java.util.Arrays; + +/** + * Author: 王俊超 + * Time: 2020-07-04 20:23 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int maximumProduct(int[] nums) { + if (nums == null || nums.length < 3) { + return 0; + } + + Arrays.sort(nums); + + int len = nums.length; + + int max = nums[len - 1] * nums[len - 2] * nums[len - 3]; + + if (nums[1] < 0) { // 前两个是负数 + max = Math.max(max, nums[0]*nums[1]*nums[len-1]); + } + + + return max; + } +} diff --git a/[0628][Maximum Product of Three Numbers]/src/SolutionTest.java b/[0628][Maximum Product of Three Numbers]/src/SolutionTest.java new file mode 100644 index 0000000..04697b4 --- /dev/null +++ b/[0628][Maximum Product of Three Numbers]/src/SolutionTest.java @@ -0,0 +1,24 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-04 20:45 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void maximumProduct() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{-4, -3, -2, -1, 60}, 720}, + }; + + for (Object[] d : data) { + int result = s.maximumProduct((int[]) d[0]); + Assert.assertEquals(d[1], result); + } + } +} \ No newline at end of file diff --git a/[0633][Sum of Square Numbers]/[0633][Sum of Square Numbers].iml b/[0633][Sum of Square Numbers]/[0633][Sum of Square Numbers].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0633][Sum of Square Numbers]/[0633][Sum of Square Numbers].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0633][Sum of Square Numbers]/src/Solution.java b/[0633][Sum of Square Numbers]/src/Solution.java new file mode 100644 index 0000000..816b16e --- /dev/null +++ b/[0633][Sum of Square Numbers]/src/Solution.java @@ -0,0 +1,36 @@ +/** + * Author: 王俊超 + * Time: 2020-07-05 16:55 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 解题思路: + * 1、假设b >=a >= 0 + * ==> sqrt(c/2) <= b <= sqrt(c) + * @param c + * @return + */ + public boolean judgeSquareSum(int c) { + + if (c < 0) { + return false; + } + + + int sqrt = (int) Math.sqrt(c); + int halfSqrt = (int) Math.sqrt(c / 2.0); + + for (int i = halfSqrt; i <= sqrt; i++) { + + int sqrt2 = (int) Math.sqrt(c - i * i); + if (sqrt2 * sqrt2 + i * i == c) { + return true; + } + } + + return false; + } +} diff --git a/[0633][Sum of Square Numbers]/src/SolutionTest.java b/[0633][Sum of Square Numbers]/src/SolutionTest.java new file mode 100644 index 0000000..a859ba2 --- /dev/null +++ b/[0633][Sum of Square Numbers]/src/SolutionTest.java @@ -0,0 +1,30 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-05 17:00 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void judgeSquareSum() { + Solution s = new Solution(); + Object[][] data = { + {0, true}, + {1, true}, + {2, true}, + {3, false}, + {4, true}, + {5, true}, + {1000, true}, + }; + + for (Object[] d : data) { + Object result = s.judgeSquareSum((int) d[0]); + Assert.assertEquals(d[1], result); + } + } +} \ No newline at end of file diff --git a/[0637][Average of Levels in Binary Tree]/[0637][Average of Levels in Binary Tree].iml b/[0637][Average of Levels in Binary Tree]/[0637][Average of Levels in Binary Tree].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0637][Average of Levels in Binary Tree]/[0637][Average of Levels in Binary Tree].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0637][Average of Levels in Binary Tree]/src/Solution.java b/[0637][Average of Levels in Binary Tree]/src/Solution.java new file mode 100644 index 0000000..9e113fb --- /dev/null +++ b/[0637][Average of Levels in Binary Tree]/src/Solution.java @@ -0,0 +1,48 @@ +import java.util.ArrayList; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +/** + * Author: 王俊超 + * Time: 2020-07-04 16:32 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public List averageOfLevels(TreeNode root) { + List result = new ArrayList<>(); + + if (root == null) { + return result; + } + + Deque curr = new LinkedList<>(); + Deque next = new LinkedList<>(); + curr.addFirst(root); + + while (!curr.isEmpty()) { + + double count = curr.size(); + double sum = 0; + while (!curr.isEmpty()) { + TreeNode node = curr.removeFirst(); + sum += node.val; + + if (node.left != null) { + next.addLast(node.left); + } + if (node.right != null) { + next.addLast(node.right); + } + } + + result.add(sum / count); + curr = next; + next = new LinkedList<>(); + } + + return result; + } +} diff --git a/[0637][Average of Levels in Binary Tree]/src/TreeNode.java b/[0637][Average of Levels in Binary Tree]/src/TreeNode.java new file mode 100644 index 0000000..5554849 --- /dev/null +++ b/[0637][Average of Levels in Binary Tree]/src/TreeNode.java @@ -0,0 +1,13 @@ +/** + * @author: wangjunchao(王俊超) + * @time: 2019-07-09 18:52 + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/[0643][Maximum Average Subarray I]/[0643][Maximum Average Subarray I].iml b/[0643][Maximum Average Subarray I]/[0643][Maximum Average Subarray I].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0643][Maximum Average Subarray I]/[0643][Maximum Average Subarray I].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0643][Maximum Average Subarray I]/src/Solution.java b/[0643][Maximum Average Subarray I]/src/Solution.java new file mode 100644 index 0000000..d0a7f8c --- /dev/null +++ b/[0643][Maximum Average Subarray I]/src/Solution.java @@ -0,0 +1,28 @@ +/** + * Author: 王俊超 + * Time: 2020-07-05 16:23 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public double findMaxAverage(int[] nums, int k) { + if (nums == null || nums.length < 1 || k < 1) { + return 0; + } + + double sum = 0; + for (int i = 0; i < nums.length && i < k; i++) { // 求nums[0, k-1]的和 + sum += nums[i]; + } + + double max = sum; + for (int j = k; j < nums.length; j++) { // 求nums[j-k,j]中的大值 + sum = sum + nums[j] - nums[j - k]; + max = Math.max(max, sum); + + } + + return max / k; + } +} diff --git a/[0643][Maximum Average Subarray I]/src/SolutionTest.java b/[0643][Maximum Average Subarray I]/src/SolutionTest.java new file mode 100644 index 0000000..f698ef1 --- /dev/null +++ b/[0643][Maximum Average Subarray I]/src/SolutionTest.java @@ -0,0 +1,25 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-05 16:29 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findMaxAverage() { + Solution s = new Solution(); + Object[][] data = { +// {new int[]{1, 12, -5, -6, 50, 3}, 4, 12.75}, + {new int[]{7, 4, 5, 8, 8, 3, 9, 8, 7, 6}, 7, 7.0}, + }; + + for (Object[] d : data) { + Object result = s.findMaxAverage((int[]) d[0], (Integer) d[1]); + Assert.assertEquals(d[2], result); + } + } +} \ No newline at end of file diff --git a/[0645][Set Mismatch]/[0645][Set Mismatch].iml b/[0645][Set Mismatch]/[0645][Set Mismatch].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0645][Set Mismatch]/[0645][Set Mismatch].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0645][Set Mismatch]/src/Solution.java b/[0645][Set Mismatch]/src/Solution.java new file mode 100644 index 0000000..006c5f8 --- /dev/null +++ b/[0645][Set Mismatch]/src/Solution.java @@ -0,0 +1,43 @@ +import java.util.HashSet; +import java.util.Set; + +/** + * Author: 王俊超 + * Time: 2020-07-05 17:13 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 通过求和找丢失的数 + * m + x + y = total + * m + x + x = sum + * y - x = total -sum + * + * @param nums + * @return + */ + public int[] findErrorNums(int[] nums) { + if (nums == null || nums.length < 1) { + return null; + } + + Set map = new HashSet<>(); + int num = 1; + int sum = 0; + + for (int n : nums) { + if (map.contains(n)) { + num = n; + } else { + map.add(n); + } + sum += n; + } + + return new int[]{num, (1 + nums.length) * nums.length / 2 - sum + num}; + + } + +} diff --git a/[0645][Set Mismatch]/src/SolutionTest.java b/[0645][Set Mismatch]/src/SolutionTest.java new file mode 100644 index 0000000..93087a5 --- /dev/null +++ b/[0645][Set Mismatch]/src/SolutionTest.java @@ -0,0 +1,25 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-05 17:19 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findErrorNums() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{1, 2, 2, 4}, new int[]{2, 3}}, + {new int[]{1, 3, 2, 4, 4}, new int[]{4, 5}}, + }; + + for (Object[] d : data) { + int[] result = s.findErrorNums((int[]) d[0]); + Assert.assertArrayEquals((int[]) d[1], result); + } + } +} \ No newline at end of file diff --git a/[0653][Two Sum IV - Input is a BST]/[0653][Two Sum IV - Input is a BST].iml b/[0653][Two Sum IV - Input is a BST]/[0653][Two Sum IV - Input is a BST].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0653][Two Sum IV - Input is a BST]/[0653][Two Sum IV - Input is a BST].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0653][Two Sum IV - Input is a BST]/src/Solution.java b/[0653][Two Sum IV - Input is a BST]/src/Solution.java new file mode 100644 index 0000000..05f7a25 --- /dev/null +++ b/[0653][Two Sum IV - Input is a BST]/src/Solution.java @@ -0,0 +1,69 @@ +import java.util.HashSet; +import java.util.Set; + +/** + * Author: 王俊超 + * Time: 2020-07-05 18:20 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: + * 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; + * 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; + * 它的左、右子树也分别为二叉排序树。 + * + * @param root + * @param k + * @return + */ + + + public boolean findTarget(TreeNode root, int k) { + Set set = new HashSet<>(); + return findTargetHelp(root, k, set); + } + + private boolean findTargetHelp(TreeNode root, int k, Set set) { + if (root == null) { + return false; + } + if (set.contains(k - root.val)) { + return true; + } + + set.add(root.val); + + return findTargetHelp(root.left, k, set) || findTargetHelp(root.right, k, set); + } + + public boolean findTarget2(TreeNode root, int k) { + if (root == null) { + return false; + } + + Set set = new HashSet<>(); + visitTree(root, set); + + for (Integer n : set) { + if (n != k - n && set.contains(k - n)) { + return true; + } + } + + return false; + } + + public void visitTree(TreeNode root, Set set) { + if (root == null) { + return; + } + + set.add(root.val); + + visitTree(root.left, set); + visitTree(root.right, set); + } +} diff --git a/[0653][Two Sum IV - Input is a BST]/src/SolutionTest.java b/[0653][Two Sum IV - Input is a BST]/src/SolutionTest.java new file mode 100644 index 0000000..792632a --- /dev/null +++ b/[0653][Two Sum IV - Input is a BST]/src/SolutionTest.java @@ -0,0 +1,19 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-05 18:35 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findTarget() { + Solution s = new Solution(); + TreeNode root = new TreeNode(1); + + Assert.assertFalse(s.findTarget(root, 2)); + } +} \ No newline at end of file diff --git a/[0653][Two Sum IV - Input is a BST]/src/TreeNode.java b/[0653][Two Sum IV - Input is a BST]/src/TreeNode.java new file mode 100644 index 0000000..5554849 --- /dev/null +++ b/[0653][Two Sum IV - Input is a BST]/src/TreeNode.java @@ -0,0 +1,13 @@ +/** + * @author: wangjunchao(王俊超) + * @time: 2019-07-09 18:52 + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/[0657][Robot Return to Origin]/[0657][Robot Return to Origin].iml b/[0657][Robot Return to Origin]/[0657][Robot Return to Origin].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0657][Robot Return to Origin]/[0657][Robot Return to Origin].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0657][Robot Return to Origin]/src/Solution.java b/[0657][Robot Return to Origin]/src/Solution.java new file mode 100644 index 0000000..e02ca96 --- /dev/null +++ b/[0657][Robot Return to Origin]/src/Solution.java @@ -0,0 +1,34 @@ +/** + * Author: 王俊超 + * Time: 2020-07-04 20:51 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public boolean judgeCircle(String moves) { + if (moves == null || moves.length() < 1) { + return true; + } + + int[] pos = {0, 0}; + for (int i = 0; i < moves.length(); i++) { + switch (moves.charAt(i)) { + case 'U': + pos[1]++; + break; + case 'D': + pos[1]--; + break; + case 'L': + pos[0]--; + break; + case 'R': + pos[0]++; + break; + } + } + + return pos[0] == 0 && pos[1] == 0; + } +} diff --git a/[0661][Image Smoother]/[0661][Image Smoother].iml b/[0661][Image Smoother]/[0661][Image Smoother].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0661][Image Smoother]/[0661][Image Smoother].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0661][Image Smoother]/src/Solution.java b/[0661][Image Smoother]/src/Solution.java new file mode 100644 index 0000000..a92e9d8 --- /dev/null +++ b/[0661][Image Smoother]/src/Solution.java @@ -0,0 +1,32 @@ +/** + * Author: 王俊超 + * Time: 2020-07-06 08:11 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + + public int[][] imageSmoother(int[][] M) { + int[][] result = new int[M.length][]; + for (int i = 0; i < M.length; i++) { + result[i] = new int[M[i].length]; + } + + for (int i = 0; i < M.length; i++) { + for (int j = 0; j < M[0].length; j++) { + int count = 0; + int sum = 0; + for (int u = Math.max(0, i - 1); u <= i + 1 && u < M.length; u++) { + for (int v = Math.max(0, j - 1); v <= j + 1 && v < M[0].length; v++) { + count++; + sum += M[u][v]; + } + } + result[i][j] = sum / count; + } + } + + return result; + } +} diff --git a/[0661][Image Smoother]/src/SolutionTest.java b/[0661][Image Smoother]/src/SolutionTest.java new file mode 100644 index 0000000..311d241 --- /dev/null +++ b/[0661][Image Smoother]/src/SolutionTest.java @@ -0,0 +1,29 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-06 08:17 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void imageSmoother() { + Solution s = new Solution(); + Object[][] data = { + { + new int[][]{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, + new int[][]{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, + }, + }; + + for (Object[] d : data) { + int[][] result = s.imageSmoother((int[][]) d[0]); + for (int i = 0; i < result.length; i++) { + Assert.assertArrayEquals(((int[][]) d[1])[i], result[i]); + } + } + } +} \ No newline at end of file diff --git a/[0665][Non-decreasing Array]/[0665][Non-decreasing Array].iml b/[0665][Non-decreasing Array]/[0665][Non-decreasing Array].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0665][Non-decreasing Array]/[0665][Non-decreasing Array].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0665][Non-decreasing Array]/src/Solution.java b/[0665][Non-decreasing Array]/src/Solution.java new file mode 100644 index 0000000..db06448 --- /dev/null +++ b/[0665][Non-decreasing Array]/src/Solution.java @@ -0,0 +1,34 @@ +/** + * Author: 王俊超 + * Time: 2020-07-06 08:27 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + + /** + * 解题思路: + * 其实说的简单点,有2种情况: + * 升序序列中,突然有个数字小了,则nums[i]=nums[i-1] + * 升序序列中,突然有个数字大了,则nums[i-1]=nums[i] + * + * @param nums + * @return + */ + public boolean checkPossibility(int[] nums) { + int fix = 0; + for (int i = 1; i < nums.length && fix <= 1; i++) { + if (nums[i] >= nums[i - 1]) { + continue; + } + fix++; + if (i - 2 >= 0 && (nums[i] < nums[i - 2])) { // 突然有个数字小了 + nums[i] = nums[i - 1]; + } else {// 突然有个数字大了 + nums[i - 1] = nums[i]; + } + } + return fix <= 1; + } +} diff --git a/[0665][Non-decreasing Array]/src/SolutionTest.java b/[0665][Non-decreasing Array]/src/SolutionTest.java new file mode 100644 index 0000000..1667886 --- /dev/null +++ b/[0665][Non-decreasing Array]/src/SolutionTest.java @@ -0,0 +1,28 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-06 08:38 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void checkPossibility() { + Solution s = new Solution(); + Object[][] data = { +// {new int[]{1, 2, 3, 4, 6, 5, 7, 8, 9}, true}, +// {new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, true}, +// {new int[]{4, 2, 3}, true}, +// {new int[]{4, 2, 1}, false}, + {new int[]{-1, 4, 2, 3}, false}, + }; + + for (Object[] d : data) { + boolean result = s.checkPossibility((int[]) d[0]); + Assert.assertEquals(d[1], result); + } + } +} \ No newline at end of file diff --git a/[0669][Trim a Binary Search Tree]/[0669][Trim a Binary Search Tree].iml b/[0669][Trim a Binary Search Tree]/[0669][Trim a Binary Search Tree].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0669][Trim a Binary Search Tree]/[0669][Trim a Binary Search Tree].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0669][Trim a Binary Search Tree]/src/Solution.java b/[0669][Trim a Binary Search Tree]/src/Solution.java new file mode 100644 index 0000000..c86b80d --- /dev/null +++ b/[0669][Trim a Binary Search Tree]/src/Solution.java @@ -0,0 +1,33 @@ +/** + * Author: 王俊超 + * Time: 2020-07-06 08:58 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public TreeNode trimBST(TreeNode root, int L, int R) { + if (root == null) { + return null; + } + + if (root.val == L) { + root.left = null; + root.right = trimBST(root.right, L, R); + return root; + } else if (root.val == R) { + root.left = trimBST(root.left, L, R); + root.right = null; + return root; + } else if (root.val < L) { + return trimBST(root.right, L, R); + } else if (root.val > R) { + return trimBST(root.left, L, R); + } + + root.left = trimBST(root.left, L, R); + root.right = trimBST(root.right, L, R); + + return root; + } +} diff --git a/[0669][Trim a Binary Search Tree]/src/TreeNode.java b/[0669][Trim a Binary Search Tree]/src/TreeNode.java new file mode 100644 index 0000000..bff99a6 --- /dev/null +++ b/[0669][Trim a Binary Search Tree]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/[0671][Second Minimum Node In a Binary Tree]/[0671][Second Minimum Node In a Binary Tree].iml b/[0671][Second Minimum Node In a Binary Tree]/[0671][Second Minimum Node In a Binary Tree].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0671][Second Minimum Node In a Binary Tree]/[0671][Second Minimum Node In a Binary Tree].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0671][Second Minimum Node In a Binary Tree]/src/Solution.java b/[0671][Second Minimum Node In a Binary Tree]/src/Solution.java new file mode 100644 index 0000000..6562244 --- /dev/null +++ b/[0671][Second Minimum Node In a Binary Tree]/src/Solution.java @@ -0,0 +1,68 @@ +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * Author: 王俊超 + * Time: 2020-07-07 08:58 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * @param root + * @return + */ + + public int findSecondMinimumValue(TreeNode root) { + if (root == null){ + return -1; + } + return help(root, root.val); + } + + private int help(TreeNode root, int min) { + if (root == null){ + return -1; + } + if (root.val > min) { + return root.val; + } + int left = help(root.left, min); + int right = help(root.right, min); + + if (left == -1) { + return right; + } + if (right == -1) { + return left; + } + + return Math.min(left, right); + } + + public int findSecondMinimumValue2(TreeNode root) { + SortedSet result = new TreeSet<>(); + findSecondMinimumValue(root, result); + + if (result.size() < 2) { + return -1; + } + + result.remove(result.first()); + return result.first(); + } + + /** + * @param root + * @param result 是一个从小到大的size=2的有序数组 + */ + private void findSecondMinimumValue(TreeNode root, SortedSet result) { + if (root == null) { + return; + } + result.add(root.val); + findSecondMinimumValue(root.left, result); + findSecondMinimumValue(root.right, result); + } +} diff --git a/[0671][Second Minimum Node In a Binary Tree]/src/SolutionTest.java b/[0671][Second Minimum Node In a Binary Tree]/src/SolutionTest.java new file mode 100644 index 0000000..26dc827 --- /dev/null +++ b/[0671][Second Minimum Node In a Binary Tree]/src/SolutionTest.java @@ -0,0 +1,24 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-07 09:13 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findSecondMinimumValue() { + Solution s = new Solution(); + + TreeNode root = new TreeNode(2); + root.left = new TreeNode(2); + root.right = new TreeNode(5); + root.right.left = new TreeNode(5); + root.right.right = new TreeNode(7); + + Assert.assertEquals(5, s.findSecondMinimumValue(root)); + } +} \ No newline at end of file diff --git a/[0671][Second Minimum Node In a Binary Tree]/src/TreeNode.java b/[0671][Second Minimum Node In a Binary Tree]/src/TreeNode.java new file mode 100644 index 0000000..bff99a6 --- /dev/null +++ b/[0671][Second Minimum Node In a Binary Tree]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/[0674][Longest Continuous Increasing Subsequence]/[0674][Longest Continuous Increasing Subsequence].iml b/[0674][Longest Continuous Increasing Subsequence]/[0674][Longest Continuous Increasing Subsequence].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0674][Longest Continuous Increasing Subsequence]/[0674][Longest Continuous Increasing Subsequence].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0674][Longest Continuous Increasing Subsequence]/src/Solution.java b/[0674][Longest Continuous Increasing Subsequence]/src/Solution.java new file mode 100644 index 0000000..5a15c9f --- /dev/null +++ b/[0674][Longest Continuous Increasing Subsequence]/src/Solution.java @@ -0,0 +1,30 @@ +/** + * Author: 王俊超 + * Time: 2020-07-07 10:03 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + + public int findLengthOfLCIS(int[] nums) { + if (nums == null) { + return 0; + } else if (nums.length < 2) { + return nums.length; + } + + int prev = 0; + int max = 0; + for (int i = 1; i < nums.length; i++) { + while (i < nums.length && nums[i - 1] < nums[i]) { + i++; + } + + max = Math.max(max, i - prev); + prev = i; + } + + return max; + } +} diff --git a/[0674][Longest Continuous Increasing Subsequence]/src/SolutionTest.java b/[0674][Longest Continuous Increasing Subsequence]/src/SolutionTest.java new file mode 100644 index 0000000..351e6f5 --- /dev/null +++ b/[0674][Longest Continuous Increasing Subsequence]/src/SolutionTest.java @@ -0,0 +1,27 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-07 10:07 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findLengthOfLCIS() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{1, 3, 5, 4, 7}, 3}, + {new int[]{2, 2, 2, 2, 2}, 1}, + {new int[]{1, 2, 3, 4, 5}, 5}, + {new int[]{2, 2, 3, 4, 5}, 4}, + }; + + for (Object[] d : data) { + int result = s.findLengthOfLCIS((int[]) d[0]); + Assert.assertEquals(d[1], result); + } + } +} \ No newline at end of file diff --git a/[0680][Valid Palindrome II]/[0680][Valid Palindrome II].iml b/[0680][Valid Palindrome II]/[0680][Valid Palindrome II].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0680][Valid Palindrome II]/[0680][Valid Palindrome II].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0680][Valid Palindrome II]/src/Solution.java b/[0680][Valid Palindrome II]/src/Solution.java new file mode 100644 index 0000000..9ae5eb2 --- /dev/null +++ b/[0680][Valid Palindrome II]/src/Solution.java @@ -0,0 +1,35 @@ +/** + * Author: 王俊超 + * Time: 2020-07-07 10:12 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public boolean validPalindrome(String s) { + int start = 0; + int end = s.length() - 1; + while (start < end) { + // 有一个不相等,可以抛弃start或者end字符,抛弃一个字符后,后面的都要是回文 + if (s.charAt(start) != s.charAt(end)) { + return validPalindrome(s, start + 1, end) || validPalindrome(s, start, end - 1); + } + start++; + end--; + } + + return true; + } + + private boolean validPalindrome(String s, int start, int end) { + while (start < end) { + if (s.charAt(start) != s.charAt(end)) { + return false; + } + start++; + end--; + } + + return true; + } +} diff --git a/[0682][Baseball Game]/[0682][Baseball Game].iml b/[0682][Baseball Game]/[0682][Baseball Game].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0682][Baseball Game]/[0682][Baseball Game].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0682][Baseball Game]/src/Solution.java b/[0682][Baseball Game]/src/Solution.java new file mode 100644 index 0000000..4af7f26 --- /dev/null +++ b/[0682][Baseball Game]/src/Solution.java @@ -0,0 +1,43 @@ +import java.util.LinkedList; +import java.util.List; + +/** + * Author: 王俊超 + * Time: 2020-07-07 12:46 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int calPoints(String[] ops) { + + if (ops == null || ops.length < 1) { + return 0; + } + List round = new LinkedList<>(); + + for (String o : ops) { + int size = round.size(); + switch (o) { + case "+": + round.add(round.get(size - 1) + round.get(size - 2)); + break; + case "D": + round.add(2 * round.get(size - 1)); + break; + case "C": + round.remove(size - 1); + break; + default: + round.add(Integer.parseInt(o)); + } + } + + int sum = 0; + for(Integer n : round) { + sum += n; + } + + return sum; + } +} diff --git a/[0682][Baseball Game]/src/SolutionTest.java b/[0682][Baseball Game]/src/SolutionTest.java new file mode 100644 index 0000000..1cf9c9d --- /dev/null +++ b/[0682][Baseball Game]/src/SolutionTest.java @@ -0,0 +1,25 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-07 12:53 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void calPoints() { + Solution s = new Solution(); + Object[][] data = { + {new String[]{"5", "2", "C", "D", "+"}, 30}, + {new String[]{"5", "-2", "4", "C", "D", "9", "+", "+"}, 27}, + }; + + for (Object[] d : data) { + int result = s.calPoints((String[]) d[0]); + Assert.assertEquals(d[1], result); + } + } +} \ No newline at end of file diff --git a/[0783][Minimum Distance Between BST Nodes]/[0783][Minimum Distance Between BST Nodes].iml b/[0783][Minimum Distance Between BST Nodes]/[0783][Minimum Distance Between BST Nodes].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0783][Minimum Distance Between BST Nodes]/[0783][Minimum Distance Between BST Nodes].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0783][Minimum Distance Between BST Nodes]/src/Solution.java b/[0783][Minimum Distance Between BST Nodes]/src/Solution.java new file mode 100644 index 0000000..0230f1b --- /dev/null +++ b/[0783][Minimum Distance Between BST Nodes]/src/Solution.java @@ -0,0 +1,50 @@ +import java.util.*; + +/** + * Author: 王俊超 + * Time: 2020-06-29 10:09 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 解题思路 + * root提个二叉搜索树 + * 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: + * 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; + * 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; + * 它的左、右子树也分别为二叉排序树。 + * + * @param root + * @return + */ + public int minDiffInBST(TreeNode root) { + if (root == null) { + return 0; + } + Deque s = new LinkedList<>(); + int min = Integer.MAX_VALUE; + int prev = 0; + boolean first = true; + while (root != null || !s.isEmpty()) { + while (root != null) { + s.push(root);//先访问再入栈 + root = root.left; + } + root = s.pop(); + + if (first) { + first = false; + } else { + min = Math.min(min, root.val - prev); + } + + prev = root.val; + root = root.right;//如果是null,出栈并处理右子树 + } + + return min; + } + +} diff --git a/[0783][Minimum Distance Between BST Nodes]/src/TreeNode.java b/[0783][Minimum Distance Between BST Nodes]/src/TreeNode.java new file mode 100644 index 0000000..bff99a6 --- /dev/null +++ b/[0783][Minimum Distance Between BST Nodes]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/[0969][Pancake Sorting]/src/Solution.java b/[0969][Pancake Sorting]/src/Solution.java index b16de77..4aafc53 100644 --- a/[0969][Pancake Sorting]/src/Solution.java +++ b/[0969][Pancake Sorting]/src/Solution.java @@ -3,6 +3,7 @@ /** * https://leetcode.com/problems/pancake-sorting/ + * * @author: wangjunchao(王俊超) * @time: 2019-07-09 18:29 **/ @@ -45,6 +46,7 @@ public class Solution { * 完成把当前最大值放到最后的操作,即完成选择排序的选择最大值到最后的操作。 * 缩减未排序的序列,即可完成排序; * + * * @param nums nums元素的值是[1, 2, ..., nums.length]的一个排列 * @return */ diff --git a/[0979][Distribute Coins in Binary Tree]/src/Solution.java b/[0979][Distribute Coins in Binary Tree]/src/Solution.java index 8819f14..fac903d 100644 --- a/[0979][Distribute Coins in Binary Tree]/src/Solution.java +++ b/[0979][Distribute Coins in Binary Tree]/src/Solution.java @@ -1,5 +1,6 @@ /** * https://leetcode.com/problems/distribute-coins-in-binary-tree/ + * * @author: wangjunchao(王俊超) * @time: 2019-07-09 18:52 **/ @@ -17,6 +18,7 @@ public class Solution { * * https://www.cnblogs.com/seyjs/p/10369614.html * + * * @param root * @return */ @@ -32,6 +34,7 @@ public int distributeCoins(TreeNode root) { /** * 自底向上,对每一个节点,只能从父结点借,或者向父节点上交coin + * * @param node * @param parent */ diff --git a/[0979][Distribute Coins in Binary Tree]/src/TreeNode.java b/[0979][Distribute Coins in Binary Tree]/src/TreeNode.java index 36ec099..5554849 100644 --- a/[0979][Distribute Coins in Binary Tree]/src/TreeNode.java +++ b/[0979][Distribute Coins in Binary Tree]/src/TreeNode.java @@ -3,7 +3,7 @@ * @time: 2019-07-09 18:52 **/ public class TreeNode { - int val; + int val; TreeNode left; TreeNode right; diff --git a/[1089][Duplicate Zeros]/src/Solution.java b/[1089][Duplicate Zeros]/src/Solution.java index 9975463..701084f 100644 --- a/[1089][Duplicate Zeros]/src/Solution.java +++ b/[1089][Duplicate Zeros]/src/Solution.java @@ -6,7 +6,7 @@ public class Solution { public void duplicateZeros(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { if (arr[i] == 0) { - move(arr, i+1); + move(arr, i + 1); arr[i + 1] = 0; i++; } diff --git a/[1108][Defanging an IP Address]/src/Solution.java b/[1108][Defanging an IP Address]/src/Solution.java index 96d080d..8667b05 100644 --- a/[1108][Defanging an IP Address]/src/Solution.java +++ b/[1108][Defanging an IP Address]/src/Solution.java @@ -1,5 +1,6 @@ /** * https://leetcode.com/problems/defanging-an-ip-address/submissions/ + * * @author: wangjunchao(王俊超) * @time: 2019-07-12 08:31 **/ @@ -21,11 +22,12 @@ public class Solution { * Input: address = "255.100.50.0" * Output: "255[.]100[.]50[.]0" * + * * @param address * @return */ public String defangIPaddr(String address) { - if (address ==null || address.length() < 1) { + if (address == null || address.length() < 1) { return address; }