Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.


📢 preface

  • 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻
  • 🌲 punch in an algorithm every day, which is not only a learning process, but also a sharing process 😜
  • 🌲 tip: the programming languages used in this column are C# and Java
  • 🌲 to maintain a state of learning every day, let us work together to become a god of algorithms 🧐!
  • 🌲 today is the force button algorithm continued to punch the card for the 7th day 🎈!
  • 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻

🌲 Example of original problem

To give you one32A signed integer x of bits that returns the result of reversing the numeric portion of x. If the integer after inversion exceeds32The range of signed integers of bits [−231.2311] and return0. Suppose the environment does not allow storage64Bit integer (signed or unsigned).Copy the code
The sample1: Enter x =123Output:321
Copy the code
The sample2: Enter x =- 123.Output:- 321.
Copy the code
The sample3: Enter x =120Output:21
Copy the code
The sample4: Enter x =0Output:0
Copy the code
Tip: --231 <= x <= 231 - 1
Copy the code

🌻C# method: mathematics

Their thinking

code

public class Solution {
    public int Reverse(int x) {
        int rev = 0;
        while(x ! =0) {
            if (rev < int.MinValue / 10 || rev > int.MaxValue / 10) {
                return 0;
            }
            int digit = x % 10;
            x /= 10;
            rev = rev * 10 + digit;
        }
        returnrev; }}Copy the code

Result the execution result is successful. The execution time is 36ms, and the memory consumption is 15.1MB

Complexity analysis

Time complexity: O (\ log | | x) space complexity: O (1)

🌻Java method: math

Their thinking

This solution is the same idea as C#, but the code is slightly different

code

class Solution {
    public int reverse(int x) {
        int rev = 0;
        while(x ! =0) {
            if (rev < Integer.MIN_VALUE / 10 || rev > Integer.MAX_VALUE / 10) {
                return 0;
            }
            int digit = x % 10;
            x /= 10;
            rev = rev * 10 + digit;
        }
        returnrev; }}Copy the code

Result the execution result passes. The execution time is 1ms, and the memory consumption is 35.6MB

Complexity analysis

Time complexity: O (\ log | | x) space complexity: O (1)


💬 summary

  • Today is the seventh day of buckle algorithm clocking!
  • This paper uses C# and Java programming languages to solve the problem
  • Some methods are also written by the god of reference force buckle, and they are also shared while learning, thanks again to the algorithm masters
  • That’s the end of today’s algorithm sharing, see you tomorrow!

Sync a few articles to Tencent cloud community, here simply say! My blog will be synchronized to Tencent Cloud + community, invite you to join:Cloud.tencent.com/developer/s…