i**********e 发帖数: 1145 | 1 这个comment 每道题目都有,没人注意到吗?
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case. |
|
f*******t 发帖数: 7549 | 2 1. 我当年领到hp本,还不如thinkpad,太垃圾了。据说现在都不给13寸mbp了,真
cheap啊。木头桌看起来土,其实不便宜,这点倒是没省。问题是这东西完全没法reuse
,需要standing desk的人得按身高做个新的,他走人后那张桌子就废掉了,浪费严重
。fb全公司换上电动升降桌,想站就站想坐就坐,虽然这种桌子不便宜,我觉得相对更
划算一些。
2. 不知道是什么。
3. lol,狂奔还是很少见的。我觉得晚上急着回家吃饭的时候比较容易出现,早晨错过
就等下一班唄。
4. 反正我从来没热血沸腾的感觉,只想混日子。
5. 我觉得作为底层码工,大家这点常识还是有的。CEO再牛B,不给发钱发福利的公司
也不是长呆之地。
6. 本来想试一次,结果出现中餐外卖了。。。
7. AWS的表示除了搞点免费EC2,没见过免费书免费电影之类的东西。冒领160停车费得
多ws?反正我是没干过。
8. 呃,就算公司股价不涨,工资也没的涨啊。
9. oncall时不想在家呆着也只能在西雅图一只手都数得过来的质次价高的中餐馆吃个
饭,吃一半BB机响了的也不少见
10. 一两年闪人就别提那点股票了。 |
|
c*b 发帖数: 3126 | 3 1. 啥叫 还不如 thinkpad。。hp比thinkpad差远了,G/FB都有用thinkpad可以选。。
reuse |
|
b****w 发帖数: 71 | 4 1. Data Scientist是在Search Science & Traffic的组,要求如下:
Job Description
• Excellent understanding of computer science fundamentals, data
structures, and algorithms. Experience in Java, Python or C++ is a
must.
• Experience in data mining and machine learning techniques like
feature extraction, decision tree, additive models, EM algorithm, SVM, etc
is a must.
• Experience in applied statistics including hypothesis testing,
analysis of variance, model evaluation, forca... 阅读全帖 |
|
l****o 发帖数: 315 | 5 public class Solution {
public int candy(int[] ratings) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
int rLen = ratings.length;
if (rLen == 0) return 0;
int min = rLen; int give = 0;
int[] gives = new int[rLen];
for (int i = 1; i < rLen; i++) {
if (ratings[i] > ratings[i - 1]) give++;
else give = 0;
gives[i] = give;
}
give = 0;
for (int i = ... 阅读全帖 |
|
b******7 发帖数: 92 | 6 给了个笨方法,也过了oj。构造图,利用拓扑排序计算
class Solution {
public:
int candy(vector &ratings) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(ratings.empty()){
return 0;
}
if(ratings.size() == 1){
return 1;
}
int sum = 0;
int n = ratings.size();
vector candy(n, 1);
vector indegree(n, 0);
vector > edges(n, vector());
fo... 阅读全帖 |
|
l*****0 发帖数: 13 | 7 class Solution {
public:
bool getSum(vector &ratings, int &sum)
{
bool isReverse = false;
int n = ratings.size();
int* candy = new int[n];
memset(candy, 0, n*sizeof(int));
candy[0] = 1;
for (int i=1; i
{
if (ratings[i] > ratings[i-1])
candy[i] = candy[i-1] + 1;
else if (ratings[i] == ratings[i-1])
candy[i] = candy[i-1];
e... 阅读全帖 |
|
l*****0 发帖数: 13 | 8 class Solution {
public:
bool getSum(vector &ratings, int &sum)
{
bool isReverse = false;
int n = ratings.size();
int* candy = new int[n];
memset(candy, 0, n*sizeof(int));
candy[0] = 1;
for (int i=1; i
{
if (ratings[i] > ratings[i-1])
candy[i] = candy[i-1] + 1;
else if (ratings[i] == ratings[i-1])
candy[i] = candy[i-1];
e... 阅读全帖 |
|
h****g 发帖数: 105 | 9 刚做了下 Graph Clone, Output Limit Exceeded. 查了半天也没看出来哪里有问题,
附上代码,请大牛门挑挑毛病,谢拉!
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if (!node) return NULL;
UndirectedGraphNode * nodecopy;
queue que;
unordered_map map;
nodecopy=new UndirectedGraphNode(node->label);
que.push(n... 阅读全帖 |
|
P*******r 发帖数: 210 | 10 用DFS 和HashMap, 但是Time Limit Exceeded. 有什么改进的方法吗?
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(node == null)
return null;
Map hMap = new HashMap<
UndirectedGraphNode,UndirectedGraphNode>();
UndirectedGraphNode root = new UndirectedGraphNode(node.label);
hMap.put(node,root);
for(Undirecte... 阅读全帖 |
|
P*******r 发帖数: 210 | 11 用错递归了,还以为是没有优化。
多谢!
reused |
|
t******n 发帖数: 19 | 12 贴一个我的BFS版本, 通过OJ了.
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(!node) return NULL;
unordered_map valMap;
queue Q1;
queue Q2;
Q1.push(node);
UndirectedGraphNode *p_dup = new UndirectedGraphNode(node->label);
Undi... 阅读全帖 |
|
a******e 发帖数: 710 | 13 我改了一下你的代码,看一下注释
class Solution{
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node) return NULL;
UndirectedGraphNode * nodecopy;
queue que;
unordered_map map;
nodecopy=new UndirectedGraphNode(node->label);
que.push(node);
map[node]=nodecopy;
while(!que.empty()){
UndirectedGraphNode* current=que.front();
... 阅读全帖 |
|
|
l****o 发帖数: 315 | 15 public class Solution {
public boolean wordBreak(String s, Set dict) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
boolean[] f = new boolean[s.length() + 1];
f[0] = true;
for (int i = 0; i <= s.length(); i++)
for (int j = 0; j < i; j++)
if (f[j] && dict.contains(s.substring(j, i))) {
f[i] = true;
break;
}
return f[s.le... 阅读全帖 |
|
T*******e 发帖数: 4928 | 16 哈,咱们思路差不多。
bool wordBreak(string s, unordered_set &dict) { //Time: O(n^2)
int sSize=s.size();
if(sSize<=0||dict.empty()) return false;
vector isWord(sSize+1, false);
isWord[0]=true;
for(int i=1; i<=sSize; ++i) {
for(int j=i-1; j>=0; --j) {
if(dict.find(s.substr(j, i-j))!=dict.end() &&isWord[j]) {
isWord[i]=true;
break;
}
}
}
return isWord[sSize];
}
reused |
|
z*********8 发帖数: 2070 | 17 这个case老是fail在这个test case:
Submission Result: Runtime Error
Last executed input: "aaaaaaa", ["aaaa","aa"]
谁能帮我看看?
public class Solution {
public boolean wordBreak(String s, Set dict) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
boolean[] result = new boolean[s.length()+1];
result[0] = true;
int size = s.length();
for(int i=1; i <= size; ++i)
{
if(result[i] == false && di... 阅读全帖 |
|
j*********6 发帖数: 407 | 18 我是用DFS,和memorized 再加上 二爷指导的 pruning, 代码如下 不够感觉写得有些
麻烦 有什么问题请大家慷慨指出
顺便求教这种问题怎么分析时间复杂度
public class Solution {
public ArrayList wordBreak(String s, Set dict) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
// 1. get the min and max length of words in dictionary, used for
pruning.
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(String str : dict){
min = Math.min(min, str... 阅读全帖 |
|
m**********g 发帖数: 153 | 19 先dp, 然后dfs, 用dp信息剪枝.
class Solution {
void getSentence(string &s, int start, string sentence, vector &
output, unordered_set &dict, bool *breakable)
{
if(start == s.size()) {
sentence.erase(sentence.end() -1);
output.push_back(sentence);
return;
}
for(int i=start; i
if(breakable[i+1] == false) //prune here
continue;
string tmp=s.substr(start, i-start+1);
... 阅读全帖 |
|
s********u 发帖数: 1109 | 20 写了一下,觉得难点主要在回溯写这个path,如果维护不好,很容易出bug。
52ms通过。
class Solution {
public:
void backtracking( int end,const vector >&prev,const string
&s, vector &paths, string &path ){
if(end == -1){
paths.push_back(path);
return;
}
int prev_end;
string word;
const string local = path;
for(int i = 0; i < prev[end].size(); i++){
prev_end = prev[end][i];
... 阅读全帖 |
|
s*****n 发帖数: 994 | 21 dp + backtracking。 之前用了hashtable能过break word I,但是过不了II,可能是
string constructor太耗时了吧
class Solution {
public:
void backTracking (const string& s, vector >&prev, int index
, vector& output){
for (int i=0; i
int prev_index = prev[index][i];
vector prev_output(0);
if (prev_index != 0)
backTracking (s, prev, prev_index, prev_output);
else
prev_output = vector (1, "");
f... 阅读全帖 |
|
w**2 发帖数: 29 | 22 其实可以用 HashMap 保存从某个index出发到终点的DFS的所有可能路径。
代码写得糙 请包涵:
public class Solution {
public ArrayList wordBreak(String s, Set dict) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
ArrayList rl = new ArrayList();
if (s==null) return rl;
HashMap> hm = new HashMap
ArrayList>();
return reach(s,0,s.length()-1,dict,hm);
}
public ArrayList阅读全帖 |
|
g***j 发帖数: 1275 | 23 经典题目,就是一个linked list,每个节点有一个指针指向random的节点,然后呢,
copy这个list,
基本思路就是在每个节点后面插入一个同样的节点,然后设置好random的指针,最后把
这个list exact出来
举例: 1->2->3->4 其中 1->3 2->4
变成 1->1'->2->2'->3->3'->4->4' 其中 1'->3' 2'->4'
始终有runtime error, 被搞死了,谢了!
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
RandomListNode * p = head;
if(p == NULL) return NULL;
... 阅读全帖 |
|
g***j 发帖数: 1275 | 24 这个问题的基本思路就是交换,然后把合适的数放在合适的位置
比如 -1,3,1,4
交换之后便成 1,-1,3,4
代码如下
但是我有一个地方想不明白
A[i] != A[A[i] - 1]
这个是什么意思?
为什么不是 A[i] != i+1?
=======================================
int firstMissingPositive(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(n == 0) return 1;
for(int i = 0; i < n; i++){
while (A[i] >=1 && A[i] <=n && A[i] != A[A[i] - 1]) {
swap(&A[i], &A[A[i]-1]);
}
... 阅读全帖 |
|
c********p 发帖数: 1969 | 25 看到的最简洁的代码,大概是这个:
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
int sum = 0, total = 0, len = gas.length, index = -1;
for(int i=0; i
sum += gas[i]-cost[i];
total += gas[i]-cost[i];
if(sum < 0){
index = i;
sum = 0;
}
}
return total>=0 ? index+1 :... 阅读全帖 |
|
t**********r 发帖数: 2153 | 26 sum 用来找起始位置。total算总的油量和油耗。好象应该index+1% length
reused |
|
S********0 发帖数: 29 | 27 同求问题,本地是过的,runtime error, 上代码。
public class Solution {
public static RandomListNode copyRandomList(RandomListNode head) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(head == null) return null;
RandomListNode pointer, res_pointer;
pointer = head;
while(pointer != null){
RandomListNode temp = new RandomListNode(pointer.label);
temp.random = null;
temp.next = pointer.next;
... 阅读全帖 |
|
S********0 发帖数: 29 | 28 话不多说直接上代码,在本地测了没发现问题。。
谢谢.....
public static RandomListNode copyRandomList(RandomListNode head) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(head == null) return null;
RandomListNode pointer, res_pointer;
pointer = head;
while(pointer != null){
RandomListNode temp = new RandomListNode(pointer.label);
temp.random = null;
temp.next = pointer.next;
RandomListNode n... 阅读全帖 |
|
b**********5 发帖数: 7881 | 29 贴个我做的
public RandomListNode copyRandomList(RandomListNode head) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
HashMap m = new HashMap<
RandomListNode, RandomListNode>();
RandomListNode result = null;
RandomListNode prev = null;
while (head != null) {
RandomListNode headCopy; RandomListNode randomCopy;
if (m.containsKey(head)) {
headCopy = m.... 阅读全帖 |
|
s*w 发帖数: 729 | 30 不熟悉 Java, 看起来比较费劲,我再琢磨下
reused |
|
c********p 发帖数: 1969 | 31 你的prev干嘛用的?
为什么要纪录pre?
reused |
|
p****U 发帖数: 109 | 32 RandomListNode *copyRandomList(RandomListNode *head) {
// Note: The Solution object is instantiated only once and is reused by
each test case.
//first parse
if(!head)
return NULL;
RandomListNode *cur=head;
while(cur){
RandomListNode *tmp=cur->next;
cur->next=new RandomListNode(cur->label);
cur=cur->next;
cur->next=tmp;
cur=cur->next;
}
//second parse
cur=head;
RandomListNode *copy;
while(cur){
copy=cu... 阅读全帖 |
|
r*******n 发帖数: 3020 | 33 vector wordBreak(string s, unordered_set &dict) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
vector result;
int n = s.size();
vector> table(n, vector(n,false));
int max_len=0;
for(auto& word:dict){
if(max_len
max_len=word.size();
}
//DP
for(int i=0; i
for(int j=i;j阅读全帖 |
|
r*******n 发帖数: 3020 | 34 多谢,改了后过了OJ。
如你所说,我加了1-D bool table来记录有没有解
后面DFS根据这个如果没有解就停止递归搜索
把整个code贴下面
vector wordBreak(string s, unordered_set &dict) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
vector result;
int n = s.size();
vector> table(n, vector(n,false));
int max_len=0;
for(auto& word:dict){
if(max_len
max_len=word.size();
}
... 阅读全帖 |
|
s*****n 发帖数: 994 | 35 class Solution {
public:
double median(int A[], int m){
if (m%2) return A[m/2];
else return 0.5*(A[m/2-1]+A[m/2]);
}
double median(int A[], int m, int val){//m>=2
if (m%2){
if (val
else if (val==A[m/2]) return val;
else return 0.5*(min(A[m/2+1],val)+ A[m/2]);
}else{
if (val < A[m/2-1]) return A[m/2-1];
else if (val > A[m/2]) r... 阅读全帖 |
|
b***e 发帖数: 1419 | 36 Here’s a pretty extreme way of looking at it:
Every problem has a brute-force solution, that is, to search the space of
all the possible solutions. Usually the space of all the possible solutions
is a tree like structure, where starting from the root, there’s a bunch of
possibilities branching out, and on and on.
DP is still searching the full space, traversing all the nodes in the
possible solution space. The only thing it does better than naive brute-
force is that, DP would reuse the search... 阅读全帖 |
|
r******j 发帖数: 92 | 37 下面是是我的dfs解法,可是过不了test,哪位能帮我看一下错在哪里了吗。
public class Solution {
public void solve(char[][] board) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
int row = board.length;
if (row == 0) {
return;
}
int column = board[0].length;
for (int i = 0; i < row; i++) {
dfs(board, i, 0);
dfs(board,i, column - 1);
}
for (int j = 1; j < column - 1; j++) {
d... 阅读全帖 |
|
g***j 发帖数: 1275 | 38 class Solution {
如下的code可以过,但是有个疑问,为啥,我吧下面的long long的部分改成int,就过
不了了啊?
public:
int divide(int dividend, int divisor) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(divisor == 1) return dividend;
if(divisor == -1) return -1*dividend;
int sign = 1;
if(dividend < 0) sign = -1*sign;
if(divisor < 0) sign = -1*sign;
long long remain = abs((long long)dividend); // ???
... 阅读全帖 |
|
t******d 发帖数: 1383 | 39 说是runtime error,有一个很大的case没过去。runtime error是说我的index过了界
么?还是说运行速度不够?请帮我看看。另外,reach+1改成 reach++,是一样的么
?把reach加了1之后的值传给新call的isEnough.我知道++reach,肯定是和reach+1等
效可以的。
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
for (int i = 0; i < gas.length;i++) {
if (isEnough(gas, cost, i, 0, i))
return i;
}
return -1;
... 阅读全帖 |
|
t******d 发帖数: 1383 | 40 说是runtime error,有一个很大的case没过去。runtime error是说我的index过了界
么?还是说运行速度不够?请帮我看看。另外,reach+1改成 reach++,是一样的么
?把reach加了1之后的值传给新call的isEnough.我知道++reach,肯定是和reach+1等
效可以的。
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
for (int i = 0; i < gas.length;i++) {
if (isEnough(gas, cost, i, 0, i))
return i;
}
return -1;
... 阅读全帖 |
|
e*******s 发帖数: 1979 | 41 Copy List with Random Pointer
A linked list is given such that each node contains an additional random
pointer which could point to any node in the list or null.
Return a deep copy of the list.
下面注释里面是别人写的能跑过的 上面的是我的 到现在都改得几乎一模一样了
可是还是超时
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomList... 阅读全帖 |
|
w**2 发帖数: 29 | 42 从版上获益不少,昨天G的店面很不理想,也发下面经吧
阿三面试官到点了 还说要几分钟弄好麦克风。
问了一下bst的性质 就让我写个validate bst的 函数
我想这不是原题吗?就把之前写过的敲了一遍。
public boolean isValidBST(TreeNode root) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(root==null) return true;
int[] last = new int[1];
last[0]=Integer.MIN_VALUE;
return isValidBST(root,last);
}
public boolean isValidBST(TreeNode root, int[] last){
if (root.left!=null){
if... 阅读全帖 |
|
w*****e 发帖数: 931 | 43 如果只有一个root节点且值为MIN_VALUE岂不是输出false?
reused |
|
w**2 发帖数: 29 | 44 从版上获益不少,昨天G的店面很不理想,也发下面经吧
阿三面试官到点了 还说要几分钟弄好麦克风。
问了一下bst的性质 就让我写个validate bst的 函数
我想这不是原题吗?就把之前写过的敲了一遍。
public boolean isValidBST(TreeNode root) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(root==null) return true;
int[] last = new int[1];
last[0]=Integer.MIN_VALUE;
return isValidBST(root,last);
}
public boolean isValidBST(TreeNode root, int[] last){
if (root.left!=null){
if... 阅读全帖 |
|
w*****e 发帖数: 931 | 45 如果只有一个root节点且值为MIN_VALUE岂不是输出false?
reused |
|
s********u 发帖数: 1109 | 46 没太明白你为什么要reverse+补0。联想Add Binary那道题就可以知道,这题是字符串
不是链表,字符串可以从后往前访问,所以完全可以不补0,直接从后往前访问。当某
一个字符串用光时,再处理另外一个字符串剩下的(注意进位),最后再看有没有多余
的一个进位。
毕竟你string是可以一直"push_front"的
Leetcode原题:
class Solution {
public:
string addBinary(string a, string b) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int aIndex = a.size() - 1, bIndex = b.size() - 1;
string res;
int carr... 阅读全帖 |
|
k******4 发帖数: 94 | 47 貌似加个dummyHead还是挺好写的
ListNode *insertionSortList(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ListNode dummyHead(INT_MIN);
ListNode *p = head;
while(p != NULL)
{
ListNode *pre = &dummyHead;
while(pre->next != NULL && pre->next->val < p->val)
pre = pre->next;
ListNode *temp = p->next;
p->nex... 阅读全帖 |
|
b********6 发帖数: 97 | 48 那我也贴个java的好了
public ListNode insertionSortList(ListNode head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if (head == null) return null;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode i = head;
while (i.next != null) {
int n = i.next.val;
ListNode j = dummy;
while (j!=i&&j.next.val < n){
... 阅读全帖 |
|
b*********s 发帖数: 115 | 49 非大牛
我是在最开始加一个dummy head
public class Solution {
public ListNode insertionSortList(ListNode head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ListNode dummy = new ListNode(Integer.MIN_VALUE);
dummy.next = head;
ListNode preNode = dummy;
ListNode curNode = dummy.next;
while (curNode != null) {
ListNode pre = dummy;
ListNode cur = dum... 阅读全帖 |
|
g*********e 发帖数: 14401 | 50 用了一个dummy node, 记得用完删掉
class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == NULL)
return NULL;
ListNode *dummy=new ListNode(0);
dummy->next=head;
head=head->next;
dummy->next->next=NULL;
while(head) {
ListNode *cur=head;
head=head->next;
... 阅读全帖 |
|