easy 最长公共前缀

在这里插入图片描述

c++ 双循环:

在这里插入图片描述


class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {   // 传入字符串容器strs
        if(strs.size()==0){  // 传入空向量表
            return "";  //返回空字符 
        }else if(strs.size()==1){   // 向量表只用一个字符
            return strs[0];    //返回第一个字符
        }else{
            for(int i = 0;i<strs[0].size();i++){  // 遍历向量表第一个单词的每个字母
                for(int j=1;j<strs.size();j++){  // 遍历向量表单词,从第二个单词开始
                    if( i==strs[j].size() || strs[0][i] != strs[j][i]){
                        return strs[0].substr(0, i);   // string切片[0,2)
                    }
                }
            }

        }
        return  strs[0];  // 遍历完第一个单词的每个字母后,都是公共前缀
    }
};


c++ 字典序最大和最小字符串的公共前缀:


class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if(strs.empty()){  // 传入向量表为空,返回空字符串
            return "";
        }
        // c++函数 minmax_element(strs.begin(),strs.end()); 寻找字典序最大和最小字符串
        // 返回最大和最小字符串列表[min_element, max_element] 指针?
        const auto[str0,str1] = minmax_element(strs.begin(),strs.end());  
        for(int i =0;i<str0->size();i++){
            if(str0->at(i) != str1->at(i)){
                return str0->substr(0,i);
            }
        }
        return *str0;
    }
};

c++ minmax_element 函数:

功能:能找出最大值和最小值 并以std::pair的形式返回其迭代器(注意不是值,也不是tuple) 返回指向范围内,最小值的元素作为第一个元素,最大的元素作为第二个元素。 因此必须用first和second来访问指向最小最大值的迭代器,并用解引用操作符“*”来获取值。

vector<int>v ={633,90,67,83,2,100};
auto X一minmax_element(v.begin(),v.end());  // auto 根据初始值自动定义变量类型

cout<<"min<<*x.first << endl;
cout<<"max<<*x.second <<endl;


// minmax_element  函数
#include <iostream>     // std::cout
#include <algorithm>    // std::minmax_element  引入函数
#include <array>        // std::array

int main () {
  std::array<int,7> foo {3,7,2,9,5,8,6};  // 字符串

  auto result = std::minmax_element (foo.begin(),foo.end());  //寻找范围:全部

  // print result:
  std::cout << "min is " << *result.first;
  std::cout << ", at position " << (result.first-foo.begin()) << '\n';
  std::cout << "max is " << *result.second;
  std::cout << ", at position " << (result.second-foo.begin()) << '\n';

  return 0;
}

Output:
min is 2, at position 2  // 找出字符串中最小数
max is 9, at position 3  // 找出字符串中最大数

字典序:

max(str)、min(str) 方法不只判断字母,会判断字符串中的所有字符,按照字符在 unicode 中的编码值来决定大小:
eg: python


#!/usr/bin/python
# max(str)

str = "this is really a string example....wow!!!";
print "Max character: " + max(str);  # Max character: y

str = "this is a string example....wow!!!";
print "Max character: " + max(str);  # Max character: x

str1 = '12345'
a = max(str1)  # 5
b = min(str1)  # 1


#!/usr/bin/perl
# maxstr
use List::Util qw/max min sum maxstr minstr shuffle/;
 
@s = ('hello', 'ok', 'china', 'unix');  # 第一位字母比完,比不出,继续比较下一位字母

print maxstr @s;     #unix   u开头,u在字母表最后
print minstr @s;     #china  c开头,c在字母表最前


python 字典序最大和最小字符串的公共前缀:


class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:  # 传入strs,strs为list,里面装str
        if not strs:
            return ""

        str0 = min(strs)
        str1 = max(strs)

        for i in range(len(str0)):
            if str0[i] != str1[i]:
                return str0[:i]  # 返回[0:i)

        return str0
        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值