题目非常简单(怀疑是不是hc都满了),感觉大多数人应该都能全ac
第一题:
int lineup(string peoples) { // write code here int ge1 = 0, ge2 = 0, n = peoples.length(); int res1 = 0, res2 = 0; for (int i = 0; i < n; ++i) { if (peoples[i] == 'G') { res1 += i - ge1; ++ge1; } else { res2 += i - ge2; ++ge2; } } return min(res1, res2); }第二题
/** * 删除重复元素 * @param array int整型一维数组 * @param arrayLen int array数组长度 * @return int整型vector */ vector<int> removeDuplicate(int *array, int arrayLen) { // write code here map<int, int> hp; for (int i = 0; i < arrayLen; ++i)hp[array[i]] = 1; int n = hp.size(); vector<int> res(n); for (int i = arrayLen - 1; i >= 0; --i) { if (hp[array[i]] == 1) { res[--n] = array[i]; hp[array[i]] = 0; } } return res; }第三题
/** * 对给定的二叉树依次完成前序,中序,后序遍历,并输出遍历结果 * @param input int整型一维数组 -1表示Nil节点 * @param inputLen int input数组长度 * @return int整型vector<vector<>> */ enum class way { front, medium, back }; void cal(int ind, vector<int> &tmp, int *arr, int len, way w) { if (ind >= len || arr[ind] == -1) return; if (w == way::front)tmp.push_back(arr[ind]); cal(ind * 2 + 1, tmp, arr, len, w); //left if (w == way::medium)tmp.push_back(arr[ind]); cal(ind * 2 + 2, tmp, arr, len, w); //right if (w == way::back)tmp.push_back(arr[ind]); } vector<vector<int> > binaryTreeScan(int *input, int inputLen) { // write code here vector<vector<int>> res(3); cal(0, res[0], input, inputLen, way::front); cal(0, res[1], input, inputLen, way::medium); cal(0, res[2], input, inputLen, way::back); return res; }
全部评论
(3) 回帖