日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

查找数组中未出现的和出现2次的数值 Set Mismatch

發布時間:2025/3/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 查找数组中未出现的和出现2次的数值 Set Mismatch 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

問題:

The set?S?originally contains numbers from 1 to?n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to?another?number in the set, which results in repetition of one number and loss of another number.

Given an array?nums?representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.

Example 1:

Input: nums = [1,2,2,4] Output: [2,3]

Note:

  • The given array size will in the range [2, 10000].
  • The given array's numbers won't have any order.
  • 解決:

    ① ?使用一個標記數組標記每個值是否出現過,若存在重復,則將重復的值加入到結果中;再遍歷一次數組,得到唯一一個未標記的值,就是缺失的值。

    public class Solution { // 7ms
    ? ? public int[] findErrorNums(int[] nums) {
    ? ? ? ? int res[] = new int[2];
    ? ? ? ? boolean[] isOcc= new boolean[nums.length + 1];
    ? ? ? ? for (int i = 0;i < nums.length ;i ++ ) {
    ? ? ? ? ? ? if(isOcc[nums[i]]) res[0] = nums[i];//該數值已經出現過
    ? ? ? ? ? ? else isOcc[nums[i]] = true;
    ? ? ? ? }
    ? ? ? ? for (int i = 1;i <= nums.length ;i ++ ) {
    ? ? ? ? ? ? if(! isOcc[i]) res[1] = i; //i 沒出現過
    ? ? ? ? }
    ? ? ? ? return res;
    ? ? }
    }

    轉載于:https://my.oschina.net/liyurong/blog/1510776

    總結

    以上是生活随笔為你收集整理的查找数组中未出现的和出现2次的数值 Set Mismatch的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。