我是靠谱客的博主 诚心小土豆,这篇文章主要介绍【LeetCode】【数组】剑指 Offer 56 - I. 数组中数字出现的次数,现在分享给大家,希望可以做个参考。

剑指 Offer 56 - I. 数组中数字出现的次数

class Solution {
public int[] singleNumbers(int[] nums) {
Arrays.sort(nums);
int[] ans = new int[2];
if(nums.length == 0){
return ans;
}
int i = 0,j = 1,k = 0; //双指针
while(j < nums.length-1){
if(nums[i] != nums[j]){
ans[k++] = nums[i];
i++;
j++;
}else{
i+=2;
j+=2;
}
}
if(nums[nums.length-1] != nums[nums.length-2]){
//用来判断是否最后一个数字是单独的情况
ans[1] = nums[nums.length-1];
}
return ans;
}
}

最后

以上就是诚心小土豆最近收集整理的关于【LeetCode】【数组】剑指 Offer 56 - I. 数组中数字出现的次数的全部内容,更多相关【LeetCode】【数组】剑指内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部