2D版本中的经典算法问题通常被描述为
给定n个非负整数表示每个柱的宽度为1的高程图,计算下雨后能够捕获的水量.
例如,给定输入
[0,1,0,2,1,0,1,3,2,1,2,1]
Run Code Online (Sandbox Code Playgroud)
返回值是
6
Run Code Online (Sandbox Code Playgroud)

我用来解决上述2D问题的算法是
int trapWaterVolume2D(vector<int> A) {
int n = A.size();
vector<int> leftmost(n, 0), rightmost(n, 0);
//left exclusive scan, O(n), the highest bar to the left each point
int leftMaxSoFar = 0;
for (int i = 0; i < n; i++){
leftmost[i] = leftMaxSoFar;
if (A[i] > leftMaxSoFar) leftMaxSoFar = A[i];
}
//right exclusive scan, O(n), the highest bar to the right each point
int rightMaxSoFar = 0;
for (int i = n - 1; i …Run Code Online (Sandbox Code Playgroud) 我在Mac OS X上遇到链接目标文件的问题.追溯问题是,这是我的C hello world程序
#include <stdio.h>
int main(){
printf("Hello, world!\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
//使用gcc编译(在mac上使用clang LLVM编译器)
$ gcc -c hello.c
Run Code Online (Sandbox Code Playgroud)
输出文件是与gcc的hello.o链接并运行可执行文件
$ gcc hello.o -o hello
$ ./hello
Run Code Online (Sandbox Code Playgroud)
现在,我必须使用mac链接器程序ld或Ld链接目标文件而不是gcc.在这种情况下,为了让程序运行,我应该将什么参数传递给ld程序?一个简单的传递目标文件名,即
$ ld hello.o
导致
ld: warning: -macosx_version_min not specified, assuming 10.6
Undefined symbols for architecture x86_64:
"_printf", referenced from:
_main in hello.o
"start", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for inferred architecture x86_64
Run Code Online (Sandbox Code Playgroud)
那么我需要包含哪些其他文件来链接或需要指定的架构信息?谢谢.
我想制作一些脚本,例如来自 android 手机的网络流量。是否可以通过从 adb shell 发出 HTTP 请求来完成任务?我注意到有“adb shell ping”命令可以测试网络连接,但它不会生成 HTTP 请求。我在想一些类似于 telnet 的实用程序。
编辑:我看到打开一个浏览器将满足我的目的,虽然不是很优雅需要命令行使用 adb 启动网络浏览器
我想定义一个界面,比如
public interface Visitor <ArgType, ResultType, SelfDefinedException> {
public ResultType visitProgram(Program prog, ArgType arg) throws SelfDefinedException;
//...
}
Run Code Online (Sandbox Code Playgroud)
在实现过程中,selfDefinedException会有所不同.(selfDefinedException为现在的通用undefined)有没有办法做到这一点?
谢谢
我试图在Linux x86环境(ubuntu)上运行Eclipse上的OpenGL代码:http: //nehe.gamedev.net/tutorial/vertex_buffer_objects/22002/ 我在命令行中输入后代码将正常运行
g ++ main.cpp error.cpp lesson45.cpp -o lesson45 -L/usr/X11R6/lib/-lGL -lGLU
sdl-config --cflags --libs
哪个是make文件中的指令.现在我试图让代码在Eclipse上运行,我知道我必须设置链接器库GL和GLU以及链接器库目录/ usr/X11R6/lib /.但是,就sdl-config --cflags --libs我而言,我不确定如何在Eclipse中配置它.
我正在定义一个小函数来创建一个整数向量的直方图.最初,我定义了以下函数,该函数首先在分配或递增值之前测试密钥是否存在于映射中.
map<int, int> histogram(vector<int> &a){
map<int, int> hist;
for (auto &x : a){
hist[x] = hist.count(x) == 0 ? 1 : hist[x] + 1; // check key existence
}
return hist;
}
Run Code Online (Sandbox Code Playgroud)
后来,我发现以下代码也可以在不检查密钥是否存在的情况下工作.因此,不存在的键的默认值应该是ZERO.我想知道这种行为在引用不存在的密钥时保证有一个默认的零值吗?
map<int, int> histogram(vector<int> &a){
map<int, int> hist;
for (auto &x : a){
hist[x]++; // without key existence checking.
}
return hist;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个字符串向量的副本,并将其附加到其原始向量的末尾,即复制其内容.例:
Input : vector<string> s = {"abc", "def"}
Output: vector<string> s = {"abc", "def", "abc", "def"}
Run Code Online (Sandbox Code Playgroud)
我正在使用插入方法,即
s.insert(s.end(), s.begin(), s.end());
Run Code Online (Sandbox Code Playgroud)
但是,这表现出与编译器相关的结果.在,LLVM clang,它给了我预期的答案.
GCC给了我
Output: vector<string> s = {"abc", "def", "", ""}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么会发生这种情况以及实现此向量复制目标最安全的方法是什么?
以下是该程序的ideone.com链接:http://ideone.com/40CH8q
例如,div > p可用于选择<p>元素的直接子元素的所有元素<div>。div > p > span用于选择<span>具有<p>父级和<div>祖父级的元素是否有效?
CSS 选择器中的其他运算符呢?使用'+' indiv + p + span或',' in是否有效div, p, span?
如何组合不同的选择器?喜欢div + p > span?