当前位置:网站首页>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).
边栏推荐
- Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
- Advancedinstaller installation package custom action open file
- Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
- Problem - 922D、Robot Vacuum Cleaner - Codeforces
- Spark独立集群Worker和Executor的概念
- Audio and video development interview questions
- 两个礼拜速成软考中级软件设计师经验
- ByteDance new programmer's growth secret: those glittering treasures mentors
- 第5章 NameNode和SecondaryNameNode
- 875. Leetcode, a banana lover
猜你喜欢

Installation and configuration of MariaDB

300th weekly match - leetcode

QT implementation fillet window

第5章 NameNode和SecondaryNameNode

Chapter 2 shell operation of hfds

Simply try the new amp model of deepfacelab (deepfake)

解决Intel12代酷睿CPU单线程只给小核运行的问题

Log statistics (double pointer)

Hbuilder X格式化快捷键设置

Chapter 5 detailed explanation of consumer groups
随机推荐
CMake速成
(lightoj - 1369) answering queries (thinking)
Tree of life (tree DP)
js时间函数大全 详细的讲解 -----阿浩博客
input 只能输入数字,限定输入
Codeforces Round #801 (Div. 2)A~C
Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
Raspberry pie 4B installation opencv3.4.0
Calculate the time difference
Market trend report, technological innovation and market forecast of desktop electric tools in China
Mp4 format details
Simple records of business system migration from Oracle to opengauss database
第三章 MapReduce框架原理
QT implementation fillet window
antd upload beforeUpload中禁止触发onchange
业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
Educational Codeforces Round 122 (Rated for Div. 2)
Study notes of Tutu - process
第5章 消费者组详解
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题