由买买提看人间百态

topics

全部话题 - 话题: elses
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
g**t
发帖数: 3
1
来自主题: JobHunting版 - one amazon interview problem
硬想出来的,没什么值得总结的思路,应该work,也满足条件。大家测测吧
public class Longest10Substring {
public static void main(String[] args) {
calc(new int[] { 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0 });
calc(new int[] { 0 });
}
public static void calc(int[] a) {
int n = a.length;
int minority = 0, cnt = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 1) {
cnt++;
}
}
if (cnt == 0 || cnt == n) {
print(a, 0, 0... 阅读全帖
V*****i
发帖数: 92
2
来自主题: JobHunting版 - 问两道google题
For the first one, do three binary searches. My code as follows. It is easy
to combine three binary searches into one routines through a flag, I wrote
this way for clearance.
int binary_search(int* num, int left, int right, int K) {
// find any pos such that num[pos] == K
if (left > right) return -1;
int mid = (left+right)/2;
if (K == num[mid]) return mid;
if (K < num[mid]) return binary_search(num, left, mid-1, K);
else return binary_search(num, mid+1, r... 阅读全帖
c**s
发帖数: 43
3
来自主题: JobHunting版 - 这题谁知道答案?
你解释的和上面storm说的是一样的。
关于paul的链表,我没看仔细,下面是我自己写的。
没想到要写这么久。真的面的时候应该就废了。
很长,有耐心就慢慢看吧。。不知道对了对,意思应该在那了。
早知道应该写成C,可以跑试试看。
这么长,只是为了比begin-end那个少扫一遍。。。
defrecord NodeType
ch char
pos int
prePos *NodeType
nxtPos *NodeType
preSameChar *NodeType
nxtSameChar *NodeType
// S2 char to first tracked S2 NodeType (pointing to the start of
// a linked list of same char NodeType by nxtSameChar)
nodesFoundFirst hashmap
// S2 char to last tracked S2 NodeType (pointing to the end of
// a linked list of ... 阅读全帖
t*******0
发帖数: 16
4
来自主题: JobHunting版 - 谷歌 电面
1. Find the given node, add the track to stack
2. Find next right node
void findnextnode(BinaryTree *root, int node_val)
{
stack s;
int error = 0;
BinaryTree *cur = root;
BinaryTree *cur = NULL;
//Find match node
while(cur!=NULL)
{
s.push(cur);
if(cur->value==node_val)
break;
else if((cur->valueright!=NULL))
{
... 阅读全帖
f****g
发帖数: 313
5
来自主题: JobHunting版 - 讨论一道面试题
We have two schedules S1&S2:
S1 = { , , ... }
S2 = { , , ... }
Assumptions:
1. S1&S2 are sorted by the starting time.
2. There are no conflicts in S1 and S2 itself.
Problem:
We try to find all the conflicts between these two schedules.
the basic idea is to merge sort to find the conflicts. the running time is
O(n).
My code in python: (code is kind of messy)
def merge(a,b):
i, j = 0, 0
unconflict = []
conflict = []
while( i < len(a... 阅读全帖
i**********e
发帖数: 1145
6
来自主题: JobHunting版 - 问道 facebook 面试题
其实不递归 代码或许会更简单些
void outputJSon(istream &in) {
bool newLine = false;
bool inBracket = false;
int indentLevel = 0;
while (in) {
char c = in.get();
if (newLine) {
cout << endl << setw(indentLevel) << c;
newLine = false;
}
else {
cout << c;
}
if (c == '{') {
indentLevel += TAB_SPACE;
newLine = true;
} else if (!inBracket && c == ',') {
newLine = true;
} else if (c == '}') {
newLine = true;
} else if (c == '[') {
... 阅读全帖
G******d
发帖数: 583
7
来自主题: JobHunting版 - interview 归来 - 求祝福
前天 interview business banking relationship manager,hiring manager 问了基
本都是 behavior questions。如下:
- chatting...
- What's your strength? what else? what else? what else? ...
- What's your weakness? what else?
- Why business banking?
- What would you do if you are hired into position?
- Any questions for me? any other questions? Do you have any questions? Any
more questions?
- chatting...
不太明白的是他不断地在strength 和 any other questions?上重复问"what else?
"和"any more question?" 领导说有可能是hiring manager想用此伎俩inti... 阅读全帖
c*********7
发帖数: 19373
8
来自主题: JobHunting版 - 请帮忙改改code
能否优化或加速。希望大家多提宝贵一间。
给一串数字,如何找到最长的翻转数字。就是从前往后和从后往前读出的数字序列是一
样的。
小于2个的数字是翻转整数,如果长度是3,那么前两个的和等于第三,或后两个的和等
于第一也符合要求
例如 11323111234,最长的翻转整数是1132311
void fib(const unsigned int *pSeq, int length,int *&pPalStart, int *
pPalLength){
*pPalLength = 0;
int *pBegin = (int *)pSeq;
pPalStart = pBegin;
int *pEnd = pPalStart + length-1;// save time on multiple usage
//fewer than 3 elements
if(length<3) {
*pPalLength = length;
}
else{
*pPalLength = 2;
int *... 阅读全帖
r******r
发帖数: 700
9
来自主题: JobHunting版 - BST insertion
这个版本的 insert 为什么不正确? 谁能帮忙看看
public void insert1(T value){
if( root == null ){
root = new BSTNode(value);
}else
insertHelper(value, root);

}
private void insertHelper(T value, BSTNode node){
if( value.compareTo(node.value_) < 0 ){
if( node.left == null )
node.left = new BSTNode(node.value_);
else
insertHelper(value, node.left);
}else if( value.compareTo(node.value_) > 0 ){
if( node.right == null )... 阅读全帖
s******e
发帖数: 108
10
来自主题: JobHunting版 - amazon interview
update:
收到invite onsite 通知,恩,国人还是挺靠谱的。感谢。
感谢NND的推荐,
不过可能被interviewer的国人给干掉了.
1.经典的stream求median,用2个heap解决。
2.给了一段程序,说一下程序的功能,
然后比较c程序和javascrip程序的区别。这个down掉。
#include
#include
#include
#include
#define MAX_RETRY 3
int remoteCall() {
return random() % 2;
}
void action(times) {
printf("calling remote procedure\n");
if (remoteCall()) {
printf("success!\n");
} else {
printf("error occured!\n");
if (times < MAX_RETRY) {
sleep(1);
... 阅读全帖
K*****k
发帖数: 430
11
要求in place, 只要求O(m + n)的方法,不需要log(m + n)那个。我写的代码还是觉得太长,怎么简化才能在白板上写的下?
double GetMedian(int A[], int m, int B[], int n)
{
bool odd_length = ((m + n) % 2 == 0)? false : true;
int m1, m2;
int pos = (m + n + 1) / 2;
int count = 0;
int i = 0;
int j = 0;
while (i < m && j < n)
{
count++;

if (A[i] <= B[j])
{
if (count == pos)
{
m1 = A[i];
if (odd_length)
return m1;... 阅读全帖
O******i
发帖数: 269
12
来自主题: JobHunting版 - atoi很不好写,头都大了...
写了一个,要考虑的情况真多...
假定是16位整数,范围是-32768到+32767
bool my_atoi(const char* str, int16& num)
{
if (str == NULL || str[0] == '\0')
return false;
int i = 0;
bool IsNeg = (str[0] == '-')? true : false;
if (str[0] == '+' || str[0] == '-')
{
if (str[1] == '\0')
return false;

i = 1;
}
num = 0;
const int num_limit = 3276;
const int digit_limit = (IsNeg)? 8 : 7;
int digit = 0;
bool max_int_reached = false;
for (; str[... 阅读全帖
w**z
发帖数: 8232
13
来自主题: JobHunting版 - atoi很不好写,头都大了...
我再贴一下Java的sourcecode。不懂为什么要把正数转成负数再查溢出呢?有什么好处?
code里有Author的comments
// Accumulating negatively avoids surprises near MAX_VALUE
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* ASCII minus sig... 阅读全帖
p*****2
发帖数: 21240
14
来自主题: JobHunting版 - 一道google 面试题
using System;
using System.Text;
using System.Collections.Generic;
public class Calculate
{
private Dictionary ht =new Dictionary();
public Calculate()
{
ht.Add('a', 1);
ht.Add('e', 5);
ht.Add('i', 9);
ht.Add('o', 15);
ht.Add('u', 21);
ht.Add('y', 25);
}
public int Calcu(string input,int start)
{
input=input.ToLower();
int i = start;
while (i < input.Length)
if (!ht.... 阅读全帖
w********0
发帖数: 2
15
来自主题: JobHunting版 - 攒人品:google电面面经
试着写了一下,好像可以,没有仔细测试。
public class FindKthFromTwoArray{
public static void main(String[] args)
{
int[] a = {2,4,6,7,8,10,19,20,24,98};
int[] b = {2,5,15,18,27,34,56,67,100,140};
// int[] a = {2,4,19,29,39,49,59};
// int[] b = {1,5,6,7,12,29,30,33,45,67,78};
// int[] a = {2};
// int[] b = {1};
int k = 16;
int result = FindKthFromTwoArray.findKth(a,b,k);
System.out.println("The kth number is: " + result);

}
p... 阅读全帖
p*****2
发帖数: 21240
16
来自主题: JobHunting版 - facebook一题
写了一个练练手。
import java.util.*;
class Node
{
Node[] children=new Node[26];

public Node Set(char c)
{
Node node=new Node();
children[c-'a']=node;
return node;
}

public Node Get(char c)
{
return children[c-'a'];
}
}
class Trie
{
Node root=new Node();

public void Add(String word)
{
int i=0;
Node node=root;
while(i {
char c=word.charAt(i);
if(node.Ge... 阅读全帖
p*****2
发帖数: 21240
17
来自主题: JobHunting版 - facebook一题
写了一个练练手。
import java.util.*;
class Node
{
Node[] children=new Node[26];

public Node Set(char c)
{
Node node=new Node();
children[c-'a']=node;
return node;
}

public Node Get(char c)
{
return children[c-'a'];
}
}
class Trie
{
Node root=new Node();

public void Add(String word)
{
int i=0;
Node node=root;
while(i {
char c=word.charAt(i);
if(node.Ge... 阅读全帖
w**z
发帖数: 8232
18
Source Code from Java
/*
* @author Lee Boynton
* @author Arthur van Hoff
* @author Josh Bloch
* @version 1.92, 04/07/06
* @since JDK1.0
*/
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* A... 阅读全帖
k*****y
发帖数: 744
19
假设写成 X + Y = Z
X, Y, Z满足
len(X) >= len(Y),
len(Z) = len(X) 或 len(X)+1
从最低位开始,一位一位填X和Y,那么Z相应位置就能算出来了。
import random
# generate random digits
N = random.randint(3,16)
S = [0]*N
count = [0]*10
for i in range(0,N):
S[i] = random.randint(1,9)
count[S[i]] += 1
print S
L = [[0, 0, 0]]*N
P = [0]*N
W = [0]*3
# X + Y = Z
# X, Y, Z are of length W[0], W[1], W[2]
# with W[0] >= W[1] and W[2] = W[0] or W[0]+1
# try to fill in the k'th bit
def fill(L, count, W, k, carry):
# terminate when X is ful... 阅读全帖
k*****y
发帖数: 744
20
来自主题: JobHunting版 - leetcode上wild match
请问像 s = "abadabadabadeabecd", p = "*ab?c*d"这样的不用递归怎么办?
我也贴一个,大家帮忙看看,谢谢~
===========================
bool isMatch(const char* s, const char* p) {
if(*s == 0) return !hasNonStar(p);
if(*p == 0) return false;
if(*p != '*'){ //case: *p != '*'
while(*s && *p && *p!='*') {
if(*p!='?' && *p!=*s)
return false;
++p; ++s;
}
return isMatch(s, p);
}
else{ // case: *p == '*'
if(!skipStars(s, p)) return false;
... 阅读全帖
S**I
发帖数: 15689
21
来自主题: JobHunting版 - 罗马数字转换成十进制
楼上的全是Java,俺来一个C的吧(各种exception懒得写了):
#include
int getDecimal(char);
int romanToDecimal(char *);
char getRoman(int);
char * decimalToRoman(int, char *);
int main(int argc, char* argv[]){
printf("Roman: %s\n", argv[1]);
int d = romanToDecimal(argv[1]);
printf("Decimal: %d\n", d);
char r[1000];
decimalToRoman(d, r);
printf("Roman: %s\n", r);
}
int getDecimal(char a){
switch(a){

case 'I':
return 1;
case 'V':
return 5;
case 'X':... 阅读全帖
w****x
发帖数: 2483
22
来自主题: JobHunting版 - 说好得FG面经,回馈板上GGJJ
//Print strings with certain prefix in a sorted string array
void GetIndex(const char* strs[], int nBeg, int nEnd, char chr, int nPos,
OUT int& index1, int& index2);
void PrintComPrefix(const char* strs[], int n, const char* szPre)
{
assert(strs && szPre && n > 0);
const char* p = szPre;
int nIndex1 = 0;
int nIndex2 = n-1;
while (*p != 0 && nIndex1 >= 0)
{
GetIndex(strs, nIndex1, nIndex2, *p, p-szPre, nIndex1, nIndex2);
p++;
}
if (nIndex1 >= 0 ... 阅读全帖
w****x
发帖数: 2483
23
//Print strings with certain prefix in a sorted string array
void GetIndex(const char* strs[], int nBeg, int nEnd, char chr, int nPos,
OUT int& index1, int& index2);
void PrintComPrefix(const char* strs[], int n, const char* szPre)
{
assert(strs && szPre && n > 0);
const char* p = szPre;
int nIndex1 = 0;
int nIndex2 = n-1;
while (*p != 0 && nIndex1 >= 0)
{
GetIndex(strs, nIndex1, nIndex2, *p, p-szPre, nIndex1, nIndex2);
p++;
}
if (nIndex1 >= 0 ... 阅读全帖
j********e
发帖数: 1192
24
我的做法也是用stack,可能简单些。
从左到右scan一遍,如果是"("或者0,就入栈,如果是“)”就
看栈顶3个元素是否“(00”,是的话,弹出这3个,
压入“0”(也就把子树替换成“0”)。
最后栈里只剩一个“0”就validated。
int validate(char *s, int len) {
if(s == NULL || len == 0 || s[0] != '(')
return -1;
int depth = 0, max_depth = 0;
char *t = new char[len+1];
int tlen = 0;
for(int i=0; i if(s[i] == '(') {
depth++;
if(depth>max_depth)
max_depth = depth;
}
else if(s[i] == '0')
t[tlen++] = s[i]... 阅读全帖
f*********m
发帖数: 726
25
A robot is located at the top-left corner of a m x n grid (marked ‘Start’
in the diagram below). The robot can only move either down or right at any
point in time. The robot is trying to reach the bottom-right corner of the
grid (marked ‘Finish’ in the diagram below). How many possible unique
paths are there?
Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths
would there be?
An obstacle and empty space is marked as 1 and 0 respectively in t... 阅读全帖
p*****2
发帖数: 21240
26
来自主题: JobHunting版 - FB面试题:binary tree inorder successor
写了一个练练手
def first(root):
if root!=None:
l=first(root.left)
if l: return l
return root
def dfs(root,node):
if root==None: return root

if root==node:
r=first(root.right)
return r if r else root
else:
l=dfs(root.left,node)
if l: return l if l!=node else root
else: return dfs(root.right,node)

def next(root,node):
ret=dfs(root,node)
return ret if ret!=node else None
l****p
发帖数: 397
27
来自主题: JobHunting版 - G家onsite面经
确实想不出比mlg(N)更快的算法。这题要在30分钟内做完确实很难,我看了楼主的思路
后还用了近50分钟才写完的……
顺便贴上我的实现(Ruby):
def nearest_nodes root, m, key
node = find_insert root, key
prev = node.prev_node
nex = node.next_node
results = []
m.times do
nearest = nil
if prev and nex
if m-prev.value nearest = prev
prev = prev.next_node
else
nearest = nex
nex = nex.next_node
end
elsif prev
nearest = prev
prev = prev.prev_node
elsif nex
nearest ... 阅读全帖
w****x
发帖数: 2483
28
partition解法, 一点都没觉得简单:
int _inner_min_set(int a[], int n, int k)
{
if (n <= 0) return 0;
if (n == 1) return a[0] >= k ? 1 : 0;
swap(a[0], a[n/2]);
int i = 1;
int j = n-1;
while (i <= j)
{
if (a[i] < a[0])
i++;
else if (a[j] >= a[0])
j--;
else swap(a[i], a[j]);
}
swap(a[0], a[j]);
int nSum = 0;
int nLft = 0;
if (j == n-1)
{
nSum = a[j];
nLft = n-1;
}
else
{
for (... 阅读全帖
r********g
发帖数: 1351
29
来自主题: JobHunting版 - 2道面试题.
第二题如果不用递归,就是iterative的post order遍历?
另外,打印也很麻烦,是不是用一个辅助stack?这样复杂度是O(nlgn)吗?
void printStack(stack S){
stack T;
while(!S.empty()){
T.push(S.top());
S.pop();
}
while(!T.empty()){
cout << T.top()->value << ' ';
T.pop();
}
cout << endl;
}
// post order traverse
void printAllPath(Node* root) {
if(!root) return;
stack S;
Node* prev = NULL;
S.push(root);
while(!S.empty()){
Node* p = S.top();
if(!prev || p == prev->left || p==prev->right... 阅读全帖
C***U
发帖数: 2406
30
来自主题: JobHunting版 - leetcode上的2个整数相除
我把dividend和divisor存到x y里面都是unsigned int了
下面这个代码对了 通过leetcode的验证了 只不过写的很丑
把最小的数拿出来单独处理了
int divide(int dividend, int divisor) {
int flag = 1;
unsigned long int x;
unsigned long int y;
unsigned long int z;
unsigned int i = 0;
unsigned int count = 0;
int result = 0;
if(!divisor) {
cout << "could not be divided by 0" << endl;
return 0;
}
if(dividend == -2147483648) {
if(divisor > 0) {
dividend += divisor;
result ... 阅读全帖
S**I
发帖数: 15689
31
N久以前写的一个:
#include
int getDecimal(char);
int romanToDecimal(char *);
char getRoman(int);
char * digitToRoman(int, int, char *);
char * decimalToRoman(int, char *);
int main(int argc, char* argv[]){
printf("Roman: %s\n", argv[1]);
int d = romanToDecimal(argv[1]);
printf("Decimal: %d\n", d);
char r[1000];
decimalToRoman(d, r);
printf("Roman: %s\n", r);
}
int getDecimal(char a){
switch(a){

case 'I':
return 1;
case 'V':
return 5;
... 阅读全帖
C***U
发帖数: 2406
32
来自主题: JobHunting版 - 请教Find Median Of Two Sorted Arrays
我把leetcode上好多例子拿到机器上测试了 都没出错
但是在leetcode上会time limit exceeded
应该是有一个死循环
我找不出来
double findMedian(int A[], int m, int B[], int n) {
if(!n) {
return (A[(m - 1)/ 2] + A[(m - 1)/ 2 + (m - 1) % 2]) / 2.0;
}
int aStart = -1, aEnd = (m + n - 1) / 2 - 1;
int bStart = -1, bEnd = 0;
while(true) {
if(bEnd + 1 < n && A[aEnd] <= B[bEnd + 1] && B[bEnd] <= A[aEnd + 1])
{
break;
}
if(bEnd == n - 1 && B[bEnd] <= A[aEnd + 1]) {
break;
... 阅读全帖
C***U
发帖数: 2406
33
来自主题: JobHunting版 - 请教Find Median Of Two Sorted Arrays
我把leetcode上好多例子拿到机器上测试了 都没出错
但是在leetcode上会time limit exceeded
应该是有一个死循环
我找不出来
double findMedian(int A[], int m, int B[], int n) {
if(!n) {
return (A[(m - 1)/ 2] + A[(m - 1)/ 2 + (m - 1) % 2]) / 2.0;
}
int aStart = -1, aEnd = (m + n - 1) / 2 - 1;
int bStart = -1, bEnd = 0;
while(true) {
if(bEnd + 1 < n && A[aEnd] <= B[bEnd + 1] && B[bEnd] <= A[aEnd + 1])
{
break;
}
if(bEnd == n - 1 && B[bEnd] <= A[aEnd + 1]) {
break;
... 阅读全帖
p*****2
发帖数: 21240
34
来自主题: JobHunting版 - Got a 4? You Were Just Fired from Microsoft
The way Microsoft’s review system works, a whole bunch of Microsoft
employees just got their annual performance reviews. The September 15
paystub will reflect their new “numbers”. If they got a merit increase
the paystub will show it. If they got a bonus, it will show up there too.
This post is for those Microsoft employees who are not happy with their
review…
That September 15 date is what motivates managers to finally get busy
delivering the bad news to employees who didn’t fare so well in s... 阅读全帖
C***U
发帖数: 2406
35
如果用的是vector而不是数组的话 无法通过online judge large
哪里可以再优化一下啊?
谢谢了
int placeQueens(int n) {
int count = 0;
int *positions = new int[n];
int p = 0;
positions[p] = 0;
int i = 0;
while(!(p == -1 && i == n)) {
bool flag = true;
while(flag && i < n) {
int j = 0;
while(j < p + 1) {
if(positions[j] == i || positions[j] - i == j - (p + 1)||
positions[j] - i == p + 1 - j) {
break;
}
... 阅读全帖
h****n
发帖数: 1093
36
来自主题: JobHunting版 - 合并两个排序好的链表, 优解?
写了三个方法,一个是递归,一个是naive方法,一个是用指向指针的指针来构建
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode* res;

//Method using pointer to pointer
res = P2PMethod(l1,l2);
//res = recursive(l... 阅读全帖
f*******w
发帖数: 1243
37
来自主题: JobHunting版 - 求助各位大牛:LeetCode的Decode Ways
在OJ上跑总是说internal error……
自己电脑上跑所有test case都没问题。
检查不出哪里错了……求助各位大牛
程序:
class Solution {
public:
int numDecodings(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (s.empty()) return 0;
int len = s.size();
if (len < 1) return 0;
int numarray[len];
int i = len - 1;
if (s[i] > '9' || s[i] < '0') return 0;
else {
numarray[i] = (s[i] == '0') ? 0 : 1;
i--;
... 阅读全帖
y***u
发帖数: 174
38
这个行么?recursive的。
我需要暂时把左子树或者右子树变成null,不然太麻烦。
void Traverse(ArrayList output, Node A, Node B){
if(A==null && B==null)
return;
else if(A==null){
output.add(B.val);
return;
}else if(B==null){
output.add(A.val);
return;
}else{
Node t1 = null, t2=null;
if(B.val>A.val){
Trav(output, A, B);
}else if(B.val < A.val){
Trav(output, B, A);
}else{
output.add(A.val);
ou... 阅读全帖
k****r
发帖数: 807
39
来自主题: JobHunting版 - 一道字符串题目
目测的话,我想用recursive+backtrack
分情况的, 我简单写了下,请指正
bool matchwords(char* word1, char* word2, int index1, int index2) {
if (index1 == strlen(word1)&& index2 == strlen(word2)) return true;
else {
if (word2[index2] == '*') {
if (matchwords(word1, word2, index1+1, index2+1)) return true;
else if (matchwords(word1, word2, index1+1, index2)) return true;
else if (word2[index2] == '?') {
return matchwords(word1, word2, index1+1, index2+1);
else {
... 阅读全帖
b*2
发帖数: 94
40
来自主题: JobHunting版 - 一道字符串题目
去年还真做过这道题:
private static boolean match(String regEx, String word) {
// TODO Auto-generated method stub
if(regEx == "*")
return true;
int reIndex = 0;
int wdIndex = 0;
for(;reIndex if(regEx.charAt(reIndex)==word.charAt(wdIndex)){
continue;
}else if(regEx.charAt(reIndex)!='?'&®Ex.charAt(reIndex)!='*'){
//simply not equivalent
return false;
}else if(regEx.charAt(reIndex)=='?'){
//deal with ?
//goto the next char
continue;
}else{
//de... 阅读全帖
l*******b
发帖数: 2586
41
来自主题: JobHunting版 - 狗电面
#include
using namespace std;
struct IntNode {
int a;
int b;
IntNode *next;
IntNode(int low, int high) : a(low), b(high), next(NULL) {}
};
struct HeadsNode {
IntNode *list;
HeadsNode *next;
HeadsNode() : next(NULL) {}
};
class MergeInt {
public:
void merger(HeadsNode *h, HeadsNode *t) {
if(h == t) return;
HeadsNode *mid = middle(h,t);
merger(h, mid);
merger(mid->next, t);
h->list = mergeTwoList(h->list, (mid->next... 阅读全帖
b******g
发帖数: 77
42
来自主题: JobHunting版 - 我也贡献一个B家店面
Example
{a[b]c} is valid
[a{b]c} is invalid
{{a} is invalid
{a}} is invalid
bool isExpressionValid(fstream & fin)
{
stack s;
char c;
while (fin.good())
{
fin >> c;
if (c == '{' || c == '[')
s.push(c);
else if (c == '}')
if (s.empty() || s.top() != '{')
return false;
else s.pop();
else if (c == ']')
if (s.empty() || s.top() != '[')
return false;
el... 阅读全帖
e****e
发帖数: 418
43
也许是我功力不够,我写起来没有我想象中的好写,虽然思路很简单。。。
public int getCount(int[] a) {
int n = a.length;
if ( a == null || n == 0 )
return 0;

int i = 0;
int j = n - 1;
int count = absEq( a[i], a[j] ) ? 1 : 2;
while( i < j ) {
if ( i + 1 == j )
break;
if ( absEq( a[i], a[j] ) ) {
if ( a[i] == a[i+1] )
i++;
else if( a[j] == a[j-1] )
j--;
else {
if ( absEq( a[i+1], a[j-1] ) )
c... 阅读全帖
s*********s
发帖数: 140
44
贴个java recursive的吧。
float compute(String exp) {

if(exp == null || exp.length() == 0) return 0;

int plusPos = exp.indexOf('+');
int minusPos = exp.indexOf('-');

if(plusPos < 0 && minusPos < 0) return convert(exp);

if(plusPos < 0 || (minusPos > 0 && minusPos < plusPos))
return convert(exp.substring(0, minusPos)) - compute(exp.
substring(minusPos+1));
if(minusPos < 0 || (plusPos > 0 && plusPos < minusPos... 阅读全帖
s*********s
发帖数: 140
45
贴个java recursive的吧。
float compute(String exp) {

if(exp == null || exp.length() == 0) return 0;

int plusPos = exp.indexOf('+');
int minusPos = exp.indexOf('-');

if(plusPos < 0 && minusPos < 0) return convert(exp);

if(plusPos < 0 || (minusPos > 0 && minusPos < plusPos))
return convert(exp.substring(0, minusPos)) - compute(exp.
substring(minusPos+1));
if(minusPos < 0 || (plusPos > 0 && plusPos < minusPos... 阅读全帖
r*********n
发帖数: 4553
46
来自主题: JobHunting版 - 用rand5()产生rand7()
idea:
first use rand5() to generate a binary random function with p = 1/7 as
follows
express 1/7 in base 5
e.g. 0.abc......
(Note if x can be expressed in base 5 with finite digits, after the last
digit, we assume an infinite number of 0s are padded)
check first digit a:
if rand5() < a return 1
else if rand5() > a return 0
else proceed to the next digit
similarly generate binary random functions with p=1/6, 1/5, ..... 1/2
denote these binary random functions as rand(), N = 2,3,...,7
if rand<7... 阅读全帖
p*****p
发帖数: 379
47
用checked标记封闭的区域,用markChecked把O变成X
small能过,large挂了,莫非是因为偷懒用dfs造成溢出了?
class Solution {
public:
void solve(vector> &board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (board.size() <= 1) return;
int rows = board.size();
int cols = board[0].size();
vector> visited(rows, vector(cols, false));
vector> checked(rows, vector(cols, false));

f... 阅读全帖
f****s
发帖数: 74
48
leetcode上给出的思想很好。就是keep一个pre和一个cur,
如果cur是pre的孩子,说明我们正从上到下,如果反过来,说明我们从下往上,就该打
印节点了。
顺便练一个,
void postorder(Node* r)
{
if(!r) return;
stack s;
Node* pre=0;
Node* cur=r;
s.push(r);
while(!s.empty()){
cur=s.top();
if(pre==0||cur==pre->left||cur==pre->right){
if(cur->left) s.push(cur->left);
else if(cur->right) s.push(cur->right);
else{
cout<val;
s.pop();
}
}else{
if(pre=cu... 阅读全帖
p*****2
发帖数: 21240
49
来自主题: JobHunting版 - G家电面题
我也做了一下
sum=(str)->
b=false #not consonant
encode=(c)->
isletter=(c)->
code=c.charCodeAt 0
code>='a'.charCodeAt 0 and code<='z'.charCodeAt 0
table={'a':1, 'e':5, 'i':9, 'o':15, 'u':21, 'y':25}
c=c.toLowerCase()
code=0
if not isletter(c)
b=false
else
if c is 'y'
code=table[c] if b
else
code= if table[c]? then table[c] else 0
b=code==0
code
res=0
prod=0
for c in str
code=encode(c)
if code is 0
... 阅读全帖
b******7
发帖数: 92
50
来自主题: JobHunting版 - 发个刚面完的rocket fuel的面经吧
在[0,n-1]间查找,再减去多余的部分
int adjust(int A[],int n, int b)
{
if(n <= 0 || b < 0) return -1;
sort(A,A+n);
int index = -1;
int cursum = -1;
int low = 0, high = n-1;
while(low <= high)
{
int mid = low + (high - low)/2;
int sum = 0;
for(int i = 0; i < n; i++)
{
sum += min(A[i],A[mid]);
}
if(sum == b) return A[mid];
else if(sum > b)
{
index = mid;
cursum = sum;
high =... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)