题解 | #牛奶工厂#
牛奶工厂
https://www.nowcoder.com/practice/264efc6e41394a0286c51eb2521ba386
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param time1 int整型一维数组
* @param time2 int整型一维数组
* @param m int整型
* @param n int整型
* @return int整型
*/
public int milk_sum (int[] time1, int[] time2, int m, int n) {
// write code here
int length1 = 0;
int length2 = 0;
int total = 0;
LinkedList<Integer> linkedList = new LinkedList<>();
while (length1 < time1.length && length2 < time2.length) {
if (time1[length1] < time2[length2]) {
linkedList.add(time1[length1]);
total += time1[length1];
length1++;
} else {
total += time2[length2];
length2++;
}
}
while (length1 < time1.length) {
linkedList.add(time1[length1]);
total += time1[length1];
length1++;
}
while (length2 < time2.length) {
linkedList.add(time2[length2]);
total += time2[length2];
length2++;
}
return total;
}
}
本题考察的知识点其实是双指针在数组合并方面的应用,所用编程语言是java
一个指针指向第一个数组的第一个位置,一个指针指向第二个数组的第一个位置,然后两个指针对应的数组数值大小进行比较,指针移动
查看8道真题和解析