Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize integer inversion with code

Shulou Source: shulou.com Published: 2022-06-02 10:02:30 09月30日 Update

This article focuses on "how to use code to achieve integer inversion", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to use code to achieve integer inversion"!

Given a 32-bit signed integer, you need to reverse the number on each of the integer.

Example 1:

Enter: 123

Output: 321

Example 2:

Enter:-123

Output:-321

Example 3:

Input: 120

Output: 21

Note:

Assuming that our environment can only store 32-bit signed integers, the range of values is [− 231,231 − 1]. Based on this assumption, 0 is returned if the integer overflows after the inversion.

Public int reverse (int x) {try {if (x > = 0) {StringBuilder sb = new StringBuilder (x); String s = sb.reverse () .toString (); return Integer.parseInt (s);} else {String S1 = x + ".substring (1); StringBuilder sb = new StringBuilder (S1); String s = sb.reverse () .toString () Return-Integer.parseInt (s);}} catch (NumberFormatException e) {return 0;}}

My flip string is too low.

Class Solution {public int reverse (int x) {int ans = 0; while (x! = 0) {int pop = x% 10; if (ans > Integer.MAX_VALUE / 10 | | (ans = = Integer.MAX_VALUE / 10 & & pop > 7) return 0 If (ans < Integer.MIN_VALUE / 10 | | (ans = = Integer.MIN_VALUE / 10 & & pop <-8)) return 0; ans = ans * 10 + pop; x / = 10;} return ans;}} so far, I believe you have a deeper understanding of "how to implement integer inversion in code". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Integers code examples input output content symbols learning practical deeper interests characters strings practicality practical simple operation numeric values numbers methods more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology Docker OPPO Reno Microsoft