当前位置:网站首页>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).
边栏推荐
- Li Kou - 298th weekly match
- JS encapsulates the method of array inversion -- Feng Hao's blog
- Market trend report, technological innovation and market forecast of double door and multi door refrigerators in China
- 业务系统从Oracle迁移到openGauss数据库的简单记录
- <li>圆点样式 list-style-type
- Anaconda下安装Jupyter notebook
- Soft music -js find the number of times that character appears in the string - Feng Hao's blog
- 图像处理一百题(11-20)
- Li Kou leetcode 280 weekly match
- 本地可视化工具连接阿里云centOS服务器的redis
猜你喜欢
Advancedinstaller installation package custom action open file
SQL快速入门
Chapter 2 shell operation of hfds
提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
Raspberry pie 4B installation opencv3.4.0
Chapter 6 datanode
Discussion on QWidget code setting style sheet
Log statistics (double pointer)
Ffmpeg command line use
Simply try the new amp model of deepfacelab (deepfake)
随机推荐
SF smart logistics Campus Technology Challenge (no T4)
Specify the format time, and fill in zero before the month and days
第五章 Yarn资源调度器
Detailed explanation of FLV format
第2章 HFDS的Shell操作
第6章 DataNode
Input can only input numbers, limited input
Market trend report, technical innovation and market forecast of China's desktop capacitance meter
Chapter 5 detailed explanation of consumer groups
Log statistics (double pointer)
Submit several problem records of spark application (sparklauncher with cluster deploy mode)
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
Tencent interview algorithm question
Generate random password / verification code
SQL快速入门
新手必会的静态站点生成器——Gridsome
Chapter 6 rebalance details
Codeforces Global Round 19
sublime text 代码格式化操作
Tert butyl hydroquinone (TBHQ) Industry Research Report - market status analysis and development prospect forecast