解题思路

  1. 基本思路:

    • 根据父母血型查表
    • 获取可能的血型列表
    • 按字典序排序返回
  2. 实现方法:

    • 使用哈希表存储血型对应关系
    • 或使用if-else判断所有情况
    • 结果排序后返回

代码

class ChkBloodType {
public:
    vector<string> chkBlood(string father, string mother) {
        vector<string> result;
        
        // O和O
        if (father == "O" && mother == "O") {
            result.push_back("O");
        }
        // A和O
        else if ((father == "A" && mother == "O") || (father == "O" && mother == "A")) {
            result.push_back("A");
            result.push_back("O");
        }
        // A和A
        else if (father == "A" && mother == "A") {
            result.push_back("A");
            result.push_back("O");
        }
        // A和B
        else if ((father == "A" && mother == "B") || (father == "B" && mother == "A")) {
            result.push_back("A");
            result.push_back("AB");
            result.push_back("B");
            result.push_back("O");
        }
        // A和AB
        else if ((father == "A" && mother == "AB") || (father == "AB" && mother == "A")) {
            result.push_back("A");
            result.push_back("AB");
            result.push_back("B");
        }
        // B和O
        else if ((father == "B" && mother == "O") || (father == "O" && mother == "B")) {
            result.push_back("B");
            result.push_back("O");
        }
        // B和B
        else if (father == "B" && mother == "B") {
            result.push_back("B");
            result.push_back("O");
        }
        // B和AB
        else if ((father == "B" && mother == "AB") || (father == "AB" && mother == "B")) {
            result.push_back("A");
            result.push_back("AB");
            result.push_back("B");
        }
        // AB和O
        else if ((father == "AB" && mother == "O") || (father == "O" && mother == "AB")) {
            result.push_back("A");
            result.push_back("B");
        }
        // AB和AB
        else if (father == "AB" && mother == "AB") {
            result.push_back("A");
            result.push_back("AB");
            result.push_back("B");
        }
        
        sort(result.begin(), result.end());
        return result;
    }
};
import java.util.*;

public class ChkBloodType {
    public String[] chkBlood(String father, String mother) {
        ArrayList<String> result = new ArrayList<String>();
        
        // O和O
        if (father.equals("O") && mother.equals("O")) {
            result.add("O");
        }
        // A和O
        else if ((father.equals("A") && mother.equals("O")) || 
                 (father.equals("O") && mother.equals("A"))) {
            result.add("A");
            result.add("O");
        }
        // A和A
        else if (father.equals("A") && mother.equals("A")) {
            result.add("A");
            result.add("O");
        }
        // A和B
        else if ((father.equals("A") && mother.equals("B")) || 
                 (father.equals("B") && mother.equals("A"))) {
            result.add("A");
            result.add("AB");
            result.add("B");
            result.add("O");
        }
        // A和AB
        else if ((father.equals("A") && mother.equals("AB")) || 
                 (father.equals("AB") && mother.equals("A"))) {
            result.add("A");
            result.add("AB");
            result.add("B");
        }
        // B和O
        else if ((father.equals("B") && mother.equals("O")) || 
                 (father.equals("O") && mother.equals("B"))) {
            result.add("B");
            result.add("O");
        }
        // B和B
        else if (father.equals("B") && mother.equals("B")) {
            result.add("B");
            result.add("O");
        }
        // B和AB
        else if ((father.equals("B") && mother.equals("AB")) || 
                 (father.equals("AB") && mother.equals("B"))) {
            result.add("A");
            result.add("AB");
            result.add("B");
        }
        // AB和O
        else if ((father.equals("AB") && mother.equals("O")) || 
                 (father.equals("O") && mother.equals("AB"))) {
            result.add("A");
            result.add("B");
        }
        // AB和AB
        else if (father.equals("AB") && mother.equals("AB")) {
            result.add("A");
            result.add("AB");
            result.add("B");
        }
        
        Collections.sort(result);
        String[] array = new String[result.size()];
        return result.toArray(array);
    }
}
class ChkBloodType:
    def chkBlood(self, father, mother):
        result = []
        
        # O-O
        if father == "O" and mother == "O":
            result.append("O")
        # A-O
        elif (father == "A" and mother == "O") or (father == "O" and mother == "A"):
            result.extend(["A", "O"])
        # A-A
        elif father == "A" and mother == "A":
            result.extend(["A", "O"])
        # A-B
        elif (father == "A" and mother == "B") or (father == "B" and mother == "A"):
            result.extend(["A", "AB", "B", "O"])
        # A-AB
        elif (father == "A" and mother == "AB") or (father == "AB" and mother == "A"):
            result.extend(["A", "AB", "B"])
        # B-O
        elif (father == "B" and mother == "O") or (father == "O" and mother == "B"):
            result.extend(["B", "O"])
        # B-B
        elif father == "B" and mother == "B":
            result.extend(["B", "O"])
        # B-AB
        elif (father == "B" and mother == "AB") or (father == "AB" and mother == "B"):
            result.extend(["A", "AB", "B"])
        # AB-O
        elif (father == "AB" and mother == "O") or (father == "O" and mother == "AB"):
            result.extend(["A", "B"])
        # AB-AB
        elif father == "AB" and mother == "AB":
            result.extend(["A", "AB", "B"])
            
        return sorted(result)

算法及复杂度

  • 算法:条件判断 + 排序
  • 时间复杂度:,其中 为可能的血型数量(排序的复杂度)
  • 空间复杂度:,用于存储结果数组