1、生成交替二进制字符串
//给你一个仅由字符 '0' 和 '1' 组成的字符串 s 。一步操作中,你可以将任一 '0' 变成 '1' ,或者将 '1' 变成 '0' 。 // // 交替字符串 定义为:如果字符串中不存在相邻两个字符相等的情况,那么该字符串就是交替字符串。例如,字符串 "010" 是交替字符串,而字符串 "0100" //不是。 // // 返回使 s 变成 交替字符串 所需的 最少 操作数。 // // // // 示例 1: // // 输入:s = "0100" //输出:1 //解释:如果将最后一个字符变为 '1' ,s 就变成 "0101" ,即符合交替字符串定义。 // // // 示例 2: // // 输入:s = "10" //输出:0 //解释:s 已经是交替字符串。 // // // 示例 3: // // 输入:s = "1111" //输出:2 //解释:需要 2 步操作得到 "0101" 或 "1010" 。 // // // // // 提示: // // // 1 <= s.length <= 104 // s[i] 是 '0' 或 '1' // // Related Topics 贪心算法 数组
直接找到奇数位和偶数位上1和0的个数,然后考虑全部变1改动少还是全部变0改动少。
public int minOperations(String s) {
int[] arr1 = new int[2];
int[] arr2 = new int[2];
for (int i = 0; i < s.length(); i++) {
if(i % 2 == 0){
arr1[s.charAt(i)-48]++;
}
}
for (int i = 0; i < s.length(); i++) {
if(i % 2 != 0){
arr2[s.charAt(i)-48]++;
}
}
return arr1[0] + arr2[1] < arr1[1] + arr2[0]?arr1[0]+arr2[1] : arr1[1]+arr2[0];
}
2、找到最近的有相同X或Y坐标的点
//给你两个整数 x 和 y ,表示你在一个笛卡尔坐标系下的 (x, y) 处。同时,在同一个坐标系下给你一个数组 points ,其中 points[i] = // [ai, bi] 表示在 (ai, bi) 处有一个点。当一个点与你所在的位置有相同的 x 坐标或者相同的 y 坐标时,我们称这个点是 有效的 。 // // 请返回距离你当前位置 曼哈顿距离 最近的 有效 点的下标(下标从 0 开始)。如果有多个最近的有效点,请返回下标 最小 的一个。如果没有有效点,请返回 - //1 。 // // 两个点 (x1, y1) 和 (x2, y2) 之间的 曼哈顿距离 为 abs(x1 - x2) + abs(y1 - y2) 。 // // // // 示例 1: // // 输入:x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]] //输出:2 //解释:所有点中,[3,1],[2,4] 和 [4,4] 是有效点。有效点中,[2,4] 和 [4,4] 距离你当前位置的曼哈顿距离最小,都为 1 。[2,4 //] 的下标最小,所以返回 2 。 // // 示例 2: // // 输入:x = 3, y = 4, points = [[3,4]] //输出:0 //提示:答案可以与你当前所在位置坐标相同。 // // 示例 3: // // 输入:x = 3, y = 4, points = [[2,3]] //输出:-1 //解释:没有有效点。 // // // // 提示: // // // 1 <= points.length <= 104 // points[i].length == 2 // 1 <= x, y, ai, bi <= 104 // // Related Topics 数组
public int nearestValidPoint(int x, int y, int[][] points) {
int min = Integer.MAX_VALUE;
int distance = 0;
int index = -1;
for (int i = 0; i < points.length; i++) {
if(points[i][0] == x || points[i][1] == y){
distance = Math.abs(x-points[i][0]) + Math.abs(y-points[i][1]);
if(distance < min){
min = distance;
index = i;
}
}
}
return index;
}
3、统计匹配检索规则的物品数量
//给你一个数组 items ,其中 items[i] = [typei, colori, namei] ,描述第 i 件物品的类型、颜色以及名称。 // // 另给你一条由两个字符串 ruleKey 和 ruleValue 表示的检索规则。 // // 如果第 i 件物品能满足下述条件之一,则认为该物品与给定的检索规则 匹配 : // // // ruleKey == "type" 且 ruleValue == typei 。 // ruleKey == "color" 且 ruleValue == colori 。 // ruleKey == "name" 且 ruleValue == namei 。 // // // 统计并返回 匹配检索规则的物品数量 。 // // // // 示例 1: // // //输入:items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone"," //gold","iphone"]], ruleKey = "color", ruleValue = "silver" //输出:1 //解释:只有一件物品匹配检索规则,这件物品是 ["computer","silver","lenovo"] 。 // // // 示例 2: // // //输入:items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","g //old","iphone"]], ruleKey = "type", ruleValue = "phone" //输出:2 //解释:只有两件物品匹配检索规则,这两件物品分别是 ["phone","blue","pixel"] 和 ["phone","gold","iphone"] //。注意,["computer","silver","phone"] 未匹配检索规则。 // // // // 提示: // // // 1 <= items.length <= 104 // 1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10 // ruleKey 等于 "type"、"color" 或 "name" // 所有字符串仅由小写字母组成 // // Related Topics 数组 字符串
这里记住字符串比较用equals,这比较的是内容,最好不要用==,它比较的是地址
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int index = 0;
int count = 0;
if("type".equals(ruleKey)){
index = 0;
}else if("color".equals(ruleKey)){
index = 1;
}else{
index = 2;
}
for (int i = 0; i < items.size(); i++) {
if(items.get(i).get(index).equals(ruleValue)){
count++;
}
}
return count;
}