Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.1903~1905 (#2102)
Browse files Browse the repository at this point in the history
* No.1903.Largest Odd Number in String
* No.1904.The Number of Full Rounds You Have Played
* No.1905.Count Sub Islands
  • Loading branch information
yanglbme authored Dec 15, 2023
1 parent 5ad2c28 commit 07a0022
Show file tree
Hide file tree
Showing 18 changed files with 546 additions and 952 deletions.
54 changes: 35 additions & 19 deletions solution/1900-1999/1903.Largest Odd Number in String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@

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

从后往前遍历字符串中的每个数字,遇到奇数则直接返回结果。若遍历结束仍未遇到奇数,返回空字符串。
**方法一:逆序遍历**

我们可以从后往前遍历字符串,找到第一个奇数,然后返回从开头到该奇数的子字符串即可。如果不存在奇数,则返回空字符串。

时间复杂度 $O(n)$,其中 $n$ 是字符串 $num$ 的长度。忽略答案字符串的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -84,24 +88,6 @@ class Solution {
}
```

### **JavaScript**

```js
/**
* @param {string} num
* @return {string}
*/
var largestOddNumber = function (num) {
let n = num.length;
for (let j = n - 1; j >= 0; j--) {
if (num.charAt(j) & (1 == 1)) {
return num.slice(0, j + 1);
}
}
return '';
};
```

### **C++**

```cpp
Expand Down Expand Up @@ -133,6 +119,36 @@ func largestOddNumber(num string) string {
}
```

### **TypeScript**

```ts
function largestOddNumber(num: string): string {
for (let i = num.length - 1; ~i; --i) {
if (Number(num[i]) & 1) {
return num.slice(0, i + 1);
}
}
return '';
}
```

### **JavaScript**

```js
/**
* @param {string} num
* @return {string}
*/
var largestOddNumber = function (num) {
for (let i = num.length - 1; ~i; --i) {
if (Number(num[i]) & 1) {
return num.slice(0, i + 1);
}
}
return '';
};
```

### **...**

```
Expand Down
54 changes: 36 additions & 18 deletions solution/1900-1999/1903.Largest Odd Number in String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@

## Solutions

**Solution 1: Reverse Traversal**

We can traverse the string from the end to the beginning, find the first odd number, and then return the substring from the beginning to this odd number. If there is no odd number, return an empty string.

The time complexity is $O(n)$, where $n$ is the length of the string $num$. Ignoring the space consumption of the answer string, the space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -72,24 +78,6 @@ class Solution {
}
```

### **JavaScript**

```js
/**
* @param {string} num
* @return {string}
*/
var largestOddNumber = function (num) {
let n = num.length;
for (let j = n - 1; j >= 0; j--) {
if (num.charAt(j) & (1 == 1)) {
return num.slice(0, j + 1);
}
}
return '';
};
```

### **C++**

```cpp
Expand Down Expand Up @@ -121,6 +109,36 @@ func largestOddNumber(num string) string {
}
```

### **TypeScript**

```ts
function largestOddNumber(num: string): string {
for (let i = num.length - 1; ~i; --i) {
if (Number(num[i]) & 1) {
return num.slice(0, i + 1);
}
}
return '';
}
```

### **JavaScript**

```js
/**
* @param {string} num
* @return {string}
*/
var largestOddNumber = function (num) {
for (let i = num.length - 1; ~i; --i) {
if (Number(num[i]) & 1) {
return num.slice(0, i + 1);
}
}
return '';
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
* @return {string}
*/
var largestOddNumber = function (num) {
let n = num.length;
for (let j = n - 1; j >= 0; j--) {
if (num.charAt(j) & (1 == 1)) {
return num.slice(0, j + 1);
for (let i = num.length - 1; ~i; --i) {
if (Number(num[i]) & 1) {
return num.slice(0, i + 1);
}
}
return '';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function largestOddNumber(num: string): string {
for (let i = num.length - 1; ~i; --i) {
if (Number(num[i]) & 1) {
return num.slice(0, i + 1);
}
}
return '';
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@

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

转换为“分钟”进行计算。
**方法一:转换为分钟数**

我们可以将输入的字符串转换为分钟数 $a$ 和 $b$,如果 $a > b$,则说明跨越了午夜,需要将 $b$ 加上一天的分钟数 $1440$。

然后我们将 $a$ 向上取整到 $15$ 的倍数,将 $b$ 向下取整到 $15$ 的倍数,最后返回 $b$ 与 $a$ 的差值即可,注意要取 $0$ 和 $b - a$ 中的较大值。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -72,15 +78,15 @@

```python
class Solution:
def numberOfRounds(self, startTime: str, finishTime: str) -> int:
def get(s: str) -> int:
def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
def f(s: str) -> int:
return int(s[:2]) * 60 + int(s[3:])

start, finish = get(startTime), get(finishTime)
if start > finish:
finish += 24 * 60
start, finish = (start + 14) // 15, finish // 15
return max(0, finish - start)
a, b = f(loginTime), f(logoutTime)
if a > b:
b += 1440
a, b = (a + 14) // 15, b // 15
return max(0, b - a)
```

### **Java**
Expand All @@ -89,38 +95,19 @@ class Solution:

```java
class Solution {
public int numberOfRounds(String startTime, String finishTime) {
int start = get(startTime), finish = get(finishTime);
if (start > finish) {
finish += 24 * 60;
public int numberOfRounds(String loginTime, String logoutTime) {
int a = f(loginTime), b = f(logoutTime);
if (a > b) {
b += 1440;
}
start = (start + 14) / 15;
finish /= 15;
return Math.max(0, finish - start);
return Math.max(0, b / 15 - (a + 14) / 15);
}

private int get(String s) {
return Integer.parseInt(s.substring(0, 2)) * 60 + Integer.parseInt(s.substring(3));
}
}
```

### **TypeScript**

```ts
function numberOfRounds(startTime: string, finishTime: string): number {
let m1 = toMinutes(startTime),
m2 = toMinutes(finishTime);
if (m1 > m2) {
m2 += 24 * 60;
private int f(String s) {
int h = Integer.parseInt(s.substring(0, 2));
int m = Integer.parseInt(s.substring(3, 5));
return h * 60 + m;
}
let ans = Math.floor(m2 / 15) - Math.ceil(m1 / 15);
return ans > 0 ? ans : 0;
}

function toMinutes(time: string): number {
let [h, m] = time.split(':').map(Number);
return h * 60 + m;
}
```

Expand All @@ -129,46 +116,51 @@ function toMinutes(time: string): number {
```cpp
class Solution {
public:
int numberOfRounds(string startTime, string finishTime) {
int start = get(startTime), finish = get(finishTime);
if (start > finish) {
finish += 24 * 60;
int numberOfRounds(string loginTime, string logoutTime) {
auto f = [](string& s) {
int h, m;
sscanf(s.c_str(), "%d:%d", &h, &m);
return h * 60 + m;
};
int a = f(loginTime), b = f(logoutTime);
if (a > b) {
b += 1440;
}
start = (start + 14) / 15;
finish /= 15;
return max(0, finish - start);
}

private:
int get(string s) {
int a, b;
sscanf(s.c_str(), "%d:%d", &a, &b);
return a * 60 + b;
return max(0, b / 15 - (a + 14) / 15);
}
};
```
### **Go**
```go
func numberOfRounds(startTime string, finishTime string) int {
start, finish := get(startTime), get(finishTime)
if start > finish {
finish += 24 * 60
func numberOfRounds(loginTime string, logoutTime string) int {
f := func(s string) int {
var h, m int
fmt.Sscanf(s, "%d:%d", &h, &m)
return h*60 + m
}
start = (start + 14) / 15
finish /= 15
if start > finish {
return 0
a, b := f(loginTime), f(logoutTime)
if a > b {
b += 1440
}
return finish - start
return max(0, b/15-(a+14)/15)
}
```

func get(s string) int {
var a, b int
fmt.Sscanf(s, "%d:%d", &a, &b)
return a*60 + b
### **TypeScript**

```ts
function numberOfRounds(startTime: string, finishTime: string): number {
const f = (s: string): number => {
const [h, m] = s.split(':').map(Number);
return h * 60 + m;
};
let [a, b] = [f(startTime), f(finishTime)];
if (a > b) {
b += 1440;
}
return Math.max(0, Math.floor(b / 15) - Math.ceil(a / 15));
}
```

Expand Down
Loading

0 comments on commit 07a0022

Please sign in to comment.