Skip to content

Commit 6a42f6c

Browse files
committed
feat: add solutions to leetcode problem: No.1365
1 parent 78906da commit 6a42f6c

File tree

8 files changed

+144
-4
lines changed

8 files changed

+144
-4
lines changed

solution/0200-0299/0251.Flatten 2D Vector/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,27 @@ iterator.hasNext(); // 返回 false
4646
<!-- 这里可写当前语言的特殊实现逻辑 -->
4747

4848
```python
49+
class Vector2D:
4950

51+
def __init__(self, vec: List[List[int]]):
52+
self.flatten = []
53+
for item in vec:
54+
for e in item:
55+
self.flatten.append(e)
56+
self.cur = -1
57+
58+
def next(self) -> int:
59+
self.cur += 1
60+
return self.flatten[self.cur]
61+
62+
def hasNext(self) -> bool:
63+
return self.cur < len(self.flatten) - 1
64+
65+
66+
# Your Vector2D object will be instantiated and called as such:
67+
# obj = Vector2D(vec)
68+
# param_1 = obj.next()
69+
# param_2 = obj.hasNext()
5070
```
5171

5272
### **Java**

solution/0200-0299/0251.Flatten 2D Vector/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,27 @@ iterator.hasNext(); // return false
4444
### **Python3**
4545

4646
```python
47+
class Vector2D:
4748

49+
def __init__(self, vec: List[List[int]]):
50+
self.flatten = []
51+
for item in vec:
52+
for e in item:
53+
self.flatten.append(e)
54+
self.cur = -1
55+
56+
def next(self) -> int:
57+
self.cur += 1
58+
return self.flatten[self.cur]
59+
60+
def hasNext(self) -> bool:
61+
return self.cur < len(self.flatten) - 1
62+
63+
64+
# Your Vector2D object will be instantiated and called as such:
65+
# obj = Vector2D(vec)
66+
# param_1 = obj.next()
67+
# param_2 = obj.hasNext()
4868
```
4969

5070
### **Java**

solution/0200-0299/0251.Flatten 2D Vector/Solution.java

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Vector2D:
2+
3+
def __init__(self, vec: List[List[int]]):
4+
self.flatten = []
5+
for item in vec:
6+
for e in item:
7+
self.flatten.append(e)
8+
self.cur = -1
9+
10+
def next(self) -> int:
11+
self.cur += 1
12+
return self.flatten[self.cur]
13+
14+
def hasNext(self) -> bool:
15+
return self.cur < len(self.flatten) - 1
16+
17+
18+
# Your Vector2D object will be instantiated and called as such:
19+
# obj = Vector2D(vec)
20+
# param_1 = obj.next()
21+
# param_2 = obj.hasNext()

solution/1300-1399/1365.How Many Numbers Are Smaller Than the Current Number/README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,49 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
计数排序,求前缀和。
54+
5355
<!-- tabs:start -->
5456

5557
### **Python3**
5658

5759
<!-- 这里可写当前语言的特殊实现逻辑 -->
5860

5961
```python
60-
62+
class Solution:
63+
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
64+
cnt = [0] * 101
65+
for num in nums:
66+
cnt[num] += 1
67+
for i in range(1, 101):
68+
cnt[i] += cnt[i - 1]
69+
res = []
70+
for num in nums:
71+
res.append(0 if num == 0 else cnt[num - 1])
72+
return res
6173
```
6274

6375
### **Java**
6476

6577
<!-- 这里可写当前语言的特殊实现逻辑 -->
6678

6779
```java
68-
80+
class Solution {
81+
public int[] smallerNumbersThanCurrent(int[] nums) {
82+
int[] cnt = new int[101];
83+
for (int e : nums) {
84+
++cnt[e];
85+
}
86+
for (int i = 1; i < 101; ++i) {
87+
cnt[i] += cnt[i - 1];
88+
}
89+
int[] res = new int[nums.length];
90+
for (int i = 0; i < nums.length; ++i) {
91+
res[i] = nums[i] == 0 ? 0 : cnt[nums[i] - 1];
92+
}
93+
return res;
94+
}
95+
}
6996
```
7097

7198
### **...**

solution/1300-1399/1365.How Many Numbers Are Smaller Than the Current Number/README_EN.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,38 @@ For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
6868
### **Python3**
6969

7070
```python
71-
71+
class Solution:
72+
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
73+
cnt = [0] * 101
74+
for num in nums:
75+
cnt[num] += 1
76+
for i in range(1, 101):
77+
cnt[i] += cnt[i - 1]
78+
res = []
79+
for num in nums:
80+
res.append(0 if num == 0 else cnt[num - 1])
81+
return res
7282
```
7383

7484
### **Java**
7585

7686
```java
77-
87+
class Solution {
88+
public int[] smallerNumbersThanCurrent(int[] nums) {
89+
int[] cnt = new int[101];
90+
for (int e : nums) {
91+
++cnt[e];
92+
}
93+
for (int i = 1; i < 101; ++i) {
94+
cnt[i] += cnt[i - 1];
95+
}
96+
int[] res = new int[nums.length];
97+
for (int i = 0; i < nums.length; ++i) {
98+
res[i] = nums[i] == 0 ? 0 : cnt[nums[i] - 1];
99+
}
100+
return res;
101+
}
102+
}
78103
```
79104

80105
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int[] smallerNumbersThanCurrent(int[] nums) {
3+
int[] cnt = new int[101];
4+
for (int e : nums) {
5+
++cnt[e];
6+
}
7+
for (int i = 1; i < 101; ++i) {
8+
cnt[i] += cnt[i - 1];
9+
}
10+
int[] res = new int[nums.length];
11+
for (int i = 0; i < nums.length; ++i) {
12+
res[i] = nums[i] == 0 ? 0 : cnt[nums[i] - 1];
13+
}
14+
return res;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
3+
cnt = [0] * 101
4+
for num in nums:
5+
cnt[num] += 1
6+
for i in range(1, 101):
7+
cnt[i] += cnt[i - 1]
8+
res = []
9+
for num in nums:
10+
res.append(0 if num == 0 else cnt[num - 1])
11+
return res

0 commit comments

Comments
 (0)