m**q 发帖数: 189 | 1 同样,当检查一个新的点时,需要把x-h之前的点从map中去掉,
其实可以用一个map存[x-h,x]中的点,key是y坐标。在向map中
添加一个新的点时,先检查map里是否有y坐标相同的点,有则删除;
同时把x-h之前的点依据它们的y坐标,把它们从map中删除。
总复杂度不超过O(nlgn)。
#include
#include |
|
n*******w 发帖数: 687 | 2 1. binary search tree
first() 从左子树一直往下,到左子树为null,返回node。
next() 没有parent指针,找中序遍历successor挺麻烦。写个递归都感觉繁琐。
思路是,先top-down,找到当前node,再bottom-up,找到successor。
下面用Java写的。假设没有duplicate keys。不然会更麻烦。
tmp = null;//tmp存储结果辅助用的。cur是当前node。
Node next(Node cur, Node root){
//find cur node, and check its right child is null or not
//if null, return null
if(root == cur){
if(root.right != null)
return first(root.right);
else
return null;
}
//tmp stores the result, if tmp is null, root... 阅读全帖 |
|
p*****2 发帖数: 21240 | 3 我昨天写了个N的,不过还没写完。没有考虑到不能被N整除的情况。
int Reverse(Node** head, int n)
{
Node* tmphead=null;
int i=0;
Node* tmp=*head;
Node* first=tmp;
while(tmp)
{
if(i==0)
first=tmp;
i++;
if(i==n)
{
tmp->next=tmphead;
tmphead=first;
i=0;
}
tmp=tmp->next;
}
if(i!=0)
{
return -1;
}
*head=tmphead;
return 0;
}
int ReverseList(Node** head, int n)
{
if(head==NULL || *head==NULL || n<0)
return -1;
if(n==0 || n==1)
return 0;
if(!... 阅读全帖 |
|
S**I 发帖数: 15689 | 4 ☆─────────────────────────────────────☆
recursive (递归) 于 (Mon Apr 11 10:56:49 2011, 美东) 提到:
大半夜收到HR的thank you note。不用管什么NDA了
本人ECE fresh PhD,背景是电路/EDA,跟G业务基本没什么关系
同学内部推荐的,很简单的一次电面就给了onsite
题都不难,但是自己没把握好机会,出了一些小bug。
总的感觉,出错就是硬伤,宁可从最简单的算法写起,也不能出错。
电面:
1,Skip list, http://en.wikipedia.org/wiki/Skip_list
写code实现struct skip_list * find(struct skip_list *head, int value)
2,sorted array with repeated elements
for given element, find out its range.
e.g. A A B B B B B C C D D E F G, given B, the out... 阅读全帖 |
|
c********t 发帖数: 5706 | 5 研究了一下你的codes, 很不错。不过最后的时候这句
find(result.begin(), result.end(), tmp)
又给复杂度*一个result.size(), 比如你给的例子[3, 6,2, 7, 4, 5]。result就是n^2
级的排列组合。
仔细又想了想,你试试把最后改成下面的,看行不行
for (j = begin; j <=end; j++){
if (twoSum[j].index1<=twoSum[i].index2) continue;
vector tmp;
tmp.push_back( num[ twoSum[i].index1] );
tmp.push_back( num[ twoSum[i].index2] );
tmp.push_back( num[ twoSum[j].index1] );
tmp.push_... 阅读全帖 |
|
c********t 发帖数: 5706 | 6 研究了一下你的codes, 很不错。不过最后的时候这句
find(result.begin(), result.end(), tmp)
又给复杂度*一个result.size(), 比如你给的例子[3, 6,2, 7, 4, 5]。result就是n^2
级的排列组合。
仔细又想了想,你试试把最后改成下面的,看行不行
for (j = begin; j <=end; j++){
if (twoSum[j].index1<=twoSum[i].index2) continue;
vector tmp;
tmp.push_back( num[ twoSum[i].index1] );
tmp.push_back( num[ twoSum[i].index2] );
tmp.push_back( num[ twoSum[j].index1] );
tmp.push_... 阅读全帖 |
|
c***e 发帖数: 542 | 7 print in lexi order, here is my code which passed both small and large tests:
class Solution {
public:
vector > permuteUnique(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector > allPerm;
sort(num.begin(), num.end());
allPerm.push_back(num);
if(num.size() < 2)
return allPerm;
vector tmp=num;
while(true)
{
... 阅读全帖 |
|
c***e 发帖数: 542 | 8 print in lexi order, here is my code which passed both small and large tests:
class Solution {
public:
vector > permuteUnique(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector > allPerm;
sort(num.begin(), num.end());
allPerm.push_back(num);
if(num.size() < 2)
return allPerm;
vector tmp=num;
while(true)
{
... 阅读全帖 |
|
h****n 发帖数: 1093 | 9 第一题,随手写了DFS和BFS,请指教
TreeNode* GetDeepestNode(TreeNode* root)
{
TreeNode* res=NULL;
int curLevel=1,preLevel=0;
DFSHelper(root, res, curLevel, preLevel);
//BFSHelper(root, res);
return res;
}
//Time O(n) space 不算递归栈则O(1) 算的话 O(lgn)
void DFSHelper(TreeNode* root, TreeNode* &res, int & curLevel, int &
preLevel)
{
if(root)
{
if(curLevel>preLevel)
{
res = root;
preLevel++;
}
curLevel++;
DFSHelper(root->right, ... 阅读全帖 |
|
t********y 发帖数: 14 | 10 DP
public List> CombinationSum(int[] input, int value)
{
if (value <= 0) return null;
Array.Sort(input);
List>[] tmp = new List>[value + 1];
tmp[0] = new List>();
for (int i = 1; i <= value; i++)
{
List> res = new List>();
for (int j = 0; j < input.Length; j++)
{
if (i - input[j] > 0... 阅读全帖 |
|
j*****y 发帖数: 1071 | 11 这是最后的 code
vector plusOne(vector &input)
{
if(input.size() == 0)
{
return vector(1, 1);
}
int remainder = 1;
for(int i = input.size() -1 ; i > -1; --i)
{
int tmp = input[i] + remainder;
if(tmp >= 10)
{
input[i] = tmp - 10;
remainder = 1;
}
esle
{
input[i] = tmp;
remainder = 0;
break;
}
}
if(remainder != 0)
{
vector tmp(1, remainder);
input.insert(input.begin(),... 阅读全帖 |
|
q****m 发帖数: 177 | 12 我的这个为什么超时了呢? 斜率我用一对整数(x, y)表示,而且让 x>= 0.斜率(0,y
)比其他的斜率都大。斜率(x1, y1) 比(x2, y2) 小的话意味着
y1/x1 < y2/x2, 转化成等价的形式 y1 * x2 < y2 * x1
/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
static bool compare(const Point &a, const Point &b)
{
if(b.x == 0)
{
return true;
}
return a.y * b.x < a.x * b.y;
}
int maxPoints(vect... 阅读全帖 |
|
s******r 发帖数: 21 | 13 用递归,基本思路是,
考虑第i位的数字,取得值可以是0-9;唯一要注意的是最后一位数字,如果5没有出现
过,只能取5.
public class Solution {
public void str5(String number, String prefix, int length, int pos,
boolean has5) {
if (pos >= length) {
System.out.println(prefix);
} else if (pos < length - 1 || has5) {
for (int i = 0; i <= 9; i++) {
String tmp = prefix + i;
if (Integer.parseInt(tmp) > Integer.parseInt(number.
substring(0, pos + 1))) {
... 阅读全帖 |
|
s******r 发帖数: 21 | 14 用递归,基本思路是,
考虑第i位的数字,取得值可以是0-9;唯一要注意的是最后一位数字,如果5没有出现
过,只能取5.
public class Solution {
public void str5(String number, String prefix, int length, int pos,
boolean has5) {
if (pos >= length) {
System.out.println(prefix);
} else if (pos < length - 1 || has5) {
for (int i = 0; i <= 9; i++) {
String tmp = prefix + i;
if (Integer.parseInt(tmp) > Integer.parseInt(number.
substring(0, pos + 1))) {
... 阅读全帖 |
|
g***j 发帖数: 1275 | 15 为啥这个可以过? 下面这个时间复杂度是 O^3 吧?
class Solution {
public:
vector > fourSum(vector &num, int target) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
vector tmp;
vector> results;
if(num.empty()) return results;
sort(num.begin(), num.end());
for(int i=0; i
{
for(int j=i+1; j
{
int temp ... 阅读全帖 |
|
a***e 发帖数: 413 | 16 我照你说的写了一个,还是比较长,但的确要清楚了很多。请问你能share一个你的么
?多谢!
我感觉如果不是特别熟,再加上现场一紧张或者发晕,很难把这么多行程序在15分钟内
bugfree的写完,汗。
vector > zigzagLevelOrder(TreeNode *root) {
queue nextLevel;
queue curLevel;
TreeNode *r = root;
vector > ret;
if (r==NULL)
return ret;
curLevel.push(r);
bool l2r=true;
while(1)
{
vector tmpV;
wh... 阅读全帖 |
|
I**********s 发帖数: 441 | 17 来个20行的。
vector fullJustify(vector &words, int L) {
vector res;
for(int i = 0, k, l; i < words.size(); i += k) {
for (k = l = 0; i + k < words.size() && l + words[i+k].size() <=
L - k; k++) {
l += words[i+k].size();
}
string tmp = words[i];
for (int j = 0; j < k - 1; j++) {
if(i + k >= words.size()) tmp += " ";
else tmp += string((L - l) / (k - 1) + (j < (L -... 阅读全帖 |
|
r********7 发帖数: 102 | 18 抛个砖~ 自己写了一个, stack 然后 backtracking。 其实写完感觉stack多此一
举,不想改了。 请大神们无视
public class Solution {
public void printCombo(List> input){
if (input == null || input.size()==0){
return;
}
Stack> stc = new Stack>();
for (int i=0; i
stc.push(input.get(i));
}
List> res = new ArrayList>();
while(!stc.isEmpty()){
List ... 阅读全帖 |
|
y*******d 发帖数: 1674 | 19 LC 39
code 如下
两处不明白
1. 为什么要先排序?
2. 为什么helper(res, target-candidates[i], tmp, candidates, i); 是i不是
i+1??
多谢指点
public List> combinationSum(int[] candidates, int target) {
List> res = new ArrayList>();
if (candidates == null || candidates.length == 0) {
return res;
}
Arrays.sort(candidates);
helper(res, target, new ArrayList(), candidates, 0);
return res;
}
void ... 阅读全帖 |
|
a*********0 发帖数: 41 | 20 select mdate, team1, sum(score1), team2, sum(score2) from (SELECT mdate,
team1,
CASE WHEN teamid=team1 THEN 1 ELSE 0 END score1,
team2,
CASE WHEN teamid = team2 THEN 1 ELSE 0 END score2
FROM game left outer JOIN goal ON matchid = id) tmp
group by tmp.mdate, tmp.team1,tmp.team2
order by tmp.mdate, tmp.team1,tmp.team2
我在外面多加了个select 在里面求sum。 得分为0的没有漏洞而且答案看起来没什么问
题,只是没有得到笑脸 |
|
y****e 发帖数: 1012 | 21 set term postscript eps color blacktext "Helvetica" 24
set output 'p1.eps'
set autoscale # scale axes automaticallyun
set label # remove any previous labels
set xtic auto # set xtics automatically
set ytic auto # set ytics automatically
set title "I/O Traffic"
set xlabel "Interval(5s)"
set ylabel "Data(KB)"
set key 0.01,10000
set xr [0:100]
set yr [0:10000]
plot "p1.tmp" using 1:2 title 'r0' ... 阅读全帖 |
|
r********s 发帖数: 179 | 22 [6:19pm] echo $SHELL
/bin/tcsh
[6:20pm] alias prsth 'echo "It Works"'
[6:20pm] cat > xixi.csh
#!/bin/csh
prsth
[6:21pm] chmod +x xixi.csh
[6:21pm] ./xixi.csh
prsth: Command not found.
[6:21pm] cat > xixi.csh
#!/bin/csh
alias prsth 'echo "It Works"'
prsth
[6:28pm] ./xixi.csh
It Works
**********
From the above test, we can see this might be the case touchsoul got stuck in.
i.e., even when you are using csh/tcsh, yo |
|
f*****h 发帖数: 228 | 23 给你贴了吧,大家想用都可以
#!/usr/bin/perl -w
#############################################
#Author: Jiang Li
#email: r**********[email protected]
#Creat Time: Tue 23 Oct 2012 01:37:54 PM CDT
#Vanderbilt Center for Quantitative Sciences
#############################################
use strict;
use warnings;
=pod
=head1 SYNOPSIS
Given a genbank format file (.gb), parse its feature parts (mRNA feature to
get exon regions) to get information like transcript id, gene name, etc.,
and store the result in gtf format
=he... 阅读全帖 |
|
m***e 发帖数: 810 | 24 济安线在同花顺软件里没有,需要自己定义公式,公式在网上很容易找到,同时贴在下面。XS我找到了。周末再学习学习。
//N 2 100 30
//M 1 15 3
AA:=ABS((2*CLOSE+HIGH+LOW)/4-MA(CLOSE,N))/MA(CLOSE,N);
济安线:DMA((2*CLOSE+LOW+HIGH)/4,AA),LINETHICK1,colorFFFFFF;
CC:=(CLOSE/济安线);
MA1:=MA(CC*(2*CLOSE+HIGH+LOW)/4,3);
MAAA:=((MA1-济安线)/济安线)/3;
TMP:=MA1-MAAA*MA1;
//J:IF(TMP<=济安线,济安线,DRAWNULL),LINETHICK3,colorcyan;
PARTLINE(济安线,TMP<=济安线,RGB(0,255,255),1,RGB(255,0,255)),LINETHICK3;
A:TMP,LINETHICK1,coloryFFFFFF;
//X:IF(TMP<=济安线,TMP,DRAWNULL),LINETHICK2,colorgreen;
PARTLINE |
|
b********h 发帖数: 119 | 25 用两个stack
void nonrec_postorder()
{
stack stk1;
stack stk2;
if(root != NULL)
stk1.push(root);
while(!stk1.empty()) {
Node* tmp = stk1.top();
stk1.pop();
stk2.push(tmp);
if(tmp->left != NULL)
stk1.push(tmp->left);
if(tmp->right != NULL)
stk1.push(tmp->right);
}
while(!stk2.empty()) {
printf("%d ", stk2.top()->ke |
|
g**e 发帖数: 6127 | 26 /**
Maintain two pointers, head and tail,
1. start sweeping the array from head if a[head]
otherwise
2. for each a[i], if current value is less or equal to a[head] (or a[tail]),
skip
3. else compute area with a[i] & a[tail] (or a[head]), set start=i (or stop=
i), update max volume if necessary
4. until head meets tail, return max volume
All items are scanned at most once, total time O(n).
*/
public static int maxVolume(int[] a) {
if (a.length<=1)
return ... 阅读全帖 |
|
r******n 发帖数: 170 | 27 在Log中找用户最多的3连击问题,有最后结论吗?
Userid PageID
A 1
A 2
A 3
B 2
B 3
C 1
B 4
A 4
找出最常用的3个访问序列:
对于用户A:1-2-3, 2-3-4
用户B:2-3-4
2-3-4 是最常见的
我大致想想,是这么做的,建一个map >用来扫描log文件
,每当针对一个usr_id,list size达到3时,把这个三链接做成t_hits结构,作为key放
到另外一个map里存起来,然后把list头的第一个pageID去掉,
这样子扫描完毕后,第二个map里就是三连击的counter map.
但是要找到最大的三连击,还得再扫描一次第二个map。觉得效率不够高?关键map_t_
hits里面的value还可能在变化,不然是个静态的,直接往个heap里插,最后顶上就是
最大的了。
大致结构如下,欢... 阅读全帖 |
|
p*****2 发帖数: 21240 | 28 我写了一个,有什么问题吗?
static void Merge(int[] arr, int start, int mid, int end)
{
int[] tmp = new int[arr.Length];
Array.Copy(arr, start, tmp, start, end - start + 1);
int left = start;
int right = mid + 1;
int current = start;
while (left <= mid)
{
if (right>end || tmp[left] <= tmp[right])
arr[current++] = tmp[left++];
else
arr[current++] = tmp[right++];
}
} |
|
p*****2 发帖数: 21240 | 29 这是全部的代码
static void MergeSort(int[] arr)
{
if (arr == null || arr.Length == 1)
return;
Sort(arr, 0, arr.Length - 1);
}
static void Sort(int[] arr, int start, int end)
{
if (start >= end)
return;
int mid = (start + end) / 2;
Sort(arr, start, mid);
Sort(arr, mid + 1, end);
Merge(arr, start,end);
}
static void Merge(int[] arr, int start,int end)
{
int[] tmp = new int[arr.Length];... 阅读全帖 |
|
b******g 发帖数: 1721 | 30 相同是说不光结构相同,而且每个对应的value也相同。根据peking2 和 wwzz建议,我
给个全的,也是个大概,可能边角没有考虑。
int matchRoot(Tree t1, Tree t2)
{
Queue q1=new Queue();
q1.enqueue(t2);
int size=0;
while(q1.size()){
Tree tmp=q1.dequeue();
if(!match(t1,tmpTree)){
if(!tmp.left)
q1.enqueue(tmp.left);
if(!tmp.right)
q1.enqueue(tmp.right);
}else{
size=tmp.size();
}
}
return size;
}
match(Tree t1, Tree t2){
if(t1.value=t2.value)
... 阅读全帖 |
|
I*******l 发帖数: 203 | 31 Update: fix some boundary condition checks
recursively call a function Node* cc(Node* root) to find out where to insert an extra node, where cc checks left subtree first and then right subtree to find a non-full subtree. It returns NULL if both subtrees are full.
Node* cc(Node* root)
{ // make sure that leaf node return NULL
if (root == NULL)
return NULL;
// first check if root has both children, if not return root
if (root->left != NULL && root->right == NULL)
return root;... 阅读全帖 |
|
h*****3 发帖数: 1391 | 32 好像carreer cup150上有这题?我写的最后一个test case没通过,我觉的好像他的
test case有问题,但我的思路应该没问题。
//这个子程序可以忽略不计
void swap (int &i,int &j){
int tmp;
tmp = i;
i = j;
j = tmp;
}
void permitUniq(vector &num,int res[],int lev,vector > &
list,int start)
{//res- buffer一个solution, lev - res 中的index, list- keep all
solutions,start- num中的起始位置
int prev;
vector tmp;
if(lev == num.size()){//找到一个solution, 存起来啊
tmp.assign(res,res+lev);
list.push... 阅读全帖 |
|
h*****3 发帖数: 1391 | 33 好像carreer cup150上有这题?我写的最后一个test case没通过,我觉的好像他的
test case有问题,但我的思路应该没问题。
//这个子程序可以忽略不计
void swap (int &i,int &j){
int tmp;
tmp = i;
i = j;
j = tmp;
}
void permitUniq(vector &num,int res[],int lev,vector > &
list,int start)
{//res- buffer一个solution, lev - res 中的index, list- keep all
solutions,start- num中的起始位置
int prev;
vector tmp;
if(lev == num.size()){//找到一个solution, 存起来啊
tmp.assign(res,res+lev);
list.push... 阅读全帖 |
|
f**********t 发帖数: 1001 | 34 我的方法如下:
int divide(int dividend, int divisor) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (divisor == 0)
return 0;
int sign = 1;
if (dividend * divisor < 0)
sign = -1;
if (dividend < 0)
dividend *= -1;
if (divisor < 0)
divisor *= -1;
int result = 0;
int tmp;
int tmpret;
while (dividend >= divisor) {
tmp = diviso... 阅读全帖 |
|
q****m 发帖数: 177 | 35 难道是因为用了 std::sort ?
class Solution {
public:
vector > threeSum(vector &num) {
vector > *set = new vector >;
vector a(num);
sort(a.begin(), a.end());
vector b(num);
for(int k = 0; k < a.size(); ++k)
{
int c = a[k];
for(int j = 0; j < b.size(); ++j)
{
b[j] = -c - a[j];
}
int i = 0;
int j = b.size() -1 ;
while(i < a.size() && j > -1)
... 阅读全帖 |
|
q****m 发帖数: 177 | 36 优化了以后,终于可以 pass large 了. 思路是先对 num 排序. 排序后 num[i] + num
[j] + num[k] = 0中只用考虑 i < j < k的情况
class Solution {
public:
vector > threeSum(vector &num) {
vector > *set = new vector >;
sort(num.begin(), num.end());
vector b;
b.reserve(num.size());
for(int k = 0; k < num.size(); ++k)
{
int c = num[k];
for(int j = 0; j < k; ++j)
{
b[j] = -c - num[j];
}
int i = 0;
... 阅读全帖 |
|
q****m 发帖数: 177 | 37 难道是因为用了 std::sort ?
class Solution {
public:
vector > threeSum(vector &num) {
vector > *set = new vector >;
vector a(num);
sort(a.begin(), a.end());
vector b(num);
for(int k = 0; k < a.size(); ++k)
{
int c = a[k];
for(int j = 0; j < b.size(); ++j)
{
b[j] = -c - a[j];
}
int i = 0;
int j = b.size() -1 ;
while(i < a.size() && j > -1)
... 阅读全帖 |
|
q****m 发帖数: 177 | 38 优化了以后,终于可以 pass large 了. 思路是先对 num 排序. 排序后 num[i] + num
[j] + num[k] = 0中只用考虑 i < j < k的情况
class Solution {
public:
vector > threeSum(vector &num) {
vector > *set = new vector >;
sort(num.begin(), num.end());
vector b;
b.reserve(num.size());
for(int k = 0; k < num.size(); ++k)
{
int c = num[k];
for(int j = 0; j < k; ++j)
{
b[j] = -c - num[j];
}
int i = 0;
... 阅读全帖 |
|
t********y 发帖数: 14 | 39 adding a null to the end of each level can be helpful. following is printing
out each level. making link list is just same, creating a new list each
time a null is Dequeued. Tail null is taken care automatically.
public void PrintOutLevel(TreeNode node)
{
// null,check.....
Queue q = new Queue();
q.Enqueue(node);
q.Enqueue(null); // This is the tail of the first levle.
while (!(q.Count == 0))
... 阅读全帖 |
|
l****c 发帖数: 782 | 40 好久没写了,我瞎写了一个,recursion的大致是这个思路吧?
void string_per(vector> result, vector tmp, int index,
char* s, int len)
{
if (index = len)
result.push_back(tmp);
else {
tmp.push_back(s[index]);
string_per(result, tmp, index+1, s, len);
tmp.pop_back();
string_per(result, tmp, index+1, s, len);
}
}
|
|
x*****0 发帖数: 452 | 41 (2) C++ code:
void split_by_whitespace(string& s, vector& result)
{
stringstream ss(s);
string cur;
while(ss >> cur)
result.push_back(cur);
}
void maxi_assonance(vector& x)
{
vector::iterator it = x.begin();
vector > r;
map key_ind;
for(; it!=x.end(); ++it)
{
string cur = *it;
if(cur[0]=='a' || cur[0]=='e' ||
cur[0]=='i' || cur[0]=='o' ||
cur[0]=='u')
{ ... 阅读全帖 |
|
n*******s 发帖数: 482 | 42 validation string DP (2d)
M[CurrentLength, Last2Index]
Last2Index:
0: AA
1: AB
..
8: CC
M[i+1, 0 - 9] is depends on M[i, 0 - 9]
也可以用两个数组pre, current来简化
pre=[1,1,1,1,1,1,1,1,1]
for(j = 3 to n)
cur= [0]*9
for i = 0 to 8
tmp = pre[i] +1
switch(i)
case 0 :
cur[0]+= tmp // AA -> AAA
cur[1]+= tmp // AA => AAB
cur[2]+= tmp // AA => AAC
break;
case... 阅读全帖 |
|
f******n 发帖数: 53 | 43 根据c++ 64位整数的设定,题目的10位hash编码可以用2^5为底数,包括0-31
可以包括26个字母,这样编码解码代码如下,unsigned long long 是64位
不会越界,但是unsigned long和int相同,不足50位会导致越界,hash代码如下
unsigned long long rollinghash(string s)
{
unsigned long long res=0;
for(int i=0; i < 10; i++)
{
unsigned long long tmp = s[i] - 'a';
if (tmp >= 0 && tmp <= 25)
{
tmp = tmp << (i*5);
res |= tmp;
}
}
return res;
} |
|
a***e 发帖数: 413 | 44 其他两种preorder和inorder都很快写出来了,这种写到这步还是不对。明早上再做算
了,不然又睡不好。
vector postorderTraversal(TreeNode *root) {
vector ret;
if (root==NULL)
return ret;
stack st;
st.push(root);
TreeNode *r = root->left;
TreeNode *lastVisitedNode=root;
while(1)
{
while(r)
{
st.push(r);
r=r->left;
}
if (st.empty())
break;
TreeN... 阅读全帖 |
|
k****r 发帖数: 807 | 45 这道题当时c++时简单的就实现了,但是类似的,我用java就不对,具体代码是这样的
public class Solution {
public int sumNumbers(TreeNode root) {
int result = 0;
int tmp = 0;
sumHelp(root, tmp, result);
return result;
}
private void sumHelp(TreeNode root, int tmp, int sum){
if (root == null) return;
if (root.left == null && root.right == null){
int newone = tmp + root.val;
sum = sum + newone;
}
if (root.right != null) sumHelp(root.right,... 阅读全帖 |
|
C****t 发帖数: 53 | 46 Q1.
def swap(nums):
n = len(nums)
for i in range(n):
tmp = origIdx(i, n//2)
while tmp < i: tmp = origIdx(tmp, n//2)
nums[i], nums[tmp] = nums[tmp], nums[i]
print(nums)
def origIdx(cur, n):
return (cur%2)*n + cur//2 |
|
c*****n 发帖数: 95 | 47 这题可以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 &&... 阅读全帖 |
|
s*******r 发帖数: 2697 | 48 sln 1: brute force
calculate cost for every possible rectangle and return the maximum rectangle
within budget.
complexity: O(n^6)
O(n^4) rectangles and calc cost for each rectangle is O(n^2)
sln2: O(n^4)
1)fix left and right column of matrix (O(n^2) possibilities)
2)for each fixed left and right column
a)for each row, calc the cost from left to right, as tmp[i] O(n^2)
b)given 1D array tmp[], find the longest sub-array within budget, can be
done in O(n)
complexity: O(n^4)
sln3: O(n^3)
in sl... 阅读全帖 |
|
F***Q 发帖数: 6599 | 49
because my desktop is that linux box. I am tired of windows, it is too
restrictive. I am sure with cygwin/mingw, you can do this as well.
here is my script:
-----------------------------------------------------------------------
#!/bin/sh
lynx -dump "http://boston.craigslist.org/search/sss?query=pentax" \
| grep -i 'pentax' | grep '\[' | sed -e 's/\[[0-9]*\]//g' |head -6 \
> /tmp/craigslist.today
NEWITEM=`diff /tmp/craigslist.today /tmp/craigslist.yesterday | wc -l`
if [ $NEWITEM != "0" ]; then... 阅读全帖 |
|
y*y 发帖数: 427 | 50 最新更新02/19/2011
PBO专用1.43 v2通用固件已经发布。新买的带官方固件的PBO都可以直接刷squashfs格式的。老的bootcode<18的可能必须要硬刷yaffs格式的。以前刷成P61-Beta Megapack bootcode neutral可能需要清空ROM,TTL硬刷。
此版本会重启两次。所以要耐心等待(保险15分钟以上)。有些机型开始会遥控失灵,因而卡在主菜单。保证至少等待如上时间后,硬开关电源启动,应该就可以正常使用了。
http://www.hdpfans.com/thread-7980-1-1.html
以下原文就可以忽略,不用折腾了。
***********************************************************************************
01/01/2011
----------------------------------------------------
本供略提供P61-Beta Megapack bootcode neutral及其中文在线电影电视播放的修改。
因为所选的... 阅读全帖 |
|