Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.10031+ (#2189)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Jan 7, 2024
1 parent 9d01a95 commit 07290a1
Show file tree
Hide file tree
Showing 24 changed files with 723 additions and 21 deletions.
2 changes: 1 addition & 1 deletion solution/0300-0399/0383.Ransom Note/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func canConstruct(ransomNote string, magazine string) bool {

```ts
function canConstruct(ransomNote: string, magazine: string): boolean {
const cnt = new Array(26).fill(0);
const cnt: number[] = Array(26).fill(0);
for (const c of magazine) {
++cnt[c.charCodeAt(0) - 97];
}
Expand Down
2 changes: 1 addition & 1 deletion solution/0300-0399/0383.Ransom Note/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func canConstruct(ransomNote string, magazine string) bool {

```ts
function canConstruct(ransomNote: string, magazine: string): boolean {
const cnt = new Array(26).fill(0);
const cnt: number[] = Array(26).fill(0);
for (const c of magazine) {
++cnt[c.charCodeAt(0) - 97];
}
Expand Down
2 changes: 1 addition & 1 deletion solution/0300-0399/0383.Ransom Note/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function canConstruct(ransomNote: string, magazine: string): boolean {
const cnt = new Array(26).fill(0);
const cnt: number[] = Array(26).fill(0);
for (const c of magazine) {
++cnt[c.charCodeAt(0) - 97];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,105 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

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
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```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++];
}
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;
}
}
}
}
```

### **C++**

```cpp

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;
}
}
}
};
```
### **Go**
```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
}
vis := [51]bool{}
for _, x := range nums {
vis[x] = true
}
for x := s; ; x++ {
if x >= len(vis) || !vis[x] {
return x
}
}
}
```

### **TypeScript**

```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;
}
for (let x = s; ; ++x) {
if (x >= vis.length || !vis[x]) {
return x;
}
}
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,103 @@
### **Python3**

```python

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
```

### **Java**

```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++];
}
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;
}
}
}
}
```

### **C++**

```cpp

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;
}
}
}
};
```
### **Go**
```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
}
vis := [51]bool{}
for _, x := range nums {
vis[x] = true
}
for x := s; ; x++ {
if x >= len(vis) || !vis[x] {
return x
}
}
}
```

### **TypeScript**

```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;
}
for (let x = s; ; ++x) {
if (x >= vis.length || !vis[x]) {
return x;
}
}
}
```

### **...**
Expand Down
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;
}
}
}
};
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
}
}
}
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;
}
}
}
}
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
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;
}
}
}
Loading

0 comments on commit 07290a1

Please sign in to comment.