File tree Expand file tree Collapse file tree 4 files changed +131
-0
lines changed
Expand file tree Collapse file tree 4 files changed +131
-0
lines changed Original file line number Diff line number Diff line change 303303
304304[ 168. Excel Sheet Column Title] ( https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/168.%20Excel%20Sheet%20Column%20Title.md )
305305
306+ [ 169. Majority Element] ( https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/169.%20Majority%20Element.md )
307+
308+ [ 171. Excel Sheet Column Number] ( https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/171.%20Excel%20Sheet%20Column%20Number.md )
309+
310+ [ 172. Factorial Trailing Zeroes] ( https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/172.%20Factorial%20Trailing%20Zeroes.md )
311+
306312## Algorithm(4th_Edition)
307313Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
308314All java realization codes are placed in different packages.
Original file line number Diff line number Diff line change 11## 169. Majority Element
2+
3+ ### Question:
4+ Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
5+
6+ You may assume that the array is non-empty and the majority element always exist in the array.
7+
8+ ```
9+ Example 1:
10+
11+ Input: [3,2,3]
12+ Output: 3
13+
14+ Example 2:
15+
16+ Input: [2,2,1,1,1,2,2]
17+ Output: 2
18+ ```
19+
220### Thinking:
321* Method1:通过hashmap实现,速度较慢
422
@@ -54,4 +72,25 @@ class Solution {
5472 return major;
5573 }
5674}
75+ ```
76+
77+ ### 二刷
78+ 还是记得一刷时候的方法三。
79+ ``` Java
80+ class Solution {
81+ public int majorityElement (int [] nums ) {
82+ int count = 1 ;
83+ int save = nums[0 ];
84+ for (int i = 1 ; i < nums. length; i++ ){
85+ if (count == 0 ){
86+ ++ count;
87+ save = nums[i];
88+ continue ;
89+ }
90+ if (nums[i] == save) count++ ;
91+ else count-- ;
92+ }
93+ return save;
94+ }
95+ }
5796```
Original file line number Diff line number Diff line change 1+ ## 171. Excel Sheet Column Number
2+
3+ ### Question:
4+ Given a column title as appear in an Excel sheet, return its corresponding column number.
5+
6+ ```
7+ For example:
8+
9+ A -> 1
10+ B -> 2
11+ C -> 3
12+ ...
13+ Z -> 26
14+ AA -> 27
15+ AB -> 28
16+ ...
17+
18+ Example 1:
19+
20+ Input: "A"
21+ Output: 1
22+
23+ Example 2:
24+
25+ Input: "AB"
26+ Output: 28
27+
28+ Example 3:
29+
30+ Input: "ZY"
31+ Output: 701
32+ ```
33+
34+ ### Thinking:
35+ * Method1:通过hashmap实现,速度较慢
36+
37+ ``` Java
38+ class Solution {
39+ public int titleToNumber (String s ) {
40+ Map<Character , Integer > map = new HashMap<> ();
41+ Character c = ' Z' ;
42+ for (int i = 26 ; i > 0 ; i-- ){
43+ map. put(c, i);
44+ c-- ;
45+ }
46+ int count = 0 ;
47+ if (s. length() == 0 ) return count;
48+ for (int i = 0 ; i < s. length(); i++ ){
49+ count = map. get(s. charAt(i)) + count * 26 ;
50+ }
51+ return count;
52+ }
53+ }
54+ ```
55+
56+ ### 二刷
57+ 1 . 一刷中用hashmap唯一的用处就是记录char和int的对应关系,但是我们完全可以通过当前char的数值和A之间的差值+1获得数字。
58+ ``` Java
59+ class Solution {
60+ public int titleToNumber (String s ) {
61+ if (s == null || s. length() == 0 ) return 0 ;
62+ int result = 0 ;
63+ for (int i = 0 ; i < s. length(); i++ ){
64+ result *= 26 ;
65+ int c = s. charAt(i) - ' A' + 1 ;
66+ result += c;
67+ }
68+ return result;
69+ }
70+ }
71+ ```
Original file line number Diff line number Diff line change @@ -35,3 +35,18 @@ class Solution {
3535 }
3636}
3737```
38+
39+ ### 二刷
40+ ![ Imgur] ( https://i.imgur.com/IBejjyd.jpg )
41+ ``` Java
42+ class Solution {
43+ public int trailingZeroes (int n ) {
44+ int result = 0 ;
45+ while (n > 4 ){
46+ result += n / 5 ;
47+ n /= 5 ;
48+ }
49+ return result;
50+ }
51+ }
52+ ```
You can’t perform that action at this time.
0 commit comments