(Easy) Set Mismatch -LeetCode

Description:

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:

  1. The given array size will in the range [2, 10000].
  2. The given array‘s numbers won‘t have any order.

Accepted

52,621

Submissions

128,173

Solution:

class Solution {
    public int[] findErrorNums(int[] nums) {

        if(nums ==null||nums.length==0){
            return null;
        }

        int [] res = new int[2];

        for(int i = 1; i<=nums.length; i++){

            if(count(nums, i)==2){

                res[0]= i;

            }

            else if( count(nums,i)==0){

                res[1] =i;
            }
        }

        return res;

    }

    public int count(int[] nums, int a){

        int num = 0; 

        for(int i = 0; i<nums.length ; i++){

            if(nums[i]==a){

                num++;
            }
        }

        return num;
    }
}

原文地址:https://www.cnblogs.com/codingyangmao/p/11401854.html

时间: 2024-10-09 20:45:51

(Easy) Set Mismatch -LeetCode的相关文章

645. Set Mismatch - LeetCode

Question 645.?Set Mismatch Solution 思路: 遍历每个数字,然后将其应该出现的位置上的数字变为其相反数,这样如果我们再变为其相反数之前已经成负数了,说明该数字是重复数,将其将入结果res中,然后再遍历原数组,如果某个位置上的数字为正数,说明该位置对应的数字没有出现过,加入res中即可 Java实现: public int[] findErrorNums(int[] nums) { /* int a = 0; for (int i : nums) { if (nu

(Easy) House Robber LeetCode

class Solution { public int rob(int[] nums) { if(nums.length<=0 || nums ==null){ return 0; } if( nums.length ==1){ return nums[0]; } if(nums.length ==2){ return Max(nums[0],nums[1]); } int[] dp = new int[nums.length]; dp[0] = nums[0]; dp[1] = Max(num

(Easy) Valid Palindrome -LeetCode

Description: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: "A man, a plan, a canal: P

(Easy) Valid Boomerang - LeetCode

Description: A boomerang is a set of 3 points that are all distinct and not in a straight line. Given a list of three points in the plane, return whether these points are a boomerang. Example 1: Input: [[1,1],[2,3],[3,2]] Output: true Example 2: Inpu

(Easy) Arranging Coins - LeetCode

Description: You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within

(Easy) Ransom Note - LeetCode

Description: Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. Each l

No.006:ZigZag Conversion

题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R Write the code that will take a string a

No.008:String to Integer (atoi)

题目: Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. 官方难度: Easy 翻译: 实现atoi算法,将一个字符串转成整数. 提示:仔细考虑各种可能情况. 补充资料: atoi函数是C语言库中的一个函数,百度了一下其实现的功能如下: Requirements for atoi:1.The function first discards as m

No.009:Palindrome Number

题目: Determine whether an integer is a palindrome. Do this without extra space. 官方难度: Easy 翻译: 判断一个整数是否为回文结构,不使用额外空间. 思路: 1.回文结构的整数,与之前回文字符串类似,形如1,121,34543,345543的结构. 2.不使用额外空间,表名要从数字而不是字符串的角度来考虑问题. 3.负数不在讨论范围. 4.确定最高位,遍历至maxLevel/2处,检查对称位置即可. 解题中可能遇