Hyoseo Lee
Home About Projects Blog Thinking
leetcode

171. Excel Sheet Column Number

Topic Math
Area Algorithms
Summary
it calls the help function. I created the help function to send the length of string as an argument of function. Otherwise, it would increase the complexity by

Problem

View on LeetCode →

Difficulty: Easy
Tags: Math, String

Intuition

RECURSION!!!

The first intuition to this problem was using recursion. It seemed I can use similar method with factorial function and it worked.

Approach

it calls the help function. I created the help function to send the length of string as an argument of function. Otherwise, it would increase the complexity by calling O(n) function every loop.

rest are pretty straight forward.

Solution

int ttmHelp(char* ct, int len){
    printf("%d\n", len);
    if(len == 1){
        return ct[0] - 'A' + 1;
    }
    else{
        return ((ct[0]-'A'+1)* pow(26,len-1)) + ttmHelp(ct + 1 , len -1);
    }
}

int titleToNumber(char* columnTitle) {
    int length = 0;
    while (columnTitle[length] != '\0'){
        length++;
    }
    return ttmHelp(columnTitle, length);
}

Complexity

Thoughts

it was a bit easy to me. I need more harder ones.