LeetCode 647. Palindromic Substrings--回文子串个数
来源:互联网 发布:甲骨文软件 薪资 编辑:程序博客网 时间:2024/06/06 04:13
Given a string, your task is to count how many palindromic substrings in this string.
The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.
Example 1:
Input: "abc"Output: 3Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: "aaa"Output: 6Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Note:
- The input string length won't exceed 1000.
package com.main;public class Main { private int lo, count; public int countSubstrings(String s) { if (s == null ) { return 0; } if(s.length() < 2){ return 1; } for (int i = 0; i < s.length(); i++) { extendLongestPalindrome(s, i, i);//输出的子串长度都是奇数,从中间某个位向两边比较 extendLongestPalindrome(s, i, i + 1);//输出的子串长度都是偶数,从中间两个相邻的位分别向两边比较 } return count; } public void extendLongestPalindrome(String s, int j, int k) { while (j >= 0 && k < s.length() && s.charAt(j) == s.charAt(k)) { j--; k++; count++; } }//extendLongestPalindrome public static void main(String[] args) { // write your code here Main main = new Main(); int count = main.countSubstrings("abcDD");//输出6 System.out.println(count); }}
130 / 130 test cases passed.
Status: Accepted
Runtime: 15 ms
Your runtime beats 66.73 % of java submissions.
阅读全文
0 0
- LeetCode 647. Palindromic Substrings--回文子串个数
- Leetcode 647. Palindromic Substrings 回文子串 解题报告
- leetcode 647. Palindromic Substrings 回文子串的数量
- 647. Palindromic Substrings 回文子串
- 647. Palindromic Substrings(回文子字符串)
- [leetcode]647. Palindromic Substrings
- leetcode 647. Palindromic Substrings
- [LeetCode]647. Palindromic Substrings
- leetcode 647. Palindromic Substrings
- [LeetCode] 647. Palindromic Substrings
- 【LeetCode】647. Palindromic Substrings
- Leetcode 647. Palindromic Substrings
- LeetCode:647. Palindromic Substrings
- #leetcode#647. Palindromic Substrings
- [LeetCode] 647. Palindromic Substrings
- leetcode 647. Palindromic Substrings
- [LeetCode]647. Palindromic Substrings
- LeetCode:M-647. Palindromic Substrings
- Android初级开发(四)——补充6、控件TabHost的使用
- VS2015无法使用编辑并继续 及 警告LNK4075的解决方法
- PHP 高级过滤器
- ZOJ 3609 求最小逆元模板题
- eclipse 与 android studio 一些常用的对应快捷键
- LeetCode 647. Palindromic Substrings--回文子串个数
- android 沉浸式状态栏 github地址
- Linux shell脚本中shift的用法说明
- 弱网下移动端网络连接处理策略
- 解决DBlink无法解析指定的连接标识符的问题
- 2016至2017年上半年iOS项目细节(1)
- python实现最大熵模型
- iOS GPUImage 的使用
- IOS开发入门----UIWindow(2017/8/8)