-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problems: No.10031+ (#2189)
- Loading branch information
Showing
24 changed files
with
723 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...0000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution { | ||
public: | ||
int missingInteger(vector<int>& nums) { | ||
int s = nums[0], j = 1; | ||
while (j < nums.size() && nums[j] == nums[j - 1] + 1) { | ||
s += nums[j++]; | ||
} | ||
bool vis[51]{}; | ||
for (int x : nums) { | ||
vis[x] = true; | ||
} | ||
for (int x = s;; ++x) { | ||
if (x >= 51 || !vis[x]) { | ||
return x; | ||
} | ||
} | ||
} | ||
}; |
15 changes: 15 additions & 0 deletions
15
...10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
func missingInteger(nums []int) int { | ||
s, j := nums[0], 1 | ||
for j < len(nums) && nums[j] == nums[j-1]+1 { | ||
s, j = s+nums[j], j+1 | ||
} | ||
vis := [51]bool{} | ||
for _, x := range nums { | ||
vis[x] = true | ||
} | ||
for x := s; ; x++ { | ||
if x >= len(vis) || !vis[x] { | ||
return x | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public int missingInteger(int[] nums) { | ||
int s = nums[0], j = 1; | ||
while (j < nums.length && nums[j] == nums[j - 1] + 1) { | ||
s += nums[j++]; | ||
} | ||
boolean[] vis = new boolean[51]; | ||
for (int x : nums) { | ||
vis[x] = true; | ||
} | ||
for (int x = s;; ++x) { | ||
if (x >= vis.length || !vis[x]) { | ||
return x; | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution: | ||
def missingInteger(self, nums: List[int]) -> int: | ||
s, n = nums[0], len(nums) | ||
j = 1 | ||
while j < len(nums) and nums[j] == nums[j - 1] + 1: | ||
s += nums[j] | ||
j += 1 | ||
vis = set(nums) | ||
for x in count(s): | ||
if x not in vis: | ||
return x |
16 changes: 16 additions & 0 deletions
16
...10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function missingInteger(nums: number[]): number { | ||
let [s, j] = [nums[0], 1]; | ||
const n = nums.length; | ||
while (j < n && nums[j] === nums[j - 1] + 1) { | ||
s += nums[j++]; | ||
} | ||
const vis: boolean[] = Array(51).fill(false); | ||
for (const x of nums) { | ||
vis[x] = true; | ||
} | ||
for (let x = s; ; ++x) { | ||
if (x >= vis.length || !vis[x]) { | ||
return x; | ||
} | ||
} | ||
} |
Oops, something went wrong.