希望可以坚持下来,一天写一章博客
250
题目乱糟糟没怎么懂,看答案发下就是一个简单的集合判断存在不存在的问题
import java.util.*;
public class ImageDithering{
public int count(String dithered, String[] screen){
Set<Character> set = new HashSet<>();
for (char i : dithered.toCharArray()){
set.add(i);
}
int result = 0;
for (String i : screen){
for (char j : i.toCharArray()){
if (set.contains(j)) result++;
}
}
return result;
}
}
500
还是题没怎么读懂,看答案懂了。。就是看看限定时间内,以1S为最小单位有几个整数百分比出现
public class ExerciseMachine{
public int getPercentages(String time){
String[] split = time.split(":");
int h = Integer.valueOf(split[0]);
int m = Integer.valueOf(split[1]);
int s = Integer.valueOf(split[2]);
int seconds = s + m * 60 + h * 3600;
int sum;
for (int i = 1; i < seconds; i++){
if (i * 100 % seconds == 0){
sum++;
}
}
return sum;
}
}
1100
做个编程题跟阅读理解似得。。。问题不难,写两个功能函数,一个是获取最大列位置,一个是判断是顺时针还是逆时针旋转。
public class VendingMachine{
private int[][] machine;
private int rows, cols;
private int sumi;
public int motorUse(String[] prices, String[] purchases){
rows = prices.length;
machine = new int[rows + 1][];
sumi = prices.length;
for ( int i = 0; i < rows; i++){
String[] strVals = prices[i].split(" ");
machine[i] = new int[strVals.length];
for (int j = 0; j < strVals.length; j++){
machine[i][j] = Integer.valueOf(strVals[j]);
}
}
cols = machine[0].length;
machine[sumi] = new int[cols];
for (int j = 0; j < cols; j++){
int sum = 0;
for (int i = 0; i < rows; i++){
sum += machine[i][j];
}
machine[sumi][j] = sum;
}
int max = getMaxColumn();
int seconds = getRotateTime(0 , max);
int currentColumn = max;
int lastMinute = Integer.valueOf(purchases[0].split(":")[1]);
for (int i = 0; i < purchases.length; i++){
String[] coordsMinute = purchases[i].split(":");
String[] coords = coordsMinute[0].split(",");
int row = Integer.valueOf(coords[0]);
int col = Integer.valueOf(coords[1]);
int minute = Integer.valueOf(coordsMinute[1]);
if (minute - lastMinute >= 5){
max = getMaxColumn();
seconds += getRotateTime(currentColumn, max);
currentColumn = max;
}
if (machine[row][col] == 0){
return -1;
}
seconds += getRotateTime(currentColumn, col);
currentColumn = col;
machine[sumi][col] -= machine[row][col];
machine[row][col] = 0;
lastMinute = minute;
}
seconds += getRotateTime(currentColumn, getMaxColumn());
return seconds;
}
private int getRotateTime(int currentColumn, int destinationColumn){
int abs = Math.abs(destinationColumn - currentColumn);
return Math.min(abs, cols - abs);
}
private int getMaxColumn(){
int maxi = 0;
int max = machine[sumi][0];
for (int i = 1; i < machine[0].length; i++){
if (machine[sumi][i] > max){
maxi = i;
max = machine[sumi][i];
}
}
return maxi;
}
}