9/23美的笔试题
1.15题单选,6道多选 java基础,linux,springboot,springmvc知识
1.编程
字符串空格
import java.util.ArrayList;
public class javameidi01 {
public static void main(String[] args) {
String[] words={"This", "is", "an", "instance", "of", "code", "alignment."};
int m=16;
ArrayList<String> res=justifyFill(words,m);
for (String re : res) {
System.out.println(re);
}
}
public static ArrayList<String> justifyFill (String[] words, int M) {
// write code here
int l=0;
ArrayList<String> list=new ArrayList<>();
while(l<words.length){
int r=l;
int len=0;
StringBuilder res=new StringBuilder();
while(((r<words.length)&&(len+words[r].length()+r-l)<=M)){
len+=words[r].length();
r++;
}
int space=M-len;
for(int i=l;i<r;i++){
//先加入这个单词
res.append(words[i]);
if(space>0){
int temp;
/*
分别讨论是最后一行还是其他行
然后在每行直接判断就是最后一行如果是最后一个元素就是直接剩余空格,不是的话就是直接1个空格
其他行的话如果是最后一个单词就是直接把所有的空格给她,添加空格就是直接对r-i-1取余
*/
if(r==words.length){
if(r-i==1) temp=space;
else temp=1;
}else{
if(r-i-1>0){
if(space%(r-i-1)==0) temp=space/(r-i-1);
else temp=space/(r-i-1)+1;
}else temp=space;
}
for (int j=0;j<temp;j++){
res.append(" ");
}
space-=temp;
}
}
l=r;
list.add(res.toString());
}
return list;
}
}
2.股票获得最大收益,维护一个min值就行了,力扣原题就完事了
小天才公司福利 1259人发布
查看9道真题和解析