我开始以势在必行的方式解决这个问题并且它起作用(DFS使用传统的三种着色技术).但是,我需要三倍的时间来弄清楚如何做Haskell而我失败了!假设我将图表表示为具有其邻接节点的节点的列表(或映射).
type Node = Int
type Graph = [(Node, [Node])]
Run Code Online (Sandbox Code Playgroud)
注意,上述表示可以是指向的或不指向的.在进行探测以检测后沿边缘时,我还将看到的集合和完成集合作为参数传递(因为在功能上没有副作用).但是,我不能在Haskell中做到这一点!我知道可能会使用State monad,但那件事并没有在我的脑海中完成.我很想知道怎么能有人指导我如何以"美丽的"Haskell风格做到这一点?
我有以下C++代码,它给了我一个惊喜.问题是如果我抛出一些东西,除了在catch块内重新抛出,程序将通过调用abort终止并在GCC4中给出错误消息,"在抛出'int'实例后调用终止".如果我只是用"扔"; 重新扔进catch区,一切都会好的.
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
int main()
{
try{
throw std::string("first throw");
}
catch(std::string &x){
try{
std::cout << x << std::endl;
// throw; // if I use this line, all is fine.
throw int(2); // but if I use this line, it causes Abort() to be called
}
catch (int &k){
throw;
}
catch(...)
{
cout << "all handled here!"<< endl;
}
}
catch(...){
std::cout<< "never printed" << endl;
}
}
Run Code Online (Sandbox Code Playgroud) 今天我遇到了一种情况,我需要决定一个由大约40个元素组成的整个结构是否为零 - 这意味着每个元素都是零.
在思考如何尽可能快速有效地思考时,我想到了3种不同的方法:
memcmp它与结构一致.例如
typedef union {
struct {
uint8_t a;
uint8_t b;
}
uint16_t c;
} STRUCTURE_A;
Run Code Online (Sandbox Code Playgroud)
然后将其与零进行比较.
我想知道您对这些解决方案的看法,您发现哪些解决方案最快,效率最高.
如果你有更好的方法,请告诉我......
谢谢.
对不起大家!我的错!谢谢你的提醒,我发现f(0,k)== f(k,0)== 1.这个问题是关于如何计算从网格(0,0)到(m,n)的最短路径数).
我现在必须解决以下等式,确切地找出f(m,n)等于什么.
1) f(m,n) = 0 : when (m,n) = (0,0)
**2) f(m,n) = 1 : when f(0,k) or f(k,0)**
3) f(m,n) = f(m-1,n) + f(m,n-1) : when else
Run Code Online (Sandbox Code Playgroud)
例如:
1) f(0,0) = 0;
2) f(0,1) = 1; f(2,0) = 1;
3) f(2,1) = f(1,1) + f(2,0) = f(0, 1) + f(1, 0) + f(2, 0) = 1 + 1 + 1 = 3
Run Code Online (Sandbox Code Playgroud)
我记得有一种标准的方法可以解决这种二进制递归方程,正如我几年前在算法类中学到的那样,但我现在还记不起来了.
任何人都可以提供任何暗示吗?或关键字如何找到答案?
如何检测到达顶部或底部的UIWebView?因为当我到达底部时我需要触发一个动作.
那可行吗?
我觉得这个有点傻,但除了必须调用date()3次之外,是否有更优雅的方式来格式化日期数字后缀(st,th)?
我想用html输出的内容:
<p>January, 1<sup>st</sup>, 2011</p>
Run Code Online (Sandbox Code Playgroud)
我现在在做什么(感觉非常沉重)在php中:
//I am omitting the <p> tags:
echo date('M j',$timestamp)
. '<sup>' . date('S', $timestamp) . '</sup>'
. date(' Y', $timestamp);
Run Code Online (Sandbox Code Playgroud)
谁知道更好的方法?
我想知道什么是最好的:数组或二进制搜索树(插入,删除,查找最大和最小)以及如何改进它们?
我偶然发现了使用按位运算符的JavaScript情况.逻辑上,按位运算符应该具有比等于运算符更高的优先级,例如
if val & 10 == 10
alert('flag set')
Run Code Online (Sandbox Code Playgroud)
但看起来这个代码会以另一种方式工作,因为在JavaScript中,按位运算符的优先级低于等式运算符(参见Mozilla的JS参考).上面的代码总是返回0任何有效的数值val,因为结果val & true是0.因此,正确的方法是将括号放在按位表达式周围:
if (val & 10) == 10
alert('flag set')
Run Code Online (Sandbox Code Playgroud)
我挖出了这个问题的历史,看起来这种行为来自于K&R的C时代,其中逻辑&&和||运算符是按比例添加的.就C中的逻辑陈述而言:
if (x == 1 & y == 0) {
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
有一个完美的感觉.但它在按位逻辑方面没有任何意义.
C++,Java,Objective-C,PHP,C#,最后Javascript也有同样的方式.Python,Ruby,Go反过来说.
您是否知道任何原因(除了来自C的遗产之外)使编程语言的设计者遵循C的优先规则?
我开始学习POSIX计时器,所以我开始做一些练习,但我立即遇到了编译器的一些问题.在编译这段代码时,我收到一些关于像CLOCK_MONOTONIC这样的宏的奇怪消息.这些在各种库中定义,如time.h等,但编译器给出了错误,好像它们没有定义.
这很奇怪,因为我使用的是Fedora 16,而且我的一些Ubuntu朋友得到的编译错误比我少:-O
我正在编译 gcc -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 -lrt
我得到的错误:
struct sigevent sigeventStruct 给出:
storage size of ‘sigeventStruct’ isn’t known
unused variable ‘sigeventStruct’ [-Wunused-variable]
Type 'sigevent' could not be resolved
unknown type name ‘sigevent’
Run Code Online (Sandbox Code Playgroud)sigeventStruct.sigev_notify = SIGEV_SIGNAL 给出:
‘SIGEV_SIGNAL’ undeclared (first use in this function)
request for member ‘sigev_notify’ in something not a structure or union
Field 'sigev_notify' could not be resolved
Run Code Online (Sandbox Code Playgroud)if(timer_create(CLOCK_MONOTONIC, sigeventStruct, numero1) == -1) 给出:
implicit declaration of function ‘timer_create’ [-Wimplicit-function- …Run Code Online (Sandbox Code Playgroud)最近我遇到了这个采访问题:
在数组中给出n个实数.如果数组中的数字超过n/10次,则数组中的数字称为十进制显性.给出O(n)时间算法以确定给定数组是否具有十进制显性.
现在我可以想出几种方法来解决这个问题,以及这个问题的任何概括(即找到在数组中出现K次的任何数字)
一种可能是在第1遍中创建哈希表,然后计算第2遍中的出现次数,即O(n),但也使用O(n)空间,
有一种方法可以使用9个桶
但是,我们有什么方法可以在一个恒定的空间里做到这一点?有什么建议??
[编辑]我还没有检查过9桶解决方案,读者可能想通过下面的nm评论