LeetCode 258. Add Digits

OpenManus本地部署实战:从源码编译到浏览器路径优化全解析 本文详细解析了OpenManus本地部署的全过程,从源码编译到浏览器路径优化。涵盖环境准备、源码获取、模型配置、依赖安装等关键步骤,特别针对Windows系统下的chrome.exe路径修改提供了实用解决方案,帮助开发者高效完成部署并优化性能。 阅读详情

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

For example:

Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.

拿到题目首先想到的就是利用循环解决问题。js中见过类似问题,先用JavaScript来解决。

解法一:

/**
 * @param {number} num
 * @return {number}
 */
var addDigits = function(num) {
    var digitsString = num + "";
    var str = digitsString.split("");
   
    while(str.length != 1){
        var a = 0;
        for(var i = 0; i < str.length; i++){
            a += parseInt(str[i]);
        }
        
        str = a.toString().split("");
    }
    
    return parseInt(str[0]);
};
利用类似的思想,用Java代码实现:

public class Solution {
    public int addDigits(int num) {
        String digitsString = num + "";
        String str[] = digitsString.split("");
        while(str.length != 1){
            int a = 0;
            for(int i = 0; i < str.length; i++){
                a += Integer.parseInt(str[i]);
            }
            digitsString = a + "";
            str = digitsString.split("");
        }
        
        return Integer.parseInt(str[0]);
    }
}

总结

1.总结Java中的parseInt方法属于Integer对象,可以有一个或两个参数,返回一个基本数据类型int。类似的方法还有Double.parseDouble()等。

2.split()函数用于按字符来切割字符串,并返回一个数组。以下转自JavaScript split() 方法-w3school

stringObject.split(separator,howmany)
参数描述
separator 必需。字符串或正则表达式,从该参数指定的地方分割 stringObject。
howmany 可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
如果把空字符串 ("") 用作  separator ,那么 stringObject 中的每个字符之间都会被分割。

也使用正则表达式作为 separator:

解法二:

转换为字符串的操作略显复杂,可以直接对数据操作:

public class Solution {
    public int addDigits(int num) {
        /*
        解法一
        */
        // String digitsString = num + "";
        // String str[] = digitsString.split("");
        // while(str.length != 1){
        //     int a = 0;
        //     for(int i = 0; i < str.length; i++){
        //         a += Integer.parseInt(str[i]);
        //     }
        //     digitsString = a + "";
        //     str = digitsString.split("");
        // }
        
        // return Integer.parseInt(str[0]);
        /*
        解法二
        */
        while(num >= 10){
            num = (num / 10) + (num % 10);
        }
        return num;
        
    }
}
这种解法简单易懂,不做赘述。

解法三:

此题还有一个follow up:

Follow up:
Could you do it without any loop/recursion in O(1) runtime?

并给出了一些提示:可以参考Digital root-Wikipedia

任何数字的题目都是有规律可循的,

public class Solution {
    public int addDigits(int num) {
        /*
        解法一
        */
        // String digitsString = num + "";
        // String str[] = digitsString.split("");
        // while(str.length != 1){
        //     int a = 0;
        //     for(int i = 0; i < str.length; i++){
        //         a += Integer.parseInt(str[i]);
        //     }
        //     digitsString = a + "";
        //     str = digitsString.split("");
        // }
        
        // return Integer.parseInt(str[0]);
        /*
        解法二
        */
        // while(num >= 10){
        //     num = (num / 10) + (num % 10);
        // }
        // return num;
        /*
        解法三
        */
        return 1 + (num-1)%9;
    }
}


相关推荐

飞行器坐标转换

本文定义的是右手直角坐标系,x−y−zx-y-zx−y−z轴分别为北-天-东。从AAA坐标系到BBB坐标系是分别绕y−z−xy-z-xy−z−x轴,即天-东-北旋转ψ−θ−γψ−θ−γ。如果AAA是惯性系,BBB是机体系,则这三个角度分别为偏航角、俯仰角、横滚角。图1 惯性系到机体系。

gongfuyd的博客 1365

[LeetCode]258. Add Digits

[LeetCode]258. Add Digits题目描述思路循环相加每一位 每次获取num的个位,加到和中,num = num / 10,循环直至num / 10为0,最后在外层sum += num, 对得到的sum进行判断,若大于等于10,递归处理 否则输出 详见代码代码class Solution { public: int addDigits(int num) {

Lcharon的博客 3466

Centos7搭建OpenStack+创建iaas云主机

主要说明在控制节点和计算节点上,通过脚本的形式,安装OpenStack的iaas安装步骤和创建可运行的云主机。提供实施简单、可大规模扩展、丰富、标准统一的云计算管理平台。

weixin_48218106的博客 1万+

leetcode.258.Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

RaymondWong的博客 256

Xilinx Aurora 8B/10B IP核详解和仿真

Xilinx Aurora 8b/10b IP核的详解,以及使用与仿真示例,对IP核的配置和如何仿真有非常详细的说明,除了对IP核官方文档的内容进行了解析以外,还对GTX引脚的选择等比较容易混淆和不好理解的地方进行了说明。最后对IP核的仿真运行进行了STEP BY STEP 的说明,对仿真的结果也进行了简要的说明。 该文档对Xilinx FPGA和ZYNQ SOC下 AURORA 8B/10B IP核的使用提供了一个快速上手的指导。

weixin_43354598的博客 1万+

200smart 模拟量转换库的建立

模拟量当前值 =(工程值 - 工程量下限)*(模拟量上限 - 模拟量下限)/ (工程量上限 - 工程量下限)+ 模拟量下限2. 建立变量表3. 程序。

mmxxoo_xxmmp的博客 4250

leetcode 258. Add Digits

Add DigitsGiven a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only on

Decorator2015的博客 528

生信分析实战指南:Linux环境下EMBOSS的安装与核心功能详解

本文详细介绍了在Linux环境下安装和使用EMBOSS工具包的实战指南,涵盖源码编译、包管理器安装和Conda安装三种方法,并演示了序列比对、序列特征提取和引物设计等核心功能。EMBOSS作为生物信息学领域的开源工具集,适用于序列分析、蛋白质结构预测等多种任务,帮助研究人员高效完成生信分析工作。

weixin_29202213的博客 473

LeetCode 258. Add Digits 题解(C++)

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

prayyojay的博客 392

为YOLOv11引入加权双向特征金字塔(BiFPN)

本文介绍了将BiFPN应用于YOLOv11模型以提升边缘设备上小目标检测精度的实践。针对传统FPN在特征融合时的信息丢失问题,采用带可学习权重的BiFPN结构进行改造,实现了不同层级特征的动态融合。重点阐述了WeightedFeatureFusion模块的实现细节、neck结构改造方法及训练部署注意事项。实验表明,该改进使小目标AP提升3.1%,参数量仅增加5%,在精度和效率间取得良好平衡。文章还分享了权重初始化、归一化处理等实用技巧,并强调需根据数据特性评估改进效果,重新计算anchor等关键点。

as1225120794的博客 359

Claude Code(8):使用 skill-creator 打造属于测试人员的 Skills

本文介绍了如何使用Claude Code的skill-creator工具快速创建测试相关的Skills。skill-creator是一个专门用于创建新Skills的Skill,相比手动创建更高效。文章详细演示了安装skill-creator的步骤,并通过创建"测试计划编写助手"的实战案例,展示了如何通过对话方式定义Skill名称、触发条件和功能需求。skill-creator会自动生成完整的SKILL.md文件和目录结构,用户只需确认即可完成Skill创建。

blues_C的博客 556

[leetcode]258. Add Digits

[leetcode]258. Add Digits Analysis 厉害的人总是那么的厉害呀~—— [菜鸡反省中~] Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 简单题,直接看代码吧。 Implement cl...

weixin_32135877的博客 135

(秋招复习)一些SLAM基础

秋招复习--SLAM基础知识篇

weixin_45432823的博客 773

LeetCode - 258. Add Digits

链接 258. Add Digits 题意 给定一个非负整数,反复做如下操作:将它的各位数相加得到一个值,若该值位数仍大于1,重复以上操作,直到位数为1。 思路 模拟即可 代码 Java: public int addDigits(int num) { while(num>=10){ num = (num/10)+num%10; } ...

170

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

leetcode-258.add digits/Digital root

leetcode-258.Add DigitsGiven a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since

绯浅yousa的笔记 668

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Leetcode 258. Add Digits

转载于:https://www.cnblogs.com/winder-knight/p/9804070.html

weixin_30418341的博客 64

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

[LeetCode 258] Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has on

coder 进阶的专栏 8847

leetcode 258: Add Digits

Add Digits Total Accepted: 5383 Total Submissions: 11876 Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 3

西施豆腐渣 7468

LeetCode 258: Add Digits 题解

刚开始以为是一道水题,一下就过了,结果题没看完,要求时间复杂度为O(1)O(1)。LeetCode 258: Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the pr

请叫我AXin 1135

leetcode 258- Add Digits

leetcode题目汇集 — Add Digitsleetcode 258- Add Digits 题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process

知足常乐,知行合一 368

leetcode 258---Add Digits, 关于C++中负数取余

一切源于leetcode 258---Add Digits这道题,https://leetcode.com/problems/add-digits/ 题目的解法很简单,有两种公式,一种是我这种笨笨的人写的: class Solution { public: int addDigits(int num) { if(num==0) return 0;

ChenLI's blog 847

[LeetCode]258. Add Digits(把数各个位上的数字相加)

258. Add DigitsGiven a non-negative integer num, repeatedly add all its digits until the result has only one digit. 题目大意: 给定一个非负整数num,重复地添加所有其数字,直到结果只有一个数字。For example:Given num = 38, the process is

whl_program的博客 920

【leetcode】258. Add Digits

problem 258. Add Digits solution1: class Solution { public: int addDigits(int num) { int sum = 0; int n = num; while(n/10) { sum = n/10 + n...

weixin_30481087的博客 90

LeetCode258——Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has on

代码菌的blog 5176

leetcode-258-Add Digits

Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2.

早睡早起好少年 664
下一篇: LeetCode 104. Maximum Depth of Binary Tree && Minimum Depth of Binary
Appletable
博客等级 码龄14年 0粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值