AlienAndPassword
#include <string>
#include <algorithm>
using namespace std;
class AlienAndPassword {
public:
int getNumber(string S) {
char c =*(S.begin());
int res = 1;
for (string::iterator it = S.begin(); it != S.end(); ++it) {
if (*it == c) {
continue;
}
else {
res ++;
c = *it;
}
}
return res;
}
};
AlienAndGame
用与运算进行判断,学习了
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class AlienAndGame {
public:
int getNumber(vector <string> board) {
int h = board.size(), w = board[0].size();
for (int l = min(h, w); l >= 2; -- l) {
for (int i = 0; i + l <= h; ++ i) {
for (int j = 0; j + l <= w; ++ j) {
int flag = 1;
for (int x = i; x < i + l; ++ x) {
int black = 1, white = 1;
for (int y = j; y < j + l; ++ y) {
black &= (board[x][y] == 'B');
white &= (board[x][y] == 'W');
}
flag &= (black != white);
}
if (flag) {
return l * l;
}
}
}
}
return 1;
}
};