leetcode168

public class Solution {
    private string Convert(int k)
        {
            var s = "";
            switch (k)
            {
                case 1:
                    s = "A";
                    break;
                case 2:
                    s = "B";
                    break;
                case 3:
                    s = "C";
                    break;
                case 4:
                    s = "D";
                    break;
                case 5:
                    s = "E";
                    break;
                case 6:
                    s = "F";
                    break;
                case 7:
                    s = "G";
                    break;
                case 8:
                    s = "H";
                    break;
                case 9:
                    s = "I";
                    break;
                case 10:
                    s = "J";
                    break;
                case 11:
                    s = "K";
                    break;
                case 12:
                    s = "L";
                    break;
                case 13:
                    s = "M";
                    break;
                case 14:
                    s = "N";
                    break;
                case 15:
                    s = "O";
                    break;
                case 16:
                    s = "P";
                    break;
                case 17:
                    s = "Q";
                    break;
                case 18:
                    s = "R";
                    break;
                case 19:
                    s = "S";
                    break;
                case 20:
                    s = "T";
                    break;
                case 21:
                    s = "U";
                    break;
                case 22:
                    s = "V";
                    break;
                case 23:
                    s = "W";
                    break;
                case 24:
                    s = "X";
                    break;
                case 25:
                    s = "Y";
                    break;
                case 0:
                    s = "Z";
                    break;
            }
            return s;
        }

        public string ConvertToTitle(int n)
        {
            var list = new List<int>();
            while (n != 0)
            {
                var k = n % 26;
                list.Add(k);
                if (k == 0)
                {
                    n = n / 26 - 1;
                }
                else
                {
                    n = n / 26;
                }
            }

            list.Reverse();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                var s = Convert(list[i]);
                sb.Append(s);
            }
            return sb.ToString();
        }
}

https://leetcode.com/problems/excel-sheet-column-title/#/description

时间: 2024-10-12 13:50:45

leetcode168的相关文章

LeetCode168——Excel Sheet Column Title

LeetCode168--Excel Sheet Column Title 题目 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 Credits: Special thanks to @ifanchu for

leetcode-168周赛-1297-子串的最大出现次数

题目描述: 自己的提交: class Solution: def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int: c = collections.Counter() for i in range(len(s)-minSize+1): if len(set(s[i:i+minSize])) <= maxLetters: c[s[i:i+minSize]] += 1 if not c: retur

leetcode-168周赛-1295-统计位数为偶数的数字

题目描述: 方法一:O(N) class Solution: def findNumbers(self, nums: List[int]) -> int: ans=0 for num in nums: if len(str(num))%2==0: ans+=1 return ans 方法二:数学 O(N) class Solution: def findNumbers(self, nums: List[int]) -> int: return sum(1 for num in nums if

leetcode-168周赛-1296-划分数字为连续数字的集合

题目描述: 自己的提交: class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: c = collections.Counter(nums) n = len(nums) m = n/k if m%1 != 0: return False while m != 0: x = k pre = min(i for i,v in c.items() if v > 0) -1 while x: if c[p