problem:https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/
有四种可能性,相等数字为A[0],出现在上面或下面;相等数字为B[0],出现在上面或下面。取这四者最小值即可。
class Solution { public: int n; int minR(vector<int>& A, vector<int>& B, int target) { int res = 0; for(int i = 0; i < n; i++) { if(A[i] == target) { } else if(B[i] == target) res++; else return INT_MAX; } return res; } int minDominoRotations(vector<int>& A, vector<int>& B) { n = A.size(); int res = min(min(minR(A, B, A[0]),minR(B, A, B[0])),min(minR(B, A, A[0]),minR(A, B, B[0]))); return res == INT_MAX ? -1 : res; } };