Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1556. 千位分隔数 #97

Open
yankewei opened this issue Jan 24, 2021 · 1 comment
Open

1556. 千位分隔数 #97

yankewei opened this issue Jan 24, 2021 · 1 comment
Labels
字符串 题目类型为字符串 简单 题目难度为简单

Comments

@yankewei
Copy link
Owner

给你一个整数 n,请你每隔三位添加点(即 "." 符号)作为千位分隔符,并将结果以字符串格式返回。

示例 1:

输入:n = 987
输出:"987"

示例 2:

输入:n = 1234
输出:"1.234"

示例 3:

输入:n = 123456789
输出:"123.456.789"

示例 4:

输入:n = 0
输出:"0"

提示:

  • 0 <= n < 2^31

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/thousand-separator
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

@yankewei yankewei added the 字符串 题目类型为字符串 label Jan 24, 2021
@yankewei
Copy link
Owner Author

字符串翻转

func thousandSeparator(n int) string {
    var ret strings.Builder
    s := strconv.Itoa(n)
    count := 0
    for i := len(s)-1; i >= 0; i-- {
	if count % 3 == 0 && count != 0{
	    ret.WriteByte('.')
	}
	ret.WriteByte(s[i])
	count++
    }
    s = ret.String()
    var r strings.Builder
    for i := len(s)-1; i >= 0; i-- {
	r.WriteByte(s[i])
    }
    return r.String()
}

@yankewei yankewei added the 简单 题目难度为简单 label Jan 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
字符串 题目类型为字符串 简单 题目难度为简单
Projects
None yet
Development

No branches or pull requests

1 participant