leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法
1.原题:
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/
Given an integer number n
, return the difference between the product of its digits and the sum of its digits.
翻译:给定一个数字n,返回乘积和总合的差。
理论上的输入输出:
Input: n = 234
Output: 15
Explanation:
Product of digits = 2 * 3 * 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
2.解题思路:
应该来说是最简单的问题了,这里我们用的是 n % 10的方法,%得出的结果是两个数相除后的余数,因此你对任何正整数用,结果都是其最小位的数字。
然后得到小数之后,使用 / 除法操作符,因为是int,所以不用担心小数。
class Solution {
public:
int subtractProductAndSum(int n)
{
int pd = 1;
int sd = 0;
for (;n > 0 ; n /= 10)
{
pd *= n%10;
sd += n%10;
}
return (pd-sd);
}
};
leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法的更多相关文章
- leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字
1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...
- leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字
1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...
- leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)
Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...
- leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping
1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...
- leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...
- leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST
1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...
- leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero
1.原题: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/ Given an integer n, retur ...
- leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点
1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...
- leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段
1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...
随机推荐
- Spring Cloud报错Error creating bean with name 'requestMappingHandlerMapping'
如果我们使用Spring Cloud的Feign实现熔断,首先需要自定义一个熔断类,实现你的feign接口,然后实现方法,这些方法就是熔断方法,最后需要在你的feign接口中指定fallback为自定 ...
- Maven设置http代理
背景:有时候公司处于安全因素的考虑,需要通过代理访问因特网,这种情况需要为Maven设置htpp代理 设置步骤如下: 1 首先确认自己无法访问外网公共的中央仓库(可通过ping repo1.maven ...
- 【Android - 自定义View】之View的工作过程简介
View的工作过程分为三个过程: View的measure过程: View的layout过程: View的draw过程. 我们知道,一个Activity就是一个窗口,这个窗口中包含一个Window.一 ...
- k8s 上部署 Redis 三主三从 集群
目录 介绍 为什么要使用Redis? 什么是Redis群集? 在Kubernetes中部署Redis集群 从 GitHub 上下载: 创建pv 创建statefulset 创建service 初始化 ...
- Scala: Case classes
Case classes are like regular classes with a few key differences which we will go over. Case classes ...
- ThinkPHP3.2.2实现持久登录(记住我)功能的方法
实现持久登录,即用户在登录时,勾选了"记住我"之后,无论是否关闭浏览器,只要不退出登录,在指定的时间内始终保持登录状态(缺点是在另一台电脑上登录过后,之前那台电脑就不能继续保持登录 ...
- js4——字符转化
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 华为鲁勇:5G+云+AI三大核心引擎将驱动广州数字经济发展
[摘要] 华为云将携手广州政企,全面释放 5G+云+AI新动能,推动广州步入高质量发展新阶段. [中国,广州] 广州是一座多样化的城市,在历史上被誉为千年的商都,现在,广州也在持续的开放.融合.与时俱 ...
- Solr单机配置详解
Solr 单机版安装 安装环境 安装 jdk:JDK 版本: jdk-8u11-linux-x64.tar.gz 环境变量配置; export JAVA_HOME=/usr/local/jdk exp ...
- MVC效验器
步骤一:导入依赖 <!--数据效验--> <dependency> <groupId>org.hibernate</groupId> <artif ...