zoukankan      html  css  js  c++  java
  • 【LeetCode】9 & 234 & 206

    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<0)return false;
        if(x==reverse(x))
            return true;
        else
            return false;
    }
    int reverse(int x)
    {
        long long result=0;
        while(x!=0)
        {
            result=result*10+x%10;
            x/=10;
            if(result>INT_MAX)return 0;
        }
        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=0,j=str.size()-1;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(-1);
        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=0,j=vec.size()-1;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; 
        }
    };
  • 相关阅读:
    share point 已在此服务器场中安装 ID 为 15/b7a69889-1789-4855-b8bd-9a3b4cfd7fc0 的功能。请使用强制属性显式地重新安装此功能。
    给一个div添加多个背景图片
    input输入框,在手机上,软键盘会将固定定位和绝对定位的按钮顶起,解决办法
    js判断浏览器,设备类型
    js中函数function的三种定义方式,声明式定义函数、函数表达式、立即执行函数的区别
    vue项目里面,__ob__对象的理解
    浏览器渲染过程-面试
    MySQL的连接查询:left join , right join , join
    MYSQL实现连表查询
    node后台koa2项目,如何发布到服务器?入门学习
  • 原文地址:https://www.cnblogs.com/irun/p/4678655.html
Copyright © 2011-2022 走看看