9 - Palindrome Number

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

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

Solution 1:reverse integer

bool isPalindrome(int x)
{
if(x<)return false;
if(x==reverse(x))
return true;
else
return false;
}
int reverse(int x)
{
long long result=;
while(x!=)
{
result=result*+x%;
x/=;
if(result>INT_MAX)return ;
}
return result;
}

Solution 2 :Integer to String

#include<string>
#include<sstream>
using namespace std;
bool isPalindrome(int x) {
stringstream ss;
ss<<x;
string str=ss.str();  //string s=to_string(x);
for(int i=,j=str.size()-;i<j;i++,j--){
if(str[i]!=str[j])return false;
}
return true;
}

234 - Palindrome Linked List

Given a singly linked list, determine if it is a palindrome.

Follow up:Could you do it in O(n) time and O(1) space?

#include<iostream>
#include<vector>
using namespace std; typedef struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL){}
}node; node* create()
{
node *head=new node(-);
node *temp=head;
int x;
while(cin>>x)
{
node *p=new node(x);
temp->next=p;
temp=temp->next;
}
head=head->next;
temp->next=NULL;
return head;
}
//===============================================================
bool isPalindrome1(node *head)
{
/*Runtime:28ms
tranverse once, put val into vector then judge*/
if(!head)return true;
vector<int> vec;
while(head)
{
vec.push_back(head->val);
head=head->next;
}
int length=vec.size();
for(int i=,j=vec.size()-;i<j;i++,j--){
if(vec[i]!=vec[j])
return false;
}
return true;
}
//===============================================================
node* getMid(node *head)
{
ListNode *slow=head,*fast=head;
while(fast->next && fast->next->next)
{
slow=slow->next;
fast=fast->next->next;
}
if(fast->next)slow=slow->next;
return slow;
}
node* reverse(node *head)
{
if(!head || !head->next)return head;
node *p=head,*cur=p->next;
while(cur)
{
node *post=cur->next;
cur->next=p;
p=cur;
cur=post;
}
head->next=NULL;
return p;
}
bool isPalindrome2(node *head)//Runtime:28ms,拆分逆转后半链表,再同时遍历两链表
{
if(!head)return true;
node *mid=getMid(head);
node *remid=reverse(mid);
while(head && remid){
if(head->val != remid->val)return false;
head=head->next;
remid=remid->next;
}
return true;
}
int main()
{
node *head=create();
cout<<isPalindrome2(head);
system("pause");
}

206 - Reverse Linked List

Reverse a singly linked list.

Hint:A linked list can be reversed either iteratively or recursively. Could you implement both?

Solution 1:iteration

  such as  funtion: node* reverse(node *head)  in previous title

Solution 2:recursion

class Solution {
public:
ListNode* reverseList(ListNode* head) { //runtime:8ms
if(head==NULL||head->next==NULL)return head; ListNode* p = head->next;
ListNode* n = reverseList(p); head->next = NULL;
p->next = head;
return n;
}
};

【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List的更多相关文章

  1. 【LeetCode】287. Find the Duplicate Number

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...

  2. 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...

  3. 【LeetCode】171. Excel Sheet Column Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 Java解法 Python解法 日期 [LeetCode] 题 ...

  4. 【LeetCode】476. 数字的补数 Number Complement

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,476, 补数,二进制,Pyth ...

  5. 【LeetCode】287. Find the Duplicate Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...

  6. 【LeetCode】171. Excel Sheet Column Number

    题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, r ...

  7. 【LEETCODE】44、509. Fibonacci Number

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  8. 【LeetCode】1419. 数青蛙 Minimum Number of Frogs Croaking (Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  9. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

随机推荐

  1. Android 使用Application类保存应用的全局数据

    在实际应用我们经常需要对数据进行交互与保存,但Intent中默认的方法对传输数据是有类型限制的,当我们需要传输或保存一个复杂的泛型数据时,使用Application是一个很好的解决办法. 顾名思义,A ...

  2. PowerDesigner-自定义生成WORD

    PowerDesigner-自定义生成WORD_旧梦重温 分类: web 2014-02-26 21:08 1563人阅读 评论(48) 收藏 举报   目录(?)[+] 1统一建立模型 2导出自定义 ...

  3. php 传址

    在php 中引用的意思是:不同的名字访问同一个变量内容. 变量的引用 PHP 的引用允许你用两个变量来指向同一个内容 例一: <?php $a="2010"; $b =&am ...

  4. Netty那点事

    一.Netty是什么 Netty,无论新手还是老手,都知道它是一个“网络通讯框架”. 所谓框架,基本上都是一个作用:基于底层API,提供更便捷的编程模型. 那么”通讯框架”到底做了什么事情呢?回答这个 ...

  5. dojo 资源库

    文档 :http://wenku.baidu.com/link?url=Nnek_tAjIC-Q3t3e9zHQmsh4LhU3f0ncC1QH8WD_U9-I8-fJ7mMisscFpgfuS8nU ...

  6. How Uuencoding Works

    做题目学习  https://www.zhihu.com/question/26598476/answer/45396765 http://email.about.com/od/emailbehind ...

  7. leetcode:Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  8. 对于json对像,怎么遍历json对象的所有key,在使用json对象时,如果无法知道key,怎么通过key变量来获取值

    对于json对像,怎么遍历json对象的所有key,在使用json对象时,如果无法知道key,怎么通过key变量来获取值?请参阅下面的关键代码: <html> <head> & ...

  9. URAL1495. One-two, One-two 2(dp)

    1495 牵扯一点数位 保存数的时候我是按2进制保存的 把1当作0算 把2当作1算 滚动数组 dp[i][j][(g*10+j)%n] = min(dp[i][j][(g*10+j)%n],dp[i- ...

  10. c#调用系统资源大集合-2

    public static void 打开格式化对话框() { Process.Start("rundll32.exe"," shell32.dll,SHFormatDr ...