Skip to content

feat: add solutions to lc problems: No.3550,3551 #4411

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
May 18, 2025
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 @@ -81,32 +81,102 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3550.Sm

<!-- solution:start -->

### 方法一
### 方法一:枚举 + 数位和

我们可以从下标 $i = 0$ 开始,遍历数组中的每个元素 $x$,计算 $x$ 的数位和 $s$。如果 $s = i$,则返回下标 $i$。如果遍历完所有元素都没有找到满足条件的下标,则返回 -1。

时间复杂度 $o(n)$,其中 $n$ 是数组的长度。空间复杂度 $o(1)$,只使用了常数级别的额外空间。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def smallestIndex(self, nums: List[int]) -> int:
for i, x in enumerate(nums):
s = 0
while x:
s += x % 10
x //= 10
if s == i:
return i
return -1
```

#### Java

```java

class Solution {
public int smallestIndex(int[] nums) {
for (int i = 0; i < nums.length; ++i) {
int s = 0;
while (nums[i] != 0) {
s += nums[i] % 10;
nums[i] /= 10;
}
if (s == i) {
return i;
}
}
return -1;
}
}
```

#### C++

```cpp

class Solution {
public:
int smallestIndex(vector<int>& nums) {
for (int i = 0; i < nums.size(); ++i) {
int s = 0;
while (nums[i]) {
s += nums[i] % 10;
nums[i] /= 10;
}
if (s == i) {
return i;
}
}
return -1;
}
};
```

#### Go

```go
func smallestIndex(nums []int) int {
for i, x := range nums {
s := 0
for ; x > 0; x /= 10 {
s += x % 10
}
if s == i {
return i
}
}
return -1
}
```

#### TypeScript

```ts
function smallestIndex(nums: number[]): number {
for (let i = 0; i < nums.length; ++i) {
let s = 0;
for (; nums[i] > 0; nums[i] = Math.floor(nums[i] / 10)) {
s += nums[i] % 10;
}
if (s === i) {
return i;
}
}
return -1;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,102 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3550.Sm

<!-- solution:start -->

### Solution 1
### Solution 1: Enumeration + Digit Sum

We can start from index $i = 0$ and iterate through each element $x$ in the array, calculating the digit sum $s$ of $x$. If $s = i$, return the index $i$. If no such index is found after traversing all elements, return -1.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$, as only constant extra space is used.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def smallestIndex(self, nums: List[int]) -> int:
for i, x in enumerate(nums):
s = 0
while x:
s += x % 10
x //= 10
if s == i:
return i
return -1
```

#### Java

```java

class Solution {
public int smallestIndex(int[] nums) {
for (int i = 0; i < nums.length; ++i) {
int s = 0;
while (nums[i] != 0) {
s += nums[i] % 10;
nums[i] /= 10;
}
if (s == i) {
return i;
}
}
return -1;
}
}
```

#### C++

```cpp

class Solution {
public:
int smallestIndex(vector<int>& nums) {
for (int i = 0; i < nums.size(); ++i) {
int s = 0;
while (nums[i]) {
s += nums[i] % 10;
nums[i] /= 10;
}
if (s == i) {
return i;
}
}
return -1;
}
};
```

#### Go

```go
func smallestIndex(nums []int) int {
for i, x := range nums {
s := 0
for ; x > 0; x /= 10 {
s += x % 10
}
if s == i {
return i
}
}
return -1
}
```

#### TypeScript

```ts
function smallestIndex(nums: number[]): number {
for (let i = 0; i < nums.length; ++i) {
let s = 0;
for (; nums[i] > 0; nums[i] = Math.floor(nums[i] / 10)) {
s += nums[i] % 10;
}
if (s === i) {
return i;
}
}
return -1;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int smallestIndex(vector<int>& nums) {
for (int i = 0; i < nums.size(); ++i) {
int s = 0;
while (nums[i]) {
s += nums[i] % 10;
nums[i] /= 10;
}
if (s == i) {
return i;
}
}
return -1;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
func smallestIndex(nums []int) int {
for i, x := range nums {
s := 0
for ; x > 0; x /= 10 {
s += x % 10
}
if s == i {
return i
}
}
return -1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int smallestIndex(int[] nums) {
for (int i = 0; i < nums.length; ++i) {
int s = 0;
while (nums[i] != 0) {
s += nums[i] % 10;
nums[i] /= 10;
}
if (s == i) {
return i;
}
}
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def smallestIndex(self, nums: List[int]) -> int:
for i, x in enumerate(nums):
s = 0
while x:
s += x % 10
x //= 10
if s == i:
return i
return -1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function smallestIndex(nums: number[]): number {
for (let i = 0; i < nums.length; ++i) {
let s = 0;
for (; nums[i] > 0; nums[i] = Math.floor(nums[i] / 10)) {
s += nums[i] % 10;
}
if (s === i) {
return i;
}
}
return -1;
}
Loading