w****x 发帖数: 2483 | 1 用hashtable保存value->index pair, 用bool数组记录对应index是否访问过, 遍历大
数组, 对每个值向两边延伸,更新hashtable和bool数组, 这样disjoint的segment就一
个个浮现出来了, O(n) |
|
r********g 发帖数: 1351 | 2 写了一个,可能有点罗嗦。。
class Solution {
public:
void setZeroes(vector > &M) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int nRow = M.size();
if(nRow <= 0) return;
int nCol = M[0].size();
if(nCol <= 0) return;
bool rowZero = false;
bool colZero = false;
for(int i = 0; i < nRow; i++) if(!M[i][0]) colZero = true;
for(int j = 0; j < nCol; j++) if(!M[0][j]) rowZer... 阅读全帖 |
|
r********g 发帖数: 1351 | 3 写了一个,可能有点罗嗦。。
class Solution {
public:
void setZeroes(vector > &M) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int nRow = M.size();
if(nRow <= 0) return;
int nCol = M[0].size();
if(nCol <= 0) return;
bool rowZero = false;
bool colZero = false;
for(int i = 0; i < nRow; i++) if(!M[i][0]) colZero = true;
for(int j = 0; j < nCol; j++) if(!M[0][j]) rowZer... 阅读全帖 |
|
E*******0 发帖数: 465 | 4 // NextPermutation.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
using namespace std;
#include
bool myfunction (int i,int j) { return (i>j); }
bool nextPermutation(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
//If vector size is 1, permutation is its self.
if (1==num.size())
return true;
// rit is the iterator indicates current ite... 阅读全帖 |
|
s*******f 发帖数: 1114 | 5 日子久了,忘了一些。搅拌到一起,无公司名。有些板上看见过的不列了,呵呵
注意编码,很难得算法不咋会考。
1.实现BigInt类。实现 ‘+’ 即可。
2.国际象棋棋盘中两个queen之间最短路径(queen只能斜着走),返回步数即可。就是
一个queen最少几步能走到另一个queen
3.class SortedArrays{
listofSortedArrays;
public:
bool HasNext();
bool Next();
}
1,3 ..
2,5 ..
4,5 ...
--> 1,2,3,4,5,5....
4. // return a^b
// pow(2, 3) = 8;
// pow(2, -3); = 1 / 8;
// if a < 0;
double pow(double a, int b){
5. binary search in sorted, but head-in-middle array. [15, 16, 1, 3, 9, 11,
13]
6. 1boogle game. give a boogle and a word, retu... 阅读全帖 |
|
l*********8 发帖数: 4642 | 6 inline bool valid(char c,char v) {return c=='1'||(c=='2'&&v<='6');}
inline bool valid(char c) {return c!='0';}
int decodingNum(const string & s)
{
char c1('0');
int n0(1), n1(1), n(1);
for (int i=0; i0; ++i) {
n = n0*valid(c1,s[i]) + n1*valid(s[i]);
n0 = n1; n1 = n;
c1 = s[i];
}
return n;
} |
|
l********t 发帖数: 878 | 7 /*accepted*/
bool equalVector(vector v1, vector v2){
for(int ii = 0; ii < v1.size(); ++ii){
if (v1[ii] != v2[ii]) return false;
}
return true;
}
bool compareVector(vector v1, vector v2){
for(int ii = 0; ii < v1.size(); ++ii){
if (v1[ii] < v2[ii]) return true;
else if (v1[ii] > v2[ii]) return false;
}
return false;
}
class Solution {
public:
vector > fourSum(vect... 阅读全帖 |
|
l********t 发帖数: 878 | 8 /*accepted*/
bool equalVector(vector v1, vector v2){
for(int ii = 0; ii < v1.size(); ++ii){
if (v1[ii] != v2[ii]) return false;
}
return true;
}
bool compareVector(vector v1, vector v2){
for(int ii = 0; ii < v1.size(); ++ii){
if (v1[ii] < v2[ii]) return true;
else if (v1[ii] > v2[ii]) return false;
}
return false;
}
class Solution {
public:
vector > fourSum(vect... 阅读全帖 |
|
n****r 发帖数: 471 | 9 谁能给讲解一下这位大牛的解法? http://discuss.leetcode.com/questions/225/permutations-ii
Code:
class Solution {
public:
vector > permuteUnique(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector used(num.size(), false);
vector path;
vector > ret;
sort(num.begin(), num.end());
sub(num, used, path, ret);
return ret;
}
void sub(vector &num, vector... 阅读全帖 |
|
n****r 发帖数: 471 | 10 谁能给讲解一下这位大牛的解法? http://discuss.leetcode.com/questions/225/permutations-ii
Code:
class Solution {
public:
vector > permuteUnique(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector used(num.size(), false);
vector path;
vector > ret;
sort(num.begin(), num.end());
sub(num, used, path, ret);
return ret;
}
void sub(vector &num, vector... 阅读全帖 |
|
t*****h 发帖数: 137 | 11 #include
#include
#include
using namespace std;
bool find_begin_word(string &sentence, vector &dict, vector &
space_pos, int pos)
{
int new_pos = 0;
bool found = false;
for ( int i = 0; i < dict.size() && !found; ++i )
{
int w_len = dict[i].length();
if ( pos + w_len <= sentence.length() && sentence.substr(pos, w_len) ==
dict[i] )
{
new_pos = pos + w_len;
if ( new_pos == sentence.length() )
{
space_pos.push_... 阅读全帖 |
|
t*****h 发帖数: 137 | 12 #include
#include
#include
using namespace std;
bool find_begin_word(string &sentence, vector &dict, vector &
space_pos, int pos)
{
int new_pos = 0;
bool found = false;
for ( int i = 0; i < dict.size() && !found; ++i )
{
int w_len = dict[i].length();
if ( pos + w_len <= sentence.length() && sentence.substr(pos, w_len) ==
dict[i] )
{
new_pos = pos + w_len;
if ( new_pos == sentence.length() )
{
space_pos.push_... 阅读全帖 |
|
p********s 发帖数: 37 | 13 写了个超级傻的,各种没效率,您别笑话囧
int pl(int a, int b) { return a + b; }
int mi(int a, int b) { return a - b; }
int ti(int a, int b) { return a * b; }
int di(int a, int b) { return (b && !(a % b)) ? (a / b) : -12345 ; }
int (*op[4])(int a, int b) = {&pl, &mi, &ti, &di};
const char *name[4] = {"+", "-", "*", "/"};
char result[128] = {0};
bool tryit(int nums[], char* res)
{
char result[128];
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
for(int k = 0; k < 4; k++)
if ((*op[k])((*op[j])((*op... 阅读全帖 |
|
p********s 发帖数: 37 | 14 写了个超级傻的,各种没效率,您别笑话囧
int pl(int a, int b) { return a + b; }
int mi(int a, int b) { return a - b; }
int ti(int a, int b) { return a * b; }
int di(int a, int b) { return (b && !(a % b)) ? (a / b) : -12345 ; }
int (*op[4])(int a, int b) = {&pl, &mi, &ti, &di};
const char *name[4] = {"+", "-", "*", "/"};
char result[128] = {0};
bool tryit(int nums[], char* res)
{
char result[128];
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
for(int k = 0; k < 4; k++)
if ((*op[k])((*op[j])((*op... 阅读全帖 |
|
d******a 发帖数: 238 | 15 我刚写了个,没怎么检查,欢迎探讨。
class Iterator{
public:
` Iterator(vector> &a) : v(a)
{
m_outer_iter = v.begin();
while (m_outer_iter != v.end())
{
if (m_outer_iter->size() != 0)
break;
else
m_outer_iter++;
}
if (m_outer_iter == v.end())
m_flag = false;
else
{
m_inner_iter = m_outer_iter->begin();
m_next = *m_inner_iter;
... 阅读全帖 |
|
c*****n 发帖数: 96 | 16 bool isInterleave(String a, String b, String c){
return isInterleave(a, b, c, "");
}
bool isInterleav(String a, String b, String c, String interleave){
if (String.IsNullOrEmpty(a) && String.IsNullOrEmpty(b)){
return c.equals(interleave);
}
if (String.IsNullOrEmpty(a))
return c.equals(interleave + b);
if (String.IsNullOrEmpty(b))
return c.equals(interleave + a);
return isInterleave(a.SubString(1), b, c, interleave + a.SubString(0,1))
|| isInterl... 阅读全帖 |
|
l****c 发帖数: 782 | 17 第二题,小测试过了,大测试Time Limit Exceeded怎么回事呢
class Solution {
public:
bool isInterleave(string s, string s1, string s2, int s_index, int s1_index,
int s2_index)
{
if (s_index == s.size()) {
if((s1_index == s1.size())&&(s2_index == s2.size()))
return true;
else
return false;
}
else {
if (s1_index==s1.size()&&s2_index==s2.size())
return false;
else if (s1_index==s1.size()) {
if (s[s_index]==s2[s2_index]&&(isInterl... 阅读全帖 |
|
i**********e 发帖数: 1145 | 18 哎呀,吓我一跳,还以为系统出了问题。
没关系,只是你程序出了个小bug。
dpt[i] = (bool*)malloc(sizeof(bool)*l1);
应该是 * l2 才对阿 :) |
|
w****x 发帖数: 2483 | 19 design一个vending machine.
class VendingMachine
{
public:
....
bool getBeverage(int nType);
private:
vector m_beverages;
};
class Beverage;
class Coca : public Beverage;
class Water : public Beverage;
VendingMachine里有一个bool getBeverage函数。要制定饮料类型获取饮料。
因为m_beverages里都是基类, 所以需要知道类型, 所以在Beverage里加了一个type
的enum 成员. 然后在getBeverage里遍历容器看基类的type flag是否和nType一致。
这样做似乎违背了OOD原则, 但是好像也没什么更好的方法, 到底因该怎么处理? |
|
s***0 发帖数: 117 | 20 Interviewer: Implement the bool getItem (ItemType) function of a Vending
Machine.
So you should know that this is a trick question: It's too easy.
What he really says is: design a set of classes so you can easily implement
the above function.
He'll throw stuff in later like: what if the items in a vending machine are
not fixed? (e.g. vending machine held sodas, now it holds chips).
What if instead of bool getItem(), I want to return the price of the item?
Can your class handle of that easily wit... 阅读全帖 |
|
w****x 发帖数: 2483 | 21 design一个vending machine.
class VendingMachine
{
public:
....
bool getBeverage(int nType);
private:
vector m_beverages;
};
class Beverage;
class Coca : public Beverage;
class Water : public Beverage;
VendingMachine里有一个bool getBeverage函数。要制定饮料类型获取饮料。
因为m_beverages里都是基类, 所以需要知道类型, 所以在Beverage里加了一个type
的enum 成员. 然后在getBeverage里遍历容器看基类的type flag是否和nType一致。
这样做似乎违背了OOD原则, 但是好像也没什么更好的方法, 到底因该怎么处理? |
|
s***0 发帖数: 117 | 22 Interviewer: Implement the bool getItem (ItemType) function of a Vending
Machine.
So you should know that this is a trick question: It's too easy.
What he really says is: design a set of classes so you can easily implement
the above function.
He'll throw stuff in later like: what if the items in a vending machine are
not fixed? (e.g. vending machine held sodas, now it holds chips).
What if instead of bool getItem(), I want to return the price of the item?
Can your class handle of that easily wit... 阅读全帖 |
|
h*****f 发帖数: 248 | 23 - Write 1 very simple program that has 2 threads.
- Thread A does an atomic write to a bool variable
- Thread B keeps spinning to do an atomic read on that bool variable
- Use cpuset w/cpu exclusive=1 for that process on 1 core (not 1 CPU)
The time from A writes to the time B picks up the change is approximately
the context switch time.
Of course, precisely, it is equal to context switch + 1 atomic write + 1
atomic read.
Anyone has an even accurate solution? |
|
i*******e 发帖数: 240 | 24 string s = "abc//xyz\nwwe*/*sdfsd/*sdfda*/sd*/cvcd";
int i;
bool isInStarComment = false;
bool isInLineComment = false;
string ans = "";
for (i=0; i
{
if (!isInStarComment && !isInLineComment)
{
if (i+1
{
if (s[i+1] == '/') {isInLineComment = true; i++; continue;}
else if (s[i+1] == '*') {isInStarComment = true; i++;
continue;}
}
... 阅读全帖 |
|
c****9 发帖数: 164 | 25 贴个过了测试的递归dp的code
class Solution {
public:
vector > map;
bool help(string& s1,string& s2,string& s3,int i, int j)
{
if(i+j==s3.size())
map[i][j]=true;
if(map[i][j]==1)
return true;
else if(map[i][j]==0)
return false;
if(i
{
if(help(s1,s2,s3,i+1,j))
map[i][j]=1 ;
else
map[i][j] = 0 ;
}
if(j阅读全帖 |
|
g***j 发帖数: 1275 | 26 做leedcode上面的关于k-way merge的题目,题目是
Merge k sorted linked lists and return it as one sorted list.
如下的code通过不了,似乎是因为我在priority_queue里面用了指针
std::priority_queue, CompareNode> myQ;
因为如果我改成
std::priority_queue, CompareNode> myQ;
并且把其他的相关->都改了dot之后,就可以全部通过了。
我用debug跟踪,发现myQ.pop()之后,虽然myQ.size()表小了,但是myQ.top()的内容
没有发生变化。
请问,这里面的trick是什么?难道这里不能用Node*么?如果是这样的,还有什么
container不能用Node*的?
大牛帮我解释一下好么?
class Pair{
public:
ListNode* node;
int index;
};
class CompareNo... 阅读全帖 |
|
g***j 发帖数: 1275 | 27 这个code就可以全部test case都通过。改动就是指针和非指针的差别。
class Pair{
public:
ListNode* node;
int index;
};
class CompareNode: public std::binary_function{
public:
bool operator()(const Pair p, const Pair q) const {
return p.node->val > q.node->val;
}
};
class Solution {
public:
ListNode *mergeKLists(vector &lists) {
if(lists.size() == 0) return NULL;
if(lists.size() == 1) return lists[0];
std::priority_queu... 阅读全帖 |
|
l*********8 发帖数: 4642 | 28 刚写了一个,发现跟二爷的很像啊,呵呵。
inline bool diffVar(Node* p, Node* q) { return p&&q && p->var != q->var;};
bool uniVar(Node * root, int & num)
{
if (!root)
return true;
if ( uniVar(root->left, num) && !diffVar(root, root->left)
&& uniVar(root->right,num) && !diffVar(root, root->right)) {
num++;
return true;
}
return false;
} |
|
l*********8 发帖数: 4642 | 29 恩, 我一开始就写错了。 错误代码如下:
inline bool diffVar(Node * p, Node* q) { return p&&q && p->var != q->var;};
bool uniVar(Node * root, int & num)
{
if (!root)
return true;
if ( !uniVar(root->left, num) || diffVar(root, root->left)
|| !uniVar(root->right,num) || diffVar(root, root->right)) {
return false;
}
num++;
return true;
} |
|
m***k 发帖数: 946 | 30 感觉(x < y)也是一种判断。
如果a、b都是int,我有一个更简单的解法:
bool b1 = a/b;
bool b2 = b/a;
return ((int)b1)*a + ((int)b2)*b;
y
as |
|
i**********e 发帖数: 1145 | 31 longway2008 贴过的代码,估计是我看过非递归解法最短的行数了。。。但不好消化
bool isMatch(const char *str, const char *pat, bool hasStar=false)
{
if (!*pat) return !*str || hasStar;
const char *s, *p;
for (s = str, p = pat; *s; ++s, ++p) {
if (*p == '*')
return isMatch(s, p+1, true);
else if ( *p != '?' && *s != *p)
return !hasStar ? false : isMatch(str+1, pat, hasStar);
}
while (*p == '*') ++p;
return (!*p);
} |
|
i**********e 发帖数: 1145 | 32 你可以看看我这个是不是跟你一样的啊?
为什么large case 里错了两三个(除掉最后一个 TLE 的case 之外)?
bool isMatch(const char *s, const char *p) {
int n=strlen(s);
int m=strlen(p);
bool dp[2][n+1];
for (int i = 0; i < 1; i++) {
for (int j = 0; j < n+1; j++)
dp[i][j] = false;
}
dp[m%2][n]=true;
for(int i=m-1;i>=0;i--)
{
for (int j=0;j
dp[i%2][j] = false;
}
dp[i%2][n]=(p[i]=='*' && dp... 阅读全帖 |
|
l*******b 发帖数: 2586 | 33 测了下,貌似没问题呀,感觉和merge interval哪个题一样
#include
#include
#include
using namespace std;
struct Interval {
int a;
int b;
bool cf;
Interval() : a(0), b(0), cf(false) {}
Interval(int start, int end) : a(start), b(end), cf(false) {}
};
bool compare(const Interval &s, const Interval &t) {
return s.a < t.a;
}
class Play {
public:
void setflag(vector &list) {
/* same idea as merge intervals, when intervals merge, they conflict
*/
... 阅读全帖 |
|
w****x 发帖数: 2483 | 34
为啥要扫第二次? 谁帮我看看下面这个解法对不对!
struct EVENT
{
int nStart;
int nEnd;
bool bFlg;
EVENT(int a, int b) : nStart(a), nEnd(b) { bFlg = false; }
};
bool lessThan(const EVENT& evt1, const EVENT& evt2)
{
return evt1.nStart < evt2.nStart;
}
void setFlg(EVENT events[], int n)
{
sort(events, events+n, lessThan);
int nMaxEndIndex = 0;
for (int i = 1; i < n; i++)
{
if (events[i].nStart <= events[nMaxEndIndex].nEnd)
{
events[i].bFlg = true;
... 阅读全帖 |
|
Y**3 发帖数: 21 | 35 void BackTrack(vector > &board,int k,bool& bFindOne)
{
if(k>=81)
{
bFindOne=true;//find a solution
return;
}
int row=k/9;
int col=k%9;
if(isdigit(board[row][col]))//already set
BackTrack(board,k+1,bFindOne);
else
{
for(int i=1;i<=9;i++)
{
board[row][col]=i+'0';
if(isValid(board,row,col))
BackTrack(boar... 阅读全帖 |
|
w****x 发帖数: 2483 | 36 class CIterIter
{
public:
CIterIter(vector>& vec) : m_vec(vec)
{
m_ni = -1;
m_nj = 0;
}
void next()
{
prob(m_ni, m_nj);
}
bool hasNext()
{
if (m_ni < 0)
{
m_ni = 0;
if (!m_vec.empty() && !m_vec[0].empty())
return true;
}
int i,j;
return prob(i, j);
}
int getVal() { return m_vec[m_ni][m_nj]; }
private:
bool prob(int& i, int& j)
{
... 阅读全帖 |
|
f*********m 发帖数: 726 | 37 第二题贴个template List版的,请各位指正:
template
class NextClass
{
public:
NextClass(list >& L):CList (L), listListIt(L.begin()), listIt((*L.
begin()).begin())
{
while (listlistIt != CList.end())
{
if ((*listlistIt).begin() != (*listlistIt).end())
{
listIt = (*listlistIt).begin();
break;
}
++listlistIt;
}
if ((*listlistIt).begin() == (*listlistIt).end())
c... 阅读全帖 |
|
d******e 发帖数: 164 | 38 然后是一个很长的char array,内容可以是任何character。然后写程序返回是否这个
array包含了所有a-z的letter。他们让我一直优化算法,我做了一个O(n)的算法,他们
还让一直优化;
bool has_all_lower(char *s) {
bool mark[26];
for (int i = 0; i < 26; i++)
mark[i] = false;
int cnt = 0;
for (char *p = s; *p; p++) {
if (islower(*p) && !mark[*p-'a']) {
mark[*p-'a'] = true;
cnt++;
if (cnt == 26)
return true;
}
}
return false;
} |
|
j*******e 发帖数: 1058 | 39 这个超级简单。
就问了2道题目,1道是judge BST。1道是linkedlist loop,多少个node在loop里面。
我写了个recursive的解法,
bool isBSTHelper(BinaryTree *p, int low, int high) {
if (!p) return true;
if (low < p->data && p->data < high)
return isBSTHelper(p->left, low, p->data) &&
isBSTHelper(p->right, p->data, high);
else
return false;
}
bool isBST(BinaryTree *root) {
// INT_MIN and INT_MAX are defined in C++'s library
return isBSTHelper(root, INT_MIN, INT_MAX);
}
java版本的。结果那烙印居然对为什么要传low和high都不知道。问... 阅读全帖 |
|
w****x 发帖数: 2483 | 40 /*Validate if a given string is numeric.
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
*/
bool isNumber(const char *s) {
bool bIntNum = false;
int nCurState = 1;
while (true)
{
if (nCurState == 1)
{
while (*s == ' ')
s++;
if (*s == '+' || *s == '-')
s++;
if (*s >= '0' && *s <= ... 阅读全帖 |
|
d******e 发帖数: 164 | 41 bool isNumber(const char *s) {
while (isspace(*s)) s++;
if (*s == '+' || *s == '-') s++;
bool num = false;
while (isdigit(*s)) {
s++;
num = true;
}
if (*s == '.') {
s++;
while (isdigit(*s)) {
s++;
num = true;
}
}
if (*s == 'e') {
if (!num) return false;
s++;
if (*s == '+' || *s == '-') s++;
... 阅读全帖 |
|
w****x 发帖数: 2483 | 42 /*Validate if a given string is numeric.
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
*/
bool isNumber(const char *s) {
bool bIntNum = false;
int nCurState = 1;
while (true)
{
if (nCurState == 1)
{
while (*s == ' ')
s++;
if (*s == '+' || *s == '-')
s++;
if (*s >= '0' && *s <= ... 阅读全帖 |
|
o***d 发帖数: 313 | 43 今天发生两次了,c++, 同样的test case,我用VS2010和cygwin都测试没问题,leetcode就
是fail........
比如这个:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
// Start typing your C/C++ solution below
// DO NOT write int main() ... 阅读全帖 |
|
c**s 发帖数: 159 | 44 不是local的,cst 6am == pst 2pm的skype电面。
没问背景,直接做题。
3个题:
(1) 旋转有序数组 没重复元素 找最小元
(2) 股票那个题,买一次,卖一次,求买进那天、卖出那天、最大利润
(3) batman, 就是n个人,batman认识所有人,所有人都不认识batman,返回batman
的id,没有的话返回-1。给了个api是bool AknowsB(int a,int b)
我说了思路 如果a认识b则b不可能是batman,否则a不可能是batman
然后写代码,一共写了3段,不断优化。
第一段,用了个vector标识是否可能是batman,再用一个vector 每次
收集为true的id。判断相邻两个是否认识,每次扔掉一半,直到剩下一个人,再喝所有
人检查一遍,决定输出他还是-1。时间复杂度O(n),空间复杂度O(n)。
他说代码可以写得简单点。
第二段,只用了一个vector a,每次判断a[0],a[1]是否认识,决定删掉谁。
他说可以O(1)空间。
第三段,保存一个可能... 阅读全帖 |
|
b*********h 发帖数: 103 | 45 看大家都用优先队列,贴一个 set 的吧 呵呵,用 C++ 的表示声明 priority queue
打字太多。。。继续说 C++ 的 priority queue 又不支持 decrease key,喜欢用 set
当 priority queue 。。。
class Solution {
public:
ListNode *mergeKLists(vector &lists) {
multiset S;
for (int i = 0; i < lists.size(); ++i) {
if (lists[i]) {
S.insert(lists[i]);
}
}
ListNode* head = 0;
ListNode* tail = 0;
while (!S.empty()) {
ListNode* nod... 阅读全帖 |
|
e******o 发帖数: 757 | 46 bool tossCoin();
bool randP(double P)
{
doule p0(0);
for ( int i=1; i!=N; ++i ) \\N根据double的精度,多少不记得了
{
if ( tossCoin() )
p0 += 1/pow(2,i);
if ( p0 > p ) return false;
if ( p - p0 ) > 1/pow(2,i) return true;
}
return false;
} |
|
d******e 发帖数: 164 | 47 贴个我写的:
bool isNumber(const char *s) {
while (isspace(*s)) s++;
if (*s == '+' || *s == '-') s++;
bool num = false;
while (isdigit(*s)) {
s++;
num = true;
}
if (*s == '.') {
s++;
while (isdigit(*s)) {
s++;
num = true;
}
}
if (*s == 'e') {
if (!num) return false;
s++;
if (*s == '+' || *s == '-') s++;
... 阅读全帖 |
|
d******e 发帖数: 164 | 48 贴个我写的:
bool isNumber(const char *s) {
while (isspace(*s)) s++;
if (*s == '+' || *s == '-') s++;
bool num = false;
while (isdigit(*s)) {
s++;
num = true;
}
if (*s == '.') {
s++;
while (isdigit(*s)) {
s++;
num = true;
}
}
if (*s == 'e') {
if (!num) return false;
s++;
if (*s == '+' || *s == '-') s++;
... 阅读全帖 |
|
j*****y 发帖数: 1071 | 49 二爷可否帮我看看这个 dp 的写法是不是有什么地方值得优化的 :)
class Solution {
public:
bool isScramble(string s1, string s2) {
int n = s2.length();
if(s1.length() != n)
{
return false;
}
bool dp[n][n][n]; // dp[i][j][k] means the scrambility of substring
of length k + 1 starting at s1[i] and s2[j];
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
for(int k = 0; k < n; ++k)
dp[i][j][k] = false;
for(in... 阅读全帖 |
|
j*****y 发帖数: 1071 | 50 多谢二爷,这是我的 recursive
class Solution {
public:
bool isScramble(const string &s1, int start1, int end1, const string &s2
, int start2, int end2)
{
string s = s1.substr(start1, end1 - start1 + 1);
string t = s2.substr(start2, end2 - start2 + 1);
sort(s.begin(), s.end());
sort(t.begin(), t.end());
if(s != t)
{
return false;
}
if(s1.substr(start1, end1 - start1 + 1) == s2.substr(start2, end2 -
start2 + 1))
{
... 阅读全帖 |
|