[DLdigest-8] 每日一道算法

2017 年 11 月 2 日 深度学习每日摘要 DLdigest

在上一期的[DLdigest-7] 每日一道算法中,题目是给定一个整数,然后将它每个位上的数字颠倒过来重新组成一个整数。

需要注意的就是输入的整数类型为32位有符号型,输出是否有溢出需要考虑,一旦溢出就输出0。在程序中判断一个数是否溢出的办法就是把这个数乘以10然后再除以10,如果可以正确复原,那么表示没有溢出,否则就是发生了溢出。

class Solution {
public:    
int reverse(int x) {
   int result=0;
   int flag=x>0?1:-1;    x = fabs(x);
   while(x>0) {
       int tmp = result*10 + x%10;
       if(tmp/10 != result)
           return 0;        result = tmp;        x = x/10;    }
   return flag*result;    } };
   
int main() {
   int s1=120300;
   int s2=1534236469;
   int s3=1563847412;    Solution s;
   cout<<s1<<"-->"<<s.reverse(s1)<<endl;
   cout<<s2<<"-->"<<s.reverse(s2)<<endl;
   cout<<s3<<"-->"<<s.reverse(s3)<<endl;
   return 0; } 运行结果:
120300-->3021
1534236469-->0
1563847412-->0

每日一道算法——String to Integer
Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

spoilers alert… click to show requirements for atoi.

Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

近期将以NLP领域论文分享及注意力机制的代码实践为主,每天一道算法会保持更新。欢迎大家留言分享深度学习知识或交流每日一道算法的思路,本期算法的解决方法将在下一期中讲解。


你可能会感兴趣的文章有:

[DLdigest-7] 每日一道算法

[DLdigest-6] 每日一道算法

[DLdigest-5] 每日一道算法

[DLdigest-4] 每日一道算法

[DLdigest-3] Python中让你无法预测的“是”

[DLdigest-2] AlphaGo Zero是自由的

[DLdigest-1] 有人用量子场论来解释深度神经网络

详解TensorFlow Eager命令式执行环境

动态层归一化(Dynamic Layer Normalization)

端对端的深度卷积神经网络在语音识别中的应用

SampleRNN语音合成模型

详述DeepMind wavenet原理及其TensorFlow实现

Layer Normalization原理及其TensorFlow实现

Batch Normalization原理及其TensorFlow实现

Maxout Network原理及其TensorFlow实现

时延神经网络(TDNN)原理及其TensorFlow实现

ConvLSTM原理及其TensorFlow实现

Network-in-Network原理及其TensorFlow实现

如何基于TensorFlow实现ResNet和HighwayNet

深度学习每日摘要|坚持技术,追求原创

微信ID:deeplearningdigest
长按二维码关注我
登录查看更多
4

相关内容

在数学和计算机科学之中,算法(Algorithm)为一个计算的具体步骤,常用于计算、数据处理和自动推理。精确而言,算法是一个表示为有限长列表的有效方法。算法应包含清晰定义的指令用于计算函数。 来自维基百科: 算法
打怪升级!2020机器学习工程师技术路线图
专知会员服务
96+阅读 · 2020年6月3日
Transformer文本分类代码
专知会员服务
116+阅读 · 2020年2月3日
强化学习最新教程,17页pdf
专知会员服务
167+阅读 · 2019年10月11日
2019年机器学习框架回顾
专知会员服务
35+阅读 · 2019年10月11日
机器学习入门的经验与建议
专知会员服务
90+阅读 · 2019年10月10日
计算机视觉最佳实践、代码示例和相关文档
专知会员服务
17+阅读 · 2019年10月9日
通过Docker安装谷歌足球游戏环境
CreateAMind
11+阅读 · 2019年7月7日
机器学习线性代数速查
机器学习研究会
18+阅读 · 2018年2月25日
爬了自己的微信,原来好友都是这样的!
七月在线实验室
4+阅读 · 2018年1月18日
各厂推荐算法!
程序猿
17+阅读 · 2018年1月13日
动手写机器学习算法:SVM支持向量机(附代码)
七月在线实验室
12+阅读 · 2017年12月5日
机器学习(29)之奇异值分解SVD原理与应用详解
机器学习算法与Python学习
3+阅读 · 2017年11月30日
机器学习(23)之GBDT详解
机器学习算法与Python学习
12+阅读 · 2017年10月25日
【推荐】树莓派/OpenCV/dlib人脸定位/瞌睡检测
机器学习研究会
9+阅读 · 2017年10月24日
【推荐】视频目标分割基础
机器学习研究会
9+阅读 · 2017年9月19日
【推荐】TensorFlow手把手CNN实践指南
机器学习研究会
5+阅读 · 2017年8月17日
Learning to See Through Obstructions
Arxiv
7+阅读 · 2020年4月2日
A Sketch-Based System for Semantic Parsing
Arxiv
4+阅读 · 2019年9月12日
Arxiv
6+阅读 · 2019年4月8日
Next Item Recommendation with Self-Attention
Arxiv
5+阅读 · 2018年8月25日
VIP会员
相关VIP内容
打怪升级!2020机器学习工程师技术路线图
专知会员服务
96+阅读 · 2020年6月3日
Transformer文本分类代码
专知会员服务
116+阅读 · 2020年2月3日
强化学习最新教程,17页pdf
专知会员服务
167+阅读 · 2019年10月11日
2019年机器学习框架回顾
专知会员服务
35+阅读 · 2019年10月11日
机器学习入门的经验与建议
专知会员服务
90+阅读 · 2019年10月10日
计算机视觉最佳实践、代码示例和相关文档
专知会员服务
17+阅读 · 2019年10月9日
相关资讯
通过Docker安装谷歌足球游戏环境
CreateAMind
11+阅读 · 2019年7月7日
机器学习线性代数速查
机器学习研究会
18+阅读 · 2018年2月25日
爬了自己的微信,原来好友都是这样的!
七月在线实验室
4+阅读 · 2018年1月18日
各厂推荐算法!
程序猿
17+阅读 · 2018年1月13日
动手写机器学习算法:SVM支持向量机(附代码)
七月在线实验室
12+阅读 · 2017年12月5日
机器学习(29)之奇异值分解SVD原理与应用详解
机器学习算法与Python学习
3+阅读 · 2017年11月30日
机器学习(23)之GBDT详解
机器学习算法与Python学习
12+阅读 · 2017年10月25日
【推荐】树莓派/OpenCV/dlib人脸定位/瞌睡检测
机器学习研究会
9+阅读 · 2017年10月24日
【推荐】视频目标分割基础
机器学习研究会
9+阅读 · 2017年9月19日
【推荐】TensorFlow手把手CNN实践指南
机器学习研究会
5+阅读 · 2017年8月17日
Top
微信扫码咨询专知VIP会员