博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
14. 最长公共前缀
阅读量:4353 次
发布时间:2019-06-07

本文共 1096 字,大约阅读时间需要 3 分钟。

14. 最长公共前缀

题目:https://leetcode-cn.com/problems/longest-common-prefix/description/

package com.test;public class Lesson014 {    public static void main(String[] args) {        String[] strs = new String[]{};        String prefix = longestCommonPrefix(strs);        System.out.println(prefix);    }    private static String longestCommonPrefix(String[] strs) {        if (strs.length == 0) {            return "";        }        // 默认公共前缀是第一个字符串        String res = strs[0];        for (int i = 1; i < strs.length; i++) {            // 获取每个字符串            String current = strs[i];            int j = 0;            // 获取循环的最大数值            int length = Math.min(res.length(), current.length());            for (; j < length; j++) {                String res01 = res.substring(j, j + 1);                String current01 = current.substring(j, j + 1);                if (!res01.equals(current01)) {                    break;                }            }            // 每一次循环修正公共前缀            res = res.substring(0, j);        }        return res;    }}

 

转载于:https://www.cnblogs.com/stono/p/9468071.html

你可能感兴趣的文章
Git 和 Github 使用指南
查看>>
20180925-4 单元测试
查看>>
mysql的数据存储
查看>>
[转载] Activiti Tenant Id 字段释疑
查看>>
[Java 8] (8) Lambda表达式对递归的优化(上) - 使用尾递归 .
查看>>
SQL Server-聚焦移除Bookmark Lookup、RID Lookup、Key Lookup提高SQL查询性能
查看>>
最小权限的挑战
查看>>
jquery 视觉特效(水平滚动图片)
查看>>
SVG笔记
查看>>
linux下使用dd命令写入镜像文件到u盘
查看>>
001---进程
查看>>
视频人脸检测——OpenCV版(三)
查看>>
php获取来访者在搜索引擎搜索某个关键词,进入网站
查看>>
物联网架构成长之路(8)-EMQ-Hook了解、连接Kafka发送消息
查看>>
2018-2019-1 20165234 20165236 实验二 固件程序设计
查看>>
IDEA的GUI连接数据库写入SQL语句的问题总结
查看>>
Xpath在选择器中正确,在代码中返回的是空列表问题
查看>>
leecode第一百九十八题(打家劫舍)
查看>>
【BZOJ 1233】 [Usaco2009Open]干草堆tower (单调队列优化DP)
查看>>
07-3. 数素数 (20)
查看>>