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 实现Path2.0中绚丽的的旋转菜单

    上图先: 那么下面开始吧~ 首先,将整个菜单动画分解开来. 1.       一级菜单按钮的旋转动画2个,十字和叉叉状态的转换. 2.       二级菜单按钮的平移动画2个,弹簧效果的in和out ...

  2. 机器人学 —— 机器人感知(Gaussian Model)

    机器人感知是UPNN机器人专项中的最后一门课程,其利用视觉方法来对环境进行感知.与之前提到的机器人视觉不同,机器人感知更侧重于对环境物体的识别与检测.与计算机视觉不同,机器人视觉所识别的物体往往不需要 ...

  3. zoj 3165 (最小割,最大点权独立集)

    胡伯涛的<最小割模型在信息学竞赛中的应用>写的真牛. 这道题是选择一些男孩和女孩参加party,邀请的男孩女孩之间不能有 8g,图就是个明显的二分图,就是选择一些点之间没有8g关系,就是二 ...

  4. IP地址的定义和含义

    IP的定义 ip 是32位无符号整数,最小,最大分别是- 0.0.0.0 - 255.255.255.255 具体来说,由一个ip由 Net-ID+Host-ID 两部分组成,Net-ID 相同,那么 ...

  5. hdu 1829-A Bug's LIfe(简单带权并查集)

    题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...

  6. BZOJ 1494 生成树计数(生成树计数-矩阵)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1494 题意: 思路: int SIZE; struct matrix { i64 a[N] ...

  7. gulp使用外部配置文件

    这很有好处,因为它使得任务很干净,并且 config.json 可以被其他的任务运行器(例如grunt)重复利用. config.json { "desktop" : { &quo ...

  8. Linux同步机制 - 多线程开发总结

    1 对于CPU开销大的场景,能利用多核,就尽量利用多核(常常自以为某需求的运算量不大,且CPU足够快,就偷懒写个单线程,结果效率很低) 2 使用多线程的时候,默认是加锁的.在加锁保证业务正常的条件下, ...

  9. 【Todo】深入PHP内核系列

    看到一个<深入PHP内核>系列,Todo: http://www.csdn.net/article/2014-09-15/2821685-exploring-of-the-php [问底] ...

  10. .NET 实现异步处理的集中方式

    对于异步,相信大家都不十分陌生.准确点来说就是方法执行后立即返回,待到执行完毕会进行通知.就是当一个任务在执行的时候,尤其是需要耗费很长的时间进行处理的任务,如果利用单线程进行操作的话,势必造成界面的 ...