1process.stdin.resume(); 2process.stdin.setEncoding('utf-8'); 3let input = ''; 4 5process.stdin.on('data', (data) => { 6 input += data; 7}); 8 9process.stdin.on('end', () => { 10 let inputArray = input.trim().split(' '); 11 12 // 将输入数组转换为数字数组 13 let nums = inputArray.map(Number); 14 15 // 获取数组的长度 n 16 let n = nums.length; 17 18 // 使用 Set 来记录数组中的元素 19 let numSet = new Set(nums); 20 21 // 标记是否有缺失的数字以及缺失的数字的数量 22 let missing = false; 23 let missingCount = 0; 24 let missingNumber = -1; 25 26 // 遍历 [0, n) 查找缺失的数字 27 for (let i = 0; i < n; i++) { 28 if (!numSet.has(i)) { 29 missing = true; 30 missingCount++; 31 if (missingCount > 1) { 32 console.log(false); 33 process.exit(); 34 } 35 missingNumber = i; 36 } 37 } 38 39 // 根据缺失情况输出结果 40 if (missing && missingCount === 1) { 41 console.log(missingNumber); 42 } else { 43 console.log(true); 44 } 45 46 process.exit(); 47});
Copyright ©2010-2022 比特日记 All Rights Reserved.
Powered By 可尔物语