QR8. 5-血型遗传检测
描述
血型遗传对照表如下:
父母血型 | 子女会出现的血型 | 子女不会出现的血型 |
---|---|---|
O与O | O | A,B,AB |
A与O | A,O | B,AB |
A与A | A,O | B,AB |
A与B | A,B,AB,O | —— |
A与AB | A,B,AB | O |
B与O | B,O | A,AB |
B与B | B,O | A,AB |
B与AB | A,B,AB | O |
AB与O | A,B | O,AB |
AB与AB | A,B,AB | O |
给定两个字符串father和mother,代表父母的血型,请返回一个字符串数组,代表孩子的可能血型(按照字典序排列)。
”A”,”A”
返回:[”A”,“O”]
C++ 解法, 执行用时: 2ms, 内存消耗: 472KB, 提交时间: 2020-10-31
class ChkBloodType { public: vector<string> chkBlood(string father, string mother) { // write code here vector<string> v; string f(father); string m(mother); if(f>m) swap(f,m); if(f=="O"&&m=="O"){ v.push_back("O"); }else if(f=="A"&&m=="O"){ v.push_back("A"); v.push_back("O"); }else if(f=="A"&&m=="A"){ v.push_back("A"); v.push_back("O"); }else if(f=="A"&&m=="B"){ v.push_back("A"); v.push_back("B"); v.push_back("AB"); v.push_back("O"); }else if(f=="A"&&m=="AB"){ v.push_back("A"); v.push_back("B"); v.push_back("AB"); }else if(f=="B"&&m=="O"){ v.push_back("B"); v.push_back("O"); }else if(f=="B"&&m=="B"){ v.push_back("B"); v.push_back("O"); }else if((f=="B"&&m=="AB")||(m=="B"&&f=="AB")){ v.push_back("A"); v.push_back("B"); v.push_back("AB"); }else if((f=="AB"&&m=="O")||(m=="AB"&&f=="O")){ v.push_back("A"); v.push_back("B"); }else if((f=="AB"&&m=="AB")||(m=="AB"&&f=="AB")){ v.push_back("A"); v.push_back("B"); v.push_back("AB"); } sort(v.begin(),v.end()); return v; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 356KB, 提交时间: 2020-12-23
class ChkBloodType { public: vector<string> chkBlood(string father, string mother) { // write code here vector<string> v; string f(father); string m(mother); if(f>m) swap(f,m); if(f=="O"&&m=="O"){ v.push_back("O"); }else if(f=="A"&&m=="O"){ v.push_back("A"); v.push_back("O"); }else if(f=="A"&&m=="A"){ v.push_back("A"); v.push_back("O"); }else if(f=="A"&&m=="B"){ v.push_back("A"); v.push_back("B"); v.push_back("AB"); v.push_back("O"); }else if(f=="A"&&m=="AB"){ v.push_back("A"); v.push_back("B"); v.push_back("AB"); }else if(f=="B"&&m=="O"){ v.push_back("B"); v.push_back("O"); }else if(f=="B"&&m=="B"){ v.push_back("B"); v.push_back("O"); }else if((f=="B"&&m=="AB")||(m=="B"&&f=="AB")){ v.push_back("A"); v.push_back("B"); v.push_back("AB"); }else if((f=="AB"&&m=="O")||(m=="AB"&&f=="O")){ v.push_back("A"); v.push_back("B"); }else if((f=="AB"&&m=="AB")||(m=="AB"&&f=="AB")){ v.push_back("A"); v.push_back("B"); v.push_back("AB"); } sort(v.begin(),v.end()); return v; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 376KB, 提交时间: 2020-09-02
class ChkBloodType { public: vector<string> chkBlood(string father, string mother) { // write code here vector<string> child; string s; s.insert(0, father); s.insert(father.size(), mother); if(s.size() == 4) { child.push_back("A");child.push_back("AB");child.push_back("B"); } if(s.size() == 3 && s!="ABO" &&s!="OAB") { child.push_back("A");child.push_back("AB");child.push_back("B"); } if(s.size()==3&&s=="ABO" || s=="OAB") { child.push_back("A");child.push_back("B"); } if(s.size() == 2) { if(s=="OO") child.push_back("O"); if(s=="AO" || s=="OA" ||s=="AA") { child.push_back("A");child.push_back("O"); } if(s=="BO" || s=="OB" ||s=="BB") { child.push_back("B");child.push_back("O"); } if(s=="BA" || s=="AB") { child.push_back("A");child.push_back("AB"); child.push_back("B");child.push_back("O"); } } return child; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 380KB, 提交时间: 2020-10-31
class ChkBloodType { public: vector<string> chkBlood(string father, string mother) { // write code here vector<string> v; string f(father); string m(mother); if(f>m) swap(f,m); if(f=="O"&&m=="O"){ v.push_back("O"); }else if(f=="A"&&m=="O"){ v.push_back("A"); v.push_back("O"); }else if(f=="A"&&m=="A"){ v.push_back("A"); v.push_back("O"); }else if(f=="A"&&m=="B"){ v.push_back("A"); v.push_back("B"); v.push_back("AB"); v.push_back("O"); }else if(f=="A"&&m=="AB"){ v.push_back("A"); v.push_back("B"); v.push_back("AB"); }else if(f=="B"&&m=="O"){ v.push_back("B"); v.push_back("O"); }else if(f=="B"&&m=="B"){ v.push_back("B"); v.push_back("O"); }else if((f=="B"&&m=="AB")||(m=="B"&&f=="AB")){ v.push_back("A"); v.push_back("B"); v.push_back("AB"); }else if((f=="AB"&&m=="O")||(m=="AB"&&f=="O")){ v.push_back("A"); v.push_back("B"); }else if((f=="AB"&&m=="AB")||(m=="AB"&&f=="AB")){ v.push_back("A"); v.push_back("B"); v.push_back("AB"); } sort(v.begin(),v.end()); return v; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 408KB, 提交时间: 2021-08-29
class ChkBloodType { public: map<string,vector<string>> rules { {"OO", {"O"}}, {"AO", {"A","O"}}, {"AA", {"A", "O"}}, {"AB", {"A","AB","B","O"}}, {"AAB", {"A","AB","B"}}, {"BO", {"B","O"}}, {"BB", {"B","O"}}, {"BAB", {"A","AB","B"}}, {"ABO", {"A","B"}}, {"ABAB", {"A","AB","B"}} }; vector<string> chkBlood(string father, string mother) { vector<string> result; if(rules.find(father + mother) != rules.end()){ result = rules[father + mother]; } else if (rules.find(mother + father) != rules.end()){ result = rules[mother + father]; } return result; } };