由买买提看人间百态

topics

全部话题 - 话题: vals
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
g*****s
发帖数: 1288
1
来自主题: JobHunting版 - 上道有意思的题
debugged code. The other solutions are not complete. Any solution shorter
than this one is wrong.
int maxRepeatedNumber(int A[], int n, int k)
{
assert(k <= n);
int i;
//replace values with occurrence
for(i = 0; i < n; i++)
{
int val = A[i];
if(val < n) //not touched
{
A[i] = (val+1)*n; //initialization, means occurrence of i is
0
//put val
for(;;)
{
int cur = A[val];
... 阅读全帖
j*******t
发帖数: 223
2
sedgewick的书里有讲过很简单的trie。
public class TrieST {
private static final int R = 256; // extended ASCII
private Node root = new Node();
private static class Node {
private Object val;
private Node[] next = new Node[R];
}
public boolean contains(String key) {
return get(key) != null;
}
public Value get(String key) {
Node x = get(root, key, 0);
if (x == null) {
return null;
}
return (Value) x.val;
}
... 阅读全帖
j*******t
发帖数: 223
3
sedgewick的书里有讲过很简单的trie。
public class TrieST {
private static final int R = 256; // extended ASCII
private Node root = new Node();
private static class Node {
private Object val;
private Node[] next = new Node[R];
}
public boolean contains(String key) {
return get(key) != null;
}
public Value get(String key) {
Node x = get(root, key, 0);
if (x == null) {
return null;
}
return (Value) x.val;
}
... 阅读全帖
w**2
发帖数: 29
4
来自主题: JobHunting版 - G家电面
从版上获益不少,昨天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**2
发帖数: 29
5
来自主题: JobHunting版 - G家电面
从版上获益不少,昨天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... 阅读全帖
f**********3
发帖数: 295
6
居然破天荒一次通过了... 但是觉得if else用得太多?不够clean,请问可以怎样改进?
public class Solution {
public ListNode insertionSortList(ListNode head) {
if (head == null || head.next ==null) return head;
ListNode p1=head, p2=head.next;
ListNode p3=head, p4=head.next;
while(p2!=null) {
if (p1.val<=p2.val) {
p1 = p1.next;
p2 = p2.next;
} else {
p1.next = p2.next;
if (p2.val ... 阅读全帖
h**o
发帖数: 548
7
来自主题: JobHunting版 - 关于用STL实现LRU cache
先前自己写的Double link list通过了。听说用STL的list<>简单, 实现了一下,怎么
就说超时那? 请问把node移到头可以用: dll.remove(n); dll.push_
front(n)吧? 我看很多例子用splice.不知道是否这个原因。 一下是我的代码:麻烦
哪位给指点一下:
class Node{
public:
Node(int key=-1, int val=-1):key(key), val(val){}
int key;
int val;
};
class LRUCache{
public:
LRUCache(int capacity) {
_capacity = capacity;
_entry = new Node[_capacity];
_pos = 0; //其实不需要
}
~LRUCache() {delete[] _entry; }

int get(i... 阅读全帖
a*********4
发帖数: 7
8
来自主题: JobHunting版 - 一道面试题
a java solution:
import java.util.*;
public class FindNonDupeSubTree {
private class Node{
int val;
Node left;
Node right;
Node (int val){
this.val = val;
left = null;
right = null;
}
}

public boolean FindSubTree(Node root, Set nodeSet, ArrayList<
Integer> validTreeRoots){
if (root == null)
return true;
Set sleft = new HashSet();
Set ... 阅读全帖
a*********4
发帖数: 7
9
来自主题: JobHunting版 - 一道面试题
a java solution:
import java.util.*;
public class FindNonDupeSubTree {
private class Node{
int val;
Node left;
Node right;
Node (int val){
this.val = val;
left = null;
right = null;
}
}

public boolean FindSubTree(Node root, Set nodeSet, ArrayList<
Integer> validTreeRoots){
if (root == null)
return true;
Set sleft = new HashSet();
Set ... 阅读全帖
P**********r
发帖数: 755
10
来自主题: JobHunting版 - 贡献Google 电面面经
struct TreeNode {
string val;
TreeNode *left;
TreeNode *right;
TreeNode(string x): val(x), left(NULL), right(NULL) {}
};
void getexp(TreeNode *root, string &result)
{
if(root->val == "+" || root->val == "-" || root->val == "*" || root->val
== "/")
{
result += '(';
getexp(root->left,result);
result += root->val;
getexp(root->right, result);
result += ')';
}
else
{
result +=root->val;
}
}
string expressiontree(... 阅读全帖
s*w
发帖数: 729
11
来自主题: JobHunting版 - [leetcode] merge k lists 求助
不知道为啥,我的 heap 时灵时不灵的?请看下面的 code 和输出显示
#include
#include
#include
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int v):val(v),next(NULL) {}
};
bool minHeapPredicate(ListNode* lhs,ListNode* rhs) {
return lhs->val > rhs->val;
}
class Solution {
public:
ListNode *mergeKLists(vector lists) {
ListNode *retHead = NULL, *retTail = NULL;
// store values and nodes into heap
vector ... 阅读全帖
H****S
发帖数: 1359
12
来自主题: JobHunting版 - 请教L家生成H2O水分子的题。
kinda remember someone talked about this in his blog. Here is the idea,
import java.util.concurrent._
import scala.collection.mutable
val hq = mutable.Buffer[Condition]
val oq = mutable.Buffer[Condition]
val lock = new ReentrantLock()
def h() {
lock.lock()
try {
if (hq.size >= 1 && oq.size >= 1) {
val ch = hq.last
ch.signal()
val co = oq.last
oh.signal()
hq.trimEnd(1)
oq.trimEnd(1)
println("h")
} else {
val ch = lock.newCond... 阅读全帖
j**********0
发帖数: 20
13
来自主题: JobHunting版 - G的一道考题
public static class BinaryNode{
BinaryNode leftChild;
BinaryNode rightChild;
int val;
public BinaryNode(int val) {
super();
this.val = val;
}

}

public int find(BinaryNode node, int from, int to){
int[] inRangeSubTree=new int[1];
find(node, from, to, inRangeSubTree);
return inRangeSubTree[0];
}

public boolean find(BinaryNode node, int from, int to, int[]
inRangeSubTree){
... 阅读全帖
T******7
发帖数: 1419
14
写了一个很丑的code.
求拍。
public class uniValueTree {
public int countUniTree(TreeNode root){
if(root.left == null && root.right == null) return 1;

return helper(root);
}

private int helper(TreeNode node){
if(node.left == null && node.right == null){
return 1;
}
if(node.left == null){
if(isUni(node.right)){
if(node.val == node.right.val){
return 1 + helper(node.right);
... 阅读全帖
m******0
发帖数: 222
S*******C
发帖数: 822
16
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
public class LRUCache {
private final int capacity;
private ConcurrentLinkedQueue queue;
private ConcurrentHashMap map;
public LRUCache(final int capacity) {
this.capacity = capacity;
this.queue = new ConcurrentLinkedQueue();
this.map = new ConcurrentHashMap(capacity);
}
//Check whether the items exists in the cache. Returns n... 阅读全帖
B*****g
发帖数: 34098
17
来自主题: Database版 - Oracle 11g坑爹还是公司DBA坑爹
前面recursive和distinct就不提了. 看下面这个:
WITH a AS (
SELECT 1 ID, 'A' VAL FROM DUAL
UNION ALL
SELECT 2, 'A' FROM DUAL),
b AS (
SELECT 2 AS ID FROM DUAL
)
SELECT a.id, b.id
FROM a, b
WHERE a.val LIKE '%A%'
AND a.id = b.id(+);
下面这些都对:
WITH a AS (
SELECT 1 ID, 'A' VAL FROM DUAL
UNION ALL
SELECT 2, 'A' FROM DUAL),
b AS (
SELECT 2 AS ID FROM DUAL
UNION
SELECT 2 AS ID FROM DUAL
)
SELECT a.id, b.id
FROM a, b
WHERE a.val LIKE '%A%'
AND a.id = b.id(+);
WITH a AS (
SELECT 1 ID, 'A' VAL FROM DUAL
UNION ALL
SELECT ... 阅读全帖
g*********s
发帖数: 1782
18
来自主题: Programming版 - 经典题atoi的溢出处理 (转载)
this is my solution casting an unsigned int to an int. but gcc gives me
warning:
string_api.cpp: In function ‘bool overflow(unsigned int, bool, char)’:
string_api.cpp:330: warning: integer overflow in expression
string_api.cpp: In function ‘int str2int(const char*)’:
string_api.cpp:346: warning: integer overflow in expression
explict type casting can fix the warning. but i don't like this solution
very much. anyone has a better alternative?
static
bool overflow(unsigned int val, bool negative, c... 阅读全帖
c*******d
发帖数: 353
19
来自主题: Investment版 - fidelity 401k 求投资建议
I can take moderate risk, I seek moderate risk growth in 401k, here are my
first 401k fund selections. Another question I would like to ask is if I
should merge my 2nd 401k into my first. Funds I already own is marked by *.
Thank you!
Fund name, inception date, 1Y, 3Y, 5Y, 10Y, lifetime, As of Date return
rate
Stock Investments
Large Cap
ABF LG CAP VAL INV
Inception Date 08/01/1994 -0.55 17.78 -1.86 5.11 7.75
01/31/2012
EATON LG CAP VALUE A
Inception Date 09/23/1931 ... 阅读全帖
g*****x
发帖数: 799
20
来自主题: JobHunting版 - 问一个算法题
void rangeBinarySearch(const vector &vec, const int val, int &range_beg
, int &range_end)
{
range_beg = -1;
range_end = -1;
int beg = 0;
int end = vec.size();
int mid;
while(beg < end)
{
mid = (end - beg) / 2 + beg;
if(vec[mid] == val && (mid == 0 || vec[mid - 1] < val))
{
range_beg = mid;
break;
}
if(vec[mid] >= val)
end = mid;
else
beg = mid + 1;
}
if(range_b... 阅读全帖
y******5
发帖数: 43
21
来自主题: JobHunting版 - 两个有点难度很有意思的题
Level 1: A(val: 1)
Level 2: F(val: 2), B(val: 2), C(val:3)
Level 3: X(val:3), Y(val:4), Z(val:5)
So, the maximum value is 5, and we backtrack from Z.
L***Q
发帖数: 508
22
assert(s)应该不对,s为null应该返回0.
const char *p = s;必要不大,可以直接在s上操作,已经是const char*了。俺贴一下
俺的,在http://www.leetcode.com/onlinejudge上测试通过了。
思路比较简单,在判断overflow上借鉴了ihasleetcode大牛用long long的方法。
- step1:NULL string和空string的判断
- step2:跳过最初的空格,判断符号
- step3:读取字符直到非数字。用long long保存中间结果,和INT_MAX和INT_MIN比较
。这样比较简单。
int atoi(const char* str)
{
if ((str == NULL) || (*str == '\0'))
return 0;
bool sign = false;
while (*str == ' ')
str ++;
if (*str == '-')
{
sign = true;
... 阅读全帖
l***i
发帖数: 1309
23
来自主题: JobHunting版 - 问一道老题
use hellobruce's idea, here is code
assume input is a[N] and difference is K
typedef pair pii;
// small keeps track of min while p1 is moving to right
// large keeps track of max while p1 is moving to right
deque small, large;
int p1, p2;
int ans=0, curr=0;
small.clear(); large.clear();
for(p1=p2=0; p2 {
int val = a[p2];
while(!small.empty() && val > small.front().first + K)
{
p1=small.front().second +1;
small.pop_front();
}
while(!large.empty() && val < ... 阅读全帖
p********s
发帖数: 37
24
来自主题: JobHunting版 - 这道题到底是应该怎么做的?
插入删除都只要用修改表示就可以了,再就一个范围查询,具体如下
不大明白“某个维度”是指啥,如果问题转化为一维了,只要对于每条对角线建立一个
这样的数据结构就行了。
树状数组http://baike.baidu.com/view/1420784.htm
好像又叫胜者树,其实就是外排序k路归并里用的那个数据结构,很好实现
val[0][]表示原始数据,存着第0..n-1个元素
val[i+1][j]表示val[i][j*2]和val[i][j*2+1]中胜出者(这里比如是更大的那个)的
index
所以最高层val[logn][0]就是0..n-1里面的最终胜出者
设m为小于等于k的最大的2的倍数,则
查询:0..k-1中最大的数就是递归查询(0..m-1)和(m..k)中最大的数,O(logn)
修改:修改val[0][k]的值,并沿着一路修改其祖先的值,O(logn)
初始化:逐层建树,O(n)
这题里对于一条对角线,val[0][]的index就表示dj,值的话如果(cj,dj)进入集合里就
修改为dj,不在就修改为-1。这样如果查询0..bi中的最大的那个元素,就可以获得目
前集合... 阅读全帖
h****n
发帖数: 1093
25
来自主题: JobHunting版 - 请教这么一个题:BST maximum sum path
贴一个通过OJ的版本,能处理全部负数的情况
楼主的版本很牛逼,不过有个大的bug,就是递归的结束条件没法达到,因为调用前判
断是不是指针为空了,不为空才调用,但是需要为空的时候才能达到递归结束条件
g才能正确的自底向上
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxPathSum(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int res;
int g;

res = ... 阅读全帖
l********t
发帖数: 878
26
Judge small 全对
Judge large, 一共92个test case,有90个是对滴,
其中一个错误的是这个test case
{9,6,-3,#,#,-6,2,#,#,2,#,-6,-6,-6}
还有一个太长,不写了。
我自己编译运行结果是16,也是正确答案。上OJ非说我的结果是15...
leetcode,或者哪位大牛给看看?谢谢啊!
/**
* Definition for binary tree */
#include
#include
#include
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int maxValue;
int maxPathSum(TreeNode... 阅读全帖
b***m
发帖数: 5987
27

Perl的变量是灵活的,不用声明类型,比如如下声明:
my Val = "I love you";
Val += Val;
print Val;
Val = 100;
Val *= 100;
print Val;
输出结果是
I love youI love you
10000
如何在内存中实现和管理。
e****e
发帖数: 418
28
我来个Recursion code,欢迎指正:
public void print(TreeNode n1, TreeNode n2) {
if ( n1 == null && n2 == null )
return;
else if ( n1 == null && n2 != null )
print(n2);
else if ( n1 != null && n2 == null )
print(n1);
else {
print(n1.left, n2.left)
if( n1.val > n2.val) {
println(n2.val);
println(n1.val);
} else{
println(n1.val);
println(n2.val);
}
print(n1.right, n2.right)
}
}
private void print(TreeNode n) {
if ( n == null )
return... 阅读全帖
p*****2
发帖数: 21240
29
我写了一个,练了练。主要是输出的时候有几个小地方要注意。
def fullJustify(words:Array[String], L:Int)={
process(0)

def process(i:Int):Unit={
val j=getNext(i,0)
if(i {
printSlice(words.slice(i,j))
process(j)
}
}

def printSlice(words:Array[String]):Unit={
if(words.size==1) {println(words(0));return}

val total=words.map(_.length()).sum
val spaces=... 阅读全帖
c****7
发帖数: 13
30
来自主题: JobHunting版 - 请教各位大牛一个K-way merge 的问题
原题是leetcode上的merge k sorted lists
http://discuss.leetcode.com/questions/204/merge-k-sorted-lists
这个题有好几种解法,比如divide and conquer, priority queue.
我写了个priority queue的。我的问题是, 如果现在要做 merge k sorted
array,怎样来设计这个priority queue才能最简单有效呢?
附上 merge-k-sorted-lists的代码
public static ListNode mergeKLists_priorityQueue(ArrayList kLists)
{
// border case
if(kLists.size()==0) return null;
if(kLists.size()==1) return kLists.get(0);
// create a Comparator based on the nod... 阅读全帖
l*******b
发帖数: 2586
31
来自主题: JobHunting版 - 对scala很失望
这个循环的测试有些意思,在linux下for比while慢1倍。在windows下20倍。
不过用functional的写法比前两个都快很多
val start = System.currentTimeMillis()
var total = 0
for(i <- 0 until 100000) total += i
val end = System.currentTimeMillis()
println(end-start)
println(total)
val ss = System.currentTimeMillis()
var tt = 0
var i = 0
while(i < 100000) { i = i + 1; tt += i}
val ee = System.currentTimeMillis()
println(ee-ss)
println(tt)
def f(x: Int, sum: Int): Int = if(x == 0) sum else f(x - 1, sum + x)
val mm = System.currentTimeMillis()
val ... 阅读全帖
l*******b
发帖数: 2586
32
来自主题: JobHunting版 - 对scala很失望
研究了一下IO确实占主要部分,计算部分改成functional慢很多
100000个字符的输入下
Reading: ~20
Time: ~350
IO: ~1400
最开始循环的办法看起来最好 Time: ~30
for和while的区别也不大
object test2 extends App {
val beforeRead = System.currentTimeMillis
val s = readLine
val start = System.currentTimeMillis
val ss = s.zipWithIndex.partition(_._1 != 'l')
val a = ss._1.map(_._2 + 1).mkString("\n") +
ss._2.reverseMap(_._2 + 1).mkString("\n")
val end = System.currentTimeMillis
println(a)
val ioend = System.currentTimeMillis
println("... 阅读全帖
I**********s
发帖数: 441
33
最喜欢 wwwyhx的解法: recursive descent.
我也写了个, 用的是建造AST, 再evaluate AST. 应该相当完整了: 数字之前可以有+-
号, 中间可以有一个小数点. 数字和运算符号之间可以有空格. 可以使用+,-,*,/,^,以
及括号. 可以检测错误输入并报出错误位置. 就是比较长, 不适合面试用. 供大家参考.
#include
#include // for pow()
using namespace std;
struct Node {
double val;
char op;
int op_prec; // precedence of operator
int type; // 0 - operand, 1 - operator
Node * left;
Node * right;
Node(double _val, char _op, int _type) : val(_val), op(_op),
type(_type), lef... 阅读全帖
I**********s
发帖数: 441
34
最喜欢 wwwyhx的解法: recursive descent.
我也写了个, 用的是建造AST, 再evaluate AST. 应该相当完整了: 数字之前可以有+-
号, 中间可以有一个小数点. 数字和运算符号之间可以有空格. 可以使用+,-,*,/,^,以
及括号. 可以检测错误输入并报出错误位置. 就是比较长, 不适合面试用. 供大家参考.
#include
#include // for pow()
using namespace std;
struct Node {
double val;
char op;
int op_prec; // precedence of operator
int type; // 0 - operand, 1 - operator
Node * left;
Node * right;
Node(double _val, char _op, int _type) : val(_val), op(_op),
type(_type), lef... 阅读全帖
p*****2
发帖数: 21240
35
来自主题: JobHunting版 - 发现一个很恶心的基础问题
merge=(node1, node2)->
return node2 if !node1
return node1 if !node2
node1.right=merge(node1.right,node2)
return node1
deleteNode=(root, val)->
if(root.val==val)
root=merge(root.left,root.right)
else if(val root.left=deleteNode(root.left,val)
else
root.right=deleteNode(root.right,val)
return root
b******7
发帖数: 92
36
来自主题: JobHunting版 - 透露两个G的onsite题
ListNode * add(ListNode * a, ListNode * b)
{
if(a == NULL || b == NULL) return NULL;
ListNode * head = NULL;
ListNode * pre = NULL;
ListNode * preLess9 = NULL;
for(; a != NULL; a = a->next, b = b->next)
{
ListNode * temp = new ListNode(a->val, b->val);
if(pre != NULL)
pre->next = temp;
else
head = temp;
... 阅读全帖
w********g
发帖数: 106
37
来自主题: JobHunting版 - 小公司面经
knowledge题和coding都很常见,就不说了。
但是有个题我不知道考点是什么:
已给出全局变量
struct val {
char *s;
};
struct val input[100];//已事先存好100个val
struct val output[100];//已事先存好100个val,但都是任意赋值
char buffer[足够大];
要求写两个函数
InputToBuffer(void *buffer, struct val input)
BufferToOutput(struct val output, void *buffer)
第一个函数把input放到buffer里,第二个函数反之。
要求把两个函数依次执行完之后,input和output相等。
题目虽然稍微有些繁琐,但是其实意思很明确。
原题特意说过不必考虑内存不够用、字符串与结构体不valid的情况。
我不明白这题考点是什么,谁给我讲讲?
我的做法就是定义一个char *b = (char *)buffer,
然后两个函数里分别
memcpy(b, input[i], size+1)
memcpy(o... 阅读全帖
s******n
发帖数: 20
38
来自主题: JobHunting版 - a leetcode problem: 重建BST
这是Leetcode上的一篇文章:
leetcode.com/2010/09/saving-binary-search-tree-to-file.html
里面的重建BST代码我觉得有问题:
void readBSTHelper(int min, int max, int &insertVal,
BinaryTree *&p, ifstream &fin) {
if (insertVal > min && insertVal < max) {
int val = insertVal;
p = new BinaryTree(val);
if (fin >> insertVal) {
readBSTHelper(min, val, insertVal, p->left, fin);
readBSTHelper(val, max, insertVal, p->right, fin);
}
}
}
void readBST(BinaryTree *&root, ifstream &fin) {... 阅读全帖
s*******s
发帖数: 1031
39
来自主题: JobHunting版 - leetcode的2sum
我的代码:
struct itemRecord {
int index;
int val;
itemRecord(int i, int v): index(i), val(v) {}
};
bool itemRecordLess(itemRecord i1, itemRecord i2) {
return i1.val < i2.val;
}
class Solution {
public:
vector twoSum(vector &numbers, int target) {
vector result;
int n = numbers.size();

vector numRecords;
for(int i=0; i numRe... 阅读全帖
z*******3
发帖数: 13709
40
来自主题: JobHunting版 - leetcode 129
这是新的代码
建 route 的时候,不从字典里面找,而是挨个替换字符串的char,换成其他char,组
建成新字符串,然后再去看字典里有没有,有的话,放到set里去,再把set放到map里
,每个pair表示从key string出发,能够转换的点的集合
然后从start开始找,用linkedlist,不用queue,queue是二叉树,效率低,用
linkedlist快,bfs都用linkedlist,先进先出,linkedlist的pollfirst效率高
然后建立一个类,node,这个node记录当前的string,以及上一个string,还有当前
路径长度
然后方法主体,每次把当前string对应的点取出来,然后看能走到那些点,挨个对比这
些点,看是不是end string,如果是,则表示找到,那就记录当前长度,因为还有可能
有相同长度的解,然后继续,如果不是end string,则查找visitedmap,看是否访问过
,如果访问过,比较当前路径长度跟上次访问的长度,如果上次访问的长度更短,则把
set里面这个node给移除,要不然会产生死循环,这里还要避开并发修改的问题,... 阅读全帖
z****e
发帖数: 54598
41
来自主题: JobHunting版 - word ladder ii 谁给个大oj不超时的?
public
class Solution {
public ArrayList> findLadders(String start, String end
, HashSet dict) {
// Start typing your Java solution below
// DO NOT write main() function
dict.add(start);
dict.add(end);
HashMap> routs = new HashMap >();
this.generateRouts(dict, routs);
ArrayList> result = new ArrayList >>();
LinkedList阅读全帖
i********s
发帖数: 22
42
不知道理解得是否正确,
如果存在多个值等于val,则返回in-order遍历最先找到的这个节点
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
};
bool find(TreeNode* root, TreeNode*& ret, int val) {
if (root == NULL) {
return false;
}
if (root->left != NULL && find(root->left, ret, val)) {
return true;
}
if (root->val == val) {
ret = root;
return true;
}
if (root->right != NULL && find(root->right, ret, val)) {
return... 阅读全帖
f**********e
发帖数: 288
43
来自主题: JobHunting版 - 请教一道, leetcode题.
我也贴一个, 但最复杂的case没过, 有空在查查. 请大家指正.
public class LRUCache {

int capacity;
LinkedList list = new LinkedList();
HashMap map = new HashMap();

public LRUCache(int capacity) {
this.capacity = capacity;
}

public int get(int key) {

if(!map.containsKey(key))
return -1;
CacheEntry c = map.get(key);
int val = c.val;
moveToHead(c);
return va... 阅读全帖
f****e
发帖数: 923
44
class ListNode{
int val;
ListNode next;
ListNode(int val){
this.val = val;
}
}
class Solution{
public int live(int n, int k){
ListNode head = new ListNode(1);
ListNode node = head;
for(int i = 2; i <= n; i++){
node.next = new ListNode(i);
node = node.next;
}
node.next = head;
System.out.println("before remove");
printList(head);
System.out.println("removing the nodes");
whi... 阅读全帖
y**b
发帖数: 10166
45
来自主题: Programming版 - 形参可以直接使用私有数据成员?
class HasPtr {
public:
HasPtr(const int &p, int i): ptr(new int(p)), val(i) {}
HasPtr(const HasPtr &orig):
ptr(new int (*orig.ptr)), val(orig.val) { }
HasPtr& operator=(const HasPtr&);

~HasPtr() { delete ptr; }
int get_ptr_val() const { return *ptr; }
int get_int() const { return val; }
void set_ptr(int *p) { ptr = p; }
void set_int(int i) { val = i; }
int *get_ptr() const { return ptr; }
void set_ptr_val(int p) const { *ptr = p; }

... 阅读全帖
h****i
发帖数: 254
46
haha, try this:
int floor(float f) {
int fi = *((int*)(&f));
int exp = ((fi<<1)-0x7f000000)>>24;
int val = fi&0x007fffff|0x00800000;
int shift = exp - 23;
if(f>=0) {
if(shift>0) return val< else return val>>(-shift);
}
else {
if(shift>0) return -(val< else {
shift = -shift;
int odds = val&(~((-1)< if(odds==0) return -(val>>shift);
else return -1-(val>>shift);
}
}
}

things?
e**********6
发帖数: 78
47
来自主题: JobHunting版 - Amazon onsite面试的惨痛经历
第三题感觉就是:个位数从0-25,其余位都是1-26;把十进制转换为26进制。特殊处理
个位即可。。
这么写应该就行:
stack mys;
mys.push('A'+val%26);
val/=26;
while(val){
val--;
mys.push('A'+val%26);
val/=26;
}
while(!mys.empty()){
cout< mys.pop();
}
cout<
b*******8
发帖数: 37364
48
来自主题: JobHunting版 - 求顺时针打印矩阵code
以前我写的,测试过:
#include
void main()
{
const int size=7;
int a[size][size];
int x,y,val=0;
for(x=0;x for(y=x;y for(y=x;y for(y=size-x-1;y>x;y--) a[size-x-1][y]=++val;
for(y=size-x-1;y>x;y--) a[y][x]=++val;
}
if (size%2) a[size/2][size/2]=++val;
for(x=0;x for(y=0;y printf("\n");
}
getchar();
}
i**********e
发帖数: 1145
49
来自主题: JobHunting版 - 贡献某公司onsite面经
我写的代码如下。
这样打印出来的所有路线还真不是一般的多,
我在想题目的原意是不是指只要路线有相交就不允许?
例如:
1->9->7->3 就不允许,因为 7->3 与 1->9 相交。
如果有以上的条件的话,用 visited cells 就不能解决了。
似乎比较复杂。
#include
#include
using namespace std;
int crossPoint[10][10] = {
{0},
{0, 0, 0, 2, 0, 0, 0, 4, 0, 5},
{0, 0, 0, 0, 0, 0, 0, 0, 5, 0},
{0, 2, 0, 0, 0, 0, 0, 5, 0, 6},
{0, 0, 0, 0, 0, 0, 5, 0, 0, 0},
{0},
{0, 0, 0, 0, 5, 0, 0, 0, 0, 0},
{0, 4, 0, 5, 0, 0, 0, 0, 0, 8},
{0, 0, 5, 0, 0, 0, 0, 0, 0, 0},
{0, 5, 0, 6, 0, 0, 0, 8... 阅读全帖
b**********r
发帖数: 91
50
来自主题: JobHunting版 - 问道amazon的面试题
Run the ultimate example around 400 ms
here is my code
4, 13, 5, 4, 13, 6, 8, 12, 16, 9, 13, 13, 13, 16, 9, 2, 6, 5,
0, 4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167,
4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167, 13, 18, 22, 35, 41, 49, 61, 77, 86, 99, 112, 125, 141, 150,
152, 158, 163, 5, 9, 22, 28, 36, 48, 64, 73, 86, 99, 112, 128, 137, 139,
145, 150, 4, 17, 23, 31, 43, 59, 68, 81, 94, 107, 123, 132, 134, 140,
145, 13, 19, 27, 39... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)