首页 > 代码库 > leetcode First Missing Positive hashset简单应用

leetcode First Missing Positive hashset简单应用

 1 public class Solution { 2     public int firstMissingPositive(int[] A) { 3         HashSet<Integer> hash=new HashSet<Integer>(); 4         int count=0; 5         int sum=0; 6          7         for(int i:A) 8         { 9             if(i>0)10             {11                 hash.add(i);12             }13         }14          15          int beg=1;16          while(hash.contains(beg))17          {18              beg++;19              20              21          }22          23          return beg;24         25        26         27     }28 }
V
 1 public class Solution { 2     public int firstMissingPositive(int[] A) { 3         HashSet<Integer> hash=new HashSet<Integer>(); 4         int count=0; 5         int sum=0; 6          7         for(int i:A) 8         { 9             if(i>0)10             {11                 hash.add(i);12             }13         }14          15          int beg=1;16          while(hash.contains(beg))17          {18              beg++;19              20              21          }22          23          return beg;24         25        26         27     }28 }

 

w Code