Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
yanglbme merged 1 commit intomainfromdev
May 18, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
feat: add solutions to lc problems: No.3550,3551
  • Loading branch information
@yanglbme
yanglbme committedMay 18, 2025
commit427f79b6a70a4cc3c7375b49eea27ae0d491a8cc
View file
Open in desktop
Original file line numberDiff line numberDiff 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

classSolution:
defsmallestIndex(self,nums: List[int]) ->int:
for i, xinenumerate(nums):
s=0
while x:
s+= x%10
x//=10
if s== i:
return i
return-1
```

####Java

```java

classSolution {
publicintsmallestIndex(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

classSolution {
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) {
returni;
}
}
return-1;
}
```

<!-- tabs:end -->
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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

classSolution:
defsmallestIndex(self,nums: List[int]) ->int:
for i, xinenumerate(nums):
s=0
while x:
s+= x%10
x//=10
if s== i:
return i
return-1
```

####Java

```java

classSolution {
publicintsmallestIndex(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

classSolution {
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) {
returni;
}
}
return-1;
}
```

<!-- tabs:end -->
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
classSolution {
public:
intsmallestIndex(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;
}
};
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
funcsmallestIndex(nums []int)int {
fori,x:=rangenums {
s:=0
for ;x>0;x/=10 {
s+=x%10
}
ifs==i {
returni
}
}
return-1
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
classSolution {
publicintsmallestIndex(int[]nums) {
for (inti =0;i <nums.length; ++i) {
ints =0;
while (nums[i] !=0) {
s +=nums[i] %10;
nums[i] /=10;
}
if (s ==i) {
returni;
}
}
return -1;
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
classSolution:
defsmallestIndex(self,nums:List[int])->int:
fori,xinenumerate(nums):
s=0
whilex:
s+=x%10
x//=10
ifs==i:
returni
return-1
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
functionsmallestIndex(nums:number[]):number{
for(leti=0;i<nums.length;++i){
lets=0;
for(;nums[i]>0;nums[i]=Math.floor(nums[i]/10)){
s+=nums[i]%10;
}
if(s===i){
returni;
}
}
return-1;
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp