LeetCode Online Judge 题目C# 练习 - Palindrome Number
2012-10-02 22:45
387 查看
Determine whether an integer is a palindrome. Do this without extra space.
代码分析:
递归的做法,先递归到最底,然后回溯头一位跟尾一位比较。
例如:“12321”
“1232”, “12321”
-- “123”, “12321”
-- “12”, “12321”
-- “1”, “12321”
-- “0”, “12321” true;递归到这里最底
-- “1”, “12321” 比较 1 % 10 == 12321 % 10,y /= 10, 返回 true;
-- “12”, “1232” 比较 12 % 10 == 1232 % 10,y /= 10, 返回 true;
-- "123", "123" 比较 123 % 10 == 123 % 10,y /= 10, 返回 true;
-- “1232”, “12” 比较 1232 % 10 == 12 % 10,y /= 10, 返回 true;
-- “12321”, “1” 比较 12321 % 10 == 1 % 10, y /= 10, 返回true;
代码分析:
迭代的做法,先把x 的位数找到。x = 12321, div = 10000;
然后再 l = x / div; r = x % 10; 分别得到头尾,比较相同后 x = ( x % div) / 10; 去头去尾, div /= 100;
看题目要求negative number算不算,如果算,先做一次绝对值就好了,LEETCODE上的TEST CASE negetive nubmer是不算的。
public static bool PalindromeNumberRec(int x) { return isPalindrome(x, ref x); } public static bool isPalindrome(int x, ref int y) { if(x < 0) return false; if(x == 0) return true; if(isPalindrome(x / 10, ref y) && (x % 10 == y % 10)) { y /= 10; return true; } else return false; }
代码分析:
递归的做法,先递归到最底,然后回溯头一位跟尾一位比较。
例如:“12321”
“1232”, “12321”
-- “123”, “12321”
-- “12”, “12321”
-- “1”, “12321”
-- “0”, “12321” true;递归到这里最底
-- “1”, “12321” 比较 1 % 10 == 12321 % 10,y /= 10, 返回 true;
-- “12”, “1232” 比较 12 % 10 == 1232 % 10,y /= 10, 返回 true;
-- "123", "123" 比较 123 % 10 == 123 % 10,y /= 10, 返回 true;
-- “1232”, “12” 比较 1232 % 10 == 12 % 10,y /= 10, 返回 true;
-- “12321”, “1” 比较 12321 % 10 == 1 % 10, y /= 10, 返回true;
public static bool PalindromeNumber(int x) { if (x < 0) return false; int div = 1; while (x / div >= 10) { div *= 10; } while (x != 0) { int l = x / div; int r = x % 10; if (l != r) return false; x = (x % div) / 10; div /= 100; } return true; }
代码分析:
迭代的做法,先把x 的位数找到。x = 12321, div = 10000;
然后再 l = x / div; r = x % 10; 分别得到头尾,比较相同后 x = ( x % div) / 10; 去头去尾, div /= 100;
看题目要求negative number算不算,如果算,先做一次绝对值就好了,LEETCODE上的TEST CASE negetive nubmer是不算的。
相关文章推荐
- LeetCode Online Judge 题目C# 练习 - Swap Nodes in Pairs
- LeetCode Online Judge 题目C# 练习 - Trapping Rain Water
- LeetCode Online Judge 题目C# 练习 - Container With Most Water
- LeetCode Online Judge 题目C# 练习 - Integer to Roman
- LeetCode Online Judge 题目C# 练习 - Sqrt(x)
- LeetCode Online Judge 题目C# 练习 - Jump Game
- LeetCode Online Judge 题目C# 练习 - Roman to Integer
- LeetCode Online Judge 题目C# 练习 - Search in Rotated Sorted Array
- LeetCode Online Judge 题目C# 练习 - Search in Rotated Sorted Array II
- LeetCode Online Judge 题目C# 练习 - Simplify Path
- LeetCode Online Judge 题目C# 练习 - Multiply Strings
- LeetCode Online Judge 题目C# 练习 - Permutations
- LeetCode Online Judge 题目C# 练习 - Remove Duplicates from Sorted Array
- LeetCode Online Judge 题目C# 练习 - Scramble String
- LeetCode Online Judge 题目C# 练习 - Unique Binary Search Trees
- LeetCode Online Judge 题目C# 练习 - Remove Duplicates from Sorted Array II
- LeetCode Online Judge 题目C# 练习 - Remove Duplicates from Sorted List
- LeetCode Online Judge 题目C# 练习 - Sort Color
- LeetCode Online Judge 题目C# 练习 - Longest Valid Parentheses
- LeetCode Online Judge 题目C# 练习 - Unique Paths II