摘要:進制復雜度時間空間思路得到數字,其實就是把進制的數轉換為進制的數。算法就是基本的進制轉換方法,從后往前第位的值乘上。不過因為是,而不是,相當于進制的數都整體減,才能對應上從開始的十進制數。
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(); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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...
閱讀 2423·2021-10-09 09:59
閱讀 2177·2021-09-23 11:30
閱讀 2591·2019-08-30 15:56
閱讀 1145·2019-08-30 14:00
閱讀 2939·2019-08-29 12:37
閱讀 1253·2019-08-28 18:16
閱讀 1656·2019-08-27 10:56
閱讀 1022·2019-08-26 17:23