解压缩编码列表
class Solution{
public int[] decompressRLElist(int[] nums){
List<Integer> list = new ArrayList<>();
for(int i = 0; i < nums.length / 2; i++) {
for(int j = 0; j < nums[i * 2]; j++) {
list.add(nums[i * 2 + 1]);
}
}
int[] res = new int[list.size()];
int index = 0;
for(int num : list) {
res[index++] = num;
}
return res;
}
}
猜数字
class Solution {
public int game(int[] guess, int[] answer) {
int i;
int res=0;
for(i=0;i<3;i++)
if(guess[i]==answer[i])
res++;
return res;
}
}
统计位数为偶数的数字
class Solution {
public int findNumbers(int[] nums) {
int Temp=0;
for(int i=0;i<nums.length;i++){
Temp=((String.valueOf(nums[i])).length()%2)==0 ? ++Temp:Temp;
}
return Temp;
}
}
整数的各位积和之差
class Solution {
public int subtractProductAndSum(int n) {
int muti = 1;
int sum = 0;
while(n!=0){
muti *=n%10;
sum +=n%10;
n/=10;
}
return muti-sum;
}
}
宝石与石头
class Solution {
public int numJewelsInStones(String J, String S) {
int res = 0;
for (char s: S.toCharArray())
for (char j: J.toCharArray())
if (j == s) {
res++;
break;
}
return res;
}
}
ip地址无效化
class Solution {
public String defangIPaddr(String address) {
String str = "";
String[] split = address.split("[.]");
for (int i = 0; i < 4; i++) {
if(i < 3)
str += split[i] += "[.]";
else
str += split[i];
}
return str;
}
}