Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missi
来源:互联网 发布:java agent 做监控 编辑:程序博客网 时间:2024/05/17 22:32
<pre name="code" class="java">/*Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.</pre><pre name="code" class="java">利用等差数列求和公式即可,空间复杂度为1,算法复杂度为O(n)*/</pre><pre name="code" class="java">public class FindLost {public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println(new FindLost().missingNumber(new int[]{0,1,2,3,4,5,6}));} public int missingNumber(int[] nums) { int length=nums.length; int sum=0; for(int i=0;i<length;i++) { sum+=nums[i]; } return (length*length+length-2*sum)/2; }}
0 0
- Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missi
- Given an array of size n, find the majority element. The majority element is the element that appear
- find the N max numbers from an array
- 【LeetCode】169. Majority Element Majority Element Given an array of size n, find the majority eleme
- Given an array say [9,20,-2,-45,23,5,1], find the minimum positive missing number from the array.
- Given an array of integers, every element appears twice except for one. Find that single one.
- Given an array of integers, every element appears twice except for one. Find that single one.
- Given an array of integers, every element appears twice except for one. Find that single one.
- Given an array of integers, every element appears twice except for one. Find that single one.
- leetcode :Find the contiguous subarray within an array (containing at least one number) which has th
- You’re given an array containing both positive and negative integers and required to find the sub-a
- Find M Integers from an Array of Size N equally
- 448 Find All Numbers Disappeared in an Array找到[1,n]内没有出现的数字
- Given an array [a1b2c3d4] convert to [abcd1234] with 0(1) space and O(n) time
- Find a number in the array having least difference with the given number (n)
- Given an array of size N in which every number is between 1 and N, determine if there are any dupli
- CareerCup Given an array A[], find (i, j) such that A[i] < A[j] and (j - i) is maximum.
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- 串的模式匹配KMP算法
- osg节点访问和遍历
- SSH 公钥私钥认证
- 约瑟夫环--数组实现
- hdoj The sum problem 2058 (数学等差公式&技巧转换)
- Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missi
- Gradle for Android(二)全局设置、自定义BuildConfig、混淆
- 解决 RHEL 7/ CentOS 7/Fedora 出现Unit iptables.service failed to load
- acm_problem_1013
- 感兴趣区域检测技术
- 视图布局向上偏移的原因及解析
- iOS开发-进阶:NSArray倒序输出
- gem install sass 本地配置和淘宝源无效的解决办法
- Java 四种线程池的用法分析