Skip to content

168. Excel表列名称 #59

Open
Open
@swiftwind0405

Description

@swiftwind0405

方法一:递归

解题思想:
将输入转化为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('');
};

复杂度分析:

  • 时间复杂度:
  • 空间复杂度:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions