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

简介: 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]
>>> 












目录
相关文章
|
5月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
65 6
|
5月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
130 2
|
3月前
|
JSON 数据格式 Python
Python实用记录(十四):python统计某个单词在TXT/JSON文件中出现的次数
这篇文章介绍了一个Python脚本,用于统计TXT或JSON文件中特定单词的出现次数。它包含两个函数,分别处理文本和JSON文件,并通过命令行参数接收文件路径、目标单词和文件格式。文章还提供了代码逻辑的解释和示例用法。
56 0
Python实用记录(十四):python统计某个单词在TXT/JSON文件中出现的次数
|
3月前
Leetcode(最后一个单词长度)
这篇文章介绍了两种解决LeetCode第58题的方法,即计算给定字符串中最后一个单词的长度,方法包括翻转字符串和逆向遍历统计。
23 0
|
3月前
【LeetCode 20】151.反转字符串里的单词
【LeetCode 20】151.反转字符串里的单词
27 0
|
3月前
|
小程序 IDE 开发工具
Python编写单词复习小程序
Python编写单词复习小程序
24 0
|
5月前
|
算法
LeetCode第58题最后一个单词的长度
LeetCode第58题"最后一个单词的长度"的解题方法,通过从字符串末尾向前遍历并计数非空格字符,直接得出最后一个单词的长度。
LeetCode第58题最后一个单词的长度
|
5月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
78 7
|
5月前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
60 3
|
5月前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
28 3