Skip to content
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

feat: update solutions to lc problems: No.0189,0238,0041 #3896

Merged
merged 2 commits into from
Dec 28, 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
102 changes: 43 additions & 59 deletions solution/0000-0099/0041.First Missing Positive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@ tags:
```python
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
def swap(i, j):
nums[i], nums[j] = nums[j], nums[i]

n = len(nums)
for i in range(n):
while 1 <= nums[i] <= n and nums[i] != nums[nums[i] - 1]:
swap(i, nums[i] - 1)
j = nums[i] - 1
nums[i], nums[j] = nums[j], nums[i]
for i in range(n):
if i + 1 != nums[i]:
if nums[i] != i + 1:
return i + 1
return n + 1
```
Expand All @@ -93,12 +91,12 @@ class Solution {
public int firstMissingPositive(int[] nums) {
int n = nums.length;
for (int i = 0; i < n; ++i) {
while (nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
while (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
swap(nums, i, nums[i] - 1);
}
}
for (int i = 0; i < n; ++i) {
if (i + 1 != nums[i]) {
if (nums[i] != i + 1) {
return i + 1;
}
}
Expand All @@ -121,12 +119,12 @@ public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n; ++i) {
while (nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
while (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
swap(nums[i], nums[nums[i] - 1]);
}
}
for (int i = 0; i < n; ++i) {
if (i + 1 != nums[i]) {
if (nums[i] != i + 1) {
return i + 1;
}
}
Expand All @@ -141,12 +139,12 @@ public:
func firstMissingPositive(nums []int) int {
n := len(nums)
for i := range nums {
for nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i]-1] {
for 0 < nums[i] && nums[i] <= n && nums[i] != nums[nums[i]-1] {
nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
}
}
for i, v := range nums {
if i+1 != v {
for i, x := range nums {
if x != i+1 {
return i + 1
}
}
Expand All @@ -159,18 +157,18 @@ func firstMissingPositive(nums []int) int {
```ts
function firstMissingPositive(nums: number[]): number {
const n = nums.length;
let i = 0;
while (i < n) {
const j = nums[i] - 1;
if (j === i || j < 0 || j >= n || nums[i] === nums[j]) {
i++;
} else {
for (let i = 0; i < n; i++) {
while (nums[i] >= 1 && nums[i] <= n && nums[i] !== nums[nums[i] - 1]) {
const j = nums[i] - 1;
[nums[i], nums[j]] = [nums[j], nums[i]];
}
}

const res = nums.findIndex((v, i) => v !== i + 1);
return (res === -1 ? n : res) + 1;
for (let i = 0; i < n; i++) {
if (nums[i] !== i + 1) {
return i + 1;
}
}
return n + 1;
}
```

Expand All @@ -180,21 +178,18 @@ function firstMissingPositive(nums: number[]): number {
impl Solution {
pub fn first_missing_positive(mut nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut i = 0;
while i < n {
let j = nums[i] - 1;
if (i as i32) == j || j < 0 || j >= (n as i32) || nums[i] == nums[j as usize] {
i += 1;
} else {
nums.swap(i, j as usize);
for i in 0..n {
while nums[i] > 0 && nums[i] <= n as i32 && nums[i] != nums[nums[i] as usize - 1] {
let j = nums[i] as usize - 1;
nums.swap(i, j);
}
}
for i in 0..n {
if nums[i] != (i + 1) as i32 {
return (i + 1) as i32;
}
}
(nums
.iter()
.enumerate()
.position(|(i, &v)| (v as usize) != i + 1)
.unwrap_or(n) as i32)
+ 1
return (n + 1) as i32;
}
}
```
Expand Down Expand Up @@ -231,56 +226,45 @@ public class Solution {
```c
int firstMissingPositive(int* nums, int numsSize) {
for (int i = 0; i < numsSize; ++i) {
while (nums[i] >= 1 && nums[i] <= numsSize && nums[i] != nums[nums[i] - 1]) {
swap(&nums[i], &nums[nums[i] - 1]);
while (nums[i] > 0 && nums[i] <= numsSize && nums[i] != nums[nums[i] - 1]) {
int j = nums[i] - 1;
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
for (int i = 0; i < numsSize; ++i) {
if (i + 1 != nums[i]) {
if (nums[i] != i + 1) {
return i + 1;
}
}
return numsSize + 1;
}

void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
```

#### PHP

```php
class Solution {
/**
* @param integer[] $nums
* @return integer
* @param Integer[] $nums
* @return Integer
*/

function firstMissingPositive($nums) {
$n = count($nums);

for ($i = 0; $i < $n; $i++) {
if ($nums[$i] <= 0) {
$nums[$i] = $n + 1;
}
}

for ($i = 0; $i < $n; $i++) {
$num = abs($nums[$i]);
if ($num <= $n) {
$nums[$num - 1] = -abs($nums[$num - 1]);
while ($nums[$i] >= 1 && $nums[$i] <= $n && $nums[$i] != $nums[$nums[$i] - 1]) {
$j = $nums[$i] - 1;
$t = $nums[$i];
$nums[$i] = $nums[$j];
$nums[$j] = $t;
}
}

for ($i = 0; $i < $n; $i++) {
if ($nums[$i] > 0) {
if ($nums[$i] != $i + 1) {
return $i + 1;
}
}

return $n + 1;
}
}
Expand Down
102 changes: 43 additions & 59 deletions solution/0000-0099/0041.First Missing Positive/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,13 @@ The time complexity is $O(n)$, where $n$ is the length of the array. The space c
```python
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
def swap(i, j):
nums[i], nums[j] = nums[j], nums[i]

n = len(nums)
for i in range(n):
while 1 <= nums[i] <= n and nums[i] != nums[nums[i] - 1]:
swap(i, nums[i] - 1)
j = nums[i] - 1
nums[i], nums[j] = nums[j], nums[i]
for i in range(n):
if i + 1 != nums[i]:
if nums[i] != i + 1:
return i + 1
return n + 1
```
Expand All @@ -95,12 +93,12 @@ class Solution {
public int firstMissingPositive(int[] nums) {
int n = nums.length;
for (int i = 0; i < n; ++i) {
while (nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
while (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
swap(nums, i, nums[i] - 1);
}
}
for (int i = 0; i < n; ++i) {
if (i + 1 != nums[i]) {
if (nums[i] != i + 1) {
return i + 1;
}
}
Expand All @@ -123,12 +121,12 @@ public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n; ++i) {
while (nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
while (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
swap(nums[i], nums[nums[i] - 1]);
}
}
for (int i = 0; i < n; ++i) {
if (i + 1 != nums[i]) {
if (nums[i] != i + 1) {
return i + 1;
}
}
Expand All @@ -143,12 +141,12 @@ public:
func firstMissingPositive(nums []int) int {
n := len(nums)
for i := range nums {
for nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i]-1] {
for 0 < nums[i] && nums[i] <= n && nums[i] != nums[nums[i]-1] {
nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
}
}
for i, v := range nums {
if i+1 != v {
for i, x := range nums {
if x != i+1 {
return i + 1
}
}
Expand All @@ -161,18 +159,18 @@ func firstMissingPositive(nums []int) int {
```ts
function firstMissingPositive(nums: number[]): number {
const n = nums.length;
let i = 0;
while (i < n) {
const j = nums[i] - 1;
if (j === i || j < 0 || j >= n || nums[i] === nums[j]) {
i++;
} else {
for (let i = 0; i < n; i++) {
while (nums[i] >= 1 && nums[i] <= n && nums[i] !== nums[nums[i] - 1]) {
const j = nums[i] - 1;
[nums[i], nums[j]] = [nums[j], nums[i]];
}
}

const res = nums.findIndex((v, i) => v !== i + 1);
return (res === -1 ? n : res) + 1;
for (let i = 0; i < n; i++) {
if (nums[i] !== i + 1) {
return i + 1;
}
}
return n + 1;
}
```

Expand All @@ -182,21 +180,18 @@ function firstMissingPositive(nums: number[]): number {
impl Solution {
pub fn first_missing_positive(mut nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut i = 0;
while i < n {
let j = nums[i] - 1;
if (i as i32) == j || j < 0 || j >= (n as i32) || nums[i] == nums[j as usize] {
i += 1;
} else {
nums.swap(i, j as usize);
for i in 0..n {
while nums[i] > 0 && nums[i] <= n as i32 && nums[i] != nums[nums[i] as usize - 1] {
let j = nums[i] as usize - 1;
nums.swap(i, j);
}
}
for i in 0..n {
if nums[i] != (i + 1) as i32 {
return (i + 1) as i32;
}
}
(nums
.iter()
.enumerate()
.position(|(i, &v)| (v as usize) != i + 1)
.unwrap_or(n) as i32)
+ 1
return (n + 1) as i32;
}
}
```
Expand Down Expand Up @@ -233,56 +228,45 @@ public class Solution {
```c
int firstMissingPositive(int* nums, int numsSize) {
for (int i = 0; i < numsSize; ++i) {
while (nums[i] >= 1 && nums[i] <= numsSize && nums[i] != nums[nums[i] - 1]) {
swap(&nums[i], &nums[nums[i] - 1]);
while (nums[i] > 0 && nums[i] <= numsSize && nums[i] != nums[nums[i] - 1]) {
int j = nums[i] - 1;
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
for (int i = 0; i < numsSize; ++i) {
if (i + 1 != nums[i]) {
if (nums[i] != i + 1) {
return i + 1;
}
}
return numsSize + 1;
}

void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
```

#### PHP

```php
class Solution {
/**
* @param integer[] $nums
* @return integer
* @param Integer[] $nums
* @return Integer
*/

function firstMissingPositive($nums) {
$n = count($nums);

for ($i = 0; $i < $n; $i++) {
if ($nums[$i] <= 0) {
$nums[$i] = $n + 1;
}
}

for ($i = 0; $i < $n; $i++) {
$num = abs($nums[$i]);
if ($num <= $n) {
$nums[$num - 1] = -abs($nums[$num - 1]);
while ($nums[$i] >= 1 && $nums[$i] <= $n && $nums[$i] != $nums[$nums[$i] - 1]) {
$j = $nums[$i] - 1;
$t = $nums[$i];
$nums[$i] = $nums[$j];
$nums[$j] = $t;
}
}

for ($i = 0; $i < $n; $i++) {
if ($nums[$i] > 0) {
if ($nums[$i] != $i + 1) {
return $i + 1;
}
}

return $n + 1;
}
}
Expand Down
Loading
Loading