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 realize integer inversion with code

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

Share

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

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!

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