由买买提看人间百态

topics

全部话题 - 话题: namespaces
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
d*******l
发帖数: 338
1
来自主题: JobHunting版 - 尘埃落定里面的矩形题
不是很麻烦吧。
#include
#include
#include
using namespace std;
#define MP make_pair
typedef pair PII;
typedef vector VI;
struct FindVertex
{
struct rect
{
int x1, y1, x2, y2;
rect(int a, int b, int c, int d) : x1(a), y1(b), x2(c), y2(d) {}
};
vector rects;
void add(int a, int b, int c, int d)
{
rect r(a, b, c, d);
rects.push_back(r);
}
void solve()
{
int i, j;
VI v;
... 阅读全帖
Y********f
发帖数: 410
2
来自主题: JobHunting版 - heap里面delete一个非root的节点
测试过的代码:
#include
#include
using namespace std;
void deleteNode(int heap[], int n, int pos)
{
// put the last elements to where the node removed
heap[pos] = heap[n-1];
// adjust elements above pos
int i = pos;
while(i != 0 && heap[i] < heap[(i - 1) / 2])
{
swap(heap[i], heap[(i - 1) / 2]);
i = (i - 1) / 2;
}
// adjust elements below pos
i = pos;
while(true)
{
if (2 * i + 1 > n -2)
{
//al... 阅读全帖
f*******t
发帖数: 7549
3
来自主题: JobHunting版 - large file的一道题
这个是很基本的数据结构,建议看一下wiki
我前几天实现了它,只有基本的insert和search,没有对查找child list进行优化,代
码贴出来供参考:
#include
#define MAX_LENGTH 64
using namespace std;
static char buff[MAX_LENGTH];
class Trie {
public:
Trie();
Trie(char value);
bool Insert(const char *str);
bool Search(const char *str) const;
void PrintAll(int pos) const;
private:
char _value;
Trie *_sibling;
Trie *_child;
};
Trie::Trie()
{
_value = 0;
_sibling = NULL;
_child = NULL;
}
Trie::Trie(char value)
... 阅读全帖
m******e
发帖数: 353
4
来自主题: JobHunting版 - 从水木上看到个数组题
ok, my bad, keep track of range [0, neg) and [numNeg, pos), and do not
rearrange those (since they are already in the correct place)
use [neg, numNeg) and [pos, N) as circular buffer for un-processed elements
#include
#include
#include
using namespace std;
void print(const vector& input) {
for(size_t i = 0; i < input.size(); ++i) {
cout << input[i] << " ";
}
cout << endl;
}
int numNegatives(const vector& input) {
int cnt = 0;
... 阅读全帖
m**q
发帖数: 189
5
来自主题: JobHunting版 - 几道老题 的解答
同样,当检查一个新的点时,需要把x-h之前的点从map中去掉,
其实可以用一个map存[x-h,x]中的点,key是y坐标。在向map中
添加一个新的点时,先检查map里是否有y坐标相同的点,有则删除;
同时把x-h之前的点依据它们的y坐标,把它们从map中删除。
总复杂度不超过O(nlgn)。
#include
#include
#include
#include
#include
using namespace std;
#define REVESE_PAIR(x) (make_pair(x.second, x.first))
#define PAIR(x) (make_pair(x.first, x.second))
double dist(pair a, pair b)
{
int x = abs(b.first - a.first);
int y = abs(b.second - a.second);
doubl... 阅读全帖
s****j
发帖数: 67
6
来自主题: JobHunting版 - 几道老题 的解答
平面最近点对那题,提供一种方法供参考,该方法看似复杂度o(n^2),但实际使用中几
乎是o(n)(不算nlgn排序的预处理)。主要是利用了三角形两边之差小于第三边的性质
,大大减枝。另外该方法实现简单,比传统o(nlgn)的实现简单很多也直观很多。
代码如下:
#include
#include
#include
#include
#include
using namespace std;
const int maxn=100005;
const double eps=1e-8;
struct Point
{
double x,y,dis;
} p[maxn];
int n;
bool cmpp(const Point &p1,const Point &p2)
{
return p1.dis }
void solve()
{
sort(p,p+n,cmpp);
double ans=1e18,now;
int i,j;
for (i=0;i阅读全帖
s*****y
发帖数: 897
7
写了个简单的,解你这个例子的,
#include
#include
#include
#include
#include
#include
using namespace std;
int matrix[4][4] = {{5,0,0,0},
{9,6,0,0},
{4,6,8,0},
{0,7,1,5}};
int getMax()
{
vector dp_table(4, 0);
for (int i = 0; i < 4; i++)
dp_table[i] = matrix[3][i];
for (int j = 2; j>= 0; j--) {
for (int i = 0; i <= j; i++)
dp_table[i] = max(matr... 阅读全帖
c**********e
发帖数: 2007
8
What is the output of the following code? And why?
#include
#include
using namespace std;
int main() {
vector v;
for(int i=0;i<100;i++) v.push_back(i);
cout << v.capacity() << endl;
return 0;
}
l*********y
发帖数: 142
9
来自主题: JobHunting版 - 请教四边形不等式
我在做 uva 10003, 题目如下,
You have to cut a wood stick into pieces. The most
affordable company, The Analog Cutting Machinery, Inc. (ACM),
charges money according to the length of the stick being
cut.
Their procedure of work requires that they only make one
cut at a time.
It is easy to notice that different selections in the
order of cutting can led to different prices. For exa... 阅读全帖
c**********e
发帖数: 2007
10
来自主题: JobHunting版 - amazon 2nd phone interview
Quick Selection:
选target个最小的。最终目的是,使a[0], …, a[target-1]为最小的。因为是递归调用子函数,子函数对数组的一部分a[low], …, a[high]操作。
#include
using namespace std;
#define swap(x,y) { int z=x; x=y; y=z; }
int low_n(int a[], int low, int high, int target) {
int pivot; int mid=(low+high)/2;
if(a[low]<=a[mid])
{
if(a[high]<=a[low]) pivot=a[low];
else if(a[high]<=a[mid]) pivot=a[high];
else pivot=a[mid];
} else
{
if(a[high]<=a[mid]) pivot=a[mid];
else if(a[high]<=a[low]) pivot=a[high];
... 阅读全帖
c**********e
发帖数: 2007
11
来自主题: JobHunting版 - 问个google面试题
code:
#include
using namespace std;
int main() {
for(long n=31623;n<100000;n++) {
long nsquare=n*n;
int i,j, a[10];
for(i=0;i<10;i++) {
a[i]=nsquare % 10;
nsquare = nsquare/10;
}
for(i=0;i<10;i++) {
for(j=i+1;j<10;j++) {
if(a[i]>a[j]) {
int k=a[i]; a[i]=a[j]; a[j]=k;
}
}
}
i=0;
while(a[i]==i) i++;
if(i==10) cout << n << " " << nsquare << endl;
}
return 0;
}
c**********e
发帖数: 2007
12
来自主题: JobHunting版 - 问个google面试题
This code gives the complete answer. The previous one does not give a
complete answer due to restriction of long int (about 2-billion, 2^31-1).
#include
using namespace std;
int main() {
for(long n=31623;n<100000;n++) {
//long nsquare=n*n;
double nsquare=n*0.1*n;
long nsq=nsquare;
int i,j, a[10];
a[0]=(nsquare-nsq)*10;
for(i=1;i<10;i++) {
a[i]=nsq % 10;
nsq = nsq/10;
}
for(i=0;i<10;i++) {
for(j=i+1;j<10;j++) {
if(a[i]>a[j]) {
... 阅读全帖
d*******l
发帖数: 338
13
来自主题: JobHunting版 - 回文数的问题
我觉得到不一定区分奇数偶数,大概模拟起来就是这样:
#include
#include
using namespace std;
void solve()
{
char c;
stack st;
while(cin >> c) {
if(!st.empty() && c == st.top())
st.pop();
else st.push(c);
if(st.empty())
return ;
}
}
int main()
{
solve();
return 0;
}
a***r
发帖数: 93
14
memory O(n)
time O(nlogn)
//~ Given a array,find out if there exist a subarray such its sum is zero
#include
#include
using namespace std;
static void swap(int *p, int i, int j) {
int t=p[i]; p[i]=p[j];p[j]=t;
}
static int partition(int *p,int left,int right) {
int j=left;
for(int i=left;i if(p[i] }
swap(p,j,right);
return j;
}
static void binary_sort(int *p, int left, int right) {
if(left>=r... 阅读全帖
l*********y
发帖数: 142
15
来自主题: JobHunting版 - 这个facebook puzzle样题怎么做?
顺手写了一个,46min整。也没用状态压缩,待会看一下gloomyturkey的code。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 9;
const int maxk = 6;
int N, K;
struct State {
State() {
for (in... 阅读全帖
l*********y
发帖数: 142
16
来自主题: JobHunting版 - Palantir面经2
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct Segment {
int from, to;
};
struct cmp {
bool operator() (const Segment& s1, const Segment& s2) {
return s1.from < s2.from;
}
};
bool ... 阅读全帖
c**********e
发帖数: 2007
17
来自主题: JobHunting版 - C++ Q96: function inheritance
Error information in the following code is Error: equals is not a member of
B.
My question: Why equals() is not a member of B but print() is?
While this seems obvious, what is the general rule that a function will be
inherited?
#include
using namespace std;
class A {
public:
bool equal(A a) { return true; }
void print() { cout << "print\n"; }
};
class B: public A {};
int main() {
B b;
bool x = b.equals(b);
b.print();
return 0;
}
c**********e
发帖数: 2007
18
来自主题: JobHunting版 - C++ Q96: function inheritance
Faint. I made such a low level mistake. Then what is the output of the
following code?
#include
using namespace std;
class A {
public:
bool equals(A a) { return data==a.data; }
void setData(int input) { data=input; }
private:
int data;
};
class B: public A {};
void main() {
A a;
a.setData(7);
B b;
b.setData(7);
cout << a.equals(b) << endl;
cout << b.equals(a) << endl;
}
a********d
发帖数: 195
19
来自主题: JobHunting版 - Amazon最近电面面经
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace testSplitStringToNArray
{
class Program
{
static void Main(string[] args)
{
const string originalString = "
Thisjdfkldjsflkdswieruewirfjkmkdfsdfdsfdsfdsfjkjowieorewjtoirekfdjngmvkmvlkn
ldsfjkljlkjlkjoijeroijelqrefdsakfjlkswxy";
//int arraySize =Convert.ToInt32(args[0]);
int arraySize = 5;//you name it
for... 阅读全帖
a********d
发帖数: 195
20
来自主题: JobHunting版 - Amazon最近电面面经
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace testSplitStringToNArray
{
class Program
{
static void Main(string[] args)
{
const string originalString = "
Thisjdfkldjsflkdswieruewirfjkmkdfsdfdsfdsfdsfjkjowieorewjtoirekfdjngmvkmvlkn
ldsfjkljlkjlkjoijeroijelqrefdsakfjlkswxy";
//int arraySize =Convert.ToInt32(args[0]);
int arraySize = 5;//you name it
for... 阅读全帖
c**********e
发帖数: 2007
21
来自主题: JobHunting版 - C++ Q97: reference and virtual
What is the output of the following code? Why?
#include
using namespace std;
class student {
public:
virtual void sleep() { cerr << "student sleep" << endl; }
};
class grad: public student {
public:
virtual void sleep() { cerr << "grad sleep" << endl; }
};
void exam(student& stu) {
stu.sleep();
}
void main() {
grad g;
exam(g);
}
c**********e
发帖数: 2007
22
What is the output of the following code? Why?
#include
using namespace std;
class base {
public:
void pay() { cout << "Base::pay" << endl; }
virtual void eat() { pay(); }
};
class derived: public base {
public:
void pay() { cout << "Derived::pay" << endl; }
};
void main() {
base* p = new derived;
p->eat();
}
e*****w
发帖数: 144
23
我也发个加法:
#include
using namespace std;
int Add(int a, int b) {
return (b ? Add(a ^ b, (a & b) << 1) : a);
}
int main() {
cout << "Add(3, 4) = " << Add(3, 4) << endl;
cout << "Add(3, -8) = " << Add(3, -8) << endl;
cout << "Add(-8, 40) = " << Add(-8, 40) << endl;
}
e*****w
发帖数: 144
24
加减乘,没用+-*/号。
#include
using namespace std;
int Add(int a, int b) {
return (b ? Add(a ^ b, (a & b) << 1) : a);
}
int Sub(int a, int b) {
return Add(a, Add(~b, 1));
}
int Mul(int a, int b) {
if (b == 0) return 0;
if (b < 0) return Mul(Sub(0, a), Sub(0, b));
return Add(a, Mul(a, Sub(b, 1)));
}
int main() {
cout << "Add(3, 4) = " << Add(3, 4) << endl;
cout << "Add(3, -8) = " << Add(3, -8) << endl;
cout << "Add(-8, 40) = " << Add(-8, 40) << endl;
cout << "Sub(3, 4) = " <<... 阅读全帖
e*****w
发帖数: 144
25
我也发个加法:
#include
using namespace std;
int Add(int a, int b) {
return (b ? Add(a ^ b, (a & b) << 1) : a);
}
int main() {
cout << "Add(3, 4) = " << Add(3, 4) << endl;
cout << "Add(3, -8) = " << Add(3, -8) << endl;
cout << "Add(-8, 40) = " << Add(-8, 40) << endl;
}
e*****w
发帖数: 144
26
加减乘,没用+-*/号。
#include
using namespace std;
int Add(int a, int b) {
return (b ? Add(a ^ b, (a & b) << 1) : a);
}
int Sub(int a, int b) {
return Add(a, Add(~b, 1));
}
int Mul(int a, int b) {
if (b == 0) return 0;
if (b < 0) return Mul(Sub(0, a), Sub(0, b));
return Add(a, Mul(a, Sub(b, 1)));
}
int main() {
cout << "Add(3, 4) = " << Add(3, 4) << endl;
cout << "Add(3, -8) = " << Add(3, -8) << endl;
cout << "Add(-8, 40) = " << Add(-8, 40) << endl;
cout << "Sub(3, 4) = " <<... 阅读全帖
q****x
发帖数: 7404
27
来自主题: JobHunting版 - Bloomberg Online Assessment 样题
第13题,operator= 重载后怎么没返回语句?编译也过了。
struct A {
int i;
public:
A(int x) { i = x; }
A(const A& a) { i = a.i; i ++; }
A& operator=(const A& a) { i = a.i; i --; }
};
#include
using namespace std;
int main()
{
A a(4);
A b = a;
return 0;
}
k***t
发帖数: 276
28
来自主题: JobHunting版 - Facebook电面题目
本人一直只写C,看过一点STL。欢迎大家 Code Review。谢谢。
觉得has_next_level flag 部分不够简单明了,但没有它不好
在输出中换行和终止加dummy node.
#include
using namespace std;
typedef struct TreeNode_ {
int value;
struct TreeNode_ *left;
struct TreeNode_ *right;
} TreeNode;
int level (TreeNode *root) {
queue Q;
TreeNode *cur, dummy;
bool has_next_level;

if (!root) return -1;
Q.push(root); Q.push(&dummy);
has_next_level = false;

while(!Q.empty()) {
cur = Q.front();
... 阅读全帖
f*******t
发帖数: 7549
29
来自主题: JobHunting版 - 问一道C# interview testing quesiton
1.成员变量应该设成static
2.numberOfLetters多余
3.for (int i = 0; i < strIn.Length; i++) 从头做到尾相当于又把string还原了,
应该是i < strln.Length / 2
4.strIn[strIn.Length - i]当i=0时越界,应该是strIn[strIn.Length - i - 1]
5.MyClass和函数都要加public,否则可能无法从其它namespace访问
6.输出函数自带format功能,不需要string.format()
k***t
发帖数: 276
30
The main part should be functional correct. It passed my test cases. There
might be boundary cases to cover though, not sure.
Got an improved version with comparison skipping on the right side. Not an O
(n), but should be close in a normal case as lots of comparisons are indeed
skipped. For this kind of questions, if it was never seen, I would be more
than happy if I could ever reach this far in an interview:)
#include
using namespace std;
bool maxji (int a[], int N, int *resi, int *res... 阅读全帖
l*********y
发帖数: 142
31
来自主题: JobHunting版 - 上个题大家给评评 (G家的)
#include
#include
#include
using namespace std;
struct Segment {
Segment(int l, int r) : left(l), right(r) {};
int left;
int right;
};
vector input;
vector output;
void MergeInput(vector& input, Segment& cand)
{
int left = cand.left;
int right = cand.right;
// 处理边界情况
if (input.size() == 0 || cand.right < input.front().left || cand.left >
input.back().right) {
output.push_back(Segment(cand.left, cand.right... 阅读全帖
j*******g
发帖数: 4
32
来自主题: JobHunting版 - atoi很不好写,头都大了...
见过很多次这个题目了,一直没有招,下面写了一个又臭又长的, 方法很笨, 求建议
, 欢迎批评和指正
这样的代码面试可以通过吗?
////////////////////////////////////////////////////////////////
#include
#include
using namespace std;
//假设16位的整型
// -32768 , +32767
const char MAX_INT[] = "32767";
const char MIN_INT[] = "32768";
const int MAX_STRLEN = 5;
bool my_atoi(const char *str, int &res)
{
if(str == NULL)
{
cout << "Invalid pointer" << endl;
return false;
}

int index = 0;

if(str[index] == '-' || s... 阅读全帖
q****x
发帖数: 7404
33
来自主题: JobHunting版 - 问个C++模板定义的问题
关于什么时候用,什么时候不用
#include
using namespace std;
template
class X {
// Why "class X {" not working?
public:
X() { cout << "T" << endl; }
X(const X& in);
private:
T x;
};
template
X::X(const X& in): x(in) {}
// Why "X::X(const X& in): x(in) {}" not working?
A**u
发帖数: 2458
34
来自主题: JobHunting版 - G家电面砸了,面经
我有笨办法
#include
#include
#include
#include
using namespace std;
vector P( int n );
vector R( int n );
vector P( int n )
{ vector code;
if ( n == 1 )
{
code.push_back(string("0"));
code.push_back(string("1"));
}
else
... 阅读全帖
q****x
发帖数: 7404
35
来自主题: JobHunting版 - c++ inline问题
下面这个例子,即使没有inline关键字,X::f()也必须inline,否则ODR违例?
这里的inline语法上必须有,但编译器还是可以处理成普通函数,还是说必须inline?
// class_X.h
#include
using namespace std;
class X {
public:
void f();
};
void X::f()
{
cout << "X::f()" << endl;
}
//class_X1.cpp
#include "class_X.h"
void f()
{
X x;
x.f();
}
// class_X2.cpp
#include "class_X.h"
int main()
{
X x;
x.f();
f();
}
g++ class_X1.cpp class_X2.cpp
q****x
发帖数: 7404
36
来自主题: JobHunting版 - An example of strategy pattern
/*
Is template method in parallel with strategy to abstract different algorithms? If so, how to modify the following example to template method?
*/
#include
using namespace std;
class Robot;
class Search {
public:
virtual void apply(Robot*) = 0;
};
class Linear: public Search {
public:
void apply(Robot*) { cout << "linear" << endl;}
};
class Spiral: public Search {
public:
void apply(Robot*) { cout << "spiral" << endl;}
};
class Robot {
public:
void go() { m_search->ap... 阅读全帖
A**u
发帖数: 2458
37
来自主题: JobHunting版 - 算法题求教
这个题目我刚好练过
这里有介绍
http://www.geeksforgeeks.org/archives/14469
用backtracking.
关键是,这个树的有些node, 可以不用访问
比如 你要找9.
假设从1开始那么,到达(1,2,4)node.
(1,2,4)的和 加上 {5,7,8}的最小值 > 9.
所以不用考虑 包含1,2,4的node了,这样可以可以排序不少点
这是我的算法
#include
#include
#include
using namespace std;
void re(int depth, int sum, vector selected,int w[], int N, int target){
if ( depth == N )
{
if ( sum == target )
{
for(vector::iterator it = selected.begin(); it != selecte... 阅读全帖
f*******t
发帖数: 7549
38
来自主题: JobHunting版 - Exposed上一道string permutation的题
我改进了一下,这回貌似行了,不过输出没能按照字典序:
#include
#include
#include
using namespace std;
void swap(string& s, int idx1, int idx2) {
if(idx1 == idx2)
return;
char temp = s[idx1];
s[idx1] = s[idx2];
s[idx2] = temp;
}
void permutation(string& s, int index, int size) {
if (index == size)
cout << s << endl;
else {
char last = 0;
for (int i=index; i if(s[i] == last)
continue;
... 阅读全帖
L**********u
发帖数: 194
39
来自主题: JobHunting版 - Exposed上一道string permutation的题
This problem can be solved by binary representation of an integer.
I am a rookie of C++. thanks for comments
my codes
#include
#include
#include
using namespace std;
const int N=5;
void subset(int arr[], int n)
{
bitset n_bit(n);
cout<<"{";
for(int i=0;i if(n_bit[i]==1)
cout< cout<<"};";
cout< }
//main function
int main()
{
cout<<"The set has "< int arr[N];
for(int i=0;i arr[i]=... 阅读全帖
f*******t
发帖数: 7549
40
来自主题: JobHunting版 - Exposed上一道string permutation的题
#include
#include
#include
#define BUFFSIZE 1024
using namespace std;
char buff[BUFFSIZE];
int idx;
void enumerate(const string& s, int pos)
{
if(pos == s.size()) {
if(idx == 0)
printf("{Empty}\n");
buff[idx] = 0;
printf("%s\n", buff);
}
else if(pos != 0 && s[pos] == s[pos-1]) {
enumerate(s, pos+1);
}
else {
buff[idx] = s[pos];
idx++;
enumerate(s, pos+1);
idx--;
enume... 阅读全帖
b*****e
发帖数: 131
41
来自主题: JobHunting版 - 求教 合并两数组 并排除重复
Time: O((m+n)logn)
Space: O(1)
using namespace std;
void union(int* a, int m, int* b, int n, int* res, int &len)
{
if(m < n) {
swap(a, b);
swap(m, n);
}
sort(b, b+n);
int* p;
for(int i = 0; i < m; i++) {
p = find(b, b+n, a[i]);
if(p != b+n)
*p = INT_MIN;
}
copy(a, a+m, res);
len = m;
for(int i = 0; i < n; i++)
if(b[i] != INT_MIN)
res[len++] = b[i];
}
f*******t
发帖数: 7549
42
来自主题: JobHunting版 - 不用暴力,这道题有没有优化解
写了很长的代码,感觉不太对,如果一个数组里面有重复的数,应该会输出重复的组合
,但程序实际运行结果貌似是没有,不知道为什么。
#include
#include
#include
#include
#include
#define N 5
using namespace std;
struct combination {
int indices[N];
int sum;
combination();
bool operator<(const combination& c) const;
void operator=(const combination& c);
bool operator() (const combination& c) const;
bool operator== (const combination& c) const;
};
combination::combination()
{
sum = 0;
f... 阅读全帖
k***t
发帖数: 276
43
来自主题: JobHunting版 - 问个算法题
也来凑个热闹。主要是练练trie。
花了些时间才调通:( 谁帮忙给Review一下?谢了。
运行结果:
ad: 5
bc: 3
bb: 2
aaa: 1
aa: 1
源码:
#include
#include
using namespace std;
struct Node {
int cnt;
char c;
struct Node *n[26];
char *p; // to the 1st occurrence in the original input string
};
#define idx(x) (x-'a')
void add2trie (Node *r, char *p1, char *p2) {
char *p; Node *cur=r; Node *n;
p=p1;
cur=r;
while (p!=p2 && cur->n[idx(*p)]) {cur=cur->n[idx(*p)]; ++p;}
if (p==p2) { cur->cnt++;... 阅读全帖
f*******t
发帖数: 7549
44
平衡括号的题可以用贪心法做吧
#include
#include
#include
#include
#define INVALIDCHAR -1
using namespace std;
char *balance(char *str)
{
int len = strlen(str);
if(len < 1)
return NULL;

char *buff = (char*)calloc(len + 1, 1);
stack left;
for(int i = 0; i < len; i++) {
if(str[i] == '(')
left.push(i);
else if(str[i] == ')') {
if(left.empty()) {
buff[i] = INVALIDCHAR;
co... 阅读全帖
f*******t
发帖数: 7549
45
来自主题: JobHunting版 - google的一道题求解
array装水的题有很多变种,算法一般都是O(N)复杂度。
这题的思路是从前往后、从后往前各找一个递增序列,然后从两边往中间靠拢。
如果当前考察的左右两个线段分别是(ix, aix)和(iy, aiy),下一对要考察的线段一
定保留aix与aiy里较大的一条,而另一条换成它相应递增序列里的下一个。如果aix与
aiy相等,ix和iy同时取下一个。
#include
#include
#include
using namespace std;
inline int min(int a, int b)
{
return a > b ? b : a;
}
inline int max(int a, int b)
{
return a > b ? a : b;
}
int maxVolume(int arr[], int size)
{
if(size < 2)
return 0;
vector inc, dec;
inc.push_back(0);
dec.pus... 阅读全帖
i*****e
发帖数: 113
46
来自主题: JobHunting版 - google的一道题求解
有点像哪个加减加减的问题
我这个算法对不对,帮忙看看
#!/usr/bin/env python
class Bottle(object):
def __init__(self, point1, point2):
self.point1 = point1
self.point2 = point2
def volume(self):
width = self.point2[0] - self.point1[0]
depth = self.point1[1] \
if self.point1[1] <= self.point2[1] \
else self.point2[1]
return width * depth
def __gt__(self, bottle):
return self.volume() > bottle.volume()
def __lt__(self, bot... 阅读全帖
c**********e
发帖数: 2007
47
来自主题: JobHunting版 - google的一道题求解
这是改过的。过了所有测试。复杂度O(n)。
#include
using namespace std;
inline int min(int a, int b) { return a < b ? a : b; }
int maxArea(int a[], int size) {
int mArea=0, area, left=0, right=size-1;
while(left area=(right-left) * min(a[left],a[right]);
if(area>mArea) mArea=area;
if(a[left]<=a[right]) {
int i=left;
while(i left=i;
} else {
int j=right;
while(j>left && a[j]<=a[right]) j--;
right=j;
}
}
... 阅读全帖
f*******t
发帖数: 7549
48
来自主题: JobHunting版 - google的一道题求解
array装水的题有很多变种,算法一般都是O(N)复杂度。
这题的思路是从前往后、从后往前各找一个递增序列,然后从两边往中间靠拢。
如果当前考察的左右两个线段分别是(ix, aix)和(iy, aiy),下一对要考察的线段一
定保留aix与aiy里较大的一条,而另一条换成它相应递增序列里的下一个。如果aix与
aiy相等,ix和iy同时取下一个。
#include
#include
#include
using namespace std;
inline int min(int a, int b)
{
return a > b ? b : a;
}
inline int max(int a, int b)
{
return a > b ? a : b;
}
int maxVolume(int arr[], int size)
{
if(size < 2)
return 0;
vector inc, dec;
inc.push_back(0);
dec.pus... 阅读全帖
i*****e
发帖数: 113
49
来自主题: JobHunting版 - google的一道题求解
有点像哪个加减加减的问题
我这个算法对不对,帮忙看看
#!/usr/bin/env python
class Bottle(object):
def __init__(self, point1, point2):
self.point1 = point1
self.point2 = point2
def volume(self):
width = self.point2[0] - self.point1[0]
depth = self.point1[1] \
if self.point1[1] <= self.point2[1] \
else self.point2[1]
return width * depth
def __gt__(self, bottle):
return self.volume() > bottle.volume()
def __lt__(self, bot... 阅读全帖
c**********e
发帖数: 2007
50
来自主题: JobHunting版 - google的一道题求解
这是改过的。过了所有测试。复杂度O(n)。
#include
using namespace std;
inline int min(int a, int b) { return a < b ? a : b; }
int maxArea(int a[], int size) {
int mArea=0, area, left=0, right=size-1;
while(left area=(right-left) * min(a[left],a[right]);
if(area>mArea) mArea=area;
if(a[left]<=a[right]) {
int i=left;
while(i left=i;
} else {
int j=right;
while(j>left && a[j]<=a[right]) j--;
right=j;
}
}
... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)