Type Here to Get Search Results !

LeetCode Thousand Separator Solution | CodingHumans |

0

 

Thousand Separator 


Given an integer n, add a dot (".") as the thousands separator and return it in string format.

Example 1:

Input: n = 987

Output: "987"

Example 2:

Input: n = 1234

Output: "1.234"

Example 3:

Input: n = 123456789

Output: "123.456.789"

Example 4:

Input: n = 0

Output: "0"

Constraints:

0 <= n < 2^31


Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.

Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.

HAVE A GOOD DAY 😁


Logic is to look and find the required position and to insert(".") to the  position.

Solution
( Java )


class Solution {
    public String thousandSeparator(int n) {
         StringBuilder s = new StringBuilder().append(n);
        for(int i=s.length()-3;i>0;i-=3){
            s.insert(i,".");
        }
        return s.toString();
        
    }
}

Post a Comment

0 Comments

Top Post Ad

Below Post Ad