检查数组中的某些变量是否相等,然后将它们赋值给另一个数组。

huangapple go评论107阅读模式
英文:

Check if some variables in an array are equal and then assign those to another array

问题

我有这个一维数组,包含4个值。

例如:

  1. moves[] = { 2, 2, 3, 3 };

我希望能够检查数组中每个相等且最小的值。所以最终我希望我的新数组看起来是这样的:

  1. moves1[] = {2, 2};

如何在Java中实现这个?

英文:

I have this one-dimensional array, that holds 4 values.

For example:

  1. moves[] = { 2, 2, 3, 3 };

I want to be able to check each value in the array that is equal and the smallest. So at the end I want my new array to be like this:

  1. moves1[] = {2, 2};

How can I achieve this in Java?

答案1

得分: 0

循环遍历数组,在每次遇到新的较小值时,重置结果列表:

  1. public static void main(String[] args) {
  2. int moves[] = { 2, 2, 3, 3 };
  3. int lowest = Integer.MAX_VALUE;
  4. List<Integer> low = new ArrayList<>();
  5. for (int i = 0; i < moves.length; i++) {
  6. if (moves[i] < lowest) {
  7. low = new ArrayList<>();
  8. lowest = moves[i];
  9. }
  10. if (moves[i] == lowest)
  11. low.add(moves[i]);
  12. }
  13. Integer[] result = low.toArray(new Integer[low.size()]);
  14. }
英文:

Loop through the array, each time you encounter a new lower value, reset the result list:

  1. public static void main(String[] args) {
  2. int moves[] = { 2, 2, 3, 3 };
  3. int lowest=Integer.MAX_VALUE;
  4. List&lt;Integer&gt; low=new ArrayList&lt;&gt;();
  5. for(int i=0;i&lt;moves.length;i++) {
  6. if(moves[i]&lt;lowest)
  7. {
  8. low=new ArrayList&lt;&gt;();
  9. lowest=moves[i];
  10. }
  11. if (moves[i]==lowest)
  12. low.add(moves[i]);
  13. }
  14. Integer[] result=low.toArray(new Integer[low.size()]);
  15. }

答案2

得分: 0

以下解决方案不假设数组已排序,并且运行时复杂度为 O(n)。此外,它的内存占用非常低,因为它不会创建数组的副本来进行排序。

  1. public static int[] smallestAndEqual(int[] arr) {
  2. if (arr.length == 0) {
  3. return new int[0]; // 如果需要,可以缓存此结果
  4. }
  5. int smallest = Integer.MAX_VALUE;
  6. int count = 0;
  7. for (int i : arr) {
  8. if (i < smallest) {
  9. smallest = i;
  10. count = 1;
  11. } else if (i == smallest) {
  12. count++;
  13. }
  14. }
  15. int[] ints = new int[count];
  16. Arrays.fill(ints, smallest);
  17. return ints;
  18. }

示例:

  1. public static void main(String[] args) {
  2. int[] ints = {2, 2, 3, 3};
  3. int[] result = smallestAndEqual(ints);
  4. System.out.println(Arrays.toString(result));
  5. }

执行此示例时,输出为:

  1. [2, 2]
英文:

The following solution does not assume that the array is sorted and has a runtime complexity of O(n). Furthermore it has a very low memory footprint, since it does not create a copy of the array to sort it.

  1. public static int[] smallestAndEqual(int[] arr) {
  2. if (arr.length == 0) {
  3. return new int[0]; // can be cached if needed
  4. }
  5. int smallest = Integer.MAX_VALUE;
  6. int count = 0;
  7. for (int i : arr) {
  8. if (i &lt; smallest) {
  9. smallest = i;
  10. count = 1;
  11. } else if (i == smallest) {
  12. count++;
  13. }
  14. }
  15. int[] ints = new int[count];
  16. Arrays.fill(ints, smallest);
  17. return ints;
  18. }

An example:

  1. public static void main(String[] args) {
  2. int[] ints = {2, 2, 3, 3};
  3. int[] result = smallestAndEqual(ints);
  4. System.out.println(Arrays.toString(result));
  5. }

Which outputs the following line when executed:

  1. [2, 2]

huangapple
  • 本文由 发表于 2020年10月12日 23:04:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64320365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定