Skip to content

Commit b845742

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 60b687e commit b845742

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@
297297

298298
[165. Compare Version Numbers](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/165.%20Compare%20Version%20Numbers.md)
299299

300+
[166. Fraction to Recurring Decimal](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/166.%20Fraction%20to%20Recurring%20Decimal.md)
301+
302+
[167. Two Sum II - Input array is sorted](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/167.%20Two%20Sum%20II%20-%20Input%20array%20is%20sorted.md)
303+
304+
[168. Excel Sheet Column Title](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/168.%20Excel%20Sheet%20Column%20Title.md)
305+
300306
## Algorithm(4th_Edition)
301307
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
302308
All java realization codes are placed in different packages.

leetcode/166. Fraction to Recurring Decimal.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,44 @@ class Solution {
6060
}
6161
}
6262
```
63+
64+
### 二刷
65+
1. 注意int的上下限问题。
66+
2. 如何退出程序,返回结果:
67+
* 余数为0.
68+
* 余数不为零,但是余数已经出现过了。这说明会出现循环小数。
69+
```Java
70+
class Solution {
71+
public String fractionToDecimal(int numerator, int denominator) {
72+
if(denominator == 0) return "NaN";
73+
Map<Long, Integer> map = new HashMap<>();
74+
StringBuilder sb = new StringBuilder();
75+
if(numerator > 0 && denominator < 0 || numerator < 0 && denominator > 0) sb.append('-');
76+
long aa = Math.abs((long)numerator);
77+
long bb = Math.abs((long)denominator);
78+
long digit = aa / bb;
79+
sb.append(digit);
80+
long remain = aa % bb;
81+
if(remain == 0) return sb.toString();
82+
sb.append(".");
83+
int count = 1;
84+
while(remain != 0 && !map.containsKey(remain *10)){
85+
remain *= 10;
86+
map.put(remain, count++);
87+
sb.append(Math.abs(remain / bb));
88+
remain %= bb;
89+
}
90+
if(remain != 0){
91+
sb.append(")");
92+
int val = map.get(remain * 10);
93+
String result = sb.toString();
94+
int index = result.indexOf('.');
95+
sb.insert(val + index, '(');
96+
}
97+
return sb.toString();
98+
}
99+
}
100+
```
101+
102+
103+

leetcode/167. Two Sum II - Input array is sorted.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,23 @@ class Solution {
6464
return low >= high ? null : result;
6565
}
6666
}
67+
```
68+
69+
### 二刷
70+
1. 双指针,一个指向数组头,一个指向数组尾。
71+
2. 如果sum小了,low指针右移,不然high指针左移。
72+
```Java
73+
class Solution {
74+
public int[] twoSum(int[] numbers, int target) {
75+
int len = numbers.length;
76+
int low = 0, high = len - 1;
77+
int sum = numbers[low] + numbers[high];
78+
while(sum != target){
79+
if(sum < target) low++;
80+
else high--;
81+
sum = numbers[low] + numbers[high];
82+
}
83+
return new int[]{low + 1, high + 1};
84+
}
85+
}
6786
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## 168. Excel Sheet Column Title
2+
3+
### Question
4+
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
5+
6+
```
7+
For example:
8+
9+
1 -> A
10+
2 -> B
11+
3 -> C
12+
...
13+
26 -> Z
14+
27 -> AA
15+
28 -> AB
16+
...
17+
18+
Example 1:
19+
20+
Input: 1
21+
Output: "A"
22+
23+
Example 2:
24+
25+
Input: 28
26+
Output: "AB"
27+
28+
Example 3:
29+
30+
Input: 701
31+
Output: "ZY"
32+
```
33+
34+
### Thinking:
35+
```Java
36+
class Solution {
37+
public String convertToTitle(int n) {
38+
if(n == 0) return "";
39+
StringBuilder sb = new StringBuilder();
40+
String map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
41+
while(n != 0){
42+
sb.append(map.charAt((n - 1) % 26));
43+
n --;
44+
n /= 26;
45+
}
46+
return sb.reverse().toString();
47+
}
48+
}
49+
```
50+
51+
### 二刷
52+
1. 唯一恶心的就是都是从1开始计数,所以每次计算之前需要-1.
53+
```Java
54+
class Solution {
55+
public String convertToTitle(int n) {
56+
StringBuilder sb = new StringBuilder();
57+
while(n != 0){
58+
n--;
59+
int remain = n % 26;
60+
sb.insert(0, (char)(remain + 'A'));
61+
n /= 26;
62+
}
63+
return sb.toString();
64+
}
65+
}
66+
```

0 commit comments

Comments
 (0)