268 Missing Number
Posted tobeabetterpig
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了268 Missing Number相关的知识,希望对你有一定的参考价值。
268 Missing Number class Solution { public int missingNumber(int[] nums) { int target = nums.length * (nums.length + 1) / 2; int sum = 0; for(int i = 0; i < nums.length; i++){ sum += nums[i]; } int result = target - sum; return result; } } Bs class Solution { public int missingNumber(int[] nums) { Arrays.sort(nums); int left = 0; int right = nums.length - 1; while( left <= right){ int mid = left + (right - left) / 2; if(nums[mid] > mid){ right = mid - 1; }else{ left = mid + 1; } } return left; } }
以上是关于268 Missing Number的主要内容,如果未能解决你的问题,请参考以下文章