「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 ...
随机推荐
- php数组键值操作和数组统计函数-函数
1.数组函数//作用:提供了很多官方写的很多有用的代码段,提高编写速度 1)数组的键值操作函数 array_values();//获取数组中的值 array_keys();//获取数组中的键 in_a ...
- selenium通过python字典获取配置
python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...
- 使用Apache HttpClient 4.5设置超时时间
使用HttpClient,一般都需要设置连接超时时间和获取数据超时时间.这两个参数很重要,目的是为了防止访问其他http服务时,由于超时导致自己的应用受影响. 4.5版本中,这两个参数的设置都抽象到了 ...
- ASP.Net GridView 基础
SP.NET 在开发过程中经常使用的微软提供的服务器控件(GridView),但在开发中很少使用界面化来操作.导致了有点不太会使用界面化操作了,还有就是一些不经常使用的属性也没什么印象了,在网上找了好 ...
- 指纹协查统计sql
select dic.name, NVL(zc.zc_djzs,0),NVL(zc.zc_shzs,0),NVL(zc.zc_bzzs,0), NVL(zt.zt_djzs,0),NVL(zt.zt ...
- nRF5 SDK for Mesh(六) BLE MESH 的 基础概念
Basic Bluetooth Mesh concepts The Bluetooth Mesh is a profile specification developed and published ...
- nodejs( koa2 )配置 browserHistory
前言 既然能搜到并且还点进来看这篇文章, 那么肯定是知道后台为什么要配置 browserHistory, 也肯定知道为什么非要去用相对来说更麻烦的吧browserHistory, 而不用更简单点的不需 ...
- #leetcode刷题之路30-串联所有单词的子串
给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置.注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考 ...
- Docker 学习记录(基础命令)
1. 获取镜像 docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签] ===> docker pull ubuntu:16:04 2.运 ...
- 给网页标签页添加logo
先把logo转换成后缀名是ico的图片,然后在网页头部,也就是<head></head>中间放上<link rel="shortcut icon"ty ...