z**********n 发帖数: 3 | 1 #include
#include
using namespace std;
class Matrix
{
public:
Matrix(int r, int c)
{
pData = new int[r * c];
_rowNum = r;
_colNum = c;
}
// TODO pData is a pointer, so deep copy struct is needed
// TODO write copy structor and assign operateo
//Matrix(Matrix& m)
//{
//}
//Matrix& operator = (Matrix& m)
//{
//}
void SetNum(int r, int c, int num)
{
pData[r * _colNum + c] = num;
}
const i... 阅读全帖 |
|
s********k 发帖数: 6180 | 2 上一个我写的,没有检查字符串是否都在0-9和'.',不知道这个是否需要补上(如果某
个版本不符合要求(10.1.a),返回什么?)。基本思路是第一个.之前按照数字大小比较
,然后其余的一个个位置比较,只要某一个位置其中一个大,剩下的就不需要比较了(比
如2.07和2.1,小数点之后后面的1》前面的0,直接2.1版本大),然后如果某个字符串先
到.,别的还有数字,那么后者大(1.05>1.0.5),如果最后一个已经晚了,另外一个还有
数字,后面大(3.2.1<3.2.10或者3.2.1<3.2.1.5)
大牛指点一下
const char *compVersion(const char *version1, const char * version2){
const char *v1 = version1;
const char *v2 = version2;
//compare the digit before first '.'
if(getNum(v1)>getNum(v2)) return v1;
else if(getNum(v1)>ge... 阅读全帖 |
|
e****e 发帖数: 418 | 3 java:
public void remove(Node head) {
Node pre = null;
for ( Node node = head; node != null; node = node.getNext() ) {
boolean foundDuplidate = node.getNext() != null && node.getNum()
== node.getNext().getNum() ? true : false;
while ( node.getNext() != null && node.getNum() == node.getNext(
).getNum() ) {
node = node.getNext();
}
if ( foundDuplidate )
if ( pre == null )
... 阅读全帖 |
|
a****m 发帖数: 693 | 4 总是说不能display, 如果把所有的class里面的改成public, 就可以直接用num and
denum
输出到 display, 但是如果private,怎么都不行,谢谢。
#include
using namespace std;
class CFraction
{
private: //private method to get GCD
int num;
int denum;
public:
CFraction(int num, int denum);
int getnum();
int getdenum();
void display();
};
//constructor...
int CFraction::getnum(){
return num;
}
int CFraction::getdenum(){
return denum;
}
CFraction::CFraction( int a, int b )
{
num = a;
denum = b;
}
void CFraction::display() {
cout << ... 阅读全帖 |
|
w****x 发帖数: 2483 | 5
const char* getNum(const char* q, int& res)
{
const char* p = q;
if (NULL == p) return NULL;
res = 0;
while (*p != '.' && *p != 0)
res = 10*res + *p++ - '0';
if (*p == '.') p++;
return p;
}
bool lessThan(const char* str1, const char* str2)
{
if (NULL == str1 || NULL == str2)
return false;
const char* p1 = str1;
const char* p2 = str2;
while (*p1 != 0 && *p2 != 0)
{
int x,y;
p1 = getNum(p1, x);
p2 = getNum(p2, y);... 阅读全帖 |
|
g*********e 发帖数: 14401 | 6 int getNum(char **s){
if(**s == '\0')
return 0;
int val=0;
while(**s != '.' && **s != '\0'){
val = val*10 + (**s)-'0';
(*s)++;
}
return val;
};
int compareVersion( char * s1, char * s2){
if(*s1 == '\0' && *s2 == '\0')
return 0;
if(*s1 == '.')
s1++;
if(*s2 == '.')
s2++;
int a = getNum(&s1);
int b = getNum(&s2);
printf("comparing %d and %d\n", a, b);
if(a > b)
return 1;
else if(a < b)
... 阅读全帖 |
|
g*********e 发帖数: 14401 | 7 fix after the leading zero issue
int getNum(char **s, int &leadZeros){
if(**s == '\0')
return 0;
int val=0;
bool lead=true;
leadZeros=0;
while(**s != '.' && **s != '\0'){
if(lead && **s == '0')
leadZeros++;
else
lead = false;
val = val*10 + (**s)-'0';
(*s)++;
}
return val;
};
int compareVersion( char * s1, char * s2){
if(*s1 == '\0' && *s2 == '\0')
return 0;
if(*s1 == '.')
s1++;
... 阅读全帖 |
|
w****x 发帖数: 2483 | 8 struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
int getHeight(NODE* pNode, bool bLft)
{
int nRet = 0;
NODE* pIter = pNode;
while (NULL != pIter)
{
if (bLft)
pIter = pIter->pLft;
else
pIter = pIter->pRgt;
nRet++;
}
return nRet;
}
int getNum(int h)
{
return pow((double)2, h) -1;
}
int getNumberOfNodes(NODE* pRoot)
{
NODE* pNode = pRoot;
int h = get... 阅读全帖 |
|
f**********t 发帖数: 1001 | 9 // Given a nested list of integers, returns the sum of all integers in the
list
// weighted by their depth. For example, given the list {{1,1},2,{1,1}} the
// function should return 10 (four 1's at depth 2, one *2 at depth 1). Given
// the list {1,{4,{6}}} the function should return 27 (one 1 at depth 1, one
4
// at depth 2, and *one 6 at depth 3)
int getNum(string::iterator &i, string::iterator end) {
if (i == end) {
return 0;
}
bool neg = false;
if (*i == '-') {
neg = true;
... 阅读全帖 |
|
c*****n 发帖数: 95 | 10 这题可以O(n3)
遍历所有pair of rectangle, 对于每个pair 可以得到两个(因为可以旋转)相交区域的
lower bound (x' , y'). 这时res = 2
如果x' * y' >= limit
然后对剩下的rectangle 如果其长和宽都大于对应(x', y') 就res++
最后取最大的res
如果所有lower bound < limit, 返回-1
int getNum(int l1, int l2, int w1, int w2, vector& X, vector&
Y, int limit, int i, int j) {
int l = l1 < l2? l1:l2;
int w = w1 < w2? w1:w2;
if(l * w >= limit) {
int res = 2;
for(int k = 0; k < X.size(); k++) {
if(k != i &&... 阅读全帖 |
|
b********n 发帖数: 609 | 11 按着思路写了一个,是work的。
"
#include
class A {
public:
class Adapter {
public:
Adapter(A& parent)
: ref(parent.a) {
}
private:
friend class B;
int& ref;
};
A() : a(0), i(*this) {
}
Adapter& getAdapter() {
return i;
}
int getNum() const {
return a;
}
private:
int a;
Adapter i;
};
class B {
public:
void changeNum(A& a, int x) {
A::Adapter& i = a.getAdapter();
i.ref = x;
}
};
int main() {
A a;
std::cout << "Original value: " <<... 阅读全帖 |
|
w****x 发帖数: 2483 | 12
//Turn Roman number to decimal
//I(1), V(5), X(10), L(50), C(100), D(500), M(1000)
int GetNum(char c)
{
if (c == 'I' || c == 'i')
return 1;
else if (c == 'V' || c == 'v')
return 5;
else if (c == 'X' || c == 'x')
return 10;
else if (c == 'L' || c == 'l')
return 50;
else if (c == 'C' || c == 'c')
return 100;
else if (c == 'D' || c == 'd')
return 500;
else if (c == 'M' || c == 'm')
return 1000;
return 0;
}
... 阅读全帖 |
|
f**********t 发帖数: 1001 | 13 Well, my code is complex and sigfault. Let me go back if I have time.
This is too hard for a 45 minutes interview.
void skipSpace(const char **s) {
while (isspace(**s)) {
++(*s);
}
}
int getNum(const char **s) {
bool neg = false;
if (**s == '-') {
neg = true;
++(*s);
}
int cur = 0;
while (isdigit(**s)) {
cur = cur * 10 + (**s - '0');
++(*s);
}
--(*s);
return neg ? -cur : cur;
}
int opNum(int x, int y, char op) {
switch (op) {
case '+':
return x + y... 阅读全帖 |
|
d*******r 发帖数: 208 | 14 用sort的方法有两个缺点
1. tie break
2. overflow
how about use raw permutation ?
code:
void findMax(const std::vector& prefix, const std::vector& exist,
int& max) {
if (exist.empty()) {
int cur = getNum(prefix);
if (cur > max) {
max = cur;
}
} else {
for (std::vector::const_iterator i=exist.begin(); i!=exist.end(
); ++i) {
std::vector new_pre = prefix;
new_pre.push_back(*i);
std::vector new_ex(... 阅读全帖 |
|
w***o 发帖数: 109 | 15 倒着来想法很新颖,但好像有问题,比如 IVX = (V - I) + X, not (X - V - I)
const char* pIter = szNum + nLen - 1;
int nRet = 0;
int nPrev = 0;
while (pIter >= szNum)
{
int nCur = GetNum(*pIter);
if (nCur >= nPrev)
nRet += nCur;
else nRet -= nCur;
nPrev = nCur;
pIter--;
} |
|
y***x 发帖数: 22 | 16 写了个JavaScript的,想法跟二爷一开始的一样,扫两遍,一遍处理+-, 一遍处理*/
function getnum(str){
var n = str.split(/[+\-]+/);
var c = str.split(/[0-9\*\/]+/);
for(var i=0; i
n[i] = getmdnum(n[i]);
}
for(var j=0; j
n[j+1] = cal(n[j], n[j+1], c[j+1]);
}
return n[j];
}
function getmdnum(str){
var n = str.split(/[\*\/]/);
var c = str.split(/[0-9]+/);
if(n.length==1) return n[0]-0;
for(var i=0; i
... 阅读全帖 |
|
y***x 发帖数: 22 | 17 写了个JavaScript的,想法跟二爷一开始的一样,扫两遍,一遍处理+-, 一遍处理*/
function getnum(str){
var n = str.split(/[+\-]+/);
var c = str.split(/[0-9\*\/]+/);
for(var i=0; i
n[i] = getmdnum(n[i]);
}
for(var j=0; j
n[j+1] = cal(n[j], n[j+1], c[j+1]);
}
return n[j];
}
function getmdnum(str){
var n = str.split(/[\*\/]/);
var c = str.split(/[0-9]+/);
if(n.length==1) return n[0]-0;
for(var i=0; i
... 阅读全帖 |
|
g**e 发帖数: 6127 | 18 这几天翻effective java, josh bloch推荐了一个enum singleton的写法,他提到
"it is more concise, provides the serialization machinery for free, and
provides an
ironclad guarantee against multiple instantiation, even in the face of
sophisticated
serialization or reflection attacks. While this approach has yet to be
widely
adopted, a single-element enum type is the best way to implement a
singleton."
但是这个不是lazy的
public final class SingletonEnum {
public enum enumSingleton {
INSTANCE;
... 阅读全帖 |
|
a****m 发帖数: 693 | 19 Yes, I am naive of that. thanks. I missed bracket. should getnum() |
|