leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit d39c61b0d9917f98e23684b32c00e4cea321ec40
parent 02dc98ae31f5f50ea6825710232ca2c153a2e0e6
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Mon, 29 May 2023 10:26:10 -0500

Completed first missing positive using dart and a set

Diffstat:
Afirst-missing-positive/first-missing-positive.dart | 24++++++++++++++++++++++++
1 file changed, 24 insertions(+), 0 deletions(-)

diff --git a/first-missing-positive/first-missing-positive.dart b/first-missing-positive/first-missing-positive.dart @@ -0,0 +1,24 @@ +//Given a list nums return the lowest positive integer that does not appear +//in the nums list. +//To solve this I first created a set and placed all values of the nums list into it. +//Then I went through all positive numbers until finding the first one that does not appear in the set. +//At that point I return the value that was not found. +//Time: 295ms Beats: 100% +//Memory: 178MB Beats: 50% + +class Solution { + int firstMissingPositive(List<int> nums) { + Set<int> vals = {}; + for(int i = 0 ; i < nums.length ; ++i){ + vals.add(nums[i]); + } + int itr = 1; + while(true){ + if(!vals.contains(itr)){ + return itr; + } + itr += 1; + } + return 0; + } +}