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

Commit 2c15377

Browse files
Smallest Index With Digit Sum Equal to Index
1 parent c958349 commit 2c15377

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Link: https://leetcode.com/problems/smallest-index-with-digit-sum-equal-to-index/
2+
Language: C#
3+
4+
5+
6+
7+
8+
9+
public class Solution {
10+
public int SmallestIndex(int[] nums)
11+
{
12+
for (int i = 0; i < nums.Length; i++)
13+
{
14+
if (nums[i] >= 0)
15+
{
16+
int sum = 0;
17+
while (nums[i] > 0)
18+
{
19+
int index = nums[i] % 10;
20+
sum += index;
21+
nums[i] /= 10;
22+
}
23+
if (sum == i)
24+
return i;
25+
}
26+
}
27+
28+
return -1;
29+
}
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /