Skip to content

feat: add solutions to lc problem: No.2996 #2203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:模拟 + 哈希表**

我们先求出数组 $nums$ 的最长顺序前缀和 $s$,然后从 $s$ 开始枚举整数 $x$,如果 $x$ 不在数组 $nums$ 中,那么 $x$ 就是答案。这里我们可以用哈希表来快速判断一个整数是否在数组 $nums$ 中。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。

<!-- tabs:start -->

### **Python3**
Expand All @@ -52,8 +58,7 @@
```python
class Solution:
def missingInteger(self, nums: List[int]) -> int:
s, n = nums[0], len(nums)
j = 1
s, j = nums[0], 1
while j < len(nums) and nums[j] == nums[j - 1] + 1:
s += nums[j]
j += 1
Expand All @@ -70,9 +75,9 @@ class Solution:
```java
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++];
int s = nums[0];
for (int j = 1; j < nums.length && nums[j] == nums[j - 1] + 1; ++j) {
s += nums[j];
}
boolean[] vis = new boolean[51];
for (int x : nums) {
Expand All @@ -93,13 +98,13 @@ class Solution {
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++];
int s = nums[0];
for (int j = 1; j < nums.size() && nums[j] == nums[j - 1] + 1; ++j) {
s += nums[j];
}
bool vis[51]{};
bitset<51> vis;
for (int x : nums) {
vis[x] = true;
vis[x] = 1;
}
for (int x = s;; ++x) {
if (x >= 51 || !vis[x]) {
Expand All @@ -114,9 +119,9 @@ public:

```go
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
s := nums[0]
for j := 1; j < len(nums) && nums[j] == nums[j-1]+1; j++ {
s += nums[j]
}
vis := [51]bool{}
for _, x := range nums {
Expand All @@ -134,17 +139,13 @@ func missingInteger(nums []int) int {

```ts
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;
let s = nums[0];
for (let j = 1; j < nums.length && nums[j] === nums[j - 1] + 1; ++j) {
s += nums[j];
}
const vis: Set<number> = new Set(nums);
for (let x = s; ; ++x) {
if (x >= vis.length || !vis[x]) {
if (!vis.has(x)) {
return x;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,20 @@

## Solutions

**Solution 1: Simulation + Hash Table**

First, we calculate the longest prefix sum $s$ of the array $nums$. Then, starting from $s$, we enumerate the integer $x$. If $x$ is not in the array $nums$, then $x$ is the answer. Here, we can use a hash table to quickly determine whether an integer is in the array $nums$.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def missingInteger(self, nums: List[int]) -> int:
s, n = nums[0], len(nums)
j = 1
s, j = nums[0], 1
while j < len(nums) and nums[j] == nums[j - 1] + 1:
s += nums[j]
j += 1
Expand All @@ -60,9 +65,9 @@ class Solution:
```java
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++];
int s = nums[0];
for (int j = 1; j < nums.length && nums[j] == nums[j - 1] + 1; ++j) {
s += nums[j];
}
boolean[] vis = new boolean[51];
for (int x : nums) {
Expand All @@ -83,13 +88,13 @@ class Solution {
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++];
int s = nums[0];
for (int j = 1; j < nums.size() && nums[j] == nums[j - 1] + 1; ++j) {
s += nums[j];
}
bool vis[51]{};
bitset<51> vis;
for (int x : nums) {
vis[x] = true;
vis[x] = 1;
}
for (int x = s;; ++x) {
if (x >= 51 || !vis[x]) {
Expand All @@ -104,9 +109,9 @@ public:

```go
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
s := nums[0]
for j := 1; j < len(nums) && nums[j] == nums[j-1]+1; j++ {
s += nums[j]
}
vis := [51]bool{}
for _, x := range nums {
Expand All @@ -124,17 +129,13 @@ func missingInteger(nums []int) int {

```ts
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;
let s = nums[0];
for (let j = 1; j < nums.length && nums[j] === nums[j - 1] + 1; ++j) {
s += nums[j];
}
const vis: Set<number> = new Set(nums);
for (let x = s; ; ++x) {
if (x >= vis.length || !vis[x]) {
if (!vis.has(x)) {
return x;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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++];
int s = nums[0];
for (int j = 1; j < nums.size() && nums[j] == nums[j - 1] + 1; ++j) {
s += nums[j];
}
bool vis[51]{};
bitset<51> vis;
for (int x : nums) {
vis[x] = true;
vis[x] = 1;
}
for (int x = s;; ++x) {
if (x >= 51 || !vis[x]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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
s := nums[0]
for j := 1; j < len(nums) && nums[j] == nums[j-1]+1; j++ {
s += nums[j]
}
vis := [51]bool{}
for _, x := range nums {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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++];
int s = nums[0];
for (int j = 1; j < nums.length && nums[j] == nums[j - 1] + 1; ++j) {
s += nums[j];
}
boolean[] vis = new boolean[51];
for (int x : nums) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Solution:
def missingInteger(self, nums: List[int]) -> int:
s, n = nums[0], len(nums)
j = 1
s, j = nums[0], 1
while j < len(nums) and nums[j] == nums[j - 1] + 1:
s += nums[j]
j += 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
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;
let s = nums[0];
for (let j = 1; j < nums.length && nums[j] === nums[j - 1] + 1; ++j) {
s += nums[j];
}
const vis: Set<number> = new Set(nums);
for (let x = s; ; ++x) {
if (x >= vis.length || !vis[x]) {
if (!vis.has(x)) {
return x;
}
}
Expand Down