Algorithm
给你一个字符串
s
,找到s
中最长的回文子串。
class Solution {
public String longestPalindrome(String s) {
if (s.length() == 1) {
return s;
}
String result = "";
for (int i = 0; i < s.length(); i++) {
if (result.length() > s.length() - i) {
break;
}
int lastIndex = s.lastIndexOf(s.charAt(i));
while (lastIndex != -1 && lastIndex > i) {
boolean flag = true;
for (int k = 0; k <= (lastIndex - i) / 2; k++) {
if (s.charAt(i + k) != s.charAt(lastIndex - k)) {
flag = false;
break;
}
}
if (flag && (lastIndex - i + 1) > result.length()) {
result = s.substring(i, lastIndex + 1);
lastIndex = -1;
} else {
lastIndex = s.substring(0, lastIndex).lastIndexOf(s.charAt(i));
}
}
}
if (result.length() > 0) {
return result;
}
return s.substring(0, 1);
}
}
Review
# 6 Killer Productivity Apps for Programmers
在https://medium.com/网站上随便找了一篇文章,很简单,这周没时间了,哈哈。不过这篇文件还是很有意思的,讲了6个程序员大杀器的工具:
- Habitica 以游戏的方式帮助你养成一个好的习惯
- Forest 专注森林,很多人都应该用过,帮助你离开手机,专注到工作和学习
- Cold Turkey 一个网站拦截器,建议有特殊爱好控住不住自己的小伙伴们试一试。
- VS Code 最好用的文本编译器,不接受反驳
- GitHub Copilot 一款智能写代码的东西,读的不是很懂。
- Notion 非常强的一款笔记软件,不过我选择obsidian
Tip
本周主要研究了一下Spring的@Order注解的东西。
以前我一直以为==@Order==的作用是spring控制bean的初始化的顺序,但是实际并不是,看下面的小例子:
@Order(6)
@Slf4j(topic = "e")
@Component
public class ABeanFactoryPostProcessor extends E implements BeanFactoryPostProcessor {
/**
* 实例化顺序,jvm对他实例化
*/
public ABeanFactoryPostProcessor() {
log.debug("==constructor bean a");
}
/**
* 把一个对象new出来以后,执行的第一步操作
* spring自己调用
*/
@PostConstruct
public void initMethod() {
log.debug("annotation init bean a");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
int orderValue = 0;
if(this.getClass().isAnnotationPresent(Order.class)){
Order order = this.getClass().getAnnotation(Order.class);
orderValue = order.value();
}
log.debug("execute postProcessBeanFactory a order={}",orderValue);
}
}
@Order(5)
@Slf4j(topic = "e")
@Component
public class BBeanFactoryPostProcessor extends E implements BeanFactoryPostProcessor {
/**
* 实例化顺序,jvm对他实例化
*/
public BBeanFactoryPostProcessor() {
log.debug("==constructor bean b");
}
/**
* 把一个对象new出来以后,执行的第一步操作
* spring自己调用
*/
@PostConstruct
public void initMethod() {
log.debug("annotation init bean b");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
int orderValue = 0;
if(this.getClass().isAnnotationPresent(Order.class)){
Order order = this.getClass().getAnnotation(Order.class);
orderValue = order.value();
}
log.debug("execute postProcessBeanFactory b order={}",orderValue);
}
}
@Order(7)
@Slf4j(topic = "e")
@Component
public class CBeanFactoryPostProcessor extends E implements BeanFactoryPostProcessor, InitializingBean {
/**
* 实例化顺序,jvm对他实例化
*/
public CBeanFactoryPostProcessor() {
log.debug("==constructor bean c");
}
/**
* 把一个对象new出来以后,执行的第一步操作
* spring自己调用
*/
@PostConstruct
public void initMethod() {
log.debug("annotation init bean c");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
int orderValue = 0;
if(this.getClass().isAnnotationPresent(Order.class)){
Order order = this.getClass().getAnnotation(Order.class);
orderValue = order.value();
}
log.debug("execute postProcessBeanFactory c order={}",orderValue);
}
@Override
public void afterPropertiesSet() throws Exception {
log.debug("InitializingBean init method bean c");
}
}
测试结果:
@Test
public void orderModel() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.test.auto.model.order");
context.refresh();
}
constructor bean a
constructor bean b
constructor bean c
execute postProcessBeanFactory a order=6
execute postProcessBeanFactory a order=5
execute postProcessBeanFactory a order=7
居然对对象的初始化和Spring的某些行为都没有任何的作用,那么它具体的作用是什么呢?下周学习后继续分享。
Share
分享一下自己最近看到的一本书《高效能人士的7个习惯》,这是一本发行量仅次于圣经的书。
-
依赖期:以‘你’为核心,你照顾我;你得为我的得失成败负责。这种人,你与他之间的交往是很累的,在工作和生活中,尽量的远离。
-
独立期:以‘我’为核心,我可以做到;我可以负责;我可以靠自己;我有权选择。这是一个成年人的标准,我们最低也是要自己打到这个层次。同时在生活和工作中,跟我们链接的人,最低也要是这个层次的才可以
-
互赖期:以‘我们’为核心,我们可以做到;我们可以合作;我们可以融合彼此的智慧和能力,共创前程。这是一个成熟的人的习惯,也是我们合作协同的基础,这个阶段的伙伴才是我们可以互相信赖,互相托付的。
研究尚浅,自己也在积极的锻炼这些行为习惯。