# 一、5472. 重新排列字符串 Easy

通过的用户数3797



输入：s = "codeleet", indices = [4,5,6,7,0,2,1,3]



输入：s = "abc", indices = [0,1,2]



输入：s = "aiohn", indices = [3,1,4,2,0]



输入：s = "aaiougrt", indices = [4,0,2,6,7,3,1,5]



输入：s = "art", indices = [1,0,2]



• s.length == indices.length == n
• 1 <= n <= 100
• s 仅包含小写英文字母。
• 0 <= indices[i] < n
• indices 的所有的值都是唯一的（也就是说，indices 是整数 0 到 n - 1 形成的一组排列）。

## 方法：暴力

class Solution {
public String restoreString(String s, int[] indices) {
char[] chars = s.toCharArray();
char[] resultArray = new char[s.length()];
int j = 0;
for (int i : indices) {
resultArray[i] = chars[j++];
}
return new String(resultArray);
}
}


class Solution {
public String restoreString(String s, int[] indices) {
char[] ans = new char[s.length()];
for(int i = 0; i < s.length(); i++){
ans[indices[i]] = s.charAt(i);
}
return new String(ans);
}
}


# 二、5473. 灯泡开关 IV Medium

通过的用户数2796



输入：target = "10111"



输入：target = "101"



输入：target = "00000"



输入：target = "001011101"



1 <= target.length <= 10^5
target[i] == ‘0’ 或者 target[i] == ‘1’

## 方法：从左往右，只要和flag不一致，就翻转

class Solution {
public int minFlips(String target) {
char flag = '0';
int count = 0;
for (int i = 0; i < target.length(); i++) {
if (target.charAt(i) != flag) {
count++;
if (target.charAt(i) == '1') {
flag = '1';
} else {
flag = '0';
}
//flag = target.charAt(i) == '1' ? '1' : '0';
}
}
return count;
}
}


# 三、5474. 好叶子节点对的数量 Medium

通过的用户数838



输入：root = [1,2,3,null,4], distance = 3



输入：root = [1,2,3,4,5,6,7], distance = 3



输入：root = [7,1,4,6,null,5,3,null,null,null,null,null,2], distance = 3



输入：root = [100], distance = 1



输入：root = [1,1,1], distance = 2



• tree 的节点数在 [1, 2^10] 范围内。
• 每个节点的值都在 [1, 100] 之间。
• 1 <= distance <= 10

## 方法：dfs

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode() {}
*     TreeNode(int val) { this.val = val; }
*     TreeNode(int val, TreeNode left, TreeNode right) {
*         this.val = val;
*         this.left = left;
*         this.right = right;
*     }
* }
*/
class Solution {
public int countPairs(TreeNode root, int distance) {
dfs(root, distance);
return ans;
}
int ans = 0;
public void handle(TreeNode root, int[] state, int d){
int[] ret = dfs(root, d);
for(int i = 0; i <= d; i++){
for(int j = 0; j <= d; j++){
if(i + j + 1 <= d){
ans += ret[i] * state[j];
}
}
}
for(int i = 0; i < d; i++){
state[i + 1] += ret[i];
}

}
public int[] dfs(TreeNode root, int d){
int[] state = new int[d + 1];
if(root.left == null && root.right == null){
state[0] = 1;
return state;
}
if(root.left != null){
handle(root.left, state, d);
}
if(root.right != null){
handle(root.right, state, d);
}
return state;
}
}


/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode() {}
*     TreeNode(int val) { this.val = val; }
*     TreeNode(int val, TreeNode left, TreeNode right) {
*         this.val = val;
*         this.left = left;
*         this.right = right;
*     }
* }
*/
class Solution {
int ans = 0;
public int countPairs(TreeNode root, int distance) {
dfs(root, distance);
return ans;
}

private List<Integer> dfs(TreeNode root, int distance){
if(root.left == null && root.right == null){
List<Integer> list = new ArrayList<>();
return list;
}
List<Integer> left = new ArrayList<>();
if(root.left != null){
left = dfs(root.left, distance);
}
List<Integer> right = new ArrayList<>();
if(root.right != null){
right = dfs(root.right, distance);
}
if(left.size() > 0 && right.size() > 0){
for(int t:left){
for(int t1:right){
if(t + t1 + 2 <= distance){
ans++;
}
}
}
}
List<Integer> list = new ArrayList<>();
for(int t:left){
}
for(int t:right){
}
return list;
}
}


# 四、5462. 压缩字符串 II Hard

通过的用户数30



输入：s = "aaabcccd", k = 2



输入：s = "aabbaa", k = 2



输入：s = "aaaaaaaaaaa", k = 0

`

• 1 <= s.length <= 100
• 0 <= k <= s.length
• s 仅包含小写英文字母

## 方法：不会做，晚上补

07-12 149

07-19 685

06-21 131

03-22 81

07-26 110

07-05 151

07-12 1810

05-24 99

11-12 1542

05-17 63

#### Leetcode 第189 场周赛题解

©️2020 CSDN 皮肤主题: 撸撸猫 设计师: 设计师小姐姐

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。