Palindrome Number(回文串)

本文介绍了一种不使用额外空间判断整数是否为回文串的方法,并提供了两种实现思路,通过比较首尾元素来确定回文特性。

题目:

Determine whether an integer is a palindrome. Do this without extra space.

分析:

该题目来源于leetcode。回文串是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。当然整数形式的回文串也是类似的。但负数不是回文串。两种思路:

  1. 按定义来,依次比较串的首尾,直到中间相隔1个或0个元素(取决于整数是奇数位数还是偶数位数)。优点是当不是回文串时,可以很快发现。如果是回文串,则必须比较全部位数。
  2. 根据回文串的特点,正反序,其数是相同的。优点是不必按位数来判断。

代码:

<span style="font-size:12px;font-weight: normal;">class Solution {
public:
	bool isPalindrome(int x) {
		vector<int> v;
		int i, j;
		if (x == 0)
			return true;

		while (x)
		{
			v.push_back(x % 10);
			x /= 10;
		}
		i = 0;
		j = v.size()-1;
		while (i < j)
		{
			if (v.at(i++) != v.at(j--))
				return false;
		}

		return true;
	}
};</span>

class Solution {
public:
	bool isPalindrome(int x) {
		int y = 0;
		int t = x;

		if (t < 0) //负数不是回文串
			return false;

		while (t)
		{
			y = y * 10 + t % 10;
			t /= 10;
		}
		if (y ^ x) //判断正反序是否相等
			return false;

		return true;
	}
};


下面是一个用MIPS汇编语言实现的回文串判断程序: ``` .data msg1: .asciiz "Enter a string: " msg2: .asciiz "The string is a palindrome." msg3: .asciiz "The string is not a palindrome." str: .space 100 .text main: # Print message to enter string li $v0, 4 # syscall code for printing a string la $a0, msg1 # load address of message to print syscall # Read string from user input li $v0, 8 # syscall code for reading a string la $a0, str # load address of buffer to store string li $a1, 100 # maximum number of characters to read syscall # Calculate length of string move $t0, $zero # initialize length to 0 la $t1, str # load address of string loop: lb $t2, ($t1) # load byte from string beqz $t2, check # if byte is null, go to check addi $t0, $t0, 1 # increment length addi $t1, $t1, 1 # increment pointer j loop # go back to loop check: subi $t1, $t1, 1 # move pointer to end of string div $t0, $two # divide length by 2 mflo $t2 # store quotient in $t2 j check_loop # jump to check loop check_loop: beqz $t2, palindrome # if quotient is 0, go to palindrome lb $t3, ($t1) # load byte from end of string lb $t4, (str) # load byte from start of string bne $t3, $t4, not_palindrome # if bytes are not equal, go to not_palindrome addi $t1, $t1, -1 # decrement pointer to end of string addi $t0, $t0, -1 # decrement length j check_loop # go back to check loop palindrome: # Print message that string is a palindrome li $v0, 4 # syscall code for printing a string la $a0, msg2 # load address of message to print syscall j exit # jump to exit not_palindrome: # Print message that string is not a palindrome li $v0, 4 # syscall code for printing a string la $a0, msg3 # load address of message to print syscall exit: li $v0, 10 # syscall code for exiting program syscall ``` 该程序首先从用户输入中读取一个字符串,并计算出字符串的长度。然后,程序从字符串的两端开始比较每个字符,如果字符相同,则继续比较下一个字符,如果字符不同,则程序将打印一条消息,指出该字符串不是回文串。如果程序成功比较了整个字符串,那么将打印一条消息,指出该字符串是回文串。最后,程序将退出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值