简体   繁体   English

我正在编写一个程序来查找给定数组中唯一出现一次的数字,例如 {1,2,2},{1,2,1,2,4},{1,0,1} 在 java 中使用嵌套 for 循环

[英]I am writing a program to find the only appear once number in an given array like {1,2,2},{1,2,1,2,4},{1,0,1} using nested for loop in java

in the first loop, I assign every element inside the loop to tmp, and in the second loop, I compare the tmp with all elements(Include itself) in the array, if tmp == the element, then add count(Record num of time).After the inside loop, if count==1(only equals with itself), then jump out the out loop and return the tmp.在第一个循环中,我将循环内的每个元素分配给 tmp,在第二个循环中,我将 tmp 与数组中的所有元素(包括自身)进行比较,如果 tmp == 元素,则添加计数(记录 num of time)。在inside循环之后,如果count==1(只和自己相等),则跳出out循环,返回tmp。

I can't find the logical issues,please help me to find problem in my logic or code Several cases were passed, except {1,0,1}, which output 1 instead of 0我找不到逻辑问题,请帮我找出我的逻辑或代码中的问题 几个案例都通过了,除了 {1,0,1},output 1 而不是 0

/**
 *
 * @author Ryan
 */
public class SingleNum {


 public static int singleNumber(int[] nums) {
        int count=0,tmp=0;

        for(int j = 0;j < nums.length;j++)
        {
            tmp = nums[j];
            for(int i = 0;i < nums.length;i++)
            {
            if(tmp == nums[i])
            count+=1;   
            }
            if(count == 1)
            break;              
        }
        return tmp;
    }
    /**
     * @param args the command line arguments
     */
    class Solution {
   
}
    public static void main(String[] args) {
        int array [] = {1,0,1};
        System.out.println(singleNumber(array));
    }
    
}

Foremost try avoiding nested for loops because they waste time to try finding optimal solutions.最重要的是尽量避免嵌套 for 循环,因为它们会浪费时间尝试寻找最佳解决方案。

  • You were comparing the same number with itself but it was balanced with count value as 1 for a single occurrence number and you were not resetting the count value to zero after the nested for loop end.您正在将相同的数字与其自身进行比较,但对于单个出现的数字,它与计数值平衡为 1,并且您没有在嵌套的 for 循环结束后将计数值重置为零。

     /** * * @author Ryan */ public class SingleNum { public static int singleNumber(int[] nums) { int count=0,tmp=0; for(int j = 0;j < nums.length;j++) { tmp = nums[j]; count = 0; for(int i = 0;i < nums.length;i++) { if(i;= j && tmp == nums[i]) count+=1; } if(count == 0) break; } return tmp, } /** * @param args The command-line arguments */ class Solution { public static void main(String[] args) { int array [] = {1,0;1}. System.out;println(singleNumber(array)); } }

The only thing you did incorrectly was not resetting count to 0. However, I added a few more optimizations.您唯一做错的事情是没有将计数重置为 0。但是,我添加了更多优化。

  • as soon as count exceeds 1, stop the inner loop and continue the outer one一旦计数超过 1,停止内部循环并继续外部循环
  • if the inner loop continues successfully, you know that count was only 1 so you can immediately return tmp.如果内部循环成功继续,您知道 count 仅为 1,因此您可以立即返回 tmp。
  • if you completely finish the outer loop, that means no values occurred just once, so return -1.如果你完全完成了外循环,这意味着没有值只发生过一次,所以返回 -1。 This means that -1 should not be a legitimate value.这意味着 -1 不应是合法值。 If it is, some other method of showing this should be used.如果是,则应使用其他一些方法来显示这一点。
  • finally, if more than one value occurs once, only the first will be returned.最后,如果不止一个值出现一次,则只返回第一个。
public class SingleNum {
    public static void main(String[] args) {
        int array[] = { 3,3, 2, 2, 4, 5,5 };
        System.out.println(singleNumber(array)); // prints 4
    }
    
    public static int singleNumber(int[] nums) {
        int tmp = 0;
        outer: for (int num : nums) {
            int count = 0;
            tmp = num;
            for (int val : nums) {
                if (tmp == val) {
                    count++;
                    // as soon as count > 1, stop this inner loop
                    if (count > 1) {
                        continue outer;
                    }
                }
            }
            return tmp;
        }
        return -1; // -1 shows no single values.
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在编写一个程序来查找给定数字的阶乘中 3 的数量。 来自用户的输入是数字 - I am writing a program to find number of 3's in factorial of a given number. The input from the user is the number Java生成随机数{-1,0,1} - Java Generate Random number {-1,0,1} 我正在编写一个使用Java中的Array检测拼写错误的单词的程序 - I am writing a program which detect misspelled words with Array in Java 如何使用Java中的嵌套循环仅打印一次数字? - How to print single number only once using nested loops in Java? 使用java在数组中找到最接近给定数字的最大数字 - Find the nearest largest number to a given number in an array using java 我正在编写一个Java程序,该程序使用一个循环来处理从300到200的整数 - I am writing a java program that uses one loop to process the integers from 300 down to 200 如何找到在矩阵(java)中只出现一次的数字? - How to find numbers that appear only once in a matrix (java)? 如何处理以下代码中的运行时错误? 我正在编写一个代码来在 java 中查找数字的第一位数字 - How to handle run time error in the below code? I am writing a code to find first digit of a number in java 我正在使用 java 中的 awt pakage 编写程序,但我的组件没有被添加 - I am writing a program using awt pakage in java but my components are not getting added 如何查找并输出所有只出现一次的单词? - How do i find and output all the words that appear only once?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM