zoukankan      html  css  js  c++  java
  • JZ3 数组中重复的数字

    描述

    在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任一一个重复的数字。 例如,如果输入长度为7的数组[2,3,1,0,2,5,3],那么对应的输出是2或者3。存在不合法的输入的话输出-1
     
    数据范围:
    进阶:时间复杂度,空间复杂度
     

    示例1

    输入:
    [2,3,1,0,2,5,3]
    返回值:
    2
    说明:
    2或3都是对的  

    struct Solution{
    
    }
    
    impl Solution {
        fn new() -> Self {
            Solution{}
        }
    
        /**
        * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
        *
        * 
            * @param numbers int整型一维数组 
            * @return int整型
        */
        pub fn duplicate(&self, numbers: Vec<i32>) -> i32 {
            // write code here
            if numbers.len() == 0 {
                return -1;
            }
            
            let mut nums:Vec<i32> = numbers;
            for i in 0..nums.len() {
                while nums[i]  != i as i32 {
                    if nums[i] == nums[nums[i] as usize] {
                        return nums[i];
                    }
                    let tmp = nums[i] as usize;
                    nums.swap(tmp, i);
                }
            }
            return -1;
        }
    }





  • 相关阅读:
    promise请求数据(all方法)
    右键的点击事件
    微信小程序的接口调用封装
    微信小程序HTTP接口请求封装
    微信小程序得路由跳转
    管理系统得操作与解决思路
    HTTP协议
    动态语言概述
    AsynclAwait
    三种跨域解决方案
  • 原文地址:https://www.cnblogs.com/dingxiaoqiang/p/15614728.html
Copyright © 2011-2022 走看看