维基百科上有国家代码可用的所有电话号码列表.是这里
我会创建两个哈希图.一个包含所有四位数代码,另一个包含两位和三位数代码.
Hashmap fourdigitcodes;
//check if first digit is 7
if(firstdigit==7)
//country definitely russia
else
if country code begins with 1.
Check the first four digits are in the hashmap with four digit codes and return the value
if no value is found then answer is certainly usa.
otherwise
String country="";
HashMap ccode = new HashMap();
ccode.put("353","Ireland");//List all country codes
ccode.put("49","Germany");
String value = ccode.get("49");//See if I have a value for the first 2 digits
if(value != null){
country = value; //If I found a country for the first two digits we are done as the first two digits of the country code consisting of two digits cannot be in a country code with 3
}else{
the country is certainly 3 digit. return the first three digits by entering the first 3 digits as the key of the hashmap countaining the first three digits.
}
综上所述.如果第一个数字是7俄语.
如果是1则检查前四个数字的四位数代码的散列图
如果发现四位数代码返回相应的country.if不回答是美国
否则检查包含2或3位数的散列图中的前两位数.如果找到两个然后返回答案,那么肯定是三位数代码并从散列中返回相应的国家/地区.
制作hashmap会很繁琐,但我认为这是一种有效的解决方案.