从连续整数序列[1,2,3...n+1] 中抽掉一个整数,然后将剩余的n个整数随机打乱,如何快速找出被抽掉的那个整数。
arr: 从[1,2,3...n+1] 整数序列中抽掉一个整数后剩余的整数序列 n: 数组长度
被抽掉的整数
arr=[1,3,4,5] n=5
2
import java.util.*; public class Main { public int solution(int[] arr, int n) { int result = 0; int sum = 0; for(int i=1; i<n+1; i++){ sum += i; } int temp = 0; for(int i=0; i<arr.length; i++){ temp += arr[i]; } result = sum - temp; return result; } }