#include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param cows int整型vector * @return int整型vector */ vector<int> sortCows(vector<int>& cows) { // write code here vector<int>ve; int n = cows.size(); for (int i = 0; i < n; ++i) { if (cows[i] == 0) ve.push_back(cows[i]); } for (int i = 0; i < n; ++i) { if (cows[i] == 1) ve.push_back(cows[i]); } for (int i = 0; i < n; ++i) { if (cows[i] == 2) ve.push_back(cows[i]); } return ve; } };
方法二
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param cows int整型vector * @return int整型vector */ vector<int> sortCows(vector<int>& cows) { // write code here int a = 0, b = 0; int n = cows.size(); for (int i = 0; i < n; ++i) { if (cows[i] == 0) { cows[a++] = 0; } if (cows[i] == 1)b++; } for (int i = a; i < a + b; ++i) cows[i] = 1; for (int i = a + b; i < n; ++i) cows[i] = 2; return cows; } };
一、题目考察的知识点
模拟
二、题目解答方法的文字分析
感觉代码已经很清晰易懂了,具体解析看代码
三、本题解析所用的编程语言
c++