u***l 发帖数: 51 | 1 用 python 做的,如果用O(n^2) 的方法做,发现下面 a)的code可以过,但是 b)会超时
https://oj.leetcode.com/problems/longest-palindromic-substring/
a 和 b 中只有 function f 不一样,请问这两个到底是哪里产生的不同?谢谢。
OJ 显示超时的例子是 "aaaa...(很长) b(在靠中间的位置) aaaa(很长)"
###########################
a)
class Solution:
# @return a string
def longestPalindrome(self, s):
def f(s, p, q): # find longest palindrome centered at p and q
while p >= 0 and q < len(s) and s[p] == s[q]:
p -= 1; q += 1
return s[p + 1 :... 阅读全帖 |
|
i*****f 发帖数: 578 | 2 题目看错了。。。以为是最大化node个数。。。
这里是新的解,借鉴了wwwar3的解,而且应该可以handle树不complete的情况。牛牛们
看看还有什么为题没有?(测试及测试结果在最后面)
'''
Tree node is a 3-tuple (value, left child, right child)
'''
D = {}
def MaxPathSum(N):
'''
Find the path from N to a leaf that maximize the sum from N to the leaf.
Returns [leaf, sum]
'''
assert N!=None
if D.has_key(N): return D[N]
if not N[1] and not N[2]: return [N, N[0]]
r = [None, float('-inf')]
if N[1]:
r = MaxPathSum(N[1])
if N[2]:
r =... 阅读全帖 |
|
S**I 发帖数: 15689 | 3 ☆─────────────────────────────────────☆
Bayesian1 (Jason) 于 (Tue Jun 21 01:52:31 2011, 美东) 提到:
Given a binary tree, find 2 leaf nodes say X and Y such that F(X,Y) is
maximum where F(X,Y) = sum of nodes in the path from root to X + sum of
nodes in the path from root to Y - sum of nodes in the common path from root
to first common ancestor of the Nodes X and Y
☆─────────────────────────────────────☆
SecretVest (Secret Vest) 于 (Tue Jun 21 04:01:30 2011, 美东) 提到:
not hard if someone is used... 阅读全帖 |
|
h*****3 发帖数: 1391 | 4 喔,那我理解错了,我还以为是array里面有1个数重复3遍呢。
1)那这个xor以后,res1;
2) 比如说最后一个bit是1的话,再XOR扫描一遍最后一个bit是1的数,如果结果和前面
一次的相等,那就是1,1,1,如果不等,那有个数就找出来了。设为a
3)a xor res1,那其他2个数的差别就出来了。
4)如果是1,1,1的话,就找res1中的下个1,再repeat 2);如果res1中没1了,bit为
0的话,不就是有2个0,一个1,或者2个1,一个0吗?xor一遍该位为1的,如果为1,那
就是第一种情况,如果为0,那就是第二种情况,再xor一遍该位为0的,反正能找出一
个数出来。然后repeat 3 就可以了。 |
|
w********n 发帖数: 4752 | 5 It's my codes below. Any other better solutions?
class Solution:
# @param version1, a string
# @param version2, a string
# @return an integer
def compareVersion(self, version1, version2):
v1=version1.split('.')
v2=version2.split('.')
size1=len(v1)
size2=len(v2)
size=max(size1,size2)
res1=0
for i in range(size1):
res1 += 10**(size-i)*int(v1[i])
res2=0
for i in range(size2):
res2 += 10**... 阅读全帖 |
|
I*****a 发帖数: 5425 | 6 你这个不算是吧。
n = 1000 # training size
ntest = 1000 # test size; make this big only for illustration
id.train = 1:n
id.test = (n + 1):(n + ntest)
ratio = 0.99
n0 = round(n * ratio)
n1 = n - n0
nsimu = 100
res = NULL
for (i in 1:nsimu){
p = c(runif(n0, 0, 0.5), runif(n1, 0.5, 1), runif(ntest, 0.6, 1) )
y = sapply(p, function(x){rbinom(n = 1, size = 1, prob = x)})
x = log(p / (1 - p)) # beta is c(0, 1)
dat = data.frame(x = x, y = y)
f... 阅读全帖 |
|
e********e 发帖数: 12 | 7 用merge的方法写了下。
template
T findKthOf2SortedArray1(T P[], int s, T Q[], int t, int k)
{
// assume: 1 <= k <= (s+t)
int i=0, j=0;
for (int l = 0; l < k; l++){
if (i == s) j++;
else if (j == t) i++;
else {
if (P[i] < Q[j]) i++;
else j++;
}
}
if (i == 0) return Q[j-1];
else if (j == 0) return P[i-1];
else return max(P[i-1], Q[j-1]);
}
template
T medianOfTwoSortedArray3(T P[], int s, T Q[], int... 阅读全帖 |
|
d****o 发帖数: 1055 | 8 我完成了一个版本,可以过small test,但是过不了large test。
1. 请问我的版本的时间复杂度是多少?
2. 哪位有更好得解法,请教一下。
bool wordSearch(vector > &board, string word, int level,
vector >& used, int curRow, int curCol)
{
if(curRow<0||curRow>=board.size()||curCol<0||curCol>=board[0].size()
){
return false;
}
if(used[curRow][curCol])
return false;
if(board[curRow][curCol]!=word[level])
return false;
if(level==word.siz... 阅读全帖 |
|
a**d 发帖数: 64 | 9 第一题的java答案抄了一个,运行通过的。
https://github.com/nagajyothi/InterviewBit/blob/master/DynamicProgramming/
AvgSet.java
各路大神看看还有没有更优解。
// Sum_of_Set1 / size_of_set1 = total_sum / total_size, and so, Sum_of_Set1
= size_of_set1 * total_sum / total_size.
// Now we can just iterate on size_of_set1, and we would know the required
Sum_of_Set1.
static List res = new ArrayList();
static boolean[][][] dp;
public static List> partitionAverage(int[] num) {
List阅读全帖 |
|
c*********o 发帖数: 471 | 10 http://wallisranch.com/
Here is their Pre-sale pricing.
Fielding:
Res1: 1809 sqft, $975K
Res1(alt): 1877 sqft, $995K
Res2: 2069 sqft, $1,019K
Res3: 2258 sqft, $1,049K
Res4: 2685 sqft, $1,139K
Driftsong:
Plan 1: 2210-2233 sqft $998,880 to $1,021,880
Plan 2: 2264-2310 sqft $1,008,880 to $1,039,880
Plan 3: 2775-2796 sqft $1,061,880 to $1,108,880
Rumor 据说新的 East Dublin 高中将在这里 |
|
l********a 发帖数: 1154 | 11 用正则吧
#! /urs/bin/env python
import re
s = '''
Mailing Address
573 JIE FANG RD. N.
GUANGZHOU F4 00000
Business Address
573 JIE FANG RD. N.
... 阅读全帖 |
|
|
a*******y 发帖数: 1040 | 13 如果res1中没1了,bit为
这句不对,应该是三个0或者1,1,0,
还有一种情况就是,你都试过了所有的位置了都不行,那么就是这三个数都是一样的 |
|
l*******b 发帖数: 2586 | 14 反转整个链表貌似好写点, 反转 m, n需要有几个边角情况, leetcode上的解法挺好呀.
val c = List(1,2,3,4,5)
def rev(x:List[Int], t:List[Int]) : List[Int] = {
if (x.tail == Nil) return x ::: t
else rev(x.tail, x.head :: t)
}
rev(c, Nil)
得到
res1: List[Int] = List(5, 4, 3, 2, 1) |
|
l******g 发帖数: 188 | 15 get the total number of unique lines across a data set of 1000 gzipped text
files.
for instance: If every file has two lines, "this is line1" and "this a
line2", then the total count of lines is 2000, and total number of unique
lines is 2.
1 1000 machines where each machine has one gzipped text file with an
approximate size of 50GB. The file on each machine is /0/data/foo.txt.gz
2 1000 machines are named data1, data2,..data1000.
3 Data format ASCII text.
4 we have 11 machines named res1, res2…r... 阅读全帖 |
|
l******g 发帖数: 188 | 16 get the total number of unique lines across a data set of 1000 gzipped text
files.
for instance: If every file has two lines, "this is line1" and "this a
line2", then the total count of lines is 2000, and total number of unique
lines is 2.
1. 1000 machines where each machine has one gzipped text file with an
approximate size of 50GB. The file on each machine is /0/data/foo.txt.gz
2. 1000 machines are named data1, data2,..data1000.
3. Data format ASCII text.
4. we have 11 machines named res1, re... 阅读全帖 |
|
a********m 发帖数: 15480 | 17 这比较没太大意义呀,虽然halo3确实还是很牛的。mw2是不是超过halo 1+2+3了?呵呵
。
还有不知道咋算的。如果uncharted 1+2 4.5m的话, gow3现在vg上面写2.5m. res1+2+
kz2只有1m? 还有gow3最后3-3.5m怎么也会有的。
3 |
|
c******o 发帖数: 1277 | 18 Warning: boring stuff next.太长了,不知道有人看没有。
monad不是一个数据结构,它是一个“类型“的一个“类型”的一个“类型”。(
higher order type)
举例
type:
String
first order type:
List[X], can be List[String], List[Int], List[Double]
higher order type :
Monad, Can be List[X], Try[X], Option[X]
严格来说,Monad是一个高阶的类型,一个“类型“的一个“类型”的一个“类型”
他的定义就是一个类型F,如果能在之上定义
def unit[A](a: => A): F[A]
def flatMap[A,B](ma: F[A])(f: A => F[B]): F[B]
这个类型就是一个monad.
比如List
def unit[A](a: => A): List[A]
给一个x,我能用unit做一个一个元素的List: (x)
scala> List("a")
res0: List[String]... 阅读全帖 |
|
e***i 发帖数: 231 | 19 呵呵
scala> val arr = 5 to 12
arr: scala.collection.immutable.Range.Inclusive = Range(5, 6, 7, 8, 9, 10,
11, 12)
scala> arr.take(2).drop(2)
res0: scala.collection.immutable.Range = Range()
scala> arr.slice(2,5)
res1: scala.collection.immutable.IndexedSeq[Int] = Vector(7, 8, 9)
scala> val b = Array(1,2,3,4,5,6,7)
b: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)
scala> b.take(2).drop(2)
res2: Array[Int] = Array()
scala> b.slice(2,5)
res3: Array[Int] = Array(3, 4, 5) |
|
l*******n 发帖数: 13 | 20 the procedures deal with count data, the raw dataset should be aggregated
before analysis if it is too large. for example
A B RESPONSE
1 1 0
1 1 0
1 0 1
1 0 0
.....
TO
A B RES0 RES1
1 1 SUM(0) SUM(1)
...
PROC GENMOD IS BEST FOR LOG LINEAR MODEL (multiple responses), can also be
used to fit logit model, allowing predictor variables
PROC FREQ gives simple statistics to represent the association, not for real
-sense modeling |
|