编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""

示例 1:

  1. 输入: ["flower","flow","flight"]
  2. 输出: "fl"

示例 2:

  1. 输入: ["dog","racecar","car"]
  2. 输出: ""
  3. 解释: 输入不存在公共前缀。

说明:

所有输入只包含小写字母 a-z 。

答案参考:

  1. /**
  2. * @param {string[]} strs
  3. * @return {string}
  4. */
  5. var longestCommonPrefix = function(strs) {
  6.  
  7. let one = strs.length>0 ? String(strs[0]).split("") :false;
  8. let a = "";
  9. if (!one ) { return a;};
  10. for ( let i = 0;i<one.length ; i++ ) {
  11. let num = 0;
  12. strs.map(da=>{
  13. da.charAt(i) == one[i] ? num++ :null
  14. })
  15. if (num === strs.length ){ a=a+ one[i] }else{ break}
  16. }
  17. return a
  18.  
  19. };
  1. /**
  2. * @param {string[]} strs
  3. * @return {string}
  4. */
  5. var longestCommonPrefix = function(strs) {
  6.  
  7. var firstStrs = strs[0];
  8. var result ='';
  9. if(!strs.length){
  10. return result;
  11. }
  12. for (var i = 0; i < firstStrs.length; i++) {
  13. for (var j = 1; j < strs.length; j++) {
  14. if(firstStrs[i] != strs[j][i]){
  15. return result;
  16. }
  17. }
  18. result += firstStrs[i];
  19. }
  20. return result;
  21.  
  22. };

LeetCode14.最长公共前缀 JavaScript的更多相关文章

  1. leetcode-14最长公共前缀

    leetcode-14最长公共前缀 题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower& ...

  2. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

  3. [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix

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

  4. LeetCode14.最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  5. leetcode14最长公共前缀

    class Solution { public: string longestCommonPrefix(vector<string>& strs) { ) return " ...

  6. 【Leetcode】【简单】【14最长公共前缀】【JavaScript】

    题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...

  7. Leetcode14._最长公共前缀

    题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow&q ...

  8. LeetCode 14. 最长公共前缀(Longest Common Prefix)

    14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...

  9. [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

    题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...

随机推荐

  1. LeetCode 319 ——Bulb Switcher——————【数学技巧】

    319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...

  2. ztree使用方法 python后台

    一.在提前加载js的地方写一段js,判断该页面是否需要添加ztree,我的项目所有提前加载的js都写在admin.js中 //增加ztree $(document).ready(function() ...

  3. bzoj 5302: [Haoi2018]奇怪的背包

    Description Solution 首先 \(v_1,v_2,v_3...v_n,P\) 能够构成的最小数是 \(gcd(P,v_1,v_2,v_3...v_n)\) 然后 \(gcd(P,v_ ...

  4. HttpServletRequest的获取客户端真实IP

    摘自:http://chenyoulu.diandian.com/post/2012-11-14/40042540378 request方法客户端IP: request.getRemoteAddr() ...

  5. 搭建一个最简单的node服务器

    搭建一个最简单的node服务器 1.创建一个Http服务并监听8888端口 2.使用url模块 获取请求的路由和请求参数 var http = require('http'); var url = r ...

  6. SQL Server 数据库定时自动备份(转)

    本文转载自:http://www.cnblogs.com/zhangq723/archive/2012/03/13/2394102.html 作者:清风寻梦 在SQL Server中出于数据安全的考虑 ...

  7. Spring Chapter4 WebSocket 胡乱翻译 (一)

    4. WebSocket 包含了Servlet stack,原生WebSocket交互,通过SockJS模拟,并且通过STOMP在WebSocket之上订阅.发布消息. 4.1 简介 不扯了,看到这个 ...

  8. Use the list and while to Build Shop car

    #Author: Gordonsalary = int(input("请输入你的工资:"))goods = [('0',"Iphone",5000),('1', ...

  9. IO流之缓冲流

    缓冲流 Java中提高了一套缓冲流,它的存在,可提高IO流的读写速度 缓冲流,根据流的分类字节缓冲流与字符缓冲流. 字节缓冲流 字节缓冲流根据流的方向,共有2个 l  写入数据到流中,字节缓冲输出流 ...

  10. C++基础--malloc和new的区别

    (1)malloc在C和C++中都可以使用,用来申请一段内存:申请的内存一定要用free释放,然后把指针置为null: new只能在C++中使用,用于动态内存分配:new的对象要delete掉: (2 ...