题解 | #牛的回文编号III#
牛的回文编号III
https://www.nowcoder.com/practice/6af8b6e39b004329a48cc2cd823e5b30
题目考察的知识点:双指针
题目解答方法的文字分析:将十进制转换为二进制,然后判断是否回文
本题解析所用的编程语言:c++
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param x int整型
* @return bool布尔型
*/
string GetBinaryStringFromHexString(string strHex)
{
string sReturn = "";
unsigned int len = strHex.length();
for (unsigned int i = 0; i < len; i++)
{
switch (strHex[i])
{
case '0': sReturn.append("0000"); break;
case '1': sReturn.append("0001"); break;
case '2': sReturn.append("0010"); break;
case '3': sReturn.append("0011"); break;
case '4': sReturn.append("0100"); break;
case '5': sReturn.append("0101"); break;
case '6': sReturn.append("0110"); break;
case '7': sReturn.append("0111"); break;
case '8': sReturn.append("1000"); break;
case '9': sReturn.append("1001"); break;
case 'a': sReturn.append("1010"); break;
case 'b': sReturn.append("1011"); break;
case 'c': sReturn.append("1100"); break;
case 'd': sReturn.append("1101"); break;
case 'e': sReturn.append("1110"); break;
case 'f': sReturn.append("1111"); break;
}
}
return sReturn;
}
bool isPalindromeNumberIII(int x)
{
// write code here
char buffer[9] = { 0 };
snprintf(buffer, sizeof(buffer), "%08x", x);
string s(buffer);
s = GetBinaryStringFromHexString(s);
int p1 = 0, p2 = s.size() - 1;
while (p1 < p2)
if (s[p1++] != s[p2--])
return false;
return true;
}
};
查看10道真题和解析