原题:牛客10道练习题_meiribaofu的博客-CSDN博客
1、求解连续数列
已知连续正整数数列{K}=K1,K2,K3...Ki的各个数相加之和为S,i=N (0<S<100000, 0<N<100000), 求此数列K。
输入描述:
输入包含两个参数,1)连续正整数数列和S,2)数列里数的个数N。
输出描述:
如果有解输出数列K,如果无解输出-1
示例1
输入
525 6
输出
85 86 87 88 89 90
//等差公式 (max + min) * n / 2
public static String successionArr(int sum,int count){
//sum = (min + max) * count /2
// count = max - min + 1
// cur = sum * 2 / count = min + max
int cur = (sum * 2) / count;
//min = cur - max
//max = count + min -1
//min = cur - (count + min -1) = cur - count - min + 1
//2min = cur - count + 1
//min = (cur - count + 1) / 2
int min = (cur - count + 1) / 2;
StringBuffer sb = new StringBuffer();
if(min < 0){
return "-1";
}else {
for (int i = 0; i < count; i++) {
sb.append(min + i).append(" ");
}
return sb.toString().trim();
}
}
2、查找众数及中位数
众数是指一组数据中出现次数量多的那个数,众数可以是多个
中位数是指把一组数据从小到大排列,最中间的那个数,如果这组数据的个数是奇数,那最中间那个就是中位数,如果这组数据的个数为偶数,那就把中间的两个数之和除以2,所得的结果就是中位数
查找整型数组中元素的众数并组成一个新的数组,求新数组的中位数
public static float findCenter(int[] arr){
int max = 0;
HashMap<Integer,Integer> hashMap = new HashMap();
for(int i = 0 ;i < arr.length ; i++){
if(hashMap.containsKey(arr[i])){
int count = hashMap.get(arr[i]);
hashMap.put(arr[i],++count);
max = Math.max(max,count);
}else{
hashMap.put(arr[i],1);
}
}
PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
for(Integer key : hashMap.keySet()){
if(hashMap.get(key) == max){
queue.offer(key);
}
}
int[] s = new int[queue.size()];
for(int i = 0 ; i < s.length ; i++){
s[i] = queue.poll();
}
int index = s.length/2;
if(s.length % 2 != 0){
return s[index];
}else{
return (s[index-1] + s[index]) / 2;
}
}
3、寻找相同子串
给你两个字符串 t 和 p ,要求从 t 中找到一个和 p 相同的连续子串,并输出该字串第一个字符的下标。
示例1
输入
AVERDXIVYERDIAN
RDXI
输出
4
public static int strChild(String str1,String str2){
int start = 0;
for(int i = start ; i < str1.length(); i++){
for(int j = 0 ; j < str2.length() ; j++){
if(str1.charAt(i) == str2.charAt(j)){
i++;
if(j == str2.length()-1){
return i - str2.length()+1;
}
}else{
start = i;
break;
}
}
}
return -1;
}
4、字符串统计
给定两个字符集合,一个为全量字符集,一个为已占用字符集。已占用的字符集中的字符不能再使用,要求输出剩余可用字符集。
输入描述:
1、输入为一个字符串,一定包含@符号。@前的为全量字符集,@后的字为已占用字符集。
2、已占用字符集中的字符一定是全量字符集中的字符。字符集中的字符跟字符之间使用英文逗号分隔。
3、每个字符都表示为字符加数字的形式,用英文冒号分隔,比如a:1,表示1个a字符。
4、字符只考虑英文字母,区分大小写,数字只考虑正整形,数量不超过100。
5、如果一个字符都没被占用,@标识仍然存在,例如a:3,b:5,c:2@
输出描述:
输出可用字符集,不同的输出字符集之间回车换行。
注意,输出的字符顺序要跟输入一致。不能输出b:3,a:2,c:2
如果某个字符已全被占用,不需要再输出。
示例1
输入
a:3,b:5,c:2@a:1,b:2
输出
a:2,b:3,c:2
public static String getStr(String str){
String[] split = str.split("@");
if(split.length == 1){
return split[0];
}else{
//存放已占用的
HashMap<String,Integer> hashMap = new HashMap<>();
String[] s2 = split[1].split(",");
for(int i = 0 ; i < s2.length; i++){
String[] s2Son = s2[i].split(":");
hashMap.put(s2Son[0],Integer.valueOf(s2Son[1]));
}
String[] s1 = split[0].split(",");
StringBuffer sb = new StringBuffer();
for(int i = 0 ; i < s1.length; i++){
String[] s1Son = s1[i].split(":");
if(hashMap.containsKey(s1Son[0])){
Integer res = Integer.valueOf(s1Son[1]) - hashMap.get(s1Son[0]);
String resStr = res > 0 ? String.valueOf(res) : "0";
sb.append(s1Son[0] + ":" + resStr + ",");
}else {
sb.append(s1Son[0] + ":" + s1Son[1] + ",");
}
}
return sb.toString();
}
}
5、磁盘容量排序
磁盘的容量单位常用的有M,G,T这三个等级,它们之间的换算关系为1T = 1024G,1G = 1024M,现在给定n块磁盘的容量,请对它们按从小到大的顺序进行稳定排序,例如给定5块盘的容量,1T,20M,3G,10G6T,3M12G9M排序后的结果为20M,3G,3M12G9M,1T,10G6T。注意单位可以重复出现,上述3M12G9M表示的容量即为3M+12G+9M,和12M12G相等。
public static String[] capacity(Integer n,String[] strs){
PriorityQueue<String> priorityQueue = new PriorityQueue<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return countCapacity(o1) - countCapacity(o2);
}
});
for(int i = 0 ; i < n; i++){
priorityQueue.offer(strs[i]);
}
String[] newStrs = new String[n];
for(int i = 0 ; i < n; i++){
newStrs[i] = priorityQueue.poll();
}
return newStrs;
}
public static int countCapacity(String str){
//3M12G9M
//12G
//i = 2 /put G - 12
HashMap<Character, Integer> hashMap = new HashMap<>();
int capacity = 0;
int left = 0;
for(int i = 0 ; i < str.length(); i++){
if(hashMap.containsKey(str.charAt(i))){
hashMap.put(str.charAt(i),hashMap.get(str.charAt(i)) + Integer.valueOf(str.substring(left,i)));
left = i + 1;
}else{
//i = 1 put M-3 left = i+1 = 2
//i = 4 put G-12 left = 5
//if containsKey get + 7
//i = 6 put M-9 left = 7
if(str.charAt(i) == 'M' || str.charAt(i) == 'G' || str.charAt(i) == 'T'){
hashMap.put(str.charAt(i),Integer.valueOf(str.substring(left,i)));
left = i + 1;
}
}
}
for (Character key : hashMap.keySet()) {
if(key == 'M'){
capacity += hashMap.get(key);
}else if(key == 'G'){
capacity += hashMap.get(key) * 1024;
}else{
capacity += hashMap.get(key) * 1024 * 1024;
}
}
return capacity;
}