In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.