Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,39 @@

```

### **JavaScript**

```js
/**
* @param {string} s
* @return {number}
*/
var minSwaps = function(s) {
let n = s.length;
let n1 = [...s].reduce((a, c) => parseInt(c) + a, 0);
let n0 = n - n1;
let count = Infinity;
let half = n / 2;
// 101、1010
if (n1 == Math.ceil(half) && n0 == Math.floor(half)) {
let cur = 0;
for (let i = 0; i < n; i++) {
if (i % 2 == 0 && s.charAt(i) != '1') cur++;
}
count = Math.min(count, cur);
}
// 010、0101
if (n0 == Math.ceil(half) && n1 == Math.floor(half)) {
let cur = 0;
for (let i = 0; i < n; i++) {
if (i % 2 == 0 && s.charAt(i) != '0') cur++;
}
count = Math.min(count, cur);
}
return count == Infinity ? -1 : count;
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ The string is now alternating.

```

### **JavaScript**

```js
/**
* @param {string} s
* @return {number}
*/
var minSwaps = function(s) {
let n = s.length;
let n1 = [...s].reduce((a, c) => parseInt(c) + a, 0);
let n0 = n - n1;
let count = Infinity;
let half = n / 2;
// 101、1010
if (n1 == Math.ceil(half) && n0 == Math.floor(half)) {
let cur = 0;
for (let i = 0; i < n; i++) {
if (i % 2 == 0 && s.charAt(i) != '1') cur++;
}
count = Math.min(count, cur);
}
// 010、0101
if (n0 == Math.ceil(half) && n1 == Math.floor(half)) {
let cur = 0;
for (let i = 0; i < n; i++) {
if (i % 2 == 0 && s.charAt(i) != '0') cur++;
}
count = Math.min(count, cur);
}
return count == Infinity ? -1 : count;
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {string} s
* @return {number}
*/
var minSwaps = function(s) {
let n = s.length;
let n1 = [...s].reduce((a, c) => parseInt(c) + a, 0);
let n0 = n - n1;
let count = Infinity;
let half = n / 2;
// 101、1010
if (n1 == Math.ceil(half) && n0 == Math.floor(half)) {
let cur = 0;
for (let i = 0; i < n; i++) {
if (i % 2 == 0 && s.charAt(i) != '1') cur++;
}
count = Math.min(count, cur);
}
// 010、0101
if (n0 == Math.ceil(half) && n1 == Math.floor(half)) {
let cur = 0;
for (let i = 0; i < n; i++) {
if (i % 2 == 0 && s.charAt(i) != '0') cur++;
}
count = Math.min(count, cur);
}
return count == Infinity ? -1 : count;
};