其他分享
首页 > 其他分享> > 1556. Thousand Separator

1556. 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:

class Solution {
    public String thousandSeparator(int n) {
        String s = n + "";
        int le = s.length();
        StringBuilder res = new StringBuilder();
        if(le < 4) return s;
        int cur = 0;
        for(int i = le - 1; i > -1; i--) {
            if(cur % 3 == 0 && cur != 0) res.append(".");
            res.append(s.charAt(i));
            cur++;
        }
        return res.reverse().toString();
    }
}

 

标签:cur,1556,int,res,Thousand,Separator,Output,Input,Example
来源: https://www.cnblogs.com/wentiliangkaihua/p/13584423.html