我正在尝试更改haskell中的列表以在每个元素之间包括0。如果我们有初始清单,[1..20]那么我想将其更改为[1,0,2,0,3..20]
我想做的实际上是在每个函数上使用map,提取元素,然后将其添加到列表中并使用++[0]它,但不确定这是否正确。仍在学习haskell,因此可能会有错误。
我的代码:
x = map classify[1..20]
classify :: Int -> Int
addingFunction 0 [Int]
addingFunction :: Int -> [a] -> [a]
addingFunction x xs = [a] ++ x ++ xs
Run Code Online (Sandbox Code Playgroud) 尝试使用过滤器提取长度为k的子集。不确定如何处理?该列表包含100个元素。
subsets :: [a] -> [[a]]
subsets [] = [[]]
subsets (x:xs) = [zs | ys <- subsets xs, zs <- [ys, (x:ys)]]
Run Code Online (Sandbox Code Playgroud)
如果我使用过滤器,这就是我的想法:
filter (length(3)) subsets [1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
但是我可能错了。如果有其他方法而不是过滤器?我是Haskell的新手,所以不确定。
我在我的一次面试实践中遇到了这个问题,并且遇到了一个问题,它的时间复杂度比 O(N^2) 更好。在某种程度上,您必须访问列表中的每个元素。我考虑过使用哈希表,但它仍然必须执行哈希表并填充它然后进行计算。基本上我的解决方案是一个嵌套的 for 循环,我也包含了我的代码,它在 4 秒内通过了除时间异常之外的所有内容。
我的代码:
def concatenationsSum(a):
sum = 0
current_index_looking_at = 0
for i in a:
for x in a:
temp = str(i)+str(x)
sum += int(temp)
return sum
Run Code Online (Sandbox Code Playgroud)
问题描述:
Given an array of positive integers a, your task is to calculate the sum
of every possible a[i] ? a[j], where a[i] ? a[j] is the concatenation
of the string representations of a[i] and a[j] respectively.
Example
For a = [10, 2], the output should be concatenationsSum(a) …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个页面大小三分之一的容器,但出现错误No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of().,我完全不知道为什么。它在MaterialApp.
我的代码:
import 'package:flutter/material.dart';
void main() => runApp(LoginPage());
class LoginPage extends StatelessWidget{
@override
Widget build(BuildContext context){
return MaterialApp(
home: Scaffold(
body: Container(
constraints: BoxConstraints.expand(),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
color: Colors.red,
width: double.infinity,
height: MediaQuery.of(context).size.height/3,
)
],
)
)
)
);
}
}
Run Code Online (Sandbox Code Playgroud)