File tree 1 file changed +30
-7
lines changed
scripts/algorithms/T/Thousand Separator
1 file changed +30
-7
lines changed Original file line number Diff line number Diff line change 1
- # Runtime: 44 ms (Top 29.7 %) | Memory: 16.32 MB (Top 23.9 %)
1
+ # Runtime: 17 ms (Top 24.0 %) | Memory: 13.24 MB (Top 48.1 %)
2
2
3
- class Solution :
4
- def thousandSeparator (self , n : int ) -> str :
5
- s = str (n )
6
- s = s [::- 1 ]
7
- res = '.' .join (s [i :i + 3 ] for i in range (0 , len (s ), 3 ))
8
- return res [::- 1 ]
3
+ class Solution (object ):
4
+ def thousandSeparator (self , n ):
5
+ """
6
+ :type n: int
7
+ :rtype: str
8
+ """
9
+ n = str (n )
10
+ if len (n ) <= 3 :
11
+ return str (n )
12
+ result = ""
13
+ dot = '.'
14
+ index = 0
15
+ startPos = len (n ) % 3
16
+ if startPos == 0 :
17
+ startPos += 3
18
+ val = - 1
19
+ while index < len (n ):
20
+ result += n [index ]
21
+ if index == startPos - 1 :
22
+ result += dot
23
+ val = 0
24
+ if val != - 1 :
25
+ val += 1
26
+ if val > 3 and (val - 1 ) % 3 == 0 and index != len (n ) - 1 :
27
+ result += dot
28
+ val = 1
29
+ index += 1
30
+
31
+ return result
You can’t perform that action at this time.
0 commit comments