|
14 | 14 |
|
15 | 15 | ## `Today Update`
|
16 | 16 | ### Math
|
17 |
| -#### 7 Reverse Integer |
18 |
| -* [Github:#7 Reverse Integer](/Math/Math.Lib/ReverseIntegarSln.cs) |
19 |
| -* [CSDN:#7 Reverse Integer](http://blog.csdn.net/daigualu/article/details/72464418) |
20 |
| -* Tips: |
21 |
| - * an interesting way to check if happens overflow. |
| 17 | +#### 400 Nth Digit |
| 18 | +* [Github:#400 Nth Digit](/Math/Math.Lib/Nthdigit.cs) |
| 19 | +* [CSDN:#400 Nth Digit](http://blog.csdn.net/daigualu/article/details/72572244) |
| 20 | + * Tips: |
| 21 | + * careful to prevent overflowing for bas*digits, so declaring bas is long. |
| 22 | + ```C# |
| 23 | + //for this issue, there are two different ways to decribe a number |
| 24 | + //1 element. this is our common way |
| 25 | + //2 Nth digit. this is a new way |
| 26 | + public int FindNthDigit(int n) |
| 27 | + { |
| 28 | + long bas = 9; |
| 29 | + int digits = 1, i = 0; |
| 30 | + //first: getting n which digit is in |
| 31 | + while (n > bas * digits) // prevent overflowing. Since bas is long, so result of bas*digits is auto imporved as long |
| 32 | + { |
| 33 | + n -= (int)(bas * (digits++)); //nth |
| 34 | + i += (int)bas; //number of pasted elements |
| 35 | + bas *= 10; //1 digit->9; 2 digits->90; 3 digits->900, ... |
| 36 | + } |
| 37 | + //second: Nth digit ->element |
| 38 | + //in all numbers containing digits, pasted numbers |
| 39 | + int pasted = (int)((n - 1) / digits); |
| 40 | + int element = pasted + i + 1; |
| 41 | + //third: once getting the element Nth digits stands, |
| 42 | + //(n-1)%digits of element is solution |
| 43 | + int nth = (n - 1) % digits; |
| 44 | + return element.ToString()[nth] - '0'; |
| 45 | + } |
| 46 | + ``` |
22 | 47 |
|
23 | 48 | ---
|
24 | 49 | ---
|
@@ -156,6 +181,12 @@ Tags are following:
|
156 | 181 |
|
157 | 182 | * [#507 Perfect Number](http://blog.csdn.net/daigualu/article/details/69233798)
|
158 | 183 |
|
| 184 | +#### 7 Reverse Integer |
| 185 | +* [Github:#7 Reverse Integer](/Math/Math.Lib/ReverseIntegarSln.cs) |
| 186 | +* [CSDN:#7 Reverse Integer](http://blog.csdn.net/daigualu/article/details/72464418) |
| 187 | +* Tips: |
| 188 | + * an interesting way to check if happens overflow. |
| 189 | + |
159 | 190 | #### 202 Happy Number
|
160 | 191 | * [Github:#202 Happy Number](/Math/Math.Lib/HappyNumber.cs)
|
161 | 192 | * [CSDN:#202 Happy Number](http://blog.csdn.net/daigualu/article/details/71433906)
|
|
0 commit comments