File tree 3 files changed +73
-23
lines changed
solution/1300-1399/1331.Rank Transform of an Array
3 files changed +73
-23
lines changed Original file line number Diff line number Diff line change 50
50
51
51
<!-- 这里可写通用的实现逻辑 -->
52
52
53
- 离散化。
53
+ ** 方法一: 离散化**
54
54
55
55
<!-- tabs:start -->
56
56
61
61
``` python
62
62
class Solution :
63
63
def arrayRankTransform (self , arr : List[int ]) -> List[int ]:
64
- def find (x ):
65
- left, right = 0 , len (t) - 1
66
- while left < right:
67
- mid = (left + right) >> 1
68
- if t[mid] >= x:
69
- right = mid
70
- else :
71
- left = mid + 1
72
- return left + 1
73
-
74
64
t = sorted (set (arr))
75
- return [find(x) for x in arr]
65
+ return [bisect_left(t, x) + 1 for x in arr]
76
66
```
77
67
78
68
``` python
@@ -108,6 +98,25 @@ class Solution {
108
98
}
109
99
```
110
100
101
+ ``` java
102
+ class Solution {
103
+ public int [] arrayRankTransform (int [] arr ) {
104
+ Set<Integer > s = new HashSet<> ();
105
+ for (int v : arr) {
106
+ s. add(v);
107
+ }
108
+ List<Integer > alls = new ArrayList<> (s);
109
+ alls. sort((a, b) - > a - b);
110
+ int n = arr. length;
111
+ int [] ans = new int [n];
112
+ for (int i = 0 ; i < n; ++ i) {
113
+ ans[i] = Collections . binarySearch(alls, arr[i]) + 1 ;
114
+ }
115
+ return ans;
116
+ }
117
+ }
118
+ ```
119
+
111
120
### ** C++**
112
121
113
122
``` cpp
@@ -126,6 +135,20 @@ public:
126
135
};
127
136
```
128
137
138
+ ```cpp
139
+ class Solution {
140
+ public:
141
+ vector<int> arrayRankTransform(vector<int>& arr) {
142
+ unordered_set<int> s(arr.begin(), arr.end());
143
+ vector<int> alls(s.begin(), s.end());
144
+ sort(alls.begin(), alls.end());
145
+ vector<int> ans;
146
+ for (int v: arr) ans.push_back(lower_bound(alls.begin(), alls.end(), v) - alls.begin() + 1);
147
+ return ans;
148
+ }
149
+ };
150
+ ```
151
+
129
152
### ** Go**
130
153
131
154
``` go
Original file line number Diff line number Diff line change 54
54
``` python
55
55
class Solution :
56
56
def arrayRankTransform (self , arr : List[int ]) -> List[int ]:
57
- def find (x ):
58
- left, right = 0 , len (t) - 1
59
- while left < right:
60
- mid = (left + right) >> 1
61
- if t[mid] >= x:
62
- right = mid
63
- else :
64
- left = mid + 1
65
- return left + 1
66
-
67
57
t = sorted (set (arr))
68
- return [find(x) for x in arr]
58
+ return [bisect_left(t, x) + 1 for x in arr]
69
59
```
70
60
71
61
``` python
@@ -99,6 +89,25 @@ class Solution {
99
89
}
100
90
```
101
91
92
+ ``` java
93
+ class Solution {
94
+ public int [] arrayRankTransform (int [] arr ) {
95
+ Set<Integer > s = new HashSet<> ();
96
+ for (int v : arr) {
97
+ s. add(v);
98
+ }
99
+ List<Integer > alls = new ArrayList<> (s);
100
+ alls. sort((a, b) - > a - b);
101
+ int n = arr. length;
102
+ int [] ans = new int [n];
103
+ for (int i = 0 ; i < n; ++ i) {
104
+ ans[i] = Collections . binarySearch(alls, arr[i]) + 1 ;
105
+ }
106
+ return ans;
107
+ }
108
+ }
109
+ ```
110
+
102
111
### ** C++**
103
112
104
113
``` cpp
@@ -117,6 +126,20 @@ public:
117
126
};
118
127
```
119
128
129
+ ```cpp
130
+ class Solution {
131
+ public:
132
+ vector<int> arrayRankTransform(vector<int>& arr) {
133
+ unordered_set<int> s(arr.begin(), arr.end());
134
+ vector<int> alls(s.begin(), s.end());
135
+ sort(alls.begin(), alls.end());
136
+ vector<int> ans;
137
+ for (int v: arr) ans.push_back(lower_bound(alls.begin(), alls.end(), v) - alls.begin() + 1);
138
+ return ans;
139
+ }
140
+ };
141
+ ```
142
+
120
143
### ** Go**
121
144
122
145
``` go
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def arrayRankTransform (self , arr : List [int ]) -> List [int ]:
3
+ m = {v : i for i , v in enumerate (sorted (set (arr )), 1 )}
4
+ return [m [v ] for v in arr ]
You can’t perform that action at this time.
0 commit comments