File tree 5 files changed +58
-5
lines changed
solution/2400-2499/2481.Minimum Cuts to Divide a Circle
5 files changed +58
-5
lines changed Original file line number Diff line number Diff line change 63
63
- 当 $n$ 为奇数时,不存在共线的情况,返回 $n$;
64
64
- 当 $n$ 为偶数时,可以两两共线,返回 $\frac{n}{2}$。
65
65
66
+ 综上,可以得到:
67
+
68
+ $$
69
+ \text{ans} = \begin{cases}
70
+ n, & n \gt 1 \text{ 且 } n \text{ 为奇数} \\
71
+ \frac{n}{2}, & n \text{ 为其它} \\
72
+ \end{cases}
73
+ $$
74
+
66
75
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
67
76
68
77
<!-- tabs:start -->
74
83
``` python
75
84
class Solution :
76
85
def numberOfCuts (self , n : int ) -> int :
77
- return n if n > 1 and n % 2 else n >> 1
86
+ return n if ( n > 1 and n & 1 ) else n >> 1
78
87
```
79
88
80
89
### ** Java**
@@ -111,6 +120,24 @@ func numberOfCuts(n int) int {
111
120
}
112
121
```
113
122
123
+ ### ** TypeScript**
124
+
125
+ ``` ts
126
+ function numberOfCuts(n : number ): number {
127
+ return n > 1 && n & 1 ? n : n >> 1 ;
128
+ }
129
+ ```
130
+
131
+ ### ** C#**
132
+
133
+ ``` cs
134
+ public class Solution {
135
+ public int NumberOfCuts (int n ) {
136
+ return n > 1 && n % 2 == 1 ? n : n >> 1 ;
137
+ }
138
+ }
139
+ ```
140
+
114
141
### ** ...**
115
142
116
143
```
Original file line number Diff line number Diff line change @@ -52,7 +52,7 @@ Also note that the first cut will not divide the circle into distinct parts.
52
52
``` python
53
53
class Solution :
54
54
def numberOfCuts (self , n : int ) -> int :
55
- return n if n > 1 and n % 2 else n >> 1
55
+ return n if ( n > 1 and n & 1 ) else n >> 1
56
56
```
57
57
58
58
### ** Java**
@@ -87,6 +87,24 @@ func numberOfCuts(n int) int {
87
87
}
88
88
```
89
89
90
+ ### ** TypeScript**
91
+
92
+ ``` ts
93
+ function numberOfCuts(n : number ): number {
94
+ return n > 1 && n & 1 ? n : n >> 1 ;
95
+ }
96
+ ```
97
+
98
+ ### ** C#**
99
+
100
+ ``` cs
101
+ public class Solution {
102
+ public int NumberOfCuts (int n ) {
103
+ return n > 1 && n % 2 == 1 ? n : n >> 1 ;
104
+ }
105
+ }
106
+ ```
107
+
90
108
### ** ...**
91
109
92
110
```
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public int NumberOfCuts ( int n ) {
3
+ return n > 1 && n % 2 == 1 ? n : n >> 1 ;
4
+ }
5
+ }
Original file line number Diff line number Diff line change 1
- class Solution :
2
- def numberOfCuts (self , n : int ) -> int :
3
- return n if n > 1 and n % 2 else n >> 1
1
+ class Solution :
2
+ def numberOfCuts (self , n : int ) -> int :
3
+ return n if ( n > 1 and n & 1 ) else n >> 1
Original file line number Diff line number Diff line change
1
+ function numberOfCuts ( n : number ) : number {
2
+ return n > 1 && n & 1 ? n : n >> 1 ;
3
+ }
You can’t perform that action at this time.
0 commit comments