[LeetCode 214] Shortest Palindrome

FPGA远程升级避坑指南:用AXI Quad SPI IP读写N25Q128 Flash的完整流程 本文详细介绍了FPGA远程升级过程中使用AXI Quad SPI IP读写N25Q128 Flash的完整流程,包括IP核配置、Flash操作指令集、双镜像备份设计及生产级远程升级协议。通过最佳实践和避坑策略,帮助开发者实现可靠的镜像更新方案,提升系统可靠性和可维护性。 阅读详情

Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.

For example:

Given "aacecaaa", return "aaacecaaa".

Given "abcd", return "dcbabcd".


Solution:

1. O(n * lgn)

check if substring(0, end) is palindrome or not, end from s.length() to 1. If is palindrome, reverse substring(end, length()) + s

this solution will LSE


public String shortestPalindrome(String s) {
        if(s.length() <=1) return s;
        int right = s.length();
        for(int i = right-1; i>=0; i--) {
            if(isPalindrome(s, i)) {
                StringBuilder sb = new StringBuilder(s.substring(i+1));
                sb.reverse();
                return sb.toString() + s;
            }
        }
        return "";
    }
    public boolean isPalindrome(String s, int right) {
        int left = 0;
        while(left<=right) {
            if(s.charAt(left) != s.charAt(right)) return false;
            right--;
            left++;
        }
        return true;
    }


2. Improve from middle search, O(logn * logn)


public String shortestPalindrome(String s) {
        if(s.length() <=1) return s;
        int center = (s.length() - 1) /2;
        String res = "";
        for(int i = center; i>=0; i--) {
            if(s.charAt(i) == s.charAt(i+1)){
                if((res = isPalindrome(s, i, i+1)) != null) return res;
            }
            
            if((res = isPalindrome(s, i, i)) != null) return res;
        }
        return res;
    }
    public String isPalindrome(String s, int left, int right) {
        int i = 1;
        for(;left-i>=0 && right+i< s.length(); i++) {
            if(s.charAt(left-i) != s.charAt(right+i)) break;
        }
        if(left-i>=0) return null;
        StringBuilder sb = new StringBuilder(s.substring(right+i));
        sb.reverse();
        return sb + s;
    }


K8s(十二):监控与报警(163邮箱+钉钉)-Prometheus + Grafana + Alertmanager(超详细) 🔴 K8s(十二):监控与报警(163邮箱+钉钉)-Prometheus + Grafana + Alertmanager(超详细) 阅读详情

相关推荐

Calibre PEX提参实战:如何用hcell和xcell文件精准屏蔽模块(附避坑指南)

本文深入解析Calibre PEX工具中hcell与xcell文件的协同工作机制,提供模块级寄生参数提取方案。通过实战案例和避坑指南,帮助工程师精准屏蔽已验证IP模块,提升仿真效率和数据一致性,适用于65nm以下工艺节点的复杂SoC设计。

weixin_29275257的博客 626

LeetCode - 解题笔记 - 214 - Shortest Palindrome

启发:找到一个s的最短后缀子串s[i:],其中s[:i]为一个回文串,这样额外增加的前缀就是这个最短子串的逆序,整体长度最短。就是选择了一个比较大的base进行十进制转换,并用mod约束(假设碰撞概率极低),这样可以基于历史结果快速计算新前缀的哈希。本题中,我们就是试图使用该算法找到回文串(query就是自己的逆序),需要一个简单的哈希计算方法。观察:任意输入都有平凡解:s[1:]的逆序,因此新增加的前缀必然比s的长度小(最大为s-1)【官方题解】KMP算法,暂时跳过不学习,等需要的时候再学习。

支锦铭的博客 634

Python实战:5分钟搞定抖音无水印视频下载(附完整代码)

本文系统介绍了抖音无水印视频下载的多种技术方案,重点解析了抖音视频的获取逻辑与去水印原理。文章提供了从零代码的浏览器插件到可编程的Python脚本的完整实现路径,并附带了详细的代码示例,帮助用户快速构建自己的抖音视频下载工具箱。

weixin_29089057的博客 847

leetcode 214: Shortest Palindrome

leetcode 214: Shortest Palindrome java c++ python

西施豆腐渣 1万+

【电击防护篇】电击防护技术详解:从人体效应到接地系统全解析

本文系统讲解了电击防护的关键知识,包括电流对人体的影响(如30mA是心室颤动阈值)、人体电阻特性(700Ω-6100Ω)以及电气接触防护措施。重点分析了TT、TN和IT三大接地系统的结构特点与应用场景,强调TN-S系统最安全,IT系统供电连续性最佳。详细介绍了漏电保护器(RCD)的工作原理与选型指南,建议人身防护选用30mA高灵敏度RCD。文章通过对比表总结三大系统的安全性、成本及适用场景,为电气安全设计提供实用参考。

2301_81299416的博客 1742

如何在 Ubuntu 24.04 LTS 上安装 SSH 服务器?

Ubuntu是一个基于Debian的开源Linux操作系统,由Canonical Ltd.开发和维护。Ubuntu致力于提供用户友好、稳定且安全的操作系统环境,适用于个人用户、企业和服务器等不同场景。Ubuntu 24.04 LTS是Ubuntu发布的一个长期支持版本,LTS代表长期支持(Long Term Support)。这意味着Ubuntu 24.04 LTS将获得长达5年的安全更新和维护支持,使其成为企业和个人用户的理想选择。

网络技术联盟站 5888

[leetcode] 214. Shortest Palindrome 解题报告

题目链接:https://leetcode.com/problems/shortest-palindrome/ Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrom

小榕流光的专栏 4653

2026华为OD机试(JAVA&Go)真题【E卷+A卷+B卷+C卷+D卷】目录+在线OJ

华为OD机试真题题库【E卷+A卷+B卷+C卷+D卷】目录,Java实现。

MISAYAONE的博客 34万+

214. Shortest Palindrome(Leetcode每日一题-2020.08.29)

Problem Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. Example1 Input: “aacecaaa” Output: “aaacecaaa” Example2

Bryan要加油 425

zabbix系列:内存VM.MEMORY.SIZE监控之total、free、pused、available 参数说明

free -m buffer是即将要被写入磁盘的,而cache是被从磁盘中读出来的. zabbix Web total:1046/1024=1.02G Available:216/1046=20.65% zabbix参数 total- 总物理内存. free- 可用内存. active- 内存当前使用或最近使用,所以它在RAM中。 inactive- 未使用内存. wired- 被标记为始终驻留在RAM中的内...

NIO4444 5667

基于Claude Code的 规范驱动开发(SDD)指南

传统的AI辅助开发,如同手工作坊,我们向AI描述想法,它给我们零散的部件,我们再手动打磨、组装。这个过程充满了不确定性、信息损耗和重复劳动。本指南将尝试引领您进入软件开发的“工业时代”。在这里,

形上得其象,形下合于数;阴阳自济,玄理自明。 2747

【LeetCode】214. Shortest Palindrome

先贴一个C语言版的,没有通过AC。自我感觉良好,没有bug,但是在最后一个测试用例上超出运行时间。等待装好开发环境后,测试一下。char* shortestPalindrome(char* s) { char c; int i = 1; char* length =NULL;char* copyStart = NULL;char* copyEnd =NULL ;char* s

子非渔的博客 1028

leetcode 214 Shortest Palindrome

lc214 Shortest Palindrome 可以将问题转化成找到原字符串的最长palindrome子串(注意,子串必须以s[0]为起始) 例如:sdserf sds为最长palindrome子串 只需要将sds之后的子串翻转一下,补充到原字符串之前即可 fre + sdserf 那么如何找到最长palindrome子串呢? 使用KMP s + “@” + revers...

weixin_30312659的博客 143

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

【Java】【LeetCode】214. Shortest Palindrome

Given a strings, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. Example 1: Input: "aacecaaa" Output: "aaacecaaa" Example 2: .

zhelong19830726的博客 254

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

LeetCode 214. Shortest Palindrome(Manacher算法)

题目来源:https://leetcode.com/problems/shortest-palindrome/ 问题描述 214.Shortest Palindrome Hard Given a strings, you are allowed to convert it to a palindrome by adding characters in front of it. Find...

da_kao_la的博客 329

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Leetcode 214 Shortest Palindrome

思路:从后往前遍历s,每次将i所在的元素加到s最前面,再判断s.charAt(i)+s.substring(0, i+1)这个新string是否为回文,如果是,将s中的下标从i开始一直到结尾的子字符串reverse一下,然后再append到s的最前面,就得到了答案。下面给出代码: class Solution { public String shortestPalindrome(String s) { if(isPalindrome(s)){return s;} .

GoodJobJasper的博客 234

Leetcode214-Shortest Palindrome

我的新博客地址:HuHaoyu’s Blog,欢迎参观。 Find and return the shortest palindrome you can find by performing this transformation.

coderhuhy的专栏 952

LeetCode 214 shortest-palindrome

LeetCode 214 虽然求的是最短的palindrome,其实题目可以转换为求从首字母开始的最长palindrome,然后反向添加剩余字符串。 那么简单的实现就是: def shortestPalindrome2(self, s: str) -> str: #time out version if self.isPalindrome(s, len...

frankguodongchen的博客 163

leetcode 214. Shortest Palindrome

Given a strings, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. Example 1:...

PaladinDu的专栏 287

[leetcode] 214.Shortest Palindrome

题目: Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.For exam

u014673347的专栏 692

LeetCode214 Shortest Palindrome

Given a strings, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. Example 1:...

李歇特冯-兹拜因巴哈的博客 194

LeetCode 214. Shortest Palindrome

题目链接:https://leetcode.com/problems/shortest-palindrome/ 题意:已知一个字符串s,在其前面添加最少的字符数使其成为一个回文串。 思路:设字符串s的长度为len,则最多肯定是添加len长度的字符,即将s倒置。什么情况可以少添加字符呢?当s的前缀是一个回文串时,则由于该部分已经是回文串,不需要再为其匹配,添加该段后面的回文部分即可,因...

weixin_30478923的博客 129

LeetCode #214 Shortest Palindrome

Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. For exam

王旻宇 458

LeetCode //C - 214. Shortest Palindrome

【代码】LeetCode //C - 214. Shortest Palindrome。

Made in Code 1119
上一篇: [LeetCode 174] Dungeon Game
下一篇: System Design 之 Tiny Uri
Mavs
博客等级 码龄17年 166粉丝 296原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值