Lintcode415-Valid Palindrome-Medium

本文介绍了一种高效算法,用于判断字符串(仅考虑字母和数字)是否为回文。通过双指针法实现O(n)时间复杂度,并考虑了空字符串等特殊情况。

Given a string, determine if it is a palindrome, considering only alphanumeric(字母和数字) characters and ignoring cases.

Example

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama"

Example 2:

Input: "race a car"
Output: false
Explanation: "raceacar"

Challenge

O(n) time without extra memory.

Notice

Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

 

思路:

字符串中只考虑字母和数字是否构成一个回文串,no extra space可以定义两个指针。

 

注意:

  1. 两个指针命名,front 和 end,不要用i, j
  2. 同类中:方法与方法之间直接用方法名来互相调用。Java方法的调用。
  3. 多个if并列 和 else if 区别:If you have used multiple if statements then if the condition is true all will be executed. If you have used if and else if combination only one will be executed where first comes the true value 
    所以,如果把line 10 12 14 变成并列if,output会出错。因为如果连续出现两个或以上的非字母数字字符的话,就会输出false。比如 String s = a'+ba; 
  4. boolean方法命名以is开头。
  5. line 9, 不能写成 while (front != end) , 举例 s = "aa", 指针front, end 交叉后会outOfIndexException.

 

 

代码:

 1 public class Solution {
 2     
 3     public boolean isPalindrome(String s) {
 4         if (s == null || s.length() == 0)
 5             return true;
 6             
 7         int front = 0;
 8         int end = s.length() - 1;
 9         while (front < end) {
10             if (!isValid(s.charAt(front)))
11                 front++;
12             else if (!isValid(s.charAt(end)))
13                 end--;
14             else {
15                 if (isValidCase(s.charAt(front), s.charAt(end))){
16                     front++;
17                     end--;
18                 } else {
19                     return false;
20                 }
21             }
22             
23         }
24         return true;
25     }
26     
27     public boolean isValid(char c) {
28         return Character.isLetter(c) || Character.isDigit(c);
29     }
30     public boolean isValidCase(char a, char b) {
31         if (a == b)
32             return true;
33         else if (Character.toUpperCase(a) == Character.toUpperCase(b))
34             return true;
35         else
36         return false;
37     }
38 }

 

代码优化:

public class Solution {
    public boolean isPalindrome(String s) {
        if (s == null || s.length() == 0) {
            return true;
        }

        int front = 0;
        int end = s.length() - 1;
        while (front < end) {
            while (front < s.length() && !isvalid(s.charAt(front))) { // nead to check range of a/b
                front++;
            }

            if (front == s.length()) { // for empty string “.,,,”     
                return true; 
            }           

            while (end >= 0 && ! isvalid(s.charAt(end))) { // same here, need to check border of a,b
                end--;
            }

            if (Character.toLowerCase(s.charAt(front)) != Character.toLowerCase(s.charAt(end))) {
                return false;
            } else {
                front++;
                end--;
            }
        }

        return true; 
    }

    private boolean isvalid (char c) {
        return Character.isLetter(c) || Character.isDigit(c);
    }
}

 

转载于:https://www.cnblogs.com/Jessiezyr/p/10643865.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值