leetcode66-Plus One

2024-03-27 01:04
文章标签 plus one leetcode66

本文主要是介绍leetcode66-Plus One,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这道题目要求整数数组加1,考的点无非就是数组的遍历,不过这道题目是要倒着遍历而已。联想到我们计算加法时候的法则,如果某位数字加1超过10那么就要进位,所以要有一个变量标识这个进位位,特别注意最高位是9并且发生进位的情况

public class plusOne {public static void main(String[] args) {int[] arr = {8,9,9,9};int[] brr = plusOne(arr);for(int i = 0;i<brr.length;i++) {System.out.println(brr[i]);}}public static int[] plusOne(int[] arr) {int count = 0;int len = arr.length;for(int i = len-1;i>=0;i--) {int num = 0;if(i==len-1) {num = arr[i] + 1;} else {num = arr[i] +count;}if(num >= 10) {arr[i] = num % 10;count = num / 10;} else {arr[i] = num;count = 0;}}if(count >= 1) {int[] brr = new int[len+1];brr[0] = 1;for(int i = 1;i<brr.length;i++) {brr[i] = arr[i-1];}return brr;} else {return arr;}}
}

这篇关于leetcode66-Plus One的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/850446

相关文章

【C++ Primer Plus习题】13.4

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "port.h"int main() {Port p1;Port p2("Abc", "Bcc", 30);std::cout <<

Adblock Plus官方规则Easylist China说明与反馈贴(2015.12.15)

-------------------------------特别说明--------------------------------------- 视频广告问题:因Adblock Plus的局限,存在以下现象,优酷、搜狐、17173黑屏并倒数;乐视、爱奇艺播放广告。因为这些视频网站的Flash播放器被植入了检测代码,而Adblock Plus无法修改播放器。 如需同时使用ads

【C++ Primer Plus习题】12.2

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "String.h"using namespace std;int main(){String s1(" and I am a

pytorch torch.nn.functional.one_hot函数介绍

torch.nn.functional.one_hot 是 PyTorch 中用于生成独热编码(one-hot encoding)张量的函数。独热编码是一种常用的编码方式,特别适用于分类任务或对离散的类别标签进行处理。该函数将整数张量的每个元素转换为一个独热向量。 函数签名 torch.nn.functional.one_hot(tensor, num_classes=-1) 参数 t

Mybatis Plus快速重构真批量sql入库操作

Mybatis快速重构真批量sql入库操作 基本思路 重构mybatis默认方法saveBatch和saveOrUpdateBatch的实现 基本步骤 真批量保存实现类InsertBatchMethod真批量更新实现类MysqlInsertOrUpdateBath注册InsertBatchMethod和MysqlInsertOrUpdateBath到EasySqlInjector注册Eas

leetcode#66. Plus One

题目 Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. You may assume the integer do not contain any leading zero, except the number 0 itself. The digi

【C++ Primer Plus习题】12.1

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "Cow.h"using namespace std;int main(){Cow c1;Cow c2("老母牛", "喝奶"

《C Primer Plus》第10 章复习题和编程练习

目录 一、复习题二、编程练习 一、复习题 1. 下面的程序将打印什么内容? #include <stdio.h>int main(){int ref[] = {8, 4, 0 ,2};int *ptr;int index;for (index = 0, ptr = ref; index < 4; index++, ptr++){printf("%d %d\n", ref[in

MyBatis-Plus 框架 QueryWrapper UpdateWrapper 方法修复sql注入漏洞事件

什么是漏洞? 漏洞是指软件、系统或网络中存在的安全弱点或错误,这些弱点可能导致系统遭受攻击或被不当使用。在计算机安全领域,漏洞通常源于编程错误、设计缺陷或配置失误。 对于对象关系映射(ORM)框架来说,漏洞通常指的是设计或实施中的安全问题,这些问题可能让应用程序面临SQL注入攻击的风险。 SQL 注入漏洞 如果ORM框架在执行SQL操作时没有正确过滤或转义用户输入,攻击者可以利用输入的恶意数据

One-Shot Imitation Learning

发表时间:NIPS2017 论文链接:https://readpaper.com/pdf-annotate/note?pdfId=4557560538297540609&noteId=2424799047081637376 作者单位:Berkeley AI Research Lab, Work done while at OpenAI Yan Duan†§ , Marcin Andrychow