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

Commita3979f7

Browse files
authored
feat: add solutions to lc problems: No.3550,3551 (#4411)
1 parent29ee80c commita3979f7

File tree

14 files changed

+728
-14
lines changed

14 files changed

+728
-14
lines changed

‎solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/README.md

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,102 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3550.Sm
8181

8282
<!-- solution:start-->
8383

84-
###方法一
84+
###方法一:枚举 + 数位和
85+
86+
我们可以从下标 $i = 0$ 开始,遍历数组中的每个元素 $x$,计算 $x$ 的数位和 $s$。如果 $s = i$,则返回下标 $i$。如果遍历完所有元素都没有找到满足条件的下标,则返回 -1。
87+
88+
时间复杂度 $o(n)$,其中 $n$ 是数组的长度。空间复杂度 $o(1)$,只使用了常数级别的额外空间。
8589

8690
<!-- tabs:start-->
8791

8892
####Python3
8993

9094
```python
91-
95+
classSolution:
96+
defsmallestIndex(self,nums: List[int]) ->int:
97+
for i, xinenumerate(nums):
98+
s=0
99+
while x:
100+
s+= x%10
101+
x//=10
102+
if s== i:
103+
return i
104+
return-1
92105
```
93106

94107
####Java
95108

96109
```java
97-
110+
classSolution {
111+
publicintsmallestIndex(int[]nums) {
112+
for (int i=0; i< nums.length;++i) {
113+
int s=0;
114+
while (nums[i]!=0) {
115+
s+= nums[i]%10;
116+
nums[i]/=10;
117+
}
118+
if (s== i) {
119+
return i;
120+
}
121+
}
122+
return-1;
123+
}
124+
}
98125
```
99126

100127
####C++
101128

102129
```cpp
103-
130+
classSolution {
131+
public:
132+
int smallestIndex(vector<int>& nums) {
133+
for (int i = 0; i < nums.size(); ++i) {
134+
int s = 0;
135+
while (nums[i]) {
136+
s += nums[i] % 10;
137+
nums[i] /= 10;
138+
}
139+
if (s == i) {
140+
return i;
141+
}
142+
}
143+
return -1;
144+
}
145+
};
104146
```
105147
106148
#### Go
107149
108150
```go
151+
func smallestIndex(nums []int) int {
152+
for i, x := range nums {
153+
s := 0
154+
for ; x > 0; x /= 10 {
155+
s += x % 10
156+
}
157+
if s == i {
158+
return i
159+
}
160+
}
161+
return -1
162+
}
163+
```
109164

165+
####TypeScript
166+
167+
```ts
168+
function smallestIndex(nums:number[]):number {
169+
for (let i=0;i<nums.length;++i) {
170+
let s=0;
171+
for (;nums[i]>0;nums[i]=Math.floor(nums[i]/10)) {
172+
s+=nums[i]%10;
173+
}
174+
if (s===i) {
175+
returni;
176+
}
177+
}
178+
return-1;
179+
}
110180
```
111181

112182
<!-- tabs:end -->

‎solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/README_EN.md

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,102 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3550.Sm
7979

8080
<!-- solution:start-->
8181

82-
###Solution 1
82+
###Solution 1: Enumeration + Digit Sum
83+
84+
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.
85+
86+
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.
8387

8488
<!-- tabs:start-->
8589

8690
####Python3
8791

8892
```python
89-
93+
classSolution:
94+
defsmallestIndex(self,nums: List[int]) ->int:
95+
for i, xinenumerate(nums):
96+
s=0
97+
while x:
98+
s+= x%10
99+
x//=10
100+
if s== i:
101+
return i
102+
return-1
90103
```
91104

92105
####Java
93106

94107
```java
95-
108+
classSolution {
109+
publicintsmallestIndex(int[]nums) {
110+
for (int i=0; i< nums.length;++i) {
111+
int s=0;
112+
while (nums[i]!=0) {
113+
s+= nums[i]%10;
114+
nums[i]/=10;
115+
}
116+
if (s== i) {
117+
return i;
118+
}
119+
}
120+
return-1;
121+
}
122+
}
96123
```
97124

98125
####C++
99126

100127
```cpp
101-
128+
classSolution {
129+
public:
130+
int smallestIndex(vector<int>& nums) {
131+
for (int i = 0; i < nums.size(); ++i) {
132+
int s = 0;
133+
while (nums[i]) {
134+
s += nums[i] % 10;
135+
nums[i] /= 10;
136+
}
137+
if (s == i) {
138+
return i;
139+
}
140+
}
141+
return -1;
142+
}
143+
};
102144
```
103145
104146
#### Go
105147
106148
```go
149+
func smallestIndex(nums []int) int {
150+
for i, x := range nums {
151+
s := 0
152+
for ; x > 0; x /= 10 {
153+
s += x % 10
154+
}
155+
if s == i {
156+
return i
157+
}
158+
}
159+
return -1
160+
}
161+
```
107162

163+
####TypeScript
164+
165+
```ts
166+
function smallestIndex(nums:number[]):number {
167+
for (let i=0;i<nums.length;++i) {
168+
let s=0;
169+
for (;nums[i]>0;nums[i]=Math.floor(nums[i]/10)) {
170+
s+=nums[i]%10;
171+
}
172+
if (s===i) {
173+
returni;
174+
}
175+
}
176+
return-1;
177+
}
108178
```
109179

110180
<!-- tabs:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
classSolution {
2+
public:
3+
intsmallestIndex(vector<int>& nums) {
4+
for (int i =0; i < nums.size(); ++i) {
5+
int s =0;
6+
while (nums[i]) {
7+
s += nums[i] %10;
8+
nums[i] /=10;
9+
}
10+
if (s == i) {
11+
return i;
12+
}
13+
}
14+
return -1;
15+
}
16+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
funcsmallestIndex(nums []int)int {
2+
fori,x:=rangenums {
3+
s:=0
4+
for ;x>0;x/=10 {
5+
s+=x%10
6+
}
7+
ifs==i {
8+
returni
9+
}
10+
}
11+
return-1
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
classSolution {
2+
publicintsmallestIndex(int[]nums) {
3+
for (inti =0;i <nums.length; ++i) {
4+
ints =0;
5+
while (nums[i] !=0) {
6+
s +=nums[i] %10;
7+
nums[i] /=10;
8+
}
9+
if (s ==i) {
10+
returni;
11+
}
12+
}
13+
return -1;
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
classSolution:
2+
defsmallestIndex(self,nums:List[int])->int:
3+
fori,xinenumerate(nums):
4+
s=0
5+
whilex:
6+
s+=x%10
7+
x//=10
8+
ifs==i:
9+
returni
10+
return-1
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
functionsmallestIndex(nums:number[]):number{
2+
for(leti=0;i<nums.length;++i){
3+
lets=0;
4+
for(;nums[i]>0;nums[i]=Math.floor(nums[i]/10)){
5+
s+=nums[i]%10;
6+
}
7+
if(s===i){
8+
returni;
9+
}
10+
}
11+
return-1;
12+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp