当前位置:网站首页>LeetCode1556. Thousand separated number
LeetCode1556. Thousand separated number
2022-07-06 16:43:00 【Daylight629】
1556. Thousands of separators
Give you an integer n
, Please add points every three ( namely “.” Symbol ) As a thousand separator , And return the result 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"
Tips :
0 <= n < 2^31
Two 、 Method 1
simulation , Pay attention to do-while , Because there are test cases equal to 0 Of
class Solution {
public String thousandSeparator(int n) {
int count = 0;
StringBuilder res = new StringBuilder();
do {
int cnt = n % 10;
n /= 10;
count++;
res.append(cnt);
if (count % 3 == 0 && n != 0) {
res.append('.');
}
} while (n != 0);
res.reverse();
return res.toString();
}
}
Complexity analysis
- Time complexity :O(logn).
- Spatial complexity :O(logn).
边栏推荐
- Oneforall installation and use
- Cmake Express
- Research Report on market supply and demand and strategy of China's four seasons tent industry
- sublime text 代码格式化操作
- Calculate the time difference
- js时间函数大全 详细的讲解 -----阿浩博客
- Li Kou: the 81st biweekly match
- 指定格式时间,月份天数前补零
- Codeforces round 797 (Div. 3) no f
- Market trend report, technological innovation and market forecast of double door and multi door refrigerators in China
猜你喜欢
随机推荐
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
Cmake Express
第7章 __consumer_offsets topic
音视频开发面试题
Codeforces Round #801 (Div. 2)A~C
Anaconda下安装Jupyter notebook
input 只能输入数字,限定输入
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)
Chapter 5 yarn resource scheduler
【锟斤拷】的故事:谈谈汉字编码和常用字符集
QT implementation fillet window
解决Intel12代酷睿CPU单线程调度问题(二)
Research Report on market supply and demand and strategy of Chinese table lamp industry
Simple records of business system migration from Oracle to opengauss database
视频压缩编码和音频压缩编码基本原理
Research Report on market supply and demand and strategy of China's four seasons tent industry
力扣leetcode第 280 场周赛
useEffect,函数组件挂载和卸载时触发
Market trend report, technical innovation and market forecast of double-sided foam tape in China
Discussion on QWidget code setting style sheet