classSolution { public: intminBitFlips(int start, int goal){ int bit = 1; uint32_t t = start, t1 = goal; int res = 0, i = 0; while (start != goal) { int x = (start >> i) & 1; int y = (goal >> i) & 1; if (x != y) { if (y == 0) { start &= ~(1 << i); } else { start |= (1 << i); } res ++; } i ++;
classSolution { public: inttriangularSum(vector<int>& nums){ queue<int> q; for (auto num: nums) q.push(num); int bunner = nums.size() - 1, i = 0; while (q.size() != 1) { int a = q.front(); q.pop(); int b = q.front(); int sum = a + b; q.push(sum % 10); i ++; if (i == bunner) { q.pop(); i = 0; bunner = q.size() - 1; } } return q.front(); } };
空间复杂度1做法
1 2 3 4 5 6 7 8 9 10
classSolution { public: inttriangularSum(vector<int>& nums){ int n = nums.size(); for (int i = n - 1; i > 0; i --) for (int j = 0; j < i; j ++) nums[j] = (nums[j] + nums[j + 1]) % 10; return nums[0]; } };