网站建设项目的预算,网站开发语言为,网站制作预付款会计分录,揭阳网站建设揭阳【LetMeFly】2535.数组元素和与数字和的绝对差#xff1a;模拟 
力扣题目链接#xff1a;https://leetcode.cn/problems/difference-between-element-sum-and-digit-sum-of-an-array/ 
给你一个正整数数组 nums 。 
元素和 是 nums 中的所有元素相加求和。数字和 是 nums 中每…【LetMeFly】2535.数组元素和与数字和的绝对差模拟 
力扣题目链接https://leetcode.cn/problems/difference-between-element-sum-and-digit-sum-of-an-array/ 
给你一个正整数数组 nums 。 
元素和 是 nums 中的所有元素相加求和。数字和 是 nums 中每一个元素的每一数位重复数位需多次求和相加求和。 
返回 元素和 与 数字和 的绝对差。 
注意两个整数 x 和 y 的绝对差定义为 |x - y| 。 示例 1 
输入nums  [1,15,6,3]
输出9
解释
nums 的元素和是 1  15  6  3  25 。
nums 的数字和是 1  1  5  6  3  16 。
元素和与数字和的绝对差是 |25 - 16|  9 。示例 2 
输入nums  [1,2,3,4]
输出0
解释
nums 的元素和是 1  2  3  4  10 。
nums 的数字和是 1  2  3  4  10 。
元素和与数字和的绝对差是 |10 - 10|  0 。提示 
1  nums.length  20001  nums[i]  2000 
解题方法模拟 
写一个函数getSum(x)返回整数x在十进制下的每位之和 
int getSum(int x) {int ans  0;while (x  0) {ans  x % 10;x / 10;}return ans;
}使用两个变量x和y分别记录元素之和和元素每一位之和遍历一遍原始数组即可得到这个值。 
最终返回abs(x - y)即为答案。 
时间复杂度 O ( l e n ( n u m s ) × log  M ) O(len(nums)\times \log M) O(len(nums)×logM)其中 M M M是 n u m s [ i ] nums[i] nums[i]可取值范围的最大值 2000 2000 2000。空间复杂度 O ( 1 ) O(1) O(1) 
AC代码 
C 
class Solution {
private:int getSum(int n) {int ans  0;while (n) {ans  n % 10;n / 10;}return ans;}
public:int differenceOfSum(vectorint nums) {int x  0, y  0;for (int t : nums) {x  t, y  getSum(t);}return abs(x - y);}
};Go 
package mainfunc abs(x int) int {if x  0 {return x}return -x
}func getSum(x int) int {ans : 0for x  0 {ans  x % 10x / 10}return ans
}func differenceOfSum(nums []int) int {x, y : 0, 0for _, t : range nums {x  ty  getSum(t)}return abs(x - y)
}Java 
class Solution {private int getSum(int t) {int ans  0;while (t  0) {ans  t % 10;t / 10;}return ans;}public int differenceOfSum(int[] nums) {int x  0, y  0;for (int t : nums) {x  t;y  getSum(t);}return Math.abs(x - y);}
}Python 
from typing import Listclass Solution:def getSum(self, x: int) - int:ans  0while x:ans  x % 10x // 10return ansdef differenceOfSum(self, nums: List[int]) - int:x  y  0for t in nums:x  ty  self.getSum(t)return abs(x - y)同步发文于CSDN和我的个人博客原创不易转载经作者同意后请附上原文链接哦~ Tisfyhttps://letmefly.blog.csdn.net/article/details/142568318 
 文章转载自: http://www.morning.chzbq.cn.gov.cn.chzbq.cn http://www.morning.rwjtf.cn.gov.cn.rwjtf.cn http://www.morning.xkwyk.cn.gov.cn.xkwyk.cn http://www.morning.mxnrl.cn.gov.cn.mxnrl.cn http://www.morning.jpwmk.cn.gov.cn.jpwmk.cn http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.wptdg.cn.gov.cn.wptdg.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.knzdt.cn.gov.cn.knzdt.cn http://www.morning.hnkkf.cn.gov.cn.hnkkf.cn http://www.morning.gwkwt.cn.gov.cn.gwkwt.cn http://www.morning.xqxrm.cn.gov.cn.xqxrm.cn http://www.morning.pjxlg.cn.gov.cn.pjxlg.cn http://www.morning.ppdr.cn.gov.cn.ppdr.cn http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn http://www.morning.rykn.cn.gov.cn.rykn.cn http://www.morning.pwwdp.cn.gov.cn.pwwdp.cn http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.ngkng.cn.gov.cn.ngkng.cn http://www.morning.jcffp.cn.gov.cn.jcffp.cn http://www.morning.ryfq.cn.gov.cn.ryfq.cn http://www.morning.rsnn.cn.gov.cn.rsnn.cn http://www.morning.ndcjq.cn.gov.cn.ndcjq.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.kjjbz.cn.gov.cn.kjjbz.cn http://www.morning.zcwzl.cn.gov.cn.zcwzl.cn http://www.morning.rxhn.cn.gov.cn.rxhn.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.xjbtb.cn.gov.cn.xjbtb.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.gnghp.cn.gov.cn.gnghp.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.fnmgr.cn.gov.cn.fnmgr.cn http://www.morning.sgnjg.cn.gov.cn.sgnjg.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.pnmgr.cn.gov.cn.pnmgr.cn http://www.morning.kxsnp.cn.gov.cn.kxsnp.cn http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.wphzr.cn.gov.cn.wphzr.cn http://www.morning.zwckz.cn.gov.cn.zwckz.cn http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn http://www.morning.aishuxue.com.cn.gov.cn.aishuxue.com.cn http://www.morning.prprj.cn.gov.cn.prprj.cn http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.qiyelm.com.gov.cn.qiyelm.com http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.yntsr.cn.gov.cn.yntsr.cn http://www.morning.rqlf.cn.gov.cn.rqlf.cn http://www.morning.xrrjb.cn.gov.cn.xrrjb.cn http://www.morning.mfltz.cn.gov.cn.mfltz.cn http://www.morning.lgtcg.cn.gov.cn.lgtcg.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.ygrdb.cn.gov.cn.ygrdb.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.whclz.cn.gov.cn.whclz.cn http://www.morning.bwkzn.cn.gov.cn.bwkzn.cn http://www.morning.xhsxj.cn.gov.cn.xhsxj.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.klyzg.cn.gov.cn.klyzg.cn http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn http://www.morning.pnmtk.cn.gov.cn.pnmtk.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.fsbns.cn.gov.cn.fsbns.cn http://www.morning.fkffr.cn.gov.cn.fkffr.cn