leetcode-1556-easy

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"
Constraints:

0 <= n <= 231 - 1

思路一:循环取余

public String thousandSeparator(int n) {
    if (n == 0) return "0";
    StringBuilder sb = new StringBuilder();

    int count = 0;
    while (n > 0) {
        if (count % 3 == 0 && count > 0) {
            sb.insert(0, '.');
        }

        sb.insert(0, n % 10);
        n /= 10;
        count++;
    }

    return sb.toString();
}
posted @   iyiluo  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
点击右上角即可分享
微信分享提示