Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add one to a specified number by Java and javascript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "Java and javascript how to add one to the specified number", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn how to add one to the specified number of Java and javascript "this article.

Topic description

Given a non-negative integer represented by a non-empty array of integers, add one to that number.

The highest digit is stored in the first bit of the array, and only one number is stored for each element in the array.

You can assume that the integer will not start with zero except for the integer 0.

Example 1:

Input: [1Jing 2Jue 3]

Output: [1Jing 2jue 4]

Explanation: enter an array to represent the number 123.

Example 2:

Input: [4, 3, 2, 1]

Output: [4, 3, 2, 2]

Explanation: enter an array to represent the number 4321.

The idea of solving the problem

Tags: array traversal

This problem needs to be sorted out, and it will be more comfortable to deal with it.

If there is no carry in the last bit, you can add one to the last bit, because there is no carry in the last position, and it is impossible to produce carry in front, for example, 45 = > 46.

If the last bit has carry, and the carry stops in the middle position, you need to find the typical mark of carry, that is, 0 after the current bit, then add 1 to the previous bit until it is not 0, for example, 499 = > 500

There is a carry at the end, and the carry up to the front leads to an extra bit in the result. For this case, it needs to be dealt with separately on the basis of the end of traversal in the second case, for example, 999 = > 1000.

In the following Java and JavaScript code, for the third case, the other bits are assigned 0, Java compares tricky to the direct new array, and JavaScript uses ES6 syntax for assignment

Time complexity: O (n)

Code

Java version

Class Solution {

Public int [] plusOne (int [] digits) {

Int len = digits.length

For (int I = len-1; I > = 0; iMury -) {

Digits [I] + +

Digits [I]% = 10

If (digits [I]! = 0)

Return digits

}

Digits = new int [len + 1]

Digits [0] = 1

Return digits

}

}

JavaScript version

/ * *

* @ param {number []} digits

* @ return {number []}

, /

Var plusOne = function (digits) {

Const len = digits.length

For (let I = len-1; I > = 0; iMury -) {

Digits [I] + +

Digits [I]% = 10

If (digits [I]! = 0)

Return digits

}

Digits = [... Array (len + 1)] .map (_ = > 0)

Digits [0] = 1

Return digits

}

Drawing and interpretation

The above is all the content of the article "how to add one to a specified number by Java and javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report