Skip to content

168. Excel表列名称 #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
swiftwind0405 opened this issue Jan 31, 2021 · 0 comments
Open

168. Excel表列名称 #59

swiftwind0405 opened this issue Jan 31, 2021 · 0 comments

Comments

@swiftwind0405
Copy link
Owner

swiftwind0405 commented Jan 31, 2021

方法一:递归

解题思想:
将输入转化为nums数组,num[1, 26]。
例如:
1 -> [1]
26 -> [26]
27 -> [1, 1]
52 -> [1, 26]
...

代码:

var convertToTitle = function(n) {
    const mod26 = (num) => {
        if (num <= 26) { return [num]; }
        if (num % 26 === 0) {
            return mod26((num / 26) -1).concat([26]);
        }
        return mod26(parseInt(num / 26)).concat([num % 26]);
    }
    return mod26(n).map(num => String.fromCharCode(num + 65 - 1)).join('');
};

复杂度分析:

  • 时间复杂度:
  • 空间复杂度:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant