摘要:进制复杂度时间空间思路得到数字,其实就是把进制的数转换为进制的数。算法就是基本的进制转换方法,从后往前第位的值乘上。不过因为是,而不是,相当于进制的数都整体减,才能对应上从开始的十进制数。
Excel Sheet Column Number
26进制 复杂度Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28
时间 O(N) 空间 O(1)
思路得到数字,其实就是把26进制的数转换为10进制的数。算法就是基本的进制转换方法,从后往前第n位的值乘上26^(n-1)。这里26进制数是1开始的,即A是1。
代码public class Solution { public int titleToNumber(String s) { int num = 0, pow = 1; for(int i = s.length() - 1; i >= 0 ; i--){ num += (s.charAt(i) - "A" + 1)*pow; pow *= 26; } return num; } }Excel Sheet Column Title
26进制 复杂度Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB
时间 O(N) 空间 O(1)
思路把10进制的转换成26进制,做法是除26取余,一直除到0,最后把余数逆序一下就行了。不过因为A是1,而不是0,相当于26进制的数都整体减1,才能对应上从0开始的十进制数。
代码public class Solution { public String convertToTitle(int n) { StringBuilder sb = new StringBuilder(); while(n != 0){ sb.append((char)("A" + (n - 1) % 26)); n = (n - 1) / 26; } return sb.reverse().toString(); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/64699.html
Problem Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ... Example 1: Input: A Output: 1 Exa...
Problem Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 Solution ...
Problem Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ... Example 1: Input: 1Output: AEx...
摘要:微信公众号记录截图记录截图目前关于这块算法与数据结构的安排前。已攻略返回目录目前已攻略篇文章。会根据题解以及留言内容,进行补充,并添加上提供题解的小伙伴的昵称和地址。本许可协议授权之外的使用权限可以从处获得。 Create by jsliang on 2019-07-15 11:54:45 Recently revised in 2019-07-15 15:25:25 一 目录 不...
Problem Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. Example A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 2...
阅读 2399·2021-10-09 09:59
阅读 2134·2021-09-23 11:30
阅读 2529·2019-08-30 15:56
阅读 1114·2019-08-30 14:00
阅读 2904·2019-08-29 12:37
阅读 1216·2019-08-28 18:16
阅读 1630·2019-08-27 10:56
阅读 991·2019-08-26 17:23