java - 如何压缩数组,以便不会出现连续的两个或多个值并被该一个值替换

标签 java arrays oop for-loop

 /**
     * squeeze() takes an array of ints. On completion the array contains the
     * same numbers, but wherever the array had two or more consecutive
     * duplicate numbers, they are replaced by one copy of the number. Hence,
     * after squeeze() is done, no two consecutive numbers in the array are the
     * same.
     * 
     * Any unused elements at the end of the array are set to -1.
     * 
     * For example, if the input array is [ 4 , 1 , 1 , 3 , 3 , 3 , 1 , 1 ], it
     * reads [ 4 , 1 , 3 , 1 , -1 , -1 , -1 , -1 ] after squeeze() completes.
     * @param ints
     *            the input array.
     */
    public static void squeeze(int[] ints) {
         // TODO: Fill in your solution here. Ours takes linear time and is less than 10 lines long,
        // not counting blank/comment lines or lines already present in this file.
       // ArrayList<Integer> k = new ArrayList<Integer>();

下面是我的代码,但由于某种原因逻辑不正确,它返回传递给函数的相同数组,而不是修改后的数组,我相信大多数逻辑是正确的,除非 continue 语句中有错误。每当数组中出现连续的相同索引时,就应该将其替换为该索引

        int deleted = 0;
        int i = 0;
            for(int j = 1; j < ints.length; j++) {  ?
                if(ints[i] == ints[j]) {
                    deleted++;
                    continue;
                }else {
                    //k.add(ints[i]);
                    i++;
                }
            }
            ints = new int[ints.length + deleted];
            for(int s = 0; s < ints.length; s++) {
                if(ints[s] == 0) {
                    ints[s] = -1;
                }
            }
    }

    /**
     * main() runs test cases on your squeeze() method. Prints summary
     * information on basic operations and halts with an error (and a stack
     * trace) if any of the tests fail.
     */

最佳答案

继续只是跳到 for 循环的末尾,跳过任何代码。也就是说,它开始下一次交互。 正如注释中指出的,您需要修改传入的数组,因为定义的方法中没有返回值。 考虑以下因素......

    int deleted = 0;
    for(int j = 0; j < (ints.length-1); j++) {  
        if (ints[j] == -1) // if -1 then we've reached the end of the original list
            break; // this breaks out of the loop even if the constraint is not met

        if(ints[j] == ints[j+1]) { // if consecutive entries are equal
           // shift all values down in the list
           for (int k = j+1; k < (ints.length-1); k++){
               ints[k] = ints[k+1];
           } // end loop k

           // set the last entry in the array to -1 since we moved everything down
           ints[ints.length-1] = -1;

           //back our loop up one so that we stay where we were in case there was 
           // more than two consecutive numbers that were the same
           j--; 
        }
    } // end loop j

    // display the result
    for (int i = 0; i < ints.length; i++)
        System.out.print(ints[i] + ", " );

关于java - 如何压缩数组,以便不会出现连续的两个或多个值并被该一个值替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59737228/

相关文章:

java - Hibernate - 修改父对象时对子对象(集合)进行级联修改

Java HashMap put() 实现。为什么不先检查引用文献?

ios - 从字符串中获取特定值并创建数组

JavaScript,无字符串

javascript - 根据数组对象中存在的键排序

oop - 比较存储库与提供者与服务

java - 如何在java spring中将propertysource设置为从云配置服务器获取的配置文件?

java - 无法读取 org.apache.maven.plugins :maven-resources-plugin:jar:3. 0.5 的 Artifact 描述符

java - 类型不匹配 : cannot convert from List<capture#2-of ? 将 IDto> 扩展为 List<FollowingResponseDto>

oop - 使用 extend() 和 include() 函数向类添加属性