And left practice with God algorithm

Analyzing a string the string is not palindromic
this problem, it is for C ++ pointers directly before and after the two simultaneously traversed the intermediate indentation on it, this is for the purposes of the string, if it is for a digital is it? Let you judge this number is not a palindrome numbers
first comes to mind is first converted to a digital string, but if encountered coefficient complexity of card time, this bit to die

Consider which way to write
have to say that this code is considered very comprehensive, every detail of the buckle is very strict, that is very solid foundation

bool func(int num)
{
	if (num < 0)
		return false;

	int help = 1;
	while (num / help > 10)
	{
		help *= 10;
	}
	/*
	while(help < num)
	{
		help *= 10;
	}
	help /= 10;
	不这样写的主要原因就是,这样写可能会存在溢出,因为毕竟while循环中最后多乘了一个10
	*/
	while (num != 0)
	{
		if (num / help != num % 10)   //第一位不等于最后一位的话那么就直接返回0
			return false;
		num = (num % help) / 10; //将第一位和最后一位去掉,拿到的是最中间的数字
		help /= 100;
	}
	return true;
}
Published 230 original articles · won praise 28 · views 9312

Guess you like

Origin blog.csdn.net/weixin_43767691/article/details/103555301
Recommended