268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n
, find the one that is missing from the array.
Example 1
Input: [3,0,1]
Output: 2
Example 2
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
我的版本:
package leetCode;import java.util.Arrays;/*** Created by luoluo on 2018/1/27.先进行快排,然后在判断*/
public class MissingNumber {public static void main(String[] args) {int[] a = {3,0,1};missingNumber(a);System.out.println(Arrays.toString(a));}public static int missingNumber(int[] nums) {int n = nums.length;sort(nums, 0, n-1);if(nums[0] != n ){return n;}else if(nums[nums.length-1] !=0){return 0;}for (int i = 0; i < nums.length-1; i++) {if(nums[i]-nums[i+1]!=1){return nums[i]-1;}}return 0;}public static void sort(int[] nums ,int bg,int end){if (bg >= end) {return;}int partition = partition(nums, bg, end);sort(nums, bg, partition - 1);sort(nums, partition + 1, end);}public static int partition(int[] nums,int bg, int end){int l = nums[bg];int j = bg;for (int i = bg+1; i <= end; i++) {if(nums[i]>l){j++;swap(nums, i, j);}}swap(nums, j, bg);return j;}public static void swap(int[] nums ,int a ,int b) {int temp = nums[a];nums[a] = nums[b];nums[b] = temp;}
}
大神的版本:
public static int missingNumber(int[] nums) {int sum = nums.length;for (int i = 0; i < nums.length; i++)sum += i - nums[i];return sum;
}
转载于:https://www.cnblogs.com/luozhiyun/p/8367149.html
268. Missing Number相关推荐
- Leet Code OJ 268. Missing Number [Difficulty: Medium]
题目: Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is miss ...
- 268. Missing Number(缺失数字)
题目地址:https://leetcode.com/problems/missing-number/description/ Given an array containing n distinct ...
- leetcode 268. Missing Number
原地哈希.代码没怎么优化,思路清晰. int missingNumber(vector<int>& nums) {int size = nums.size();for (int i ...
- 268 missing number
问题:给定一个包含n个不同数字的数组,从0开始,例如0,1,2,-n.找到数组中缺了那个数字.例如输入nums=[0,1,3],输出2. 思路:高斯 从0到n的和为sum=(1+n)*n/2.只要计算 ...
- LeetCode 268. Missing Number--Python解法--数学题
LeetCode 268. Missing Number–Python解法–数学题 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大部 ...
- PAT甲级1144 The Missing Number:[C++题解]哈希表
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 把所有的数放进一个hash表,然后从1开始遍历,看每个数是不是在hash表中,输出第一个不在表中的元素. ac代码 #include ...
- LeetCode之Missing Number
1.题目 Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is m ...
- 1144 The Missing Number (20 分)
Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...
- C#LeetCode刷题之#268-缺失数字(Missing Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4056 访问. 给定一个包含 0, 1, 2, ..., n 中 ...
最新文章
- Science:领导决策的计算和神经生物学基础
- nginx响应高并发参数配置
- gentoo doc web site
- openssl https 单向认证连接成功示例
- opencv笔记(3):图像镜像
- IIFE(立即执行函数表达式)
- Tokyo cabinet HDB– 初始化
- java实现线性回归(简单明了,适合理解)
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
- 浅谈Object Pascal的指针
- 电机控制初学入门资料_电机控制如何入门
- 看完这篇解决你99%的运维安全陋习,快别踩坑了!
- linux系统Nginx下limit_req模块burst参数超详细解析
- 小颗粒积木步骤图纸_loz小颗粒钻石积木拼图图纸谁有
- 基本面量化与美林时钟模型
- 归一化mysql函数_数据归一化和两种常用的归一化方法
- Win10 内置 OpenSSH 使用密钥连不上的问题解决
- 查看电脑内存个数、主频(工作频率)、容量、位宽等的方法总结
- su [user] 和 su - [user]的区别
- Android开发最近新闻和功能都在这里了