找到数组中相同数字之间的最大跨度 [英] Find the largest span between the same number in an array

查看:178
本文介绍了找到数组中相同数字之间的最大跨度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



圣诞快乐,希望你精神振奋,我在Java-Arrays中有一个问题,如下图所示。我很挣扎着努力获得它。


Merry Christmas and hope you are in great Spirits,I have a Question in Java-Arrays as shown below.Im stuck up with this struggling to get it rite.

考虑数组中某些值的最左侧和最右侧的外观。我们会说跨度是两者之间的元素数量。单个值的跨度为1.编写一个** Java函数**,它返回给定数组中找到的最大跨度。

**示例:

maxSpan({1,2,1,1,3})→4,答案是4 coz MaxSpan介于1到1之间是4

maxSpan({1 ,4,2,1,4,1,4})→6,答案是6考斯MaxSpan 4到4之间是6

maxSpan({1,4,2,1,4,4, 4})→6,答案是6 coz Maxspan在4到4之间是6,大于MaxSpan在1和1之间是4,因此6> 4答案是6.

**Example:
maxSpan({1, 2, 1, 1, 3}) → 4,answer is 4 coz MaxSpan between 1 to 1 is 4
maxSpan({1, 4, 2, 1, 4, 1, 4}) → 6,answer is 6 coz MaxSpan between 4 to 4 is 6
maxSpan({1, 4, 2, 1, 4, 4, 4}) → 6,answer is 6 coz Maxspan between 4 to 4 is 6 which is greater than MaxSpan between 1 and 1 which is 4,Hence 6>4 answer is 6.

我的代码不起作用,它包含给定元素的所有Spans,我无法找到给定元素的MaxSpan。

I have the code which is not working,it includes all the Spans for a given element,im unable to find the MaxSpan for a given element.

请帮助我出去了。

上述计划的结果如下所示

预计此次运行

maxSpan({1,2,1,1,3})→4 5 X

maxSpan({1,4,2,1,4, 1,4}}→6 8 X

maxSpan({1,4,2,1,4,4,4})→6 9 X

maxSpan({3, 3,3})→3 5 X

maxSpan({3,9,3})→3 3 OK

maxSpan({3,9,9})→2 3 X

maxSpan({3 ,9})→1 1 OK

maxSpan({3,3})→2 3 X

maxSpan({})→0 1 X

maxSpan({1})→1 1 OK

:: Code ::

::Code::

public int maxSpan(int[] nums) {    
    int count=1;//keep an intial count of maxspan=1    
    int maxspan=0;//initialize maxspan=0    
    for(int i=0;i<nums.length;i++){    
        for(int j=i+1;j<nums.length;j++){    
              if(nums[i] == nums[j]){
                 //check to see if "i" index contents == "j" index contents    
                 count++;    //increment count
                 maxspan=count;  //make maxspan as your final count  
                 int number = nums[i]; //number=actual number for maxspan               
              }                            
        }       
    }    
  return maxspan+1; //return maxspan        
}    


推荐答案

我明白了您尝试的以下问题:

I see the following problems with your attempt:


  • 您的计数完全错误。您可以从 i j 中计算 count j - i + 1

  • Your count is completely wrong. You can instead calculate count from i and j: j - i + 1

你要重写 maxcount 只要您获得任何范围,那么您将最终得到最后范围,而不是最大范围。通过 maxspan = Math.max(maxspan,count);

You're overriding maxcount as soon as you get any span, so you're going to end up with the last span, not the maximum span. Fix it by going maxspan = Math.max(maxspan, count);.

修复它你可以删除它行 int number = nums [i]; ,因为你从不使用数字

You can remove the line int number = nums[i]; as you never use number.

返回 maxspan + 1中删除 +1 ;`如果你按照上面的提示。

Remove the +1 in the returnmaxspan+1;` if you follow my tips above.

如果数组中有任何值,则初始 maxspan 应该为1如果数组为空,则为0。

Initial maxspan should be 1 if there are any values in the array, but 0 if the array is empty.

这应该可以帮助您使其正常工作。请注意,您可以在数组的一次传递中执行此操作,但这可能会让您感觉太过分了。在考虑效率之前,集中精力让代码工作。

That should help you get it working. Note that you can do this in a single pass of the array, but that's probably stretching it too far for you. Concentrate on getting your code to work before considering efficiency.

这篇关于找到数组中相同数字之间的最大跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆