Python 刷Leetcode题库,顺带学英语单词(7)
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]
>>>
相关文章
- 二级Python选择题_二级python选择题题库
- Python获取时间戳_python爬虫时间戳
- python中pygame怎么安_Python中pygame安装方法图文详解
- Python入门系列(五)一篇搞懂python语句
- 【Python】python文件打开方式详解——a、a+、r+、w+、rb、rt区别[通俗易懂]
- python常见运维脚本_Python运维常用脚本[通俗易懂]
- LeetCode 700题 题解答案集合 Python
- python全局变量赋值_Python全局变量和局部变量[通俗易懂]
- Python udp编程_python socket udp
- python chmod_Python os.chmod用法及代码示例
- linux收发邮件_python邮件发送
- python归一化函数_机器学习-归一化方法
- 【测试开发】python系列教程:Python 推导式
- Python爬虫爬取百合网的男性h
- 网络工程师学Python-12-类和对象
- python-Python与PostgreSQL数据库-PostgreSQL数据库的基本知识(二)
- Python函数可变参数详解
- 如何使用Python安装MySQL(python安装mysql)
- Linux下运行Python的简单方法(linux运行.py)
- python实现ip查询示例
- python调用新浪微博API项目实践