• [经典] 回文问题(一)


    Palindrome Number 回文数字

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

    前期处理,首先负数/10的倍数,直接return false;

    然后【1】直观做法是,知道N位数,根据value/10^(N-i)和value%10^i来判断,然后value=value%10^(N-i)/10^i,其中i = 1,..., N/2 【2】Brilliant做法是,通过value的最后一位,作为新数的当前位,循环操作,直到value/=10更新后的value <= 新数,复杂度更低,return value == 新数

    Valid Palindrome 回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

    两端下标,往中间靠近,判断是否相同字母(其中大小写相差d = 'A' - 'a',根据'A~Z','a~z'范围来判断+-d)

    Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. O(1) space

    //Definition for singly-linked list.
    struct ListNode {
    int val;
    struct ListNode *next;
    };

    要求O(1)空间复杂度,通过分割、翻转、双指针遍历完成

      

    Palindrome Permutation I

    Given a string, determine if a permutation of the string could form a palindrome.

    For example,
    "code" -> False, "aab" -> True, "carerac" -> True.

    用一个数组(或者hash)计数,如果奇数数目的字母个数大于1个,则return false;否则return true。时间复杂度为O(N)遍历一次+O(1)遍历一次 = O(N)。

    Palindrome Permutation II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.

    For example:

    Given s = "aabb", return ["abba", "baab"].

    Given s = "abc", return [].

    跟1一样的思想,由于要全部输出,就DFS回溯 

  • 相关阅读:
    C the basics (DMA)
    穷举子集
    排序算法(1)
    C the basics (array, complex)
    Linux中date命令的各种实用方法
    syntaxhighlight实现帝国cms代码高亮/语法高亮(一)
    帝国cms修改评论表情每行显示个数
    java 中hashcode 与 equals的关系
    Java 远程调用与分布式通信的区别
    帝国CMS的phomenewspic/ecmsinfo标签详解
  • 原文地址:https://www.cnblogs.com/littletail/p/5364184.html
Copyright © 2020-2023  润新知