我需要编码问题的帮助。我希望找到答案的一些提示,但不是答案本身。计算非重复整数
样品输入看起来像这样3112
样品输出为2,因为整数不重复。
这里的代码
public static int lonelyInteger(int[] arr)
{
need to code this
}
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(System.in);
final String fileName = System.getenv("OUTPUT_PATH");
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
int res;
int _arr_size = Integer.parseInt(in.nextLine());
int[] _arr = new int[_arr_size];
int _arr_item;
for(int _arr_i = 0; _arr_i < _arr_size; _arr_i++)
{
_arr_item = Integer.parseInt(in.nextLine());
_arr[_arr_i] = _arr_item;
}
res = loneyInteger(_arr);
bw.write(String.valueOf(res));
bw.newLine();
bw.close();
}
你能给更多的样品测试案例?你给的那个对我来说没有意义。我不知道你是怎么得到的2. – 4castle
@ 4castle有两个不同的整数不重复自己(3和2)。 – Gendarme
只是一个概述:遍历每个数字。对于每个数字,看看它是否再次出现在数字中。如果不是,则将其中一个添加到索引计数器。 – Dando18