题解 | #牛的品种排序III#
牛的品种排序III
https://www.nowcoder.com/practice/f6ab3d7e20f54860886848f0a6374987
考察知识点:遍历,排序
题目分析:遍历k个种类,从数组中查找遍历到的类型的数据,并保存到数组中,返回结果
采用的编码语言:C
完整的编码代码:如下所示
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param cows int整型一维数组 * @param cowsLen int cows数组长度 * @param k int整型 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ #include <stdio.h> int* sortCowsIII(int* cows, int cowsLen, int k, int* returnSize ) { int *tmp = NULL; int count = 0; tmp = calloc(1, 3000); // 别问我为什么只分配不释放 for (int i = 0; i < k; i++) { // 有k个种类,那么我们就一个个种类的来查找吧 for (int j = 0; j < cowsLen; j++) { // 从数组中查找i这种类型的数据,并保存到tmp数组中 if (cows[j] == i) tmp[count++] = cows[j]; // count++用完自己加一,妈妈再也不用怕我反复写一个元素啦 } } *returnSize = cowsLen; // 新数组长度和原数组长度一样 return tmp; }
面试高频TOP202解析 文章被收录于专栏
采用Java,C,Python等方法去解答面试高频TOP202题目,