每日一练 — 2021.12.21


一、跳跃游戏 ||

1,程序简介

  • 给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。
  • 数组中的每个元素代表你在该位置可以跳跃的最大长度。
  • 判断你是否能够到达最后一个下标。
示例 1:
  • 输入:nums = [2,3,1,1,4]
  • 输出:true
  • 解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
示例 2:
  • 输入:nums = [3,2,1,0,4]
  • 输出:false
  • 解释:无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。

提示:

1 <= nums.length <= 3 * 104
0 <= nums[i] <= 105

以下程序实现了这一功能,请你填补空白处内容:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static inline int max(int a, int b)
{
	return a > b ? a : b;
}
static bool canJump(int *nums, int numsSize)
{
	int i, pos = 0;
	for (i = 0; i < numsSize - 1; i++)
	{
		___________________
		pos = max(i + nums[i], pos);
	}
	return pos >= numsSize - 1;
}
int main(int argc, char **argv)
{
	int i, count = argc - 1;
	int *nums = malloc(count * sizeof(int));
	for (i = 0; i < count; i++)
	{
		nums[i] = atoi(argv[i + 1]);
	}
	printf("%s\n", canJump(nums, count) ? "true" : "false");
	return 0;
}

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Tue Dec 21 09:05:28 2021
Function:
@author: 小梁aixj
"""
class Solution:
    def canjump(self, nums):
        length = len(nums)
        begin = length - 1
        for i in reversed(range(length - 1)):
            if i + nums[i] >= begin:
                begin = i
        return not begin
#%%
s = Solution()
print(s.canjump(nums = [2,3,1,1,4])) #true

3,运行结果

在这里插入图片描述

二、串联所有单词的子串

1,程序简介

  • 给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
  • 注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。
示例 1:
  • 输入: s = “barfoothefoobarman”, words = [“foo”,“bar”]
  • 输出:[0,9]
  • 解释:从索引 0 和 9 开始的子串分别是 “barfoo” 和 “foobar” 。输出的顺序不重要, [9,0] 也是有效答案。
示例 2:
  • 输入: s = “wordgoodgoodgoodbestword”, words = [“word”,“good”,“best”,“word”]
  • 输出:[]

以下程序实现了这一功能,请你填补空白处内容:

class Solution(object):
	def findSubstring(self, s, words):
		"""
		:type s: str
		:type words: List[str]
		:rtype: List[int]
		"""
		ls = len(s)
		word_ls = len(words[0])
		target_dict = {}
		for word in words:
			try:
				target_dict[word] += 1
			except KeyError:
				target_dict[word] = 1
		res = []
		___________________________________
			curr_dict = target_dict.copy()
			for pos in range(start, start + word_ls * len(words), word_ls):
				curr = s[pos:pos + word_ls]
				try:
					curr_dict[curr] -= 1
					if curr_dict[curr] < 0:
						break
				except KeyError:
					break
			else:
				res.append(start)
		return res
if __name__ == '__main__':
	s = Solution()
	print(s.findSubstring('wordgoodgoodgoodbestword', ["word", "good", "best", "good"]))

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Tue Dec 21 09:06:04 2021
Function:
@author: 小梁aixj
"""
class Solution(object):
    def findSubstring(self, s, words):
        ls = len(s)
        word_ls = len(words[0])
        target_dict = {}
        for word in words:
            try:
                target_dict[word] += 1
            except KeyError:
                target_dict[word] = 1
        res = []
        for start in range(ls - word_ls * len(words)):
            curr_dict = target_dict.copy()
            for pos in range(start, start + word_ls * len(words), word_ls ):
                curr = s[pos:pos + word_ls]
                try:
                    curr_dict[curr] -= 1
                    if curr_dict[curr] < 0 :
                        break
                except KeyError:
                    break
            else:
                res.append(start)
        return res
if __name__ == '__main__':
    s = Solution()
    print(s.findSubstring('wordgoodgoodgoodbestword', ['word','good','best','good']))# []
    print(s.findSubstring('barfoothefoobarman', ['foo', 'bar']))# [0, 9]
            

3,运行结果

在这里插入图片描述

三、Python输出不重复的字符

1,程序简介

  • 输入一个字符串,把最左边的10个不重复的字符(大小写算不同字符)挑选出来。 如不重复的字符不到10个,则按实际数目输出。

输入格式:

  • 输入一个字符串s。

输出格式:

  • 输出一个字符串,包含字符串s最左边10个不重复的字符。不到10个按实际输出。

输入样例1:

Hello world, hello python
输出样例1:
Helo wrd,h
输入样例2:
succeed
输出样例2:
suced

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Tue Dec 21 09:06:25 2021
Function:
@author: 小梁aixj
"""
def unique(s):
    set1=set([])
    out=''
    for i in range(len(s)):
        c=s[i]
        if c not in set1:
            out=out + c
            set1.add(c)
    return out[0:10]
def main():
    strin='Hello world, hello python'
    ret=unique(strin)
    print(ret)
if __name__=='__main__':
    main()

3,运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁辰兴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值