Leetcode25: Longest Common Prefix

2024-08-28 19:58

本文主要是介绍Leetcode25: Longest Common Prefix,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Write a function to find the longest common prefix string amongst an array of strings.

求若干字符串的最长公共前缀。

首先若无字符串,返回“”;接下来求得其中最短字符串的长度len,比较公共前缀只需最多比较len次;最后比较所有字符串里每一位上的字符。

class Solution {
public:string longestCommonPrefix(vector<string>& strs) {if(strs.size() == 0)return "";//if(strs.size() == 1)//return strs[0];int len = strs[0].length();for(int i = 1; i < strs.size(); i++){if(len > strs[i].length())len = strs[i].length();}if(len == 0)return "";int i = 0;int j = 1;bool a = true;while(i<len && a){for(j = 1; j < strs.size(); j++){if(strs[j][i] != strs[j-1][i]){a = false;break;}}if(j == strs.size()) {i++;}}return strs[0].substr(0, i);}
};


这篇关于Leetcode25: Longest Common Prefix的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1115738

相关文章

leetcode#32. Longest Valid Parentheses

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", wh

兔子--Android Studio出现错误:Error:Execution failed for task ':myapp:dexDebug'. com.android.ide.common.pro

重点在:finished with non-zero exit value 2. 这里表明了有重复的内容存在。 由于:Android Studio中引入包的方式有如下2种:    compile 'com.android.support:support-v4:22.0.0'    compile files('libs/support-v

YOLOV5入门教学-common.py文件

在 YOLOv5 框架中,common.py 文件是一个核心组件,负责定义深度学习模型的基础模块和常用操作。无论是卷积层、激活函数、特征融合还是其他复杂的模型结构,common.py 都提供了灵活且高效的实现。在这篇文章中,我们将深入解析 common.py 的设计思想、各个模块的功能以及它在 YOLOv5 中的应用。通过理解该文件的实现细节,不仅可以帮助我们更好地掌握 YOLOv5 的内部结构,

java常用算法之最长回文子串(Longest Palindromic Substring)

方法一:时间复杂度为O(n^3) public static String longestPalindrome1(String s) {int maxPalinLength = 0;String longestPalindrome = null;int length = s.length();// check all possible sub stringsfor (int i = 0; i

[LeetCode] 300. Longest Increasing Subsequence

题:https://leetcode.com/problems/longest-increasing-subsequence/description/ 题目 Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7

[LeetCode] 409. Longest Palindrome

题:https://leetcode.com/problems/longest-palindrome/description/ 题目 Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with

[LeetCode] 128. Longest Consecutive Sequence

题:https://leetcode.com/problems/longest-consecutive-sequence/description/ 题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should

[LeetCode] 594. Longest Harmonious Subsequence

题:https://leetcode.com/problems/longest-harmonious-subsequence/description/ 题目 We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactl

[LeetCode] 687. Longest Univalue Path

题:https://leetcode.com/problems/longest-univalue-path/description/ 题目 Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may no

[LeetCode] 524. Longest Word in Dictionary through Deleting

题:https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ 题目大意 对s删除某些元素,使得删除后的s 为 d中的某个元素。 思路 trie 首先将 建立tire,然后是用dfs搜索,遍历s。 boolean dfs(TrieNode node,String s,StringBuilder cu