File tree Expand file tree Collapse file tree 6 files changed +81
-12
lines changed
solution/0100-0199/0171.Excel Sheet Column Number Expand file tree Collapse file tree 6 files changed +81
-12
lines changed Original file line number Diff line number Diff line change @@ -92,9 +92,9 @@ var reverseWords = function (s) {
9292};
9393```
9494
95- ### ** C++ **
95+ ### ** Go **
9696
97- ``` cpp
97+ ``` go
9898func reverseWords (s string ) string {
9999 s = strings.Trim (s, " " )
100100 n := len (s) - 1
Original file line number Diff line number Diff line change 5252<!-- 这里可写当前语言的特殊实现逻辑 -->
5353
5454``` python
55-
55+ class Solution :
56+ def titleToNumber (self , columnTitle : str ) -> int :
57+ res = 0
58+ for c in columnTitle:
59+ res = res * 26 + (ord (c) - ord (' A' ) + 1 )
60+ return res
5661```
5762
5863### ** Java**
5964
6065<!-- 这里可写当前语言的特殊实现逻辑 -->
6166
6267``` java
68+ class Solution {
69+ public int titleToNumber (String columnTitle ) {
70+ int res = 0 ;
71+ for (char c : columnTitle. toCharArray()) {
72+ res = res * 26 + (c - ' A' + 1 );
73+ }
74+ return res;
75+ }
76+ }
77+ ```
6378
79+ ### ** C++**
80+
81+ ``` cpp
82+ class Solution {
83+ public:
84+ int titleToNumber(string columnTitle) {
85+ int res = 0;
86+ for (char c : columnTitle) {
87+ res = res * 26 + (c - 'A' + 1);
88+ }
89+ return res;
90+ }
91+ };
6492```
6593
6694### **...**
Original file line number Diff line number Diff line change @@ -65,13 +65,41 @@ AB -> 28
6565### ** Python3**
6666
6767``` python
68-
68+ class Solution :
69+ def titleToNumber (self , columnTitle : str ) -> int :
70+ res = 0
71+ for c in columnTitle:
72+ res = res * 26 + (ord (c) - ord (' A' ) + 1 )
73+ return res
6974```
7075
7176### ** Java**
7277
7378``` java
79+ class Solution {
80+ public int titleToNumber (String columnTitle ) {
81+ int res = 0 ;
82+ for (char c : columnTitle. toCharArray()) {
83+ res = res * 26 + (c - ' A' + 1 );
84+ }
85+ return res;
86+ }
87+ }
88+ ```
7489
90+ ### ** C++**
91+
92+ ``` cpp
93+ class Solution {
94+ public:
95+ int titleToNumber(string columnTitle) {
96+ int res = 0;
97+ for (char c : columnTitle) {
98+ res = res * 26 + (c - 'A' + 1);
99+ }
100+ return res;
101+ }
102+ };
75103```
76104
77105### **...**
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int titleToNumber (string columnTitle) {
4+ int res = 0 ;
5+ for (char c : columnTitle) {
6+ res = res * 26 + (c - ' A' + 1 );
7+ }
8+ return res;
9+ }
10+ };
Original file line number Diff line number Diff line change 11class Solution {
2- public int titleToNumber (String s ) {
3- char [] cs = s .toCharArray ();
4- int n = 0 ;
5- int p = 1 ;
6- for (int i = cs .length -1 ; i >= 0 ; i --) {
7- n += (cs [i ]-'A' +1 )*p ;
8- p *= 26 ;
2+ public int titleToNumber (String columnTitle ) {
3+ int res = 0 ;
4+ for (char c : columnTitle .toCharArray ()) {
5+ res = res * 26 + (c - 'A' + 1 );
96 }
10- return n ;
7+ return res ;
118 }
129}
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def titleToNumber (self , columnTitle : str ) -> int :
3+ res = 0
4+ for c in columnTitle :
5+ res = res * 26 + (ord (c ) - ord ('A' ) + 1 )
6+ return res
You can’t perform that action at this time.
0 commit comments