当前位置: 首页 > news >正文

面试经典150题——Day20

文章目录

    • 一、题目
    • 二、题解

一、题目

14. Longest Common Prefix

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

If there is no common prefix, return an empty string “”.

Example 1:

Input: strs = [“flower”,“flow”,“flight”]
Output: “fl”
Example 2:

Input: strs = [“dog”,“racecar”,“car”]
Output: “”
Explanation: There is no common prefix among the input strings.

Constraints:

1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] consists of only lowercase English letters.

题目来源: leetcode

二、题解

class Solution {
public:string longestCommonPrefix(vector<string>& strs) {int n = strs.size();int count = strs[0].length();for(int i = 0;i < n - 1;i++){int len1 = strs[i].length();int len2 = strs[i + 1].length();int tmp = 0;for(int j = 0;j < len1 && j < len2;j++){if(strs[i][j] == strs[i + 1][j]) tmp++;else break;}count = min(count,tmp);}string res = "";for(int i = 0;i < count;i++) res += strs[0][i];return res;}
};
http://www.lryc.cn/news/205149.html

相关文章:

  • [SQL开发笔记]AND OR运算符复杂表达式开发实例
  • 如何将本地 PDF 文件进行翻译
  • Node.js的readline模块 命令行交互的模块
  • 前沿重器[36] | ACL23-基于检索的大语言模型-报告阅读
  • 2023秋招笔试算法Python3题解
  • uniapp--点击上传图片到oss再保存数据给后端接口
  • 创建Secret(使用kubectl)
  • Notepad++正则查询替换操作
  • Hive特殊函数的使用
  • Unity Spine 指定导入新Spine动画的默认材质
  • lvs负载均衡集群
  • MySQL---表的增查改删(CRUD基础)
  • 听GPT 讲Rust源代码--library/std(2)
  • 力扣第1005题 K 次取反后最大化的数组和 c++ 贪心 双思维
  • Swoole 4.8版本的安装
  • ChatGPT和Copilot协助Vue火速搭建博客网站
  • javaEE -8(9000字详解网络编程)
  • FPGA从入门到精通(二十)SignalTapII
  • RHCE---shell 条件测试
  • Linux下QT打开文件选择对话框时,程序报错退出
  • PyTorch中的intrusive_ptr
  • webrtc-stream编译报错记录
  • 什么是Docker CLI
  • Java项目_家庭记账(简易版)
  • vscode json文件添加注释报错
  • vue3移动端嵌入pdf的两种办法
  • 中文编程开发语言工具系统化教程初级1上线
  • 零售数据分析模板分享(通用型)
  • Spring Cloud之微服务
  • Linux命令(104)之date