当前位置: 首页 > news >正文

Leet -- Plus One

题目描述:


Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.


给定1个数组,完成加1的操作。本题和大整数加法非常类似。 逻辑比较简单,直接实现就可以了
当前位= num[i]%10
进位= num[i]/10



实现代码:



public class Solution {
    public int[] PlusOne(int[] digits) {
        if (digits == null || digits.Length == 0){
		return null;
    	}
    	
    	
    	var len = digits.Length;
    	var c = digits[len-1] == 9 ? 1: 0;
    	
    	digits[len-1] = digits[len-1] == 9 ? 0 : digits[len-1] + 1;
    	
    	for(var i = len - 2;i >= 0 ; i--){
    		var s = digits[i] + c ;
    		c = s / 10;
    		digits[i] = s % 10;
    	}
    	
    	if(c > 0){
    		var arr = new List<int>{1};
    		arr.AddRange(digits);
    		return arr.ToArray();
    	}
    	
    	return digits;
    }
}


相关文章:

  • Leet -- Generate Parentheses
  • LeetCode -- Distinct Subsequences
  • LeetCode -- SpiralOrder
  • Windows 2003下成功配置IIS+Php+Mysql+Zend Optimizer+GD库+Phpmyadmin
  • LeetCode -- WordBreak II
  • Azure 证书配置错误: The service configuration file does not provide the certificate identification
  • Linux精彩一句话最新版
  • LeetCode -- Count Complete Tree Node
  • LeetCode -- Isomorphic Strings
  • LeetCode -- Balanced Binary Tree
  • Linux系统信息查看命令大全
  • LeetCode -- Merge Two sorted lists
  • 汇编语言程序设计的基本方法
  • LeetCode -- Binary Tree Zigzag Level Order Traversal
  • PostgreSQL+PostGIS的使用 1
  • 4个实用的微服务测试策略
  • Angular 响应式表单 基础例子
  • Angular2开发踩坑系列-生产环境编译
  • C学习-枚举(九)
  • PAT A1017 优先队列
  • PhantomJS 安装
  • python大佬养成计划----difflib模块
  • Python进阶细节
  • SpringBoot几种定时任务的实现方式
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 大主子表关联的性能优化方法
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 罗辑思维在全链路压测方面的实践和工作笔记
  • 你真的知道 == 和 equals 的区别吗?
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • hi-nginx-1.3.4编译安装
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解 ...
  • 翻译 | The Principles of OOD 面向对象设计原则
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • ​iOS安全加固方法及实现
  • #if #elif #endif
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (二十四)Flask之flask-session组件
  • (附程序)AD采集中的10种经典软件滤波程序优缺点分析
  • (七)c52学习之旅-中断
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • ... fatal error LINK1120:1个无法解析的外部命令 的解决办法
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .vue文件怎么使用_vue调试工具vue-devtools的安装
  • @FeignClient注解,fallback和fallbackFactory
  • @Transactional类内部访问失效原因详解
  • []sim300 GPRS数据收发程序
  • [ASP]青辰网络考试管理系统NES X3.5
  • [BetterExplained]书写是为了更好的思考(转载)
  • [BZOJ4566][HAOI2016]找相同字符(SAM)
  • [C#]DataTable常用操作总结【转】
  • [C#]科学计数法(scientific notation)显示为正常数字
  • [C/C++]数据结构 深入挖掘环形链表问题