我有一个带有许多对象的NSMutableArray.我可以直接从索引范围'i'到'j'提取子数组吗?是的,我知道我可以做一个循环并使用objectAtIndex:x并将每个对象提取到一个新的.
我需要调用一个输入是一个数组的方法,并且只想将这个数组用于验证数据.
给定两个数组作为参数(x和y)并找到x中第一次出现y的起始索引.我想知道最简单或最快的实现是什么.
例:
when x = {1,2,4,2,3,4,5,6}
y = {2,3}
result
starting index should be 3
Run Code Online (Sandbox Code Playgroud)
更新:由于我的代码错误,我将其从问题中删除.
好的,考虑一下:
我有一个包含一个大阵arrays,-1,a和b.
-1字段为空的意思是:
var board = [
[-1,-1, a],
[-1,-1, b],
[ b,-1, a]
]
Run Code Online (Sandbox Code Playgroud)
现在我想再次检查更小的阵列:
var solutions = [
[
[1, 1, 1]
],
[
[1],
[1],
[1]
],
[
[1],
[0,1],
[0,0,1]
],
[
[0,0,1],
[0,1],
[1]
]
]
Run Code Online (Sandbox Code Playgroud)
查看一个现有值是否board与模式匹配solutions.
不a与任何模式匹配的?
是否b匹配任何模式?
你们中的任何人都可以看到比制作一个疯狂的嵌套循环更好的方法:
var q,w,e,r,t,y;
q=w=e=r=t=y=0;
for( ; q < 3; q++ ) {
for( ; w < 3; w++ ) …Run Code Online (Sandbox Code Playgroud) 考虑一个数组 A = [5,1,7,2,3]
所有连续子数组 = { [5], [1], [7], [2], [3], [5,1], [1,7], [7,2], [2,3], [ 5,1,7], [1,7,2], [7,2,3], [5,1,7,2], [1,7,2,3], [5,1,7, 2,3]}
将上面集合中的所有数组替换为其中最大元素:
设置将如下所示: { [5], [1], [7], [2], [3], [5], [7], [7], [3], [7], [7] , [7], [7], [7], [7] }
频率信息:[5] -> 2、[1] -> 1、[7] -> 9、[2] -> 1、[3] -> 2
我的目标是找到上述频率信息。
我的方法:
首先列出 (x,y) 对。x是A中的元素,它的索引是y。
列表:[(5,1)、(1,2)、(7,3)、(2,4)、(3,5)]
相对于第一个元素按降序对列表进行排序。现在,
列表:[(7,3)、(5,1)、(3,5)、(2,4)、(1,2)]
算法:
def f( array, first_index, last_index):
->select an element from LIST starting from left which
is not …Run Code Online (Sandbox Code Playgroud) 文件input.txt由两行组成:首先具有整数N个空格,然后具有整数K(1 ?? N,K ?? 250000)。第二个具有N个以空格为单位的整数,其中每个整数都小于或等于K。可以保证数组中从1到K的每个整数。任务是找到最小长度的子数组,其中包含所有整数。并打印其开始和结束。请注意,索引从1开始。
例子:
Input Output
5 3 2 4
1 2 1 3 2
6 4 2 6
2 4 2 3 3 1
Run Code Online (Sandbox Code Playgroud)
在最近的编程竞赛中,我承担了这项任务。结束了,我没有作弊。我已经使用python 3实现了它:
with open('input.txt') as file:
N, K = [int(x) for x in file.readline().split()]
alley = [int(x) for x in file.readline().split()]
trees = {}
min_idx = (1, N)
min_length = N
for i in range(N):
trees[alley[i]] = i
if len(trees) == K:
idx = (min(trees.values())+1, max(trees.values())+1)
length = idx[1] - idx[0] + 1 …Run Code Online (Sandbox Code Playgroud) 我有一个数组可以说[1, 2, 3, 4]。我必须检查一个元素或元素的任意组合的总和是否等于特定数字。
例子
5,1 + 4 = 5和2 + 3 = 5。6,1 + 2 + 3 = 6和2 + 4 = 6一种方法可能是创建数组的幂集,如本答案所示,然后循环迭代它。但这不是一个好主意,因为如果元素数量n增加,幂集将变得内存庞大。就此而言,更好的方法是创建特定长度的子集/子数组并逐个迭代它们。
可以说k是子数组的长度
k = 2应该给我[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]k = 3应该给我[[1, 2, 3], [1, 2, 4], [2, 3, 4]]现在的问题是,我将如何创建如上所述的特定长度的子数组/子集?
我正在尝试修改 Kadane 算法以解决更具体的问题。
def max_Sum(arr, length, k):
if length < k:
print('length of array should be greater than k')
res = 0
for i in range(0, k):
res += arr[i]
current_sum = res
for i in range(k, n):
if k == n:
for j in range(0, k-1):
res += arr[j]
current_sum = res
else:
current_sum += arr[i] - arr[i-k]
print(res, current_sum)
res = max(res, current_sum)
return res
Run Code Online (Sandbox Code Playgroud)
这是最大子数组问题的代码。我想要做的是找到长度最多为 K 的最大子数组。
示例:我们有一个数组 A = [3,-5 1 2,-1 4,-3 1,-2],我们想要找到长度最多为 K = …
给定一个数字数组,查找是否有办法从数组中删除/移除一个数字并在数组中进行一个分区(将数组划分为两个子数组),使得 subarray1 中的元素总和等于 subarray2 中元素的总和.
A subarray is a contiguous part of array.
Array [1, 2, 3, 4] has (1), (1,2), (2,3,4),(1,2,3,4) etc.. as its subarrays but not (1,3) , (2,4) , (1,3,4), etc..
Run Code Online (Sandbox Code Playgroud)
现在让我们考虑一个例子:-
(Follow 0-based indexing )
Array[] = [ 6, 2, 2, 1, 3 ]
Possible solutions
Delete Array[0] => updated array: - [ 2,2,1,3 ]
Possible partition : - [2,2] and [3,1] where (2+2) = (3+1) = 4
or
Delete Array[1] => updated array: - [ …Run Code Online (Sandbox Code Playgroud) 我有一个SomeClass带有静态成员的类,该成员myMap enter code here具有HasmMap<String,ArrayList<SomeOtherClass>>从文件中反序列化的形式.
我有一个方法
public ArrayList<SomeOtherClass> getList(final String key, final int N)
Run Code Online (Sandbox Code Playgroud)
应该key在地图中查找并返回相应的第一个N元素,ArrayList如果列表中有<= N元素则返回整个元素.我该如何实现TODO以下这一行:
public ArrayList<SomeOtherClass> getList(final String key, final int N)
{
ArrayList<SomeOtherClass> arr = myMap.get(key);
if (arr == null) return null;
if (arr.size() <= N)
{
return arr;
}
else
{
// TODO: return first N elements
}
}
Run Code Online (Sandbox Code Playgroud)
有效地做到这一点,即在实际返回正确数据的同时不在内存中创建不需要的副本?
在julia中,我们可以检查数组是否包含值,如下所示:
> 6 in [4,6,5]
true
Run Code Online (Sandbox Code Playgroud)
但是,当尝试按特定顺序检查子数组时,这将返回false:
> [4,6] in [4,6,5]
false
Run Code Online (Sandbox Code Playgroud)
验证数组中是否存在特定子数组的正确语法是什么?
sub-array ×10
arrays ×7
algorithm ×4
arraylist ×1
c# ×1
ios ×1
java ×1
javascript ×1
julia ×1
objective-c ×1
python ×1
subsequence ×1
swift ×1