• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ying_vincent
博客园    首页    新随笔    联系   管理    订阅  订阅

LeetCode: Find All Numbers Disappeared in an Array

这题考的是桶排序,知道的很简单,不知道的话就很难

 1 public class Solution {
 2     public List<Integer> findDisappearedNumbers(int[] nums) {
 3         int a = 0;
 4         int b = 0;
 5         for (int i = 0; i < nums.length; i++) {
 6             while (nums[i] != nums[nums[i]-1]) {
 7                 a = nums[i];
 8                 b = nums[nums[i]-1];
 9                 nums[nums[i]-1] = a;
10                 nums[i] = b;
11             }
12         }
13         List<Integer> ans = new ArrayList<Integer>();
14         for (int i = 0; i < nums.length; i++) {
15             if (nums[i] != i+1) ans.add(i+1);
16         }
17         return ans;
18     }
19 }

 

posted @ 2017-03-15 10:12  ying_vincent  阅读(140)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3