453. 最小操作次数使数组元素相等
本人答案超时
class Solution {
public:int minMoves(vector<int>& nums) {int len = nums.size();int count = 0;if (len == 1) {count = 0;}else {while (nums[0] != nums[1] || nums[len - 2] != nums[len - 1]) {for (int i = 0; i < len - 1; i++) {nums[i]++;}sort(nums.begin(), nums.end());count++;}}return count;}
};
本题逆向思维,思路好难想,学习一下
665. 非递减数列
class Solution {
public:bool checkPossibility(vector<int>& nums) {int count = 0;for (int i = 0; i < nums.size() - 1; i++) {if (nums[i] > nums[i + 1]) {count++;if (i > 0 && nums[i + 1] < nums[i - 1]) {nums[i + 1] = nums[i];}}}if (count <= 1) {return true;}else {return false;}}
};
283. 移动零
class Solution {
public:void moveZeroes(vector<int>& nums) {int len = nums.size(), left = 0, right = 0;while (right < len) {if (nums[right]) {swap(nums[left], nums[right]);left++;}right++;}}
};