Fifth Week's ARST

ARST

A

Leetcode14–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: ["flower","flow","flight"]
Output: "fl"

Example 2:

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

Note:
All given inputs are in lowercase letters a-z.

C++实现

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if(strs.size()==0)    return "";  //如果字符串数组中没有元素,返回空字符串
        string Prefix=strs[0];
        for(int i=0;i<strs.size();i++)
        {
            if(Prefix.length()==0||strs[i].length()==0)    return "";
            
            int len;
            
            if(Prefix.length()<=strs[i].length())      
            //先比较两个字符串长度得到两个中的最小值len
            //然后就只需要比较两字符串的前len位就行了
            {
                len=Prefix.length();
            }
            else   len=strs[i].length();
            
            int j;
            for(j=0;j<len;j++)
            {
                if(Prefix[j]!=strs[i][j])  break;
            }
            Prefix=Prefix.substr(0,j);        //将比较之后相同的前缀传给Prefix
        }
        return Prefix;
    }
};

S

Bosque–一种没有循环的新编程语言
Microsoft Research推出了一种名为Bosque的新开源编程语言,它通过采用代数运算和避免创建复杂性的技术,希望简单易懂。现在编程语言各式各样,现在又出来了一种新的开源编程语言,该语言没有循环,也许会对很多编程者来说会是一种福音,计算机编程语言发展太快了,我们甚至很难跟上一些语言发展更新换代,我们需要不断的学习,才能跟得上节奏,不然就会被淘汰。不知道有没有一天可以用中文来写代码。

R

通过这周的学习,对C++有了更多的认识,也学到了一些新的东西
C++中的字符函数substr的用法:

#include<string> 
#include<iostream> 
using namespace std;
int main() 
{
	string s("12345asdf"); 
    string a=s.substr(0,5);     //获得字符串s中前五个字符组成一个新的字符串
    cout<<a<<endl;
}  

C++的常用排序算法
排序算法很多,我们往往需要找到一些既简单又快捷的排序算法是我们的计算速度更快,时间花费更少,去网上找了一个大佬的博客,他总结了几种常见的排序算法,链接如下:
常见的排序算法

T

C++中有很多算法提供给程序员使用,我们了解了这些算法,会在自己写代码的时候节约很多时间
【C++ STL】常用算法总结

猜你喜欢

转载自blog.csdn.net/Slatter/article/details/89432625
今日推荐