「Leetcode」14. Longest Common Prefix(Java)
分析
与其说是算法题,不如说是语言特性题。
这题要是对Java的String相关函数掌握的比较熟练,写起来的速度(各种意义上)就会很快。
大致的思路都是一致的,差不到哪里去,无非是枚举长度。值得一提的是,从长到短的枚举顺序要比从短到长优得多。
代码
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
String prefix = strs[0];
for (int i = 1; i < strs.length; ++i) {
while (!strs[i].startsWith(prefix)) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) {
break;
}
}
}
return prefix;
}
}
「Leetcode」14. Longest Common Prefix(Java)的更多相关文章
- LeetCode:14. Longest Commen Prefix(Easy)
1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...
- 「Leetcode」975. Odd Even Jump(Java)
分析 注意到跳跃的方向是一致的,所以我们需要维护一个数接下来跳到哪里去的问题.换句话说,就是对于一个数\(A_i\),比它大的最小值\(A_j\)是谁?或者反过来. 这里有两种方案,一种是单调栈,简单 ...
- 「Leetcode」13. Roman to Integer(Java)
分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- C# 写 LeetCode easy #14 Longest Common Prefix
14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- LeetCode题解(14)--Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...
- Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)
1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...
随机推荐
- QTP基本方法2------截取字符串
1.instr: 返回字符串在另外一个字符串中第一次出现的位置 结构:instr([start],string1,string2[,compare]) start:开始位置,可选参数,默认为1 str ...
- JSTL的下载和配置
1,首先在输入网址http://www.oracle.com/technetwork/java/jstl-137486.html 2,单击红色图标所示 3,单击Download 4,单击JSTL Im ...
- php版本跟扩展模块版本不兼容问题
安装redis扩展后查看时候出现了这样报错: [root@localhost phpredis-develop]# php -m | grep redisPHP Warning: PHP Startu ...
- 自定义组件---图片和文字实现ImageButton效果
1.效果图 2.自定义代码: <span style="font-family:Comic Sans MS;font-size:14px;">public class ...
- 深度包检测(DPI)详细介绍
目录 简介 背景 流量识别 常用功能 具体功能 做法 特征识别 架构举例 部署方式 串接方式 并接方式 存在问题 检测引擎举例 参考文献 简介 DPI(Deep Packet Inspection)深 ...
- Oracle 数据库数据结构(包括存储过程,函数,表,触发器等)版本控制器
原理: 写系统触发器,在修改数据库结构的时候,把DDL写入表中 create sequence A_Ver_Control_seq minvalue nomaxvalue start incremen ...
- 【TOJ 1072】编辑距离(动态规划)
描述 假设字符串的基本操作仅为:删除一个字符.插入一个字符和将一个字符修改成另一个字符这三种操作. 我们把进行了一次上述三种操作的任意一种操作称为进行了一步字符基本操作. 下面我们定义两个字符串的编辑 ...
- chromium之MessageLoop浅析
对chromium的MessageLoop非常感兴趣,接下来会详细分析Windows平台的具体实现. 代码版本:chromium-4.0.210.0_p26329 先看一下依赖的文件 message_ ...
- python通过xlsxwriter模块将文字写入xlsx文件
#今天和大家一起学习通过python的xlsxwriter模块 xlsxwriter模块主要用来生成excel表格,插入数据.插入图标等表格操作等. 环境:python 3 1)安装 xlsxwrit ...
- Redis笔记 -- 链表和链表节点的API函数(三)
链表和链表节点API 函数 作用 时间复杂度 listSetDupMethod 将给定的函数设置为链表的节点值复制函数 复制函数可以通过链表的dup属性直接获得,O(1) listGetDupMeth ...