LeetCode 2015.7.8 128,171,172,189,202,203,119

本文解析了五道LeetCode经典算法题:168. Excel表列标题、171. Excel表列号、172. 阶乘后的零、189. 旋转数组及202. 快乐数。通过代码实现展示了如何解决这些算法问题,包括转换数字到Excel列标题、计算Excel列标题对应的数字、计算阶乘尾数中0的数量、旋转数组元素以及判断一个数是否为快乐数。
168 Excel Sheet Column Title
class Solution {
public:
    string convertToTitle(int n) {
        int tmp=n;
        string ans;
        ans.clear();
        char chara[30];
        for(int i=1;i<=25;i++)
            chara[i]='A'+i-1;
        chara[0]='Z';
        if (n<=26)
        {
            ans.push_back(chara[n % 26]);
            return ans;
        }
        while (tmp!=0)
        {
            int res = tmp % 26;
            tmp = tmp / 26;
            if (res==0) tmp--;
            ans.push_back(chara[res]);
        }
        int len=ans.size()-1;
        string finalans;
        finalans.clear();
        for(int i=len;i>=0;i--)
            finalans.push_back(ans[i]);
        return finalans;
    }
};

171 Excel Sheet Column Number
class Solution {
public:
    int titleToNumber(string s) {
        int len=s.size()-1;
        int sum=0,count=1;
        for(int i=len;i>=0;i--)
        {
            sum+=(s[i]-'A'+1)*count;
            count*=26;
        }
        return sum;
    }
};

172 Factorial Trailing Zeroes
class Solution {
public:
    int trailingZeroes(int n) {
        int sum=0;
        while (n/5>0)
        {
            n/=5;
            sum+=n;
        }
        return sum;
    }
};

189 Rotate Array
class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        int len = nums.size();
        if (k==0) return;
        int rotatek;
        rotatek=k%len;
        if (rotatek==0) return ;
        vector<int> tmp;
        tmp.clear();
        for(int i=len-rotatek;i<len;i++)
            tmp.push_back(nums[i]);
        for(int i=0;i<len-rotatek;i++)
            tmp.push_back(nums[i]);
        for(int i=0;i<len;i++)
            nums[i]=tmp[i];
    }
};

202 Happy Number
class Solution {
public:
    bool isHappy(int n) {
        set<int> appear;
        appear.clear();
        int num=n;
        for(;;)
        {
            //cout<<num<<endl;
            if (num==1) return true;
            int sum=0;
            appear.insert(num);
            while (num!=0)
            {
                int tmp=num%10;
                sum+=tmp*tmp;
                num/=10;
            }
            if (appear.find(sum)!=appear.end())
                return false;
            else
            {
                appear.insert(sum);
                num=sum;
            }
        }
    }
};

203 Remove Linked List Elements
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode tmp(0);
        ListNode* p=&tmp;
        ListNode* q;
        p->next=head;
        head=p;
        q=head;
        p=p->next;
        while(p!=NULL)
        {
            if (p->val==val)
            {
                q->next=p->next;
                p=q->next;
            }
            else
            {
                q=p;
                p=p->next;
            }
        }
        return head->next;
    }
};

119 Pascal's Triangle II
class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> tr[2];
        int flag=0;
        int i=0;
        tr[flag].push_back(1);
        for(i=1;i<=rowIndex;i++)
        {
            int tmpflag=1-flag;
            tr[tmpflag].clear();
            tr[tmpflag].push_back(1);
            for(vector<int>::iterator j=tr[flag].begin();j!=tr[flag].end()-1;j++)
            {
                tr[tmpflag].push_back((*j)+(*(j+1)));
            }
            tr[tmpflag].push_back(1);
            flag=tmpflag;
        }
        return tr[flag];
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值