Skip to content

Commit ba55d33

Browse files
committed
readme
1 parent 5fe19a4 commit ba55d33

File tree

1 file changed

+66
-44
lines changed

1 file changed

+66
-44
lines changed

README.md

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,69 @@ Anyway, welcome to view, star and fork, then contribute.
1414

1515
## `Today Update`
1616
### Math
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)
17+
#### 168 ExcelColumnTitle
18+
* [Github:#168 ExcelColumnTitle](/Math/Math.Lib/ExcelColumnTitle.cs)
19+
* [CSDN:#168 ExcelColumnTitle](http://blog.csdn.net/daigualu/article/details/72638706)
2020
* 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)
21+
* / and % operations application
22+
```C#
23+
public string ConvertToTitle(int n)
2724
{
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
25+
//A~Z:26
26+
//AA~ZZ:26*26
27+
//...
28+
if (n == 1) return "A";
29+
char[] chdict = {'A','B','C','D','E','F','G','H','I','J','K',
30+
'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
31+
StringBuilder sb = new StringBuilder();
32+
while (n > 0)
3233
{
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, ...
34+
n--; //beacuse our chdict's index begins Zero.
35+
sb.Append(chdict[n % 26]);
36+
n = n / 26;
3637
}
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';
38+
IEnumerable<char> rtnchars = sb.ToString().Reverse();
39+
sb.Clear();
40+
foreach (var ch in rtnchars) sb.Append(ch);
41+
return sb.ToString();
4542
}
46-
```
47-
#### 69 Sqrt(x)
48-
* [Github:#69 Sqrt(x)](/Math/Math.Lib/Sqrtx.cs)
49-
* [CSDN:#69 Sqrt(x)](http://blog.csdn.net/daigualu/article/details/72578272)
43+
```
44+
45+
#### 67 Add Binary
46+
* [Github:#67 Add Binary](/Math/Math.Lib/AddBinarySln.cs)
47+
* [CSDN:#67 Add Binary](http://blog.csdn.net/daigualu/article/details/72638937)
5048
* Tips:
51-
* careful to prevent overflowing! Again careful to **overflow**!
49+
* carry bit application!
5250
```C#
53-
public int MySqrt(int x)
51+
public string AddBinary(string a, string b)
5452
{
55-
if(x==0 || x==1)return x;
56-
int lo = 0, hi = x ;
57-
while (lo - hi < -1)
58-
{
59-
//get [lo,hi] middle point,then compare pow2 to x,
60-
// lo or hi is setted by mid
61-
//so accelarate the process
62-
long mid = lo + (hi - lo) / 2; //prevent overflowing
63-
long pow2 = mid * mid; //prevent overflowing
64-
if (pow2 < x) lo = (int)mid;
65-
else if (pow2 > x) hi = (int)mid;
66-
else return (int)mid;
53+
StringBuilder sb = new StringBuilder();
54+
int carry = 0, acnt = a.Length, bcnt = b.Length;
55+
for (int i = acnt - 1, j = bcnt - 1; i >= 0 || j >= 0 || carry == 1; i--, j--){
56+
int sum2 = 0;
57+
if (i < 0 && j < 0){ //overflow solving
58+
sb.Append(carry);
59+
carry = 0;
60+
continue;
61+
}
62+
//discuss three conditions according to i and j
63+
if (i < 0) sum2 = b[j] - '0';
64+
else if (j < 0) sum2 = a[i] - '0';
65+
else sum2 = a[i] - '0' + b[j] - '0';
66+
if (sum2 + carry < 2){
67+
sb.Append(sum2 + carry);
68+
carry = 0;
69+
}
70+
else {
71+
sb.Append(sum2 + carry - 2);
72+
carry = 1;
73+
}
6774
}
68-
return lo;
75+
//reverse the sb
76+
IEnumerable<char> rtnchars = sb.ToString().Reverse();
77+
sb.Clear();
78+
foreach (var ch in rtnchars) sb.Append(ch);
79+
return sb.ToString();
6980
}
7081
```
7182
---
@@ -209,7 +220,7 @@ Tags are following:
209220
* [CSDN:#7 Reverse Integer](http://blog.csdn.net/daigualu/article/details/72464418)
210221
* Tips:
211222
* an interesting way to check if happens overflow.
212-
223+
213224
#### 202 Happy Number
214225
* [Github:#202 Happy Number](/Math/Math.Lib/HappyNumber.cs)
215226
* [CSDN:#202 Happy Number](http://blog.csdn.net/daigualu/article/details/71433906)
@@ -225,6 +236,17 @@ Tags are following:
225236
* [CSDN:#415 Add Strings](http://blog.csdn.net/daigualu/article/details/72356377)
226237
* Tips:
227238
* this is an interesting question!
239+
#### 400 Nth Digit
240+
* [Github:#400 Nth Digit](/Math/Math.Lib/Nthdigit.cs)
241+
* [CSDN:#400 Nth Digit](http://blog.csdn.net/daigualu/article/details/72572244)
242+
* Tips:
243+
* careful to prevent overflowing for bas*digits, so declaring bas is long.
244+
245+
#### 69 Sqrt(x)
246+
* [Github:#69 Sqrt(x)](/Math/Math.Lib/Sqrtx.cs)
247+
* [CSDN:#69 Sqrt(x)](http://blog.csdn.net/daigualu/article/details/72578272)
248+
* Tips:
249+
* careful to prevent overflowing! Again careful to **overflow**!
228250

229251
## Two Pointers
230252
* [#345 Reverse Vowels of a String](http://blog.csdn.net/daigualu/article/details/69257693)

0 commit comments

Comments
 (0)