LeetCode第五题:Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb" 题意就是给一个字符串,找到子串中最大的对称字符串。
我的想法就是分为两种情况,奇数情况和偶数,从其中一个字符或者两个字符往前后两个方向进行扫描。
public String longestPalindrome(String s) {
String longestPalindrome = "";
for (int i = 0; i < s.length(); i++) {
//奇数情况
int pre = i;
int next = i;
String s1 = getLongestPalindromeByIndex(pre, next, s);
//偶数情况
next++;
String s2 = getLongestPalindromeByIndex(pre, next, s);
s1 = s1.length() > s2.length() ? s1 : s2;
longestPalindrome = longestPalindrome.length() >= s1.length() ? longestPalindrome : s1;
}
return longestPalindrome;
}
public static String getLongestPalindromeByIndex(int pre, int next, String s) {
while (pre >= 0 && next < s.length() && s.charAt(pre) == s.charAt(next)) {
pre--;
next++;
}
//当退出循环时,说明当前pre和next不相等,从pre+1截取到next-1
return s.substring(pre + 1, next);
}
这个代码有一个问题,就是产生了很多无用的字符串,其实可以将返回值变成start和end也就是开始索引和结束索引,
但是我感觉这样写可读性好一点。
LeetCode第五题:Longest Palindromic Substring的更多相关文章
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
- leetcode第五题--Longest Palindromic Substring
Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maxim ...
- Leetcode:【DP】Longest Palindromic Substring 解题报告
Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...
- leetcode--5 Longest Palindromic Substring
1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...
- LeetCode(5)Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion
[Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
- LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划
题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...
- 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- (python)leetcode刷题笔记05 Longest Palindromic Substring
5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
随机推荐
- linux 无密码登录
环境:Linux 脚本:Python 功能:批量IP,远程执行命令.拷贝文件 运行:./ssh_scp.py iplist.txt 脚本内容: #!/usr/bin/env python# -*- c ...
- windows10添加电源计划修改的快捷方案
转自:http://news.mydrivers.com/1/431/431346.htm 由于目前的Windows 10预览版在UI方面还未优化到位,所以某些设置选项要想找出来是很难的,这时候如果能 ...
- JSP--内置对象&动作标签介绍
1.JSP中常用的9大内置对象? 内置对象:在JSP页面中能直接使用的对象就是JSP内置对象,事实上,JSP底层就是一个java类,可以在JSP中直接使用的,必然存在JSP翻译后的java类 下面简单 ...
- springboot+mybatis+springSecurity+thymeleaf
配置步骤: .pom <dependencies> <dependency> <groupId>org.springframework.security</g ...
- 执行Oracle存储过程报权限不足的解决方法
当前Oracle用户sofa拥有connect.dba.resource的角色权限,但奇怪的是却没有执行Oracle Procedure的权限.后来通过查找资料发现:如果sofa用户需要执行Proce ...
- EXTJS4.2 级联 下拉
items: [ { xtype: "fieldcontainer", layout: "hbox", items: [{ xtype: 'combo', na ...
- 实例说明Java中的null(转)
让我们先来看下面的语句: String x = null; 1. 这个语句到底做了些什么? 让我们回顾一下什么是变量,什么是变量值.一个常见的比喻是 变量相当于一个盒子.如同可以使用盒子来储存物品一 ...
- Apache虚拟主机配置模板
/////////////////////////////////写在前头////////////////////////////////////////1.Apache HTTP 服务器2.4文档: ...
- 2017-01-15 微信小程序胡诌一
2017年1月9日,正值iphone发布10周年,在2007年的1月9日,乔布斯发布了震惊世界的iphone,10年后张小龙正式推出了他的小程序,究竟何意也没有具体深究.最初的时候小程序并不叫小程序, ...
- TCP 3次握手建立连接
TCP 3次握手建立连接 1. (Client) –> [SYN] –> (Server) 假如Client和Server通讯. 当Client要和Server通信时,Client首先 ...