当前位置:网站首页>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).
边栏推荐
- 业务系统从Oracle迁移到openGauss数据库的简单记录
- Educational Codeforces Round 122 (Rated for Div. 2)
- Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog
- Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
- Remove the border when input is focused
- Base dice (dynamic programming + matrix fast power)
- Raspberry pie 4B installation opencv3.4.0
- Chapter 2 shell operation of hfds
- 两个礼拜速成软考中级软件设计师经验
- Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
猜你喜欢

Log statistics (double pointer)

图像处理一百题(1-10)

第7章 __consumer_offsets topic

简单尝试DeepFaceLab(DeepFake)的新AMP模型

Audio and video development interview questions

Summary of game theory

Spark独立集群动态上线下线Worker节点

Browser print margin, default / borderless, full 1 page A4

QT simulates mouse events and realizes clicking, double clicking, moving and dragging

Installation and configuration of MariaDB
随机推荐
Codeforces Round #771 (Div. 2)
图像处理一百题(1-10)
Chapter 7__ consumer_ offsets topic
How to insert mathematical formulas in CSDN blog
Spark独立集群Worker和Executor的概念
Acwing - game 55 of the week
业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
Anaconda下安装Jupyter notebook
Cmake error: could not create named generator visual studio 16 2019 solution
Problem - 922D、Robot Vacuum Cleaner - Codeforces
Problem - 1646C. Factorials and Powers of Two - Codeforces
Chapter 2 shell operation of hfds
Effet d'utilisation, déclenché lorsque les composants de la fonction sont montés et déchargés
< li> dot style list style type
Generate random password / verification code
Spark independent cluster dynamic online and offline worker node
JS time function Daquan detailed explanation ----- AHAO blog
Solve the problem that intel12 generation core CPU single thread only runs on small cores
antd upload beforeUpload中禁止触发onchange