Skip to content

Commit 912702c

Browse files
authored
feat: add solutions to lc problem: No.1344 (doocs#1321)
No.1344.Angle Between Hands of a Clock
1 parent f037490 commit 912702c

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

solution/1300-1399/1344.Angle Between Hands of a Clock/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:数学**
64+
6365
时针每小时移动 30 度,每分钟移动 0.5 度。分针每分钟移动 6 度。如果指针之间的夹角大于 180 度,则取其与 360 度的差值,以确保获得最小的夹角。
6466

67+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
68+
6569
<!-- tabs:start -->
6670

6771
### **Python3**
@@ -82,7 +86,50 @@ class Solution:
8286
<!-- 这里可写当前语言的特殊实现逻辑 -->
8387

8488
```java
89+
class Solution {
90+
public double angleClock(int hour, int minutes) {
91+
double h = 30 * hour + 0.5 * minutes;
92+
double m = 6 * minutes;
93+
double diff = Math.abs(h - m);
94+
return Math.min(diff, 360 - diff);
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
double angleClock(int hour, int minutes) {
105+
double h = 30 * hour + 0.5 * minutes;
106+
double m = 6 * minutes;
107+
double diff = abs(h - m);
108+
return min(diff, 360 - diff);
109+
}
110+
};
111+
```
112+
113+
### **Go**
114+
115+
```go
116+
func angleClock(hour int, minutes int) float64 {
117+
h := 30*float64(hour) + 0.5*float64(minutes)
118+
m := 6 * float64(minutes)
119+
diff := math.Abs(h - m)
120+
return math.Min(diff, 360-diff)
121+
}
122+
```
123+
124+
### **TypeScript**
85125

126+
```ts
127+
function angleClock(hour: number, minutes: number): number {
128+
const h = 30 * hour + 0.5 * minutes;
129+
const m = 6 * minutes;
130+
const diff = Math.abs(h - m);
131+
return Math.min(diff, 360 - diff);
132+
}
86133
```
87134

88135
### **...**

solution/1300-1399/1344.Angle Between Hands of a Clock/README_EN.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@
4242

4343
<!-- tabs:start -->
4444

45+
**Solution 1: Mathematics**
46+
4547
The hour hand moves 30 degrees every hour and an additional 0.5 degrees for each minute. The minute hand moves 6 degrees every minute. If the angle between the hands is greater than 180 degrees, take its difference from 360 degrees to ensure the smallest angle is obtained.
4648

49+
The time complexity is $O(1)$ and the space complexity is $O(1)$.
50+
4751
### **Python3**
4852

4953
```python
@@ -58,7 +62,50 @@ class Solution:
5862
### **Java**
5963

6064
```java
65+
class Solution {
66+
public double angleClock(int hour, int minutes) {
67+
double h = 30 * hour + 0.5 * minutes;
68+
double m = 6 * minutes;
69+
double diff = Math.abs(h - m);
70+
return Math.min(diff, 360 - diff);
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
double angleClock(int hour, int minutes) {
81+
double h = 30 * hour + 0.5 * minutes;
82+
double m = 6 * minutes;
83+
double diff = abs(h - m);
84+
return min(diff, 360 - diff);
85+
}
86+
};
87+
```
88+
89+
### **Go**
90+
91+
```go
92+
func angleClock(hour int, minutes int) float64 {
93+
h := 30*float64(hour) + 0.5*float64(minutes)
94+
m := 6 * float64(minutes)
95+
diff := math.Abs(h - m)
96+
return math.Min(diff, 360-diff)
97+
}
98+
```
99+
100+
### **TypeScript**
61101

102+
```ts
103+
function angleClock(hour: number, minutes: number): number {
104+
const h = 30 * hour + 0.5 * minutes;
105+
const m = 6 * minutes;
106+
const diff = Math.abs(h - m);
107+
return Math.min(diff, 360 - diff);
108+
}
62109
```
63110

64111
### **...**
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
double angleClock(int hour, int minutes) {
4+
double h = 30 * hour + 0.5 * minutes;
5+
double m = 6 * minutes;
6+
double diff = abs(h - m);
7+
return min(diff, 360 - diff);
8+
}
9+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
func angleClock(hour int, minutes int) float64 {
2+
h := 30*float64(hour) + 0.5*float64(minutes)
3+
m := 6 * float64(minutes)
4+
diff := math.Abs(h - m)
5+
return math.Min(diff, 360-diff)
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public double angleClock(int hour, int minutes) {
3+
double h = 30 * hour + 0.5 * minutes;
4+
double m = 6 * minutes;
5+
double diff = Math.abs(h - m);
6+
return Math.min(diff, 360 - diff);
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function angleClock(hour: number, minutes: number): number {
2+
const h = 30 * hour + 0.5 * minutes;
3+
const m = 6 * minutes;
4+
const diff = Math.abs(h - m);
5+
return Math.min(diff, 360 - diff);
6+
}

0 commit comments

Comments
 (0)