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. CDC ->> 在某个SQL Server中开启CDC功能

    USE AdventureWorks;GO--开启某个数据库的CDC功能EXEC sys.sp_cdc_enable_db;GO--is_cdc_enabled栏位为1代表开启CDC功能了SELECT ...

  2. Java数据结构之排序

    1.冒泡排序:时间复杂度为O(n2) 假设是由小到大排序:相邻两个数之间进行比较,较大的数在后面.一次比较过后最大的数排在最后面 如:40.8.15.18.12一次排序后为:8.15.18.12.40 ...

  3. Docker基础技术:Linux Namespace(上)

    时下最热的技术莫过于Docker了,很多人都觉得Docker是个新技术,其实不然,Docker除了其编程语言用go比较新外,其实它还真不是个新东西,也就是个新瓶装旧酒的东西,所谓的The New “O ...

  4. Android与OpenCV——重新下载安装和OpenCV匹配的Android开发环境

    Android与OpenCV——重新下载安装和OpenCV匹配的Android开发环境 !!OpenCV4Android开发之旅(一)----OpenCV2.4简介及 app通过Java接口调用Ope ...

  5. php常用代码(一)

    1.连接MYSQL数据库代码 <?php $connec=mysql_connect("localhost","root","root" ...

  6. mysql0000-00-00日期异常及解决方法

    当mysql的datetime类型的值设置为0000-00-00 00:00:00(或者 0000-00-00)的时候,java的datetime或者timestamp读取到这个值并转换的时候,将抛出 ...

  7. J2EE开发之常用开源项目介绍

    主要就我所了解的J2EE开发的框架或开源项目做个介绍,可以根据需求选用适当的开源组件进行开发.主要还是以Spring为核心,也总结了一些以前web开发常用的开源工具和开源类库 1持久层: 1)Hibe ...

  8. U3D NGUI改变GameObject Activity闪烁的问题

    不是关闭再激活GameObject会闪烁,而是再激活时,NGUI渲染步骤不一致导致的闪烁. 并且文字激活后渲染要慢一帧,如果延迟一帧处理,又会导致精灵图片快一帧,图片重叠.这个测试结果不一定准确,先记 ...

  9. MTK Android 编译命令

    一.Target 编译命令 usage: (makeMtk|mk) [options] project actions [moudles] options:       -t,-tcc         ...

  10. DataGridView中的单元格提示错误信息

    http://stackoverflow.com/questions/7713988/winforms-problems-validating-a-cell-in-a-datagridview