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

char* longestCommonPrefix(char** strs, int strsSize) {
if(strsSize==) return ""; char* ret = strs[];
int i, j;
int cmpLen;
for(int i = ; i < strsSize; i++){ //traverse strs
j = ;
cmpLen = (strlen(strs[i]) > strlen(ret))?strlen(ret):strlen(strs[i]);
while(j < cmpLen && strs[i][j]==ret[j]) j++;
ret[j] = '\0';
}
return ret;
}

14. Longest Common Prefix (截取字符串)的更多相关文章

  1. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  2. 14. Longest Common Prefix【leetcode】

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  3. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  4. leetCode练题——14. Longest Common Prefix

    1.题目 14. Longest Common Prefix   Write a function to find the longest common prefix string amongst a ...

  5. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  6. 14. Longest Common Prefix 最长的公共字符串开头

    [抄题]: Write a function to find the longest common prefix string amongst an array of strings. 在 " ...

  7. 14.Longest Common Prefix (String)

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

  8. C# 写 LeetCode easy #14 Longest Common Prefix

    14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  9. [LeetCode] 14. Longest Common Prefix 最长共同前缀

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

随机推荐

  1. js中json字符串与对象的转换及是否为空

    1.json对象(数组)转字符串 var b=[ { "CategoryName" : "Beverages", "ProductName" ...

  2. PHP反射机制实现自动依赖注入

    依赖注入又叫控制反转,使用过框架的人应该都不陌生.很多人一看名字就觉得是非常高大上的东西,就对它望而却步,今天抽空研究了下,解开他它的神秘面纱.废话不多说,直接上代码: /* * * * 工具类,使用 ...

  3. grep命令的常用选项

    Linux的grep命令是使用正则表达式进行文本搜索的,一些对程序员很有用的选项如下: -i   忽略大小写 -w  进行普通文件匹配,而不是正则表达式匹配 -c  只统计每个文件中匹配行数(默认是输 ...

  4. 关闭win10 自动更新 及蓝屏解决办法

    "控制面板-管理工具-服务"(或在"此电脑"鼠标右键,点击"管理"),找到Windows Update项目后,将"启动类型&quo ...

  5. 源码编译php5.4 ./configure参数

    ./configure \--prefix=/usr/local/php/5.4 \--with-config-file-path=/usr/local/php/5.4/etc \--with-con ...

  6. mongodb学习-练习实例

    C:\Users\issuser>mongoMongoDB shell version: 2.4.6connecting to: test> db.user.find(){ "_ ...

  7. Java工具类DateFormatUtils详解

    日期和时间格式化实用程序和常量public static String format(Calendar calendar, String pattern) 说明:将日历格式化为特定的模式:参数:cal ...

  8. epoll_wait 时 POLLERR 与 POLLIN 同时返回的现象解析(转)

    今天code review时,同事B对我代码中的poll()的处理做法提出了异议.于是做了些研究,还发现了一些好玩的故事. 异议的代码 我的代码是参考manpage写的,类似下面的做法.同事B说没有处 ...

  9. ReactiveX 学习笔记(13)基础类型

    Key Types and Life Management 本文主题是 Rx 中的基础类型 Subject 类及其生命周期的管理. 公共代码 RxNET private static void Wri ...

  10. HashMap 实现总结

    Entry类中需包含键值的hash值,防止resize时的重复计算: Map容量为2的整幂,可使用位操作取代取余操作提高效率: resize时需要将原table的桶内数据置null,利于垃圾回收: h ...