question:

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 = 111 + 1 = 2. Since 2 has only one digit, return it.

answer:(which don't think by myself)

思路:

举例:

38,11,2    38-2=36(38-11=27,11-2=9,都是9的倍数)

57,12,3    57-3=54(57-12=45,12-3=9,都是9的倍数)

所以直接用57/9 得余数为3 ??

其实不对.......忘记了特殊情况(就是整除的时候)

18,9

余数为0,不符合上述方法

所以需要先减去一个数,最后余数再加上一个数,可以避免这种情况,但是可以随意先加后减一个数吗??

不可以,举例10-2=8 8对9取余数为8,8+2=10,不符合条件

所以只能是1

leetcode笔记--6 Add Digits的更多相关文章

  1. 【LeetCode】258. Add Digits (2 solutions)

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

  2. 【一天一道LeetCode】#258. Add Digits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【LeetCode】 258. Add Digits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...

  4. 【LeetCode】258. Add Digits

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

  5. LeetCode OJ:Add Digits(数字相加)

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

  6. LeetCode算法题-Add Digits(Java实现-3种解法)

    这是悦乐书的第199次更新,第207篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第63题(顺位题号是258).给定非负整数num,重复添加其所有数字,直到结果只有一位数 ...

  7. leetcode笔记:Add Binary

    一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...

  8. LeetCode 题解之Add Digits

    1.问题描述 2.问题分析 循环拆分数字,然求和判断. 3.代码 int addDigits(int num) { ) return num; int result = num; do{ vector ...

  9. LeetCode:Add Digits - 非负整数各位相加

    1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...

随机推荐

  1. 异常:Caused by: java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z

    Spring3.0 + Hibernate3.5:启动服务器报:Caused by: java.lang.NoSuchMethodError: javax.persistence.OneToMany. ...

  2. POJ-3484 Showstopper---二分+前缀和

    题目链接: https://cn.vjudge.net/problem/POJ-3484 题目大意: 给出一系列等差数列,给出第一项和最后一项和公差 这些等差数列中每个数出现的次数只有一个是奇数,找出 ...

  3. 如何从GitHub获取源代码

    如何从GitHub获取源代码 github是当前流行的开源项目托管网站,里面有成千上万的项目值得学习和借鉴,可以把项目源代码下载到本地研究.本文介绍如何获取github的源代码. 方法1 - 克隆(C ...

  4. AngularJS 控制器函数

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  5. apache开启.htaccess及使用方法

    1 . 如何让的本地APACHE器.htaccess 如何让的本地APACHE呢?其实只要简朴修改一下apache的httpd.conf设置就让APACHE.htaccess开启了,来看看操作 打开h ...

  6. Struts2 第四讲 -- Struts2的基本配置

    5.struts2的基本配置 5.1 struts2的访问连接url 在struts1中,通过<action path=“/primer/helloWorldAction.action”> ...

  7. 修改状态栏,电池,wifi的颜色为白色

    修改状态栏,电池,wifi的颜色为白色 在info里面设置View controller-based status bar appearance,为no

  8. jquery mobile 移动web(3)

    可折叠功能块. div 元素的 data-role 属性设置为 collapsible 代码如下: <div data-role="collapsible"> < ...

  9. #leetcode刷题之路20-有效的括号

    #include <iostream> #include <string> #include <stack> using namespace std; bool i ...

  10. go 下面定义嵌套结构

    package main import ( "fmt" ) const ( URL = "http://www.163.com" UID = "adm ...