博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode – Refresh – Exceel Sheet Column Title
阅读量:6922 次
发布时间:2019-06-27

本文共 760 字,大约阅读时间需要 2 分钟。

Only trick is the 'Z'. Because when the number % 26 == 0, means there is a 'Z'. But when you n/=26, there is an extra 1 added to the n. EX:

27 % 26 = 1, 27 / 26 = 1  == > AA

26 % 26 = 0, 26 / 26 = 1  Here this extra one need to be removed. You can imagine that 'Z' becomes next around of the loop.

 

1 class Solution { 2 public: 3     string convertToTitle(int n) { 4         string result; 5         while (n > 0) { 6             int tmp = n%26; 7             n /= 26; 8             if (tmp == 0) { 9                 result = 'Z' + result;10                 n--;11             } else {12                 result = char(tmp + 'A' - 1) +result;13             }14         }15         return result;16     }17 };

 

转载于:https://www.cnblogs.com/shuashuashua/p/4349459.html

你可能感兴趣的文章
如何使用枚举的组合值
查看>>
为什么线程安全的ACE容器不支持[]操作符
查看>>
为iPhone 6设计自适应布局
查看>>
如何通过固定的IP访问阿里云OSS的文件
查看>>
【索引】反向索引--条件 范围查询
查看>>
Solr平台化搜索实战必知场景
查看>>
可以触发点击事件并变色的UILabel
查看>>
Cocos2D中Node的userObject实例变量使用时一个要注意的地方
查看>>
使用T-SQL配置日志传送
查看>>
(九十一)第六章编程练习
查看>>
ARTS-20180630
查看>>
深入理解 Go 语言 defer
查看>>
资源分享计划第四期 0518
查看>>
[译] 让我们来简化 UserDefaults 的使用
查看>>
IDEA + GIT
查看>>
学习笔记CB013: TensorFlow、TensorBoard、seq2seq
查看>>
简单来谈谈Unicode与emoji
查看>>
Android之Window和弹窗问题
查看>>
如何最骚气得在linux下聊qq(mojoqq)
查看>>
搭建harbor仓库
查看>>