• python入门练习题(含C++解法)


    剑指 Offer 58 - I. 翻转单词顺序

    剑指 Offer 58 - I. 翻转单词顺序 - 力扣(LeetCode)

    题目大意

    输入: "the sky is blue"
    输出: "blue is sky the"
    
    输入: "  hello world!  "
    输出: "world! hello"
    解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
    
    输入: "a good   example"
    输出: "example good a"
    解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    C++

    双指针,一个指针从字符串的末尾开始往前找,找到第一个空格,且空格后面不是空格,说明空格的后面就是一个单词,然后把这个单词放到提前开好的string里面.

    如"the sky is blue",从后往前找,先找到blue前面的那个空格,然后从’b’开始往后拷贝,把"blue"拷贝到一个新的字符串ans中,再往前找找到"is",把"is"再拷贝到字符串ans中,重复上面的步骤,最后返回ans.

    代码实现上有不少细节要注意,比如从后往前走的最后一个单词拷贝完不能加空格,比如从后往前走的最后一个单词前面没有空格无法进入if的条件里面,这些实现细节因人而异.

    class Solution {
    public:
    	string reverseWords(string s) {
    		if (s=="")
    		{
    			return s;
    		}
    		int len = s.size();
    		string res;
    		int cnt = 0;//拷贝的字符,相当于ans的size
    		res.resize(len);
    		for (int i = len - 1; i >= 0; i--)
    		{
    			if (i+1<len&&s[i] == ' '&&s[i+1]!=' ')//从后往前走最后一个单词前面没有空格就进不了这个if
    			{
    				int j = i + 1;
    				while (j >= 0 && j < len && s[j] != ' ')
    				{
    					res[cnt] = s[j];
    					cnt++;
    					j++;
    				}
    				res[cnt] = ' ';
    				cnt++;
    			}
    		}
    		if (s[0]!=' ')//处理循环里if的bug,这里典型的例子就是"the sky is blue",没有这段代码就不能把the拷贝下来
    		{
    			int j = 0;
    			while (j >= 0 && j < len && s[j] != ' ')
    			{
    				res[cnt] = s[j];
    				cnt++;
    				j++;
    			}
    		}
            if (cnt-1>=0&&cnt-1<len&&res[cnt-1]==' ')//"  hello world!  "为例,没有这里的判断可能会导致ans有效字符的最后一个是空格
    		{
    			res[cnt - 1] = '\0';
    		}
    		return res;
    	}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    Python

    额,python新手,利用接口即可.split里面不加参数就把字符串里的所有空格字符作为分割字符,分割出来的单词放入一个列表内返回,reverse方法反转列表,然后再用join方法把每个单词直接加上一个空格连接起来.

    具体接口直接查文档即可:内置类型 — Python 3.10.7 文档

    class Solution(object):
        def reverseWords(self, s):
            """
            :type s: str
            :rtype: str
            """
            tokens = s.split()
            tokens.reverse()
    	return  ' '.join(tokens)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    796. 旋转字符串 - 力扣

    796. 旋转字符串 - 力扣(LeetCode)

    题目大意

    看字符串s能不能旋转成goal

    输入: s = "abcde", goal = "cdeab"
    输出: true
    
    输入: s = "abcde", goal = "abced"
    输出: false
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    C++

    方法一

    在goal字符串里面建立s首字母的索引加快比对速度,建立好之后开始比对即可.

    
    class Solution {
    public:
    	bool rotateString(string s, string goal) {
    		if (s=="")
    		{
    			return goal == "";
    		}
    		int len_s = s.size();
    		int len_g = goal.size();
    		if (len_s ==len_g)
    		{
    			//建立s首字母的索引
    			char ch = s[0];
    			vector<int>v;
    			for (int i = 0; i <len_g;i++ )
    			{
    				if (goal[i]==ch)
    				{
    					v.push_back(i);
    				}
    			}
    			//开始挨个比对
    			bool f = 0;
    			for (int i=0;i<v.size();i++)
    			{
    				int beg_g = v[i];
    				int beg_s = 0;
    				while (s[beg_s]==goal[beg_g])
    				{
    					beg_s++; 
    					beg_g++;
    					beg_g %= len_g;
    					if (beg_s==len_s)
    					{
    						return true;
    					}
    				}
    			}
    			return false;
    		}
    		else
    		{
    			return false;
    		}
    
    	}
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    方法二

    确保字符串长度相等,再利用search方法查找子串.search的实现效率很高,比kmp还高,底层实现是BM算法,具体实现没了解过

    class Solution {
    public:
    	bool rotateString(string s, string goal) {
    		string s1 = s + s;
    		return s.size() == goal.size()
    			&& search(s1.begin(), s1.end(), goal.begin(), goal.end())!=s1.end();//search的效率比find高
    	}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Python

    检查长度相等,然后直接在s+s里面找子串看能不能找到goal

    class Solution(object):
        def rotateString(self, s, goal):
    	"""
    	:type s: str
    	:type goal: str
    	:rtype: bool
    	"""
    	if(len(s)!=len(goal)):
    		return False
    	return goal in (s+s)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Python3

    class Solution:
        def rotateString(self, s: str, goal: str) -> bool:
            return len(s)==len(goal) and goal in (s+s) 
    
    • 1
    • 2
    • 3

    2255. 统计是给定字符串前缀的字符串数目

    2255. 统计是给定字符串前缀的字符串数目 - 力扣(LeetCode)

    题目大意

    判断words数组里的字符串是否是s字符串的前缀,返回words是字符串前缀的字符串的数目.

    输入:words = ["a","b","c","ab","bc","abc"], s = "abc"
    输出:3
    解释:
    words 中是 s = "abc" 前缀的字符串为:
    "a""ab""abc" 。
    所以 words 中是字符串 s 前缀的字符串数目为 3 。
    
        
    输入:words = ["a","a"], s = "aa"
    输出:2
    解释:
    两个字符串都是 s 的前缀。
    注意,相同的字符串可能在 words 中出现多次,它们应该被计数多次。
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    C++

    遍历每一个单词,让每个单词的字符和s的每个字符从前往后比对来判断单词是否是s的前缀.

    class Solution {
    public:
    	int countPrefixes(vector& words, string s) {
    		int sz = s.size();
    		int cnt = 0;
    		for (auto& e:words)
    		{
    			string& tmp = e;
    			int len = tmp.size();
    			if (len>sz)
    			{
    				continue;
    			}
    			else
    			{
    				bool f = 0;
    				for (int i=0;i
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    Python

    和c++一样的思路,暴力遍历然后比对

    class Solution(object):
        def countPrefixes(self, words, s):
            """
            :type words: List[str]
            :type s: str
            :rtype: int
            """
            cnt=0;
            for word in words:
                if s.startswith(word):
                    cnt+=1;
    
            return cnt; 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    【Linux集群教程】10 集群监控 - Nagios 搭建
    RabbitMQ-死信交换机和死信队列
    No system certificates available. Try installing ca-certificates.
    ipsec主模式加密身份信息有什么意义?
    领先科技2024年3月5-7日第12届国际生物发酵展-宁泰橡塑
    Prolog 中的逻辑探险:学习9组逻辑蕴涵公式
    安装Aptos CLI 并 部署move智能合约
    Android 13 Wi-Fi状态机流程及Log分析
    算法导论实战(六)(算法导论习题三十四、三十五章)
    Sealos CLI快速部署部署K8s集群
  • 原文地址:https://blog.csdn.net/m0_53005929/article/details/126935251