public static int[] first = { 3391, 3391, 3391, 0, -1036, -3676, -4840,
3798, 0, 0, -1350, -1350, 4320, -265, -265, 3222, 3222, -77, 0, 0,
-3344, -3018, 1479, -2085, -2085, -2085, -2085, -2085, 0, 3205,
-3775, 1165, 1165, 0, 0, 747, -1584, -3320, 3301, 0, -194, 1595,
1595, 0, 440, 722, 722, 722, 722, 3130, -2978, 4215, 4215, 4215,
766, 766, -157, -3206, 0, 0, -2630, 3348, 3348, 0, 0, -3942, -1545,
326, 326, 4901, -4048, 0, -1388, -1388, 0, 0, 0, 0, 4604, -2998, 0,
-963, 0, 2555, -3578, 875, 410, 410, 2610, -4244, -4244, -4244,
3474, 4073, -3865, -1618, -467, 4630, -2874, -2874, 1660, 1660,
4236, 4236, 0, -1532, 2544, -1869, -1869, 0, -4064, 3383, -1648,
-4642, -4090, -4090, -4090, 1611, -587, 0, 0, 0, 551, 551, 551,
551, 0, 1418, 2953, 3892, -547, -547, 1847, 1719, -3701, 2335,
3193, -4723, -3818, -3818, 1140, 1140, 4154, 3947, 0, 0, -2992,
-2992, 4351, 4351, 213, 213, 213, -2177, 3029, 1275, -1086, -4651,
-4651, 3380, 669, -3962, -3962, -3962, 0, 0, 0, 1764, 496, -4707,
1554, 4758, 0, 280, 3979, -4812, 1678, 43, -979, -2610, 725, 725,
4653, -2571, -2571, -4435, -4799, 4383, -2089, -2089, -2089, -2089,
-2089, 4735, -3959, 3860, -3702, 2334, -3329, -3329, -3329, 3786,
3786, -2057, 4172, 4172, -787, 31, 31, 1702, 3410, 3410, 2216,
-1739, 770, 770, -861, 2107, 2107, -2089, 2432, 2432, 2543, 3422,
2728, 2728, -1292, 3601, 0, 0, 1218, -1979, -2632, -2632, -214,
-214, 0, -770, 4007, -4358, -1776, -867, -3334, -4020, 997, 3607,
-794, 3828, 1740, 0, -38, -38, 870, 870, 870, 1691, 0, -3390, -290,
-290, 4400, -2152, -1433, -1433, -265, -265, -4259, -2589, 4895,
735, 735, 3246, -3913, -4163, -4163, 2084, -4086, -4086, 4486,
2632, 2614, 3069, -3298, -4401, -4881, 2646, 2646, -1323, 4204, 0,
0, -4828, 2662, 470, -3909, -3909, -787, -3675, -3675, -3861,
-3861, 0, 0, -3093, -1750, -59, -3406, -1558, 2399, 4791, 2491,
1287, 1287, 0, -3019, -1008, 0, 797, 1185, -439, -439, 0, 2470,
3553, -4848, 2519, -3162, -2911, -2556, 2229, 1412, 0, 0, -350, 0,
0, -3403, -3403, 3270, 3270, 2898, 2898, 2898, -1620, 2507, 1180,
1180, 384, -4952, 2221, 3447, 3447, 3447, 3447, 1725, -2329, -2329,
-902, 4760, 1092, 1092, 0, 420, 4324, 2048, 99, -3813, -1376,
-1376, -1376, -1376, 564, 3107, -852, 3953, 0, -364, 3388, -2440,
-2440, 1077, 0, 4340, -2646, 4644, -266, -409, 492, -3000, 1073,
-1851, 1950, 1950, -1583, 4937, 3840, 3840, 0, 0, 0, 0, -2505,
-1990, 1159, 1797, 0, 0, -146, 4521, -1095, -2776, 0, 2606, -3931,
-3931, -3931, 2557, 2557, 2557, 2579, 1438, -980, 498, 0, -3139,
4407, 4407, -9, 2287, 1633, -3942, -3942, -872, 2193, 0, 3894,
-1069, -1069, -3094, 3376, 3376, 0, 0, -3017, -3017, -4278, -445,
-4408, 2724, 0, 2678, 2398, -1388, 4676, 2958, 2958, 971, 3491,
3491, 3793, 531, 0, -1278, -3552, -4599, -3125, 2763, -2599, 4505,
-2964, -2964, 0, -1208, 0, 0, 3727, 3727, 0, 0, -562, 0, 1377,
-239, -2957, -4129, -4129, -4129, -3265, 3275, -4140, -4140, -4140,
2421, 2421, 2421, -1462, 1507, 1507, 1507, -4975, 2055, 341, 341,
-2183, -2183, -2183, 348, -2964, -2964, 967, 3650, 3650, 0, 545, 0,
-3443, 3970, -1816, 4905, 4905, -621, -621, -2103, 33, 1260, 1260,
-3546, -3546, -922, -3519, 0, 0, 2382, 3425, -3021, -922, 4183,
989, -1425, -1425, 0, 0, 0, 1784, 1784, 1784, 841, -616, -616,
4488, 3820, 3820, 3820, 3768, 1050, -680, 3757, 2482, -2635, 1408,
1408, -3100, -4242, -4242, -50, -50, -2013, -2013, -2013, -484,
-4434, -4434, -4434, 1562, 1562, 1100, -763, -2597, 3203, 3203,
3203, 2512, 2512, -1100, -1100, -1100, -1100, -1100, -1100, -3462,
-3859, -1716, 4833, -1843, -1843, 421, 867, 4661, 566, 566, 2885,
2885, 2176, -639, 4285, 4285, 4285, 3229, 3229, 0, 0, -3298, -3298,
0, -1452, -3456, -1922, -1370, 4485, 4485, 422, 422, 322, 2044,
3156, 1588, -2406, -2406, -2406, 2136, 4283, 2991, -969, 0, 0,
-369, -369, 0, 4202, 85, 85, 41, -1917, -1917, -773, -3183, 1245,
-1991, 2160, -2860, -2480, 2657, 2657, 2657, 2657, 4975, 760, 2750,
-3876, 3738, 1255, -3385, -1946, 0, -1511, -4376, -3181, -3830,
1862, 1862, -3265, -3265, -3265, 3379, 956, 4833, 4833, -969, 911,
421, 421, -4156, 4045, 2478, -4151, -2899, -2721, -2721, -2721,
-2399, -4976, 2447, 2447, 3534, 0, 0, -4856, -3308, 4592, 4592,
2969, 2969, -4851, 201, 47, -1960, -1960, -1960, -1960, 0, -1181,
-4296, 0, 0, 0, -87, 0, -4796, -1630, -3903, 720, 720, 0, 0, 4341,
4341, -911, -911, -2980, -2222, -2222, -2222, 0, 0, 0, -1198, 1609,
2435, 0, 1623, 1623, 0, 1256, -3594, 3234, -316, -2103, 3289, 0,
3765, 2156, 2156, 2158, 2158, 2158, -1958, -526, -2735, -2735, 98,
3157, 4056, 3613, -3940, -3940, 1548, 2735, -4805, -475, -4285,
-4115, 0, -806, 813, -2120, -2120, 2910, -1645, -1645, 926, 926,
-1873, -1873, 556, 556, -1857, 2339, 0, 4214, 3887, 2540, -4717,
3958, 3958, 3958, -963, -963, 0, 4882, -4490, -3035, -4224, -263,
3646, 894, 0, 0, 0, 2349, 2493, -1124, 1358, -4463, 3136, 1304,
-4621, -4621, -4621, 3845, 3845, -368, -4793, 1983, -1186, -1200,
4909, 4909, 4909, 3178, -3670, -42, -1968, 3403, 812, -38, -1570,
-2693, -674, -4884, 2048, 2276, 1820, 1820, 1820, 79, -1316, -834,
-477, 0, -1873, 3307, 4222, -234, 961, -2372, 4199, 4199, -1714,
4668, 4668, -1556, -1556, -4089, 4389, 4026, 0, -3158, -1678,
-1678, -2960, -2960, -2960, -3427, -3427, 4947, 0, 0, -138, -4103,
-3391, -3391, 0, 0, -3016, -3990, 4396, -1099, 3406, -3632, -2860,
69, 4315, 4690, -891, -48, 0, 1470, -3638, 0, 4735, 4735, 631, 631,
-4769, 2231, 2231, 2231, 0, 0, 0, 0, 1903, 0, 0, 0, 2195, 4142,
-1396, -1756, 2954, -4427, 178, 4857, 4813, 4813, 547, 547, 547,
2923, -4627, 1836, -3243, -3243, 3002, 3002, 2600, 2600, 2600, 971,
0, 0, -1, -1, 4351, 4351, 4351, 2351, -2209, -1771, 3061, -3381,
-3381, -4978, 923, -2470, 512, 512, 2468, 3987, 3987, 3987, 1993,
-3338, -3338, 0, -1696, 3087, 1165, -4491, -4146, -4146, -2412,
-4969, 938, 1351, -40, 0, 4035, -3848, 4345, 0, 1176, 2887, 4333,
4333, 4281, 4281, -354, -354, -371, 4692, 415, -990, -975, 3098,
3098, 0, 0, 0, 4626, };
public static void main(String[] args) {
System.out.println("firstIndexOfLongestRepeatingInteger() = "
+ firstIndexOfLongestRepeatingInteger(first, 0));
}
// returns the first index of the longest repeating sequence of x in input
// for example, if input = {0,0,1,1,1,0,1} and x = 1, then output = 2
public static int firstIndexOfLongestRepeatingInteger(int[] input, int x) { // x = 0
int firstIndexOfLongestRepeatingInteger = 0; // 74
for(int i = 0; i < input.length; i++){
int counter1 = 0;
if(input[i]== x){
counter1++;
firstIndexOfLongestRepeatingInteger = i;
}
int counter2= 0;
if(counter1 > counter2){
counter1 = firstIndexOfLongestRepeatingInteger;
}
}
return firstIndexOfLongestRepeatingInteger;
}`
这是我尝试查找索引的方法,但是这是我多次尝试后的结果,因为我不知道如何解决这个问题。我知道必须有两个计数器,当我发现有一个序列时,它们会停止并继续对数组进行排序,看看是否有更长的重复序列。谁能引导我朝正确的方向走?我知道结果必须是74但我无法得出这个结论。
答案 0 :(得分:0)
在解决诸如此类的问题时,您通常需要两组变量。到目前为止,用于跟踪最大重复次数和起始索引的变量,以及用于跟踪您当前正在查看的内容的集合(迭代)。
将这些变量移到循环范围之外。
此外,浏览代码的每一行并评论每个代码的作用。
如果您了解自己的代码以及如何解决问题,那么通常情况下,您将找出需要解决的问题。 你最好的朋友,rubber duck,是这么说的。