zoukankan      html  css  js  c++  java
  • [经典] 回文问题(一)

    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回溯 

  • 相关阅读:
    强引用、软引用、弱引用、幻象引用有什么区别?
    vue基础指令学习
    如何设计一个自动化测试框架
    测试工程师需要了解的shell变量知识
    记一次kubernetes集群异常: kubelet连接apiserver超时
    golang http/transport 代码分析
    logging in kubernetes
    tune kubernetes eviction parameter
    kubernetes continually evict pod when node's inode exhausted
    Compile git version inside go binary
  • 原文地址:https://www.cnblogs.com/littletail/p/5364184.html
Copyright © 2011-2022 走看看