File tree 8 files changed +176
-8
lines changed
lcof2/剑指 Offer II 118. 多余的边
solution/1000-1099/1051.Height Checker
8 files changed +176
-8
lines changed Original file line number Diff line number Diff line change @@ -122,7 +122,7 @@ d[find(a)] = distance
122
122
``` python
123
123
class Solution :
124
124
def findRedundantConnection (self , edges : List[List[int ]]) -> List[int ]:
125
- p = [i for i in range (1010 )]
125
+ p = list ( range (1010 ))
126
126
127
127
def find (x ):
128
128
if p[x] != x:
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def findRedundantConnection (self , edges : List [List [int ]]) -> List [int ]:
3
- p = [ i for i in range (1010 )]
3
+ p = list ( range (1010 ))
4
4
5
5
def find (x ):
6
6
if p [x ] != x :
Original file line number Diff line number Diff line change 49
49
<li><code>1 <= heights[i] <= 100</code></li>
50
50
</ul >
51
51
52
-
53
52
## 解法
54
53
55
54
<!-- 这里可写通用的实现逻辑 -->
61
60
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
61
63
62
``` python
63
+ class Solution :
64
+ def heightChecker (self , heights : List[int ]) -> int :
65
+ bucket = [0 ] * 101
66
+ for h in heights:
67
+ bucket[h] += 1
68
+ res = i = 0
69
+ for j in range (1 , 101 ):
70
+ while bucket[j] > 0 :
71
+ bucket[j] -= 1
72
+ if heights[i] != j:
73
+ res += 1
74
+ i += 1
75
+ return res
76
+ ```
64
77
78
+ ``` python
79
+ class Solution :
80
+ def heightChecker (self , heights : List[int ]) -> int :
81
+ expected = sorted (heights)
82
+ return sum (1 for i, h in enumerate (heights) if h != expected[i])
65
83
```
66
84
67
85
### ** Java**
68
86
69
87
<!-- 这里可写当前语言的特殊实现逻辑 -->
70
88
71
89
``` java
90
+ class Solution {
91
+ public int heightChecker (int [] heights ) {
92
+ int [] expected = Arrays . copyOf(heights, heights. length);
93
+ Arrays . sort(expected);
94
+ int res = 0 ;
95
+ for (int i = 0 ; i < heights. length; ++ i) {
96
+ if (heights[i] != expected[i]) {
97
+ ++ res;
98
+ }
99
+ }
100
+ return res;
101
+ }
102
+ }
103
+ ```
104
+
105
+ ### ** C++**
106
+
107
+ ``` cpp
108
+ class Solution {
109
+ public:
110
+ int heightChecker(vector<int >& heights) {
111
+ vector<int > expected = heights;
112
+ sort(expected.begin(), expected.end());
113
+ int res = 0;
114
+ for (int i = 0; i < heights.size(); ++i)
115
+ {
116
+ if (heights[ i] != expected[ i] ) ++res;
117
+ }
118
+ return res;
119
+ }
120
+ };
121
+ ```
72
122
123
+ ### **Go**
124
+
125
+ ```go
126
+ func heightChecker(heights []int) int {
127
+ expected := make([]int, len(heights))
128
+ copy(expected, heights)
129
+ sort.Ints(expected)
130
+ res := 0
131
+ for i, h := range heights {
132
+ if h != expected[i] {
133
+ res++
134
+ }
135
+ }
136
+ return res
137
+ }
73
138
```
74
139
75
140
### ** ...**
Original file line number Diff line number Diff line change @@ -52,21 +52,86 @@ All indices match.
52
52
<li><code>1 <= heights[i] <= 100</code></li>
53
53
</ul >
54
54
55
-
56
55
## Solutions
57
56
58
57
<!-- tabs:start -->
59
58
60
59
### ** Python3**
61
60
62
61
``` python
62
+ class Solution :
63
+ def heightChecker (self , heights : List[int ]) -> int :
64
+ bucket = [0 ] * 101
65
+ for h in heights:
66
+ bucket[h] += 1
67
+ res = i = 0
68
+ for j in range (1 , 101 ):
69
+ while bucket[j] > 0 :
70
+ bucket[j] -= 1
71
+ if heights[i] != j:
72
+ res += 1
73
+ i += 1
74
+ return res
75
+ ```
63
76
77
+ ``` python
78
+ class Solution :
79
+ def heightChecker (self , heights : List[int ]) -> int :
80
+ expected = sorted (heights)
81
+ return sum (1 for i, h in enumerate (heights) if h != expected[i])
64
82
```
65
83
66
84
### ** Java**
67
85
68
86
``` java
87
+ class Solution {
88
+ public int heightChecker (int [] heights ) {
89
+ int [] expected = Arrays . copyOf(heights, heights. length);
90
+ Arrays . sort(expected);
91
+ int res = 0 ;
92
+ for (int i = 0 ; i < heights. length; ++ i) {
93
+ if (heights[i] != expected[i]) {
94
+ ++ res;
95
+ }
96
+ }
97
+ return res;
98
+ }
99
+ }
100
+ ```
101
+
102
+ ### ** C++**
103
+
104
+ ``` cpp
105
+ class Solution {
106
+ public:
107
+ int heightChecker(vector<int >& heights) {
108
+ vector<int > expected = heights;
109
+ sort(expected.begin(), expected.end());
110
+ int res = 0;
111
+ for (int i = 0; i < heights.size(); ++i)
112
+ {
113
+ if (heights[ i] != expected[ i] ) ++res;
114
+ }
115
+ return res;
116
+ }
117
+ };
118
+ ```
69
119
120
+ ### **Go**
121
+
122
+ ```go
123
+ func heightChecker(heights []int) int {
124
+ expected := make([]int, len(heights))
125
+ copy(expected, heights)
126
+ sort.Ints(expected)
127
+ res := 0
128
+ for i, h := range heights {
129
+ if h != expected[i] {
130
+ res++
131
+ }
132
+ }
133
+ return res
134
+ }
70
135
```
71
136
72
137
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int heightChecker (vector<int >& heights) {
4
+ vector<int > copy = heights;
5
+ sort (copy.begin (), copy.end ());
6
+ int res = 0 ;
7
+ for (int i = 0 ; i < heights.size (); ++i)
8
+ {
9
+ if (heights[i] != copy[i]) ++res;
10
+ }
11
+ return res;
12
+ }
13
+ };
Original file line number Diff line number Diff line change
1
+ func heightChecker (heights []int ) int {
2
+ expected := make ([]int , len (heights ))
3
+ copy (expected , heights )
4
+ sort .Ints (expected )
5
+ res := 0
6
+ for i , h := range heights {
7
+ if h != expected [i ] {
8
+ res ++
9
+ }
10
+ }
11
+ return res
12
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int heightChecker (int [] heights ) {
3
- int [] copy = Arrays .copyOf (heights , heights .length );
4
- Arrays .sort (copy );
3
+ int [] expected = Arrays .copyOf (heights , heights .length );
4
+ Arrays .sort (expected );
5
5
int res = 0 ;
6
6
for (int i = 0 ; i < heights .length ; ++i ) {
7
- if (heights [i ] != copy [i ]) {
7
+ if (heights [i ] != expected [i ]) {
8
8
++res ;
9
9
}
10
10
}
11
11
return res ;
12
12
}
13
- }
13
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def heightChecker (self , heights : List [int ]) -> int :
3
+ bucket = [0 ] * 101
4
+ for h in heights :
5
+ bucket [h ] += 1
6
+ res = i = 0
7
+ for j in range (1 , 101 ):
8
+ while bucket [j ] > 0 :
9
+ bucket [j ] -= 1
10
+ if heights [i ] != j :
11
+ res += 1
12
+ i += 1
13
+ return res
You can’t perform that action at this time.
0 commit comments