171:Excel 表列序号

This commit is contained in:
huangge1199 2021-07-30 11:30:36 +08:00
parent 1c8011ef2b
commit a42435bf84
4 changed files with 144 additions and 2 deletions

View File

@ -0,0 +1,86 @@
//给你一个字符串 columnTitle 表示 Excel 表格中的列名称返回该列名称对应的列序号
//
//
//
// 例如
//
//
// A -> 1
// B -> 2
// C -> 3
// ...
// Z -> 26
// AA -> 27
// AB -> 28
// ...
//
//
//
//
// 示例 1:
//
//
//输入: columnTitle = "A"
//输出: 1
//
//
// 示例 2:
//
//
//输入: columnTitle = "AB"
//输出: 28
//
//
// 示例 3:
//
//
//输入: columnTitle = "ZY"
//输出: 701
//
// 示例 4:
//
//
//输入: columnTitle = "FXSHRXW"
//输出: 2147483647
//
//
//
//
// 提示
//
//
// 1 <= columnTitle.length <= 7
// columnTitle 仅由大写英文组成
// columnTitle 在范围 ["A", "FXSHRXW"]
//
// Related Topics 数学 字符串
// 👍 260 👎 0
package leetcode.editor.cn;
//171:Excel 表列序号
public class ExcelSheetColumnNumber {
public static void main(String[] args) {
//测试代码
Solution solution = new ExcelSheetColumnNumber().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int titleToNumber(String columnTitle) {
char[] chs = columnTitle.toCharArray();
int result = 0;
int size = chs.length - 1;
int mul = 1;
for (int i = 0; i <= size; i++) {
int num = chs[size - i] - 'A' + 1;
result += num * mul;
mul *= 26;
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,56 @@
<p>给你一个字符串&nbsp;<code>columnTitle</code> ,表示 Excel 表格中的列名称。返回该列名称对应的列序号。</p>
<p>&nbsp;</p>
<p>例如,</p>
<pre>
A -&gt; 1
B -&gt; 2
C -&gt; 3
...
Z -&gt; 26
AA -&gt; 27
AB -&gt; 28
...
</pre>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> columnTitle = "A"
<strong>输出:</strong> 1
</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre>
<strong>输入: </strong>columnTitle = "AB"
<strong>输出:</strong> 28
</pre>
<p><strong>示例&nbsp;3:</strong></p>
<pre>
<strong>输入: </strong>columnTitle = "ZY"
<strong>输出:</strong> 701</pre>
<p><strong>示例 4:</strong></p>
<pre>
<strong>输入: </strong>columnTitle = "FXSHRXW"
<strong>输出: </strong>2147483647
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= columnTitle.length &lt;= 7</code></li>
<li><code>columnTitle</code> 仅由大写英文组成</li>
<li><code>columnTitle</code> 在范围 <code>["A", "FXSHRXW"]</code></li>
</ul>
<div><div>Related Topics</div><div><li>数学</li><li>字符串</li></div></div>\n<div><li>👍 260</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long