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. 构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统

    开篇:从50开始系统已经由MVC4+EF5+UNITY2.X+Quartz 2.0+easyui 1.3.4无缝接入 MVC5+EF6+Unity4.x+Quartz 2.3 +easyui 1.4. ...

  2. 在VS2010中使用Git(转)

    在之前的一片博客<Windows 下使用Git管理Github项目>中简单介绍了在Windows环境中使用Git管理Github项目,但是是使用命令行来进行操作的,本文将简单介绍下在VS2 ...

  3. oracle11g OEM无法连接到数据库实例解决办法

    我的电脑是32位的win7家庭版系统,那么这样的系统能不能装上oracle呢?能的!就是可能会出错,在装oracle时,每个人遇到的问题都不同,有的人装了双系统,有的人重做了系统,真心酸,先让电脑断网 ...

  4. C#服务启动以及服务指令

    Windows系统中使用 cmd 命令安装和卸载服务方法如下: 第一种方法: 1. 开始->运行->cmd 2. cd到C:\WINDOWS\Microsoft.NET\Framework ...

  5. Github原理

    See image below:

  6. sql2000无法打开1433端口及解决方法

    1.如果你是win2003,那么一定要安装sql的补丁sp3a以上版本SP 检查你的SQL有没有打补丁,没有的话要打上补丁,检查的方法是在查询分析器中运行:select @@version如果出来的版 ...

  7. cmake的使用二:链接第三方静态库

    cmake的使用二:链接第三方静态库

  8. html5 audio音频播放全解析

    序 html5开启了一个新时代,因为它让浏览器本身变得不那么被动,audio api就是一个典型的列子,在html5还没确定之前,如果想要在网页上听音乐看视频唯一的办法就是用flash意思是当你没有给 ...

  9. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  10. POJ 3308 Paratroopers (对数转换+最小点权覆盖)

    题意 敌人侵略r*c的地图.为了消灭敌人,可以在某一行或者某一列安置超级大炮.每一个大炮可以瞬间消灭这一行(或者列)的敌人.安装消灭第i行的大炮消费是ri.安装消灭第j行的大炮消费是ci现在有n个敌人 ...