LeetCode 7. Reverse Integer ( C ) - Easy

TVS 管击穿电压与钳位电压的工程解析 TVS二极管的核心参数击穿电压(VBR)和钳位电压(VC)常被混淆,但二者具有本质区别。VBR是 阅读详情

同步发于 JuzerTech 网站,里面有我软、硬件学习的纪录与科技产品开箱,欢迎进去观看。

题目为给定一个整数 ( INT ) ,反转此整数 ( INT )。

题目与范例如下

 

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).


Example 1:

Input: x = 123
Output: 321
Example 2:

Input: x = -123
Output: -321
Example 3:

Input: x = 120
Output: 21
Example 4:

Input: x = 0
Output: 0


Constraints:

-231 <= x <= 231 - 1

 

我的解题策略为,每次从 x 取出最后一位数加到 total ( 先对 total x 10 进位 ) ,在加之前都先判别是否溢位。这边有个小技巧 if ( total * 10 < INT_MAX ) 要写成 if ( total < INT_MAX/10 ) ,不然在判别时就会溢位了。

下方为我的代码

int reverse(int x){
    int total = 0,max = INT_MAX/10,min = INT_MIN/10;
    while(x != 0){
        if(total<=max && total >= min){
            total *= 10;
            total += x%10;
            x/=10;
        }
        else
            return 0;
    }
    return total;
}

下方为时间与空间之消耗

Runtime: 4 ms, faster than 55.81 % of C online submissions for Reverse Integer.

Memory Usage: 5.5 MB, less than 94.79 % of C online submissions for Reverse Integer.

 

 
华为昇腾云平台适配Baichuan2大模型记录 文件发现报错:ValueError: The pipeline stage 2 in auto_parallel_context is not equal to the pipeline_stage 1 in the config.run_baichuan2_7b.yaml`,发现默认设备数是16卡,对应的pipeline_stage为2,但实际是8卡,那就改成下面的试试看。,将Baichuan2_7B_Chat.ckpt文件复制进去,将。点击镜像管理,突然发现有个2_2_0,赶紧新建了一个,成功。 阅读详情

相关推荐

ICESat-2数据下载的两种方法

ICESat-2数据下载

weixin_46341898的博客 2899

7. Reverse Integer(Leetcode每日一题-2021.05.03)

Problem Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (

Bryan要加油 601

别再问ARM麒麟怎么装微信了!手把手教你用铠大师搞定Windows软件(飞腾/海思芯片实测)

本文详细介绍了如何在ARM架构的信创电脑(如飞腾/海思芯片)上使用铠大师安装和运行Windows软件,解决微信、Office等常用软件的兼容性问题。通过实测数据对比,铠大师在性能和易用性上显著优于传统方案,为麒麟系统用户提供了一套完整的解决方案。

weixin_28736355的博客 892

leetcode 7. Reverse Integer

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or

virtual_YL的博客 325

从JT808/JT1078协议到多端协同:构建高并发北斗车辆监控平台的技术实践

本文详细介绍了基于JT808/JT1078协议构建高并发北斗车辆监控平台的技术实践,包括协议解析、高并发架构设计、多端协同实现及数据持久化策略。通过Netty、Kafka等技术优化,系统成功支撑日均2000万条消息处理,为GPS车辆动态监控系统提供了可靠解决方案。

weixin_28358083的博客 224

信道估计之MMSE算法

信道估计之MMSE算法前言MMSE估计的原理总结 前言        前篇分析了LS信道估计算法,也说明了由于没有考虑SNR的影响,所以LS算法不适合在低信噪比的情况下使用。本篇来学习信道估计的另外一种常用的算法–MMSE。        为什么说LS没有考虑噪声大小的情况呢,因为LS算法使用的是实际观测量与估计观测量的误差,这并不能精确的代表估计量的真值与估计值的误

大白的博客 2万+

LeeCode_7.整数反转(边界条件)

一、介绍 1.题目描述 题目链接:https://leetcode-cn.com/problems/reverse-integer/ Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume.

Netceor的博客 372

Cartographer从安装到实战:手把手教你运行2D/3D建图与定位

本文提供了一份详尽的Cartographer从零安装到实战应用的完整指南。内容涵盖环境准备、依赖编译、源码构建等关键步骤,并手把手指导读者运行2D/3D官方数据集进行建图与定位测试。文章深入解析了参数配置与自定义数据适配方法,帮助开发者快速掌握这一强大的实时同步定位与建图工具,并将其应用于机器人导航与自动驾驶等实际场景。

rice5的博客 949

7.[LeetCode]Reverse Integer

我的算法:public class Solution { public int reverse(int x) { boolean isPos = (x >= 0)?true:false; x = (isPos)?x:(-x); String num = String.valueOf(x); StringBuilder numBu

薛昭君的博客 349

绕过限制?实测修改UA在PC端运行微信小程序的可行性及风险

本文探讨了通过修改User-Agent(UA)在PC端运行微信小程序的技术可行性及风险。分析了小程序设备限制的技术原理,包括`wx.getSystemInfo`API和UA检测机制,并提供了多种修改方案及其局限性。同时强调了潜在的安全和法律风险,推荐使用官方模拟器或安卓模拟器等更安全的替代方案。

weixin_30879169的博客 363

【Bug已解决】Claude Code WebFetch 报错 Unable to verify if domain is safe to fetch 解决方案

方案适用场景推荐指数放行域名校验接口根本解决方式,需要网络管理员配合⭐⭐⭐⭐⭐curl 手动验证具体拦截环节精确定位问题,避免排查方向跑偏⭐⭐⭐⭐⭐排查企业 SSL 中间人代理证书企业统一代理策略较严格的环境⭐⭐⭐⭐检查系统时间准确性排除证书有效期误判的可能性⭐⭐⭐手动复制内容作为过渡方案网络策略调整周期较长的场景⭐⭐⭐报错的本质是**WebFetch。

一键难忘的博客 4502

LeetCode刷题笔记#7 整数反转 Reverse Integer

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or.

Karthus77的博客 361

【Matlab】如何使用Simulink进行电池建模和SOC预估

“通过Simulink电池建模进行SOC预估,其实挺容易!” 1 声明 本文只描述一般性电池建模方法,文中出现的电池相关数据来自一些论文或者外文文献,对数据的真实性不做任何保证。 本文参考了不少国内高校的硕士论文和外文文献,所以本篇文章只作为学术性交流,未经...

wanff1204的博客 2万+

LeetCode_Easy心得:7. Reverse Integer(C语言)

7. Reverse Integer

hhhhhyb的博客 835

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

[Leetcode 7, Easy] Reverse integer

Leetcode, easy

算法的实践 629

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

7. Reverse Integer [easy] (Python)

题目链接https://leetcode.com/problems/reverse-integer/题目原文 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 题目翻译反转整数中的数字。 例子1:给定x=123,返回321;例子2:给定x=-123,

Coder_Orz的博客 1万+

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

水果君の日常 3667

Leetcode 7. Reverse Integer-easy

题目描述: https://leetcode.com/problems/reverse-integer/ 输入输出示例: 123 321 -123 -321 120 12 示例AC代码(C语言版)  int reverse(int x) { int res = 0; while(x) { if(res &gt; INT_MAX/10...

wanglj的博客 186

Leetcode算法练习-easy篇-Reverse Integer

Letcode算法练习easy篇-Reverse Integer

曦酱的思维工厂 413

LeetCode——Reverse Integer

LeetCode——Reverse Integer  Leedcode

cassiePython的专栏 892

LCP007 LeetCode 7 Reverse Integer

这里有N个坑。很考验基本功。最初拿到这道题,然后就去边洗衣服边想了。刚开始还想从 x 的高位开始向下,然后把 ans 的从低位到高位排起(川话)。然而太麻烦了。 后来想出了把 x 从低位向高位一位一位地吃掉,然后把啃下来的低位数字*10加上下一个啃下来的低位数字。这样的不算很麻烦的思路。 于是又开始了递归的规划过程。因为之前在朋友家完成第226题时,简直惊艳于某网友的简洁递归代码。“于是再也没能

我是一只小小小小鸟 572

LeetCode刷题(C++)——Reverse Integer(Easy)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before c

YF_Li123的博客 312

LeetCode小白菜笔记[2]:Reverse Integer

LeetCode小白菜笔记[2]:Reverse Integer

VisualCortex 677

LeetCode题解--7. Reverse Integer

7.Reverse_Integer (Easy) 题目:https://leetcode.com/problems/Reverse-Integer/ 代码(github):https://github.com/illuz/leetcode 题意反转一个数。Reverse digits of an integer.Example1: x = 123, return 321 Exa

OSKernelLAB(gatieme) 2885

Leetcode--easy系列1

近期開始刷Leetcode题目。花了一个星期先完毕了easy难度级别的题目,easy级别的题目思路比較简单,但不一定就直接AC了,主要是问题要考虑全然。特别是对特殊情况的处理。 #6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu...

weixin_34143774的博客 124

LeetCode OJ(7)

Reverse Integer可怕,又是一道easy卡了我那么久!总是会出现谜之错误!鄙渣自己的代码:int reverse(int x) { int bit[10]; long long rev_num=0; int sign=0; if(x<0){ sign=1; x*=-1; } int count=0;

#Qingmei的成长# 379
上一篇: LeetCode 6. ZigZag Conversion ( C ) - Medium
下一篇: LeetCode (力扣) 8. String to Integer (atoi) ( C ) - Medium
JuzerTech
博客等级 码龄6年 7粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值