File tree 9 files changed +133
-17
lines changed
0500-0599/0591.Tag Validator
1200-1299/1217.Minimum Cost to Move Chips to The Same Position
9 files changed +133
-17
lines changed Original file line number Diff line number Diff line change @@ -108,9 +108,7 @@ class Solution:
108
108
def isValid (self , code : str ) -> bool :
109
109
def check (tag ):
110
110
n = len (tag)
111
- if n < 1 or n > 9 :
112
- return False
113
- return all (c.isupper() for c in tag)
111
+ return 1 <= n <= 9 and all (c.isupper() for c in tag)
114
112
115
113
stk = []
116
114
i, n = 0 , len (code)
@@ -128,9 +126,8 @@ class Solution:
128
126
if i < 0 :
129
127
return False
130
128
t = code[j: i]
131
- if not check(t) or not stk or stk[ - 1 ] != t:
129
+ if not check(t) or not stk or stk.pop() != t:
132
130
return False
133
- stk.pop()
134
131
elif code[i] == ' <' :
135
132
j = i + 1
136
133
i = code.find(' >' , j)
Original file line number Diff line number Diff line change @@ -76,9 +76,7 @@ class Solution:
76
76
def isValid (self , code : str ) -> bool :
77
77
def check (tag ):
78
78
n = len (tag)
79
- if n < 1 or n > 9 :
80
- return False
81
- return all (c.isupper() for c in tag)
79
+ return 1 <= n <= 9 and all (c.isupper() for c in tag)
82
80
83
81
stk = []
84
82
i, n = 0 , len (code)
@@ -96,9 +94,8 @@ class Solution:
96
94
if i < 0 :
97
95
return False
98
96
t = code[j: i]
99
- if not check(t) or not stk or stk[ - 1 ] != t:
97
+ if not check(t) or not stk or stk.pop() != t:
100
98
return False
101
- stk.pop()
102
99
elif code[i] == ' <' :
103
100
j = i + 1
104
101
i = code.find(' >' , j)
Original file line number Diff line number Diff line change @@ -2,9 +2,7 @@ class Solution:
2
2
def isValid (self , code : str ) -> bool :
3
3
def check (tag ):
4
4
n = len (tag )
5
- if n < 1 or n > 9 :
6
- return False
7
- return all (c .isupper () for c in tag )
5
+ return 1 <= n <= 9 and all (c .isupper () for c in tag )
8
6
9
7
stk = []
10
8
i , n = 0 , len (code )
@@ -22,9 +20,8 @@ def check(tag):
22
20
if i < 0 :
23
21
return False
24
22
t = code [j : i ]
25
- if not check (t ) or not stk or stk [ - 1 ] != t :
23
+ if not check (t ) or not stk or stk . pop () != t :
26
24
return False
27
- stk .pop ()
28
25
elif code [i ] == '<' :
29
26
j = i + 1
30
27
i = code .find ('>' , j )
Original file line number Diff line number Diff line change 63
63
64
64
<!-- 这里可写通用的实现逻辑 -->
65
65
66
+ ** 方法一:脑筋急转弯**
67
+
68
+ 将所有偶数下标的芯片移动到 0 号位置,所有奇数下标的芯片移动到 1 号位置,所有的代价为 0,接下来只需要在 0/1 号位置中选择其中一个较小数量的芯片,移动到另一个位置。所需的最小代价就是那个较小的数量。
69
+
66
70
<!-- tabs:start -->
67
71
68
72
### ** Python3**
69
73
70
74
<!-- 这里可写当前语言的特殊实现逻辑 -->
71
75
72
76
``` python
73
-
77
+ class Solution :
78
+ def minCostToMoveChips (self , position : List[int ]) -> int :
79
+ a = sum (p % 2 for p in position)
80
+ b = len (position) - a
81
+ return min (a, b)
74
82
```
75
83
76
84
### ** Java**
77
85
78
86
<!-- 这里可写当前语言的特殊实现逻辑 -->
79
87
80
88
``` java
89
+ class Solution {
90
+ public int minCostToMoveChips (int [] position ) {
91
+ int a = 0 ;
92
+ for (int p : position) {
93
+ a += p % 2 ;
94
+ }
95
+ int b = position. length - a;
96
+ return Math . min(a, b);
97
+ }
98
+ }
99
+ ```
100
+
101
+ ### ** C++**
102
+
103
+ ``` cpp
104
+ class Solution {
105
+ public:
106
+ int minCostToMoveChips(vector<int >& position) {
107
+ int a = 0;
108
+ for (auto& p : position) a += p & 1;
109
+ int b = position.size() - a;
110
+ return min(a, b);
111
+ }
112
+ };
113
+ ```
81
114
115
+ ### **Go**
116
+
117
+ ```go
118
+ func minCostToMoveChips(position []int) int {
119
+ a := 0
120
+ for _, p := range position {
121
+ a += p & 1
122
+ }
123
+ b := len(position) - a
124
+ if a < b {
125
+ return a
126
+ }
127
+ return b
128
+ }
82
129
```
83
130
84
131
### ** ...**
Original file line number Diff line number Diff line change @@ -56,13 +56,56 @@ Total cost is 1.
56
56
### ** Python3**
57
57
58
58
``` python
59
-
59
+ class Solution :
60
+ def minCostToMoveChips (self , position : List[int ]) -> int :
61
+ a = sum (p % 2 for p in position)
62
+ b = len (position) - a
63
+ return min (a, b)
60
64
```
61
65
62
66
### ** Java**
63
67
64
68
``` java
69
+ class Solution {
70
+ public int minCostToMoveChips (int [] position ) {
71
+ int a = 0 ;
72
+ for (int p : position) {
73
+ a += p % 2 ;
74
+ }
75
+ int b = position. length - a;
76
+ return Math . min(a, b);
77
+ }
78
+ }
79
+ ```
80
+
81
+ ### ** C++**
82
+
83
+ ``` cpp
84
+ class Solution {
85
+ public:
86
+ int minCostToMoveChips(vector<int >& position) {
87
+ int a = 0;
88
+ for (auto& p : position) a += p & 1;
89
+ int b = position.size() - a;
90
+ return min(a, b);
91
+ }
92
+ };
93
+ ```
65
94
95
+ ### **Go**
96
+
97
+ ```go
98
+ func minCostToMoveChips(position []int) int {
99
+ a := 0
100
+ for _, p := range position {
101
+ a += p & 1
102
+ }
103
+ b := len(position) - a
104
+ if a < b {
105
+ return a
106
+ }
107
+ return b
108
+ }
66
109
```
67
110
68
111
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int minCostToMoveChips (vector<int >& position) {
4
+ int a = 0 ;
5
+ for (auto & p : position) a += p & 1 ;
6
+ int b = position.size () - a;
7
+ return min (a, b);
8
+ }
9
+ };
Original file line number Diff line number Diff line change
1
+ func minCostToMoveChips (position []int ) int {
2
+ a := 0
3
+ for _ , p := range position {
4
+ a += p & 1
5
+ }
6
+ b := len (position ) - a
7
+ if a < b {
8
+ return a
9
+ }
10
+ return b
11
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int minCostToMoveChips (int [] position ) {
3
+ int a = 0 ;
4
+ for (int p : position ) {
5
+ a += p % 2 ;
6
+ }
7
+ int b = position .length - a ;
8
+ return Math .min (a , b );
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def minCostToMoveChips (self , position : List [int ]) -> int :
3
+ a = sum (p % 2 for p in position )
4
+ b = len (position ) - a
5
+ return min (a , b )
You can’t perform that action at this time.
0 commit comments