CodingBat挑战:SameEnds流API解决方案

发布于 2025-02-06 02:36:41 字数 1026 浏览 1 评论 0原文

给定任务 sameEnds 来自 codingbat

如果n数字在数组开始和结束时的数字相同,则返回true。例如,使用{5、6、45、99、13、5、6},对于n = 0n = 2,末端相同,对于n = 1n = 3。您可以假设n在范围内0..nums.length包含。

  sameEnds([5,6,6,45,99,13,5,6],1)→false
sameEnds([[5,6,45,99,13,5,6],2)→true
sameEnds([5,6,45,99,13,5,6],3)→false
 

我解决这个问题的解决方案通过了绝大多数测试,但并非全部:

public boolean sameEnds(int[] nums, int len) {
  
  if (nums.length >= len * 2) {
    for (int i = 0, j = nums.length - 1 ; i < len && len > 0; i++, j--) {
       if (nums[i] != nums[j]) {
         return false;
       }
    }
  }
  
  return true;
}

我的问题如下:

  1. 为了解决我的解决方案,该怎么办?
  2. 是否可以使用 流API 来解决此任务?

Given the task sameEnds from CodingBat:

Return true if the group of N numbers at the start and end of the array are the same. For example, with {5, 6, 45, 99, 13, 5, 6}, the ends are the same for n=0 and n=2, and false for n=1 and n=3. You may assume that n is in the range 0..nums.length inclusive.

sameEnds([5, 6, 45, 99, 13, 5, 6], 1) → false
sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true
sameEnds([5, 6, 45, 99, 13, 5, 6], 3) → false

My solution to this problem passes the vast majority of the tests, but not all of them:

public boolean sameEnds(int[] nums, int len) {
  
  if (nums.length >= len * 2) {
    for (int i = 0, j = nums.length - 1 ; i < len && len > 0; i++, j--) {
       if (nums[i] != nums[j]) {
         return false;
       }
    }
  }
  
  return true;
}

My questions are the following:

  1. What can be done in order to fix my solution?
  2. Is it possible to solve this task using Stream API ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吐个泡泡 2025-02-13 02:36:41

您可以使用 allMatch() 操作以便使用流实现它。

该解决方案通过 codingbat 的所有测试用例:

public boolean sameEnds(int[] nums, int len) {
    return java.util.stream.IntStream.range(0, len)
        .allMatch(n -> nums[n] == nums[nums.length - (len - n)]);
}

对您的命令解决方案的解决方案可能看起来像这样:

public boolean sameEnds(int[] nums, int len) {
    for (int i = 0, j = nums.length - 1 - (len - 1); i < len; i++, j++) {
        if (nums[i] != nums[j]) {
            return false;
        }
    }
    return true;
}

我删除了包装,因为nums.length&gt; = len * 2 评估为false,这意味着需要比较重叠的子阵列,但这并不意味着这些 subsarrays 是相等的。

条件len&gt; 0是多余的,因为它可以保证在范围内[0,nums.length]

变量j表示尾巴中的位置 subaray 已初始化为nums.length -1-(len -1) - 最后有效的索引子阵列的长度。 循环的的所谓增量语句从j -更改为j ++

You can use allMatch() operation in order to implement it with streams.

This solution passes all test cases on CodingBat:

public boolean sameEnds(int[] nums, int len) {
    return java.util.stream.IntStream.range(0, len)
        .allMatch(n -> nums[n] == nums[nums.length - (len - n)]);
}

A fix to your imperative solution might look like this:

public boolean sameEnds(int[] nums, int len) {
    for (int i = 0, j = nums.length - 1 - (len - 1); i < len; i++, j++) {
        if (nums[i] != nums[j]) {
            return false;
        }
    }
    return true;
}

I removed the wrapping if condition because when nums.length >= len * 2 is evaluated to false it means that subarrays that need to be compared overlap, but it doesn't automatically mean that these subarrays are equal.

Condition len > 0 is redundant because it is guaranteed to be in the range [0,nums.length].

Variable j that denotes position in the tail subarray has been initialized to nums.length - 1 - (len - 1) - last valid index minus subarray's length. And the so-called increment statement of the for loop was changed from j-- to j++.

贩梦商人 2025-02-13 02:36:41

您可以使用内置方法 arrays.quars.equals 如果您使用的是Java 9或更高:

public boolean sameEnds(int[] nums, int len) {
    return Arrays.equals(nums, 0, len, nums, nums.length - len, nums.length);
}

You can use the built in method Arrays.equals if you are using Java 9 or higher:

public boolean sameEnds(int[] nums, int len) {
    return Arrays.equals(nums, 0, len, nums, nums.length - len, nums.length);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文