zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python 刷Leetcode题库,顺带学英语单词(7)

PythonLeetCode 题库 英语单词 顺带
2023-09-14 09:01:29 时间

Remove Element

Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array inplace with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.  [#27]

Example1:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn't matter what you leave beyond the returned length.

Example2:
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
Note that the order of those five elements can be arbitrary.
It doesn't matter what values are set beyond the returned length.

Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

题意:在给定列表上删除指定的值,不能定义新的列表、分配额外空间。

>>> def RemoveElement(val):
	global nums
	while nums.count(val)>0:
		del nums[nums.index(val)]
	return len(nums)

>>> nums = [3,2,2,3]
>>> val = 3
>>> RemoveElement(val)
2
>>> nums
[2, 2]
>>>
>>> nums = [0,1,2,2,3,0,4,2]
>>> val = 2
>>> RemoveElement(val)
5
>>> nums
[0, 1, 3, 0, 4]
>>> 


Implement strStr()

Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.  [#28]

Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1

Clarification:
What should we return when needle is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().

题意:返回子串在字符串中第一次出现的索引号,找不到返回-1

>>> haystack = "hello"; needle = "ll"
>>> func(haystack, needle)
2
>>> haystack = "aaaaa"; needle = "bba"
>>> func(haystack, needle)
-1
>>> 

Substring with Concatenation of All Words

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.  [#30]

Example 1:
Input:
s = "barfoothefoobarman",
words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.

Example 2:
Input:
s = "wordgoodgoodgoodbestword",
words = ["word","good","best","word"]
Output: []

题意:words列表中的单词任意组合在字符串的索引号

>>> from itertools import permutations as perm
>>> findwords = lambda s,words:[s.find(j) for j in {''.join(i) for i in perm(words)} if s.find(j)!=-1]
>>>
>>> s = "barfoothefoobarman";words = ["foo","bar"]
>>> findwords(s,words)
[9, 0]
>>> s = "wordgoodgoodgoodbestword"
>>>
>>> words = ["word","good","best","word"]
>>> findwords(s,words)
[]
>>> s = "wordgoodwordbestgoodgoodbestwordword"
>>> findwords(s,words)
[20, 0]
>>>