题解 | 回文子串的数量

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param str string字符串 
     * @return int整型
     */
    public int Substrings (String str) {
        int num = 0;
       // HashMap<String,Boolean> map =new HashMap<>();
        System.out.print("str"+str);
        // write code here
        for(int i = 0;i<str.length();i++){
            String indexStr = str.charAt(i)+"";
            for(int j = i;j<str.length();j++){
                String temp ="";
                if(i==j){
                temp = indexStr;
                }else{
                temp = indexStr + str.charAt(j)+"";
                }
                boolean flag = isValid (temp);
                indexStr = temp;
                //Boolean flag2 = map.get(temp);
                if(flag ){
                    num++;
                }
            }
        }
        return num;
    }


     public boolean isValid (String str) {
        int length = str.length();
        for(int i = 0;i<=length/2;i++){
            if(str.charAt(i)!=str.charAt(length- 1- i)) return false;
        }
        return true;
     }
}

全部评论

相关推荐

白火同学:大二有这水平很牛了,可以适当对关键信息加粗一点,比如关键技术、性能指标之类的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务