zoukankan      html  css  js  c++  java
  • 442. 数组中重复的数据&&448. 找到所有数组中消失的数字

    442. 数组中重复的数据

    给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。

    找到所有出现两次的元素。

    你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗?

    示例:

    输入:
    [4,3,2,7,8,2,3,1]

    输出:
    [2,3]

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/find-all-duplicates-in-an-array
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    代码1:

    class Solution {
    public:
    	void swap(int& a, int& b)
       	{
       	    int temp = a;
        	a = b;
       		b = temp;
       	}
        vector<int> findDuplicates(vector<int>& nums) {
            int len = nums.size();
    	   	int pos = 0;
    	   	while(pos < len)
    	   	{
    	   		if(nums[pos]> 0 && nums[pos] < len+1 
    			   && nums[pos] != pos + 1 && nums[pos]!= nums[nums[pos]-1])
    			{
    	    	    swap(nums[pos],nums[nums[pos]-1]);
    	   		}
    	   		else
    	   		{
    	   			++pos;
        		}
    	    }
    	    
    		/*pos = len -1;
            while(pos >= 0)
        	{
        		if(nums[pos]> 0 && nums[pos] < len+1 
    			   && nums[pos] != pos + 1 && nums[pos]!= nums[nums[pos]-1])
    			{
    	    	    swap(nums[pos],nums[nums[pos]-1]);
    	   		}
    	   		else
    	   		{
    	   			--pos;
        		}
    		}*/
    	    
    	    vector<int> ret;
    	    for(int i = 0; i < len; i++)
    	   	{
    	   		if(nums[i] != i + 1)
    	   		{
    	   		    ret.push_back(nums[i]);
        		}
    		}
    	   	return ret;
        }
    };

    448. 找到所有数组中消失的数字

    给你一个含 n 个整数的数组 nums ,其中 nums[i] 在区间 [1, n] 内。请你找出所有在 [1, n] 范围内但没有出现在 nums 中的数字,并以数组的形式返回结果。

    示例 1:

    输入:nums = [4,3,2,7,8,2,3,1]
    输出:[5,6]
    示例 2:

    输入:nums = [1,1]
    输出:[2]
     

    提示:

    n == nums.length
    1 <= n <= 105
    1 <= nums[i] <= n
    进阶:你能在不使用额外空间且时间复杂度为 O(n) 的情况下解决这个问题吗? 你可以假定返回的数组不算在额外空间内。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    方法可以和442代码1一样

    方法2:

    class Solution {
    public:
        vector<int> findDisappearedNumbers(vector<int>& nums) {
            int n = nums.size();
            for (auto& num : nums) {
                int x = (num - 1) % n;
                nums[x] += n;
            }
            vector<int> ret;
            for (int i = 0; i < n; i++) {
                if (nums[i] <= n) {
                    ret.push_back(i + 1);
                }
            }
            return ret;
        }
    };
    
    作者:LeetCode-Solution
    链接:https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/solution/zhao-dao-suo-you-shu-zu-zhong-xiao-shi-d-mabl/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    

      

  • 相关阅读:
    解决点击状态栏时ScrollView自动滚动到初始位置失效办法
    如何设计用户、角色、权限表
    Subject的功能
    shiro授权的源码分析
    shiro之认证源码分析
    shiro配置
    JSONArray转JSONObject
    parameterType
    MyBatis:Parameter Maps collection does not contain value for 的问题解决
    mybatis报ORA-00911: 无效字符
  • 原文地址:https://www.cnblogs.com/qiaozhoulin/p/15058614.html
Copyright © 2011-2022 走看看