由买买提看人间百态

topics

全部话题 - 话题: str
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
j*******g
发帖数: 4
1
来自主题: JobHunting版 - atoi很不好写,头都大了...
见过很多次这个题目了,一直没有招,下面写了一个又臭又长的, 方法很笨, 求建议
, 欢迎批评和指正
这样的代码面试可以通过吗?
////////////////////////////////////////////////////////////////
#include
#include
using namespace std;
//假设16位的整型
// -32768 , +32767
const char MAX_INT[] = "32767";
const char MIN_INT[] = "32768";
const int MAX_STRLEN = 5;
bool my_atoi(const char *str, int &res)
{
if(str == NULL)
{
cout << "Invalid pointer" << endl;
return false;
}

int index = 0;

if(str[index] == '-' || s... 阅读全帖
j*******g
发帖数: 4
2
来自主题: JobHunting版 - 弱弱的问一个问题
见过很多次这个题目了,一直没有招,下面写了一个又臭又长的, 方法很笨, 求建议
, 欢迎批评和指正
这样的代码面试可以通过吗?
////////////////////////////////////////////////////////////////
#include
#include
using namespace std;
//假设16位的整型
// -32768 , +32767
const char MAX_INT[] = "32767";
const char MIN_INT[] = "32768";
const int MAX_STRLEN = 5;
bool my_atoi(const char *str, int &res)
{
if(str == NULL)
{
cout << "Invalid pointer" << endl;
return false;
}

int index = 0;

if(str[index] == '-' ||... 阅读全帖
S********0
发帖数: 5749
3
也贴个自己的code, 跟前面一个童鞋说的类似,碰见扩号或者等号就递归,这样子也
很容易扩展到乘号或者除号.
double solve(string str, double x) {
int x_coeff, y_coeff, con;
int size = str.size();
x_coeff = y_coeff = con = 0;

getCoeff(str, 0, x_coeff, y_coeff, con);
if (y_coeff != 0) {
double y = -(con + x_coeff*x) / y_coeff;
return y;
}
else return DBL_MAX;
}
int getCoeff(string &str, int start, int &x_coeff, int &y_coeff, int &con) {
int end;
int num;
int digit_flag = 0;
int sign ... 阅读全帖
T*****n
发帖数: 82
4
String to Integer (atoi) 这一题
通过的code是:
public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() < 1)
return 0;
// trim white spaces
str = str.trim();
char flag = '+';
// check negative or positive
int i = 0;
if (str.charAt(0) == '-') {
flag = '-';
i++;
} else if (str.charAt(0) == '+') {
i++;
}
// use double to store result
double result = 0;
// calculate value
while... 阅读全帖
u**l
发帖数: 35
5
来自主题: JobHunting版 - 问一道uber onsite题目
c/c++的code好像不受欢迎:
#include
#include
using namespace std;
char* GetAlphabet(char* str) {
while ('\0' != *str && !((*str >= 'a' && *str <= 'z') || (*str >= 'A' &&
*str <= 'Z'))) {
++str;
}
return str;
}
char* GetOthers(char* str) {
while ((*str >= 'a' && *str <= 'z') || (*str >= 'A' && *str <= 'Z')) {
++str;
}
return str;
}
void Reverse(char* s, char* e) {
while (s < e) {
char c1 = *s;
char c2 = *e;
*s = c2;
... 阅读全帖
l*******s
发帖数: 7316
6
【 以下文字转载自 Prepaid 俱乐部 】
发信人: llaalways (camper), 信区: Prepaid
标 题: Moto E 1526 成功鼓捣成一开机就自动打开hotspot,每月自动拨号一次。
发信站: BBS 未名空间站 (Mon Mar 7 12:38:14 2016, 美东)
还是用Tasker, 就是在上次自动拨号的profile里加了3条。
wifi off
wifi tether on
svc data enable
但wifi tether on 在stock rom里出错,所以刷了flyme. 中间还试过CM13,CM13遇到
GAPP装不好的问题,所以放弃了。
flyme还有一个小问题,就是不能拦截拨号,这个以后再折腾。
将下面的内容保存为onBoot.prf.xml, 然后从Tasker里import profile 就可以了。


1457209583790阅读全帖
h*****g
发帖数: 312
7
来自主题: JobHunting版 - 攒人品之facebook电面面经
写了一个,有错误请指正~~
//copy on write
class String
{
private:
char *str;
int len;
public:
String():str(0),len(0)
{

}
String(char *p)
{
len=strlen(p);
char *tm=new char(len+2);
memset(tm,0,len+2);
str=tm+1;
strncpy(str,p,len+1);
}
String(const String &ms)
{
len=ms.len;
str=ms.str;
++(str[-1]);
}
Strin... 阅读全帖
h*****g
发帖数: 312
8
来自主题: JobHunting版 - 攒人品之facebook电面面经
写了一个,有错误请指正~~
//copy on write
class String
{
private:
char *str;
int len;
public:
String():str(0),len(0)
{

}
String(char *p)
{
len=strlen(p);
char *tm=new char(len+2);
memset(tm,0,len+2);
str=tm+1;
strncpy(str,p,len+1);
}
String(const String &ms)
{
len=ms.len;
str=ms.str;
++(str[-1]);
}
Strin... 阅读全帖
j*****y
发帖数: 1071
9
来自主题: JobHunting版 - 发一个刚面的startup面经
thanks。 你的第一题的code感觉有点问题, swap 以后,应该 swap 回来
for (int i = pos; i < len; i++)
{
swap(str[pos], str[i]);
_inner_print(str, len, pos+1);
swap(str[pos], str[i]);
swap(str[pos], str[i]);
}

Given a word, print out all the combinations of words from the letters that
make it. For example:
bad: abd adb bda dab dba
void _inner_print(char str[], int len, int pos)
{
if (pos == len)
{
cout< return;
}
for (int i = pos; i < len; ... 阅读全帖
x********i
发帖数: 92
10
来自主题: JobHunting版 - G家电面题
感觉对y的处理比较复杂. 我纯新手...写了一个代码, 望斧正
然后对于大小写的处理, 我就是创建一个新数组, 然后把所有的都转换成小写字母.
我运行了题目里给出的所有字符串, 结果都是正确的, 然后test了一个空字符串, 返回
为0. 求问还需要做什么样的test啊? 感觉很多代码写出来自己都不知道对不对, 因为
没有完善的test方案, 求指点...新手跪谢了
#define VOWELY 0
#define CONSOY 1
int vowelProduct(const char* newstr){
int length = strlen(newstr);
const char* consolant = "bcdfghjklmnpqrstvwxz";
const char* vowels = "aeiou";
int i;
int yFlag;
int score=0;
char* current;
char* yTest;
int point[26] = {0};
int tempScore=0;... 阅读全帖
b******7
发帖数: 92
11
来自主题: JobHunting版 - 最郁闷的facebook面试+面经。
练习下,贴段code
string maxcountchars(const string & str)
{
vector r;
int count = 0;
int start = 0;
while(start < str.length() && str[start] == ' ') start++;
for(int j = start+1; j < str.length();)
{
if(str[j] != str[start])
{
count = max(count, j-start);
while(j start = j;
}
else
{
j++;
}
}
count = max(count, (int)str.length() - star... 阅读全帖
z******e
发帖数: 82
12
来自主题: JobHunting版 - 发个Twitter的面试题
借用eswine的idea:
public static String test1(String str) {
StringBuilder sb = new StringBuilder();
int len = str.length();
boolean escape = false;
for (int i = 0; i < len; i++) {
char c = str.charAt(i);
// everything between ' '
if (c == '\'') {
escape = true;
while (i < len) {
c = str.charAt(i);
sb.append(c);
if (c == '\'' && !escape) {... 阅读全帖
s********u
发帖数: 1109
13
来自主题: JobHunting版 - ebay第一轮电话面经
你比我写的好多了,我写的大致是这样的:
// Input -> "I have 36 books, 40 pens2, and 1 notebook."
// Output -> "I evah 36 skoob, 40 2snep, dna 1 koobeton."
string reverse( string& word){
if( word.empty() || word.size() == 1)
return word;

int left = 0;
int right = word.size() - 1;
char tmp;

while( left < right ){
tmp = word[right];
word[right] = word[left];
word[left] = tmp;
left++;
right--;
}

return word;
}

// Input: test... 阅读全帖
x**********g
发帖数: 82
14
来自主题: EB23版 - 写程序处理perm 数据
Python program
# Authored by xiaofeixiang
# Copyright reserved to EBC23
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTI... 阅读全帖
t***q
发帖数: 418
15
【 以下文字转载自 Programming 讨论区 】
发信人: treeq (treeq), 信区: Programming
标 题: 如何用python web.py web service 做 multiple parameters 的 call?
发信站: BBS 未名空间站 (Sun Mar 22 23:14:33 2015, 美东)
大家好。我的那个web service 做成了。用了python 的 web.py.
install web.py
cd webpy
编辑python web service.
#!/usr/bin/env python
import web
import csv
import difflib
import re
import operator
import Levenshtein
urls = ('/title_matching2','title_matching2')
app = web.application(urls,globals())
class title_matching2:
def __init__(self):
... 阅读全帖
t***q
发帖数: 418
16
【 以下文字转载自 Programming 讨论区 】
发信人: treeq (treeq), 信区: Programming
标 题: 如何用python web.py web service 做 multiple parameters 的 call?
发信站: BBS 未名空间站 (Sun Mar 22 23:14:33 2015, 美东)
大家好。我的那个web service 做成了。用了python 的 web.py.
install web.py
cd webpy
编辑python web service.
#!/usr/bin/env python
import web
import csv
import difflib
import re
import operator
import Levenshtein
urls = ('/title_matching2','title_matching2')
app = web.application(urls,globals())
class title_matching2:
def __init__(self):
... 阅读全帖
t***q
发帖数: 418
17
大家好。我的那个web service 做成了。用了python 的 web.py.
install web.py
cd webpy
编辑python web service.
#!/usr/bin/env python
import web
import csv
import difflib
import re
import operator
import Levenshtein
urls = ('/title_matching2','title_matching2')
app = web.application(urls,globals())
class title_matching2:
def __init__(self):
self.hello = "hello world"
def GET(self):
getInput = web.input(name="World")
b=[]
with open("Book1.txt","rb") as k:
for row in k:
... 阅读全帖
f*******t
发帖数: 7549
18
来自主题: JobHunting版 - 谁能写个trie的框架?
struct Trie
{
Trie();
~Trie();
Trie *children[26];
bool isEnd;
};
Trie::Trie()
{
for(int i = 0; i < 26; i++)
children[i] = NULL;
isEnd = false;
}
Trie::~Trie()
{
for(int i = 0; i < 26; i++) {
if(children[i]) {
delete children[i];
children[i] = NULL;
}
}
}
void TrieInsert(Trie *root, const char *str)
{
if(!str || *str < 'a' || *str > 'z') {
printf("Invalid line\n");
return;
}

Trie *cur ... 阅读全帖
s*******f
发帖数: 1114
19
来自主题: JobHunting版 - amazon onsite 面经
// 给一个字符串,例如"30*(5+10)",输出计算结果。
double GetValue(double b1, double b2, char op){
switch(op){
case '+':
return b1 + b2;
case '-':
return b1 - b2;
case '*':
return b1 * b2;
case '/':
if (b2 < 0.00000001)
return 0;
return b1 / b2;
default:
return 0;
}
}
bool OpPriority(char op1, char op2){
if (op1 == '*' || op1 == '/'){
return true;
}else if ((op1 ==... 阅读全帖
s*****n
发帖数: 162
20
来自主题: JobHunting版 - Exposed上一道string permutation的题
Following is a solution based on the very interesting method called Knuth’s
Permutations. This has the duplicate permutation avoidance logic built-in.
The first call to the method is StringPermutation(str, 0).
void StringPermutation(char* str, int k) {
if (!str || (k < 0))
return;
if (str[k] == '\0') {
cout << str << endl;
return;
}
StringPermutation(str, k + 1);
int i;
for (i = k;(i > 0) && (str[i] != str[i - 1]);i--) {
char t = str[i];
... 阅读全帖
S**I
发帖数: 15689
21
来自主题: JobHunting版 - [合集] G一面,感觉不咋的
☆─────────────────────────────────────☆
libei (Bei) 于 (Wed Jan 11 15:43:39 2012, 美东) 提到:
面试官是Google+组的,
一上来她说看到我简历上的一篇测试自动化的文章,读了一遍,感觉"very
informative",让后让我介绍一下相关经验。让我小高兴了一下。
第一题是coding,做的还算顺利,后来她评价说所有的cases都覆盖到了。可能算是过
关吧。
第二题我想复杂了,然后在她提示下才解决。自我感觉很不好。其实sort一下就差不多
了,不过我往复杂的树结构想去了。虽然树结构确实能解决这个问题,不过当时我解释
得很不清楚。反正很不爽。
最后瞎聊时间,她说我提到的测试自动化实践和Google内部的基本完全一样blahblah。
。。,不过我觉得这点也算不上加分吧,是个人进google一段时间后都能学会。就怕她
觉得我想问题太复杂,直接negative。
大家有啥建议想法??
☆─────────────────────────────────────☆
peking2 (myfac... 阅读全帖
f*******t
发帖数: 7549
22
来自主题: JobHunting版 - amazon面试题目讨论贴2
#include
#include
#include
#define ENTRYNOTFOUND 0
#define ENTRYFOUND 1
#define NOTCOMPLETE 2
struct Trie;
void FindWords(Trie *root);
struct Trie
{
Trie();
~Trie();
Trie *children[26];
bool isEnd;
};
Trie::Trie()
{
for(int i = 0; i < 26; i++)
children[i] = NULL;
isEnd = false;
}
Trie::~Trie()
{
for(int i = 0; i < 26; i++) {
if(children[i]) {
delete children[i];
children[i] = NULL;
}
}
}
... 阅读全帖
x******a
发帖数: 6336
23
来自主题: Programming版 - 请问关于overloading <<
I found if I make the second parameter const, i.e.std::ostream& operator<<(
std::ostream& os, const str& s), it worked now. what happened?
std::ostream& operator<<(std::ostream& os, str& s){
//obliterate existing value(s)
for (str::size_type i=0; i!=s.size(); ++i)
os<
return os;
}
class str{
public:
typedef std::vector::size_type size_type;
//constructors
str();
str(size_type n,char c);
str(const char* cp);
template str(In b, ... 阅读全帖
w****x
发帖数: 2483
24
来自主题: 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
25
//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
26
来自主题: JobHunting版 - 刚才重新回顾了一下那题
struct NODE
{
vector vecGUID;
NODE* nodes[256];

NODE() { memset(nodes, 0, sizeof(nodes)); }
};
void _inner_get_guid(NODE* pNode, const char* str, int k, vector& vec)
{
if (NULL == pNode)
return;
if (k <= 0)
{
vec = pNode->vecGUID;
return;
}
_inner_get_guid(pNode->nodes[*str], str+1, k-1, vec);
}
vector getGUID(const char* str, NODE* pRoot, int k)
{
vector vecRet;
if (NULL == pRoot || NULL == str || *str == 0 ... 阅读全帖
s***5
发帖数: 2136
27
来自主题: JobHunting版 - L家电面
第一题用Perl regular expression可以一行搞定。
if(str =~ /[+|-]?[0|[1-9]\d*](\.[0-9]+)*/ || str =~ /[+|-]?\.\d+/)
return true;
else
return false;
如果不能用regular expression,下面有个C++的code。
bool isNumeric(string str)
{
// assume there are no leading or ending spaces in the string
int i = 0, end = str.size()-1;
if(str[i] == '+' || str[i] == '-') i++; // check the sign symbol

if(str[i] == '.') { // if the first non-sign char is '.', e.g. .56
if(i == end) return false;... 阅读全帖
w****x
发帖数: 2483
28
来自主题: JobHunting版 - 拓扑排序的题怎么做?

/*
Given an array list of lots of strings, they are arranged by unknown
alphabetical
order, print all possible alphabetical orders (Sort)
*/
void buildMap(const char* str[], int n, int nBeg, bool bMap[26][26])
{
if (str == NULL || n <= 1) // <= 1 not 0
return;
int nPrev = 0;
while (str[nPrev][nBeg] == 0 && nPrev < n) // missing nPrev < n
nPrev++;
int nIter = nPrev + 1;
while (nIter < n)
{
if (str[nPrev][nBeg] != str[nIter][nBeg])
{
... 阅读全帖
w****x
发帖数: 2483
29
来自主题: JobHunting版 - 发一个刚面的startup面经
直接贴Google doc了
Given a word, print out all the combinations of words from the letters that
make it. For example:
bad: abd adb bda dab dba
void _inner_print(char str[], int len, int pos)
{
if (pos == len)
{
cout< return;
}
for (int i = pos; i < len; i++)
{
swap(str[pos], str[i]);
_inner_print(str, len, pos+1);
swap(str[pos], str[i]);
}
}
Input: bad
step 1: 3, i = 0 - bad
step 2.: 3, i = 1 - abd
step 2.: 3, i = 2 - dabchar
void... 阅读全帖
a********e
发帖数: 53
30
class Solution {
public:
Write a function to find the longest common prefix string amongst an array
of strings.
The Time complexity is O(logM * N), while M is the size of strs, N is the
shortest length of the str in the strs
而我觉得这个时间复杂度应该是O(MN). 因为isEqual()的复杂度应该是O(M), as T(M
) = 2T(M/2) + O(1). 谢谢。
============================
bool isEqual(vector &strs, int index, int start, int end) {
if(end < start) return true;
if(end == start) {
if(strs[start].size() > index) return ... 阅读全帖
f**********t
发帖数: 1001
31
来自主题: JobHunting版 - LinkedIn Tree serialization question
// Consider this string representation for binary trees. Each node is of the
// form (lr), where l represents the left child and r represents the right
// child. If l is the character 0, then there is no left child. Similarly,
if r
// is the character 0, then there is no right child. Otherwise, the child
can
// be a node of the form (lr), and the representation continues recursively.
// For example: (00) is a tree that consists of one node. ((00)0) is a two-
node
// tree in which the root has a ... 阅读全帖
r***u
发帖数: 1272
32
1. 两幅图片同时动作
PowerPoint的动画效果比较多,但图片只能一幅一幅地动作。如果你有两幅图片要一左
一右或一上一下地向中间同时动作,可就麻烦了。其实办法还是有的,先安置好两幅图
片的位置,选中它们,将之组合起来,成为"一张图片"。接下来将之动画效果设置为"
左右向中间收缩",现在请看一看,是不是两幅图片同时动作了?
2. 滚动文本框的制作
右击工具栏打开"控件工具箱",再点击文本框,而后从"属性"里面把滚动条打开,在TEXT
里面输入文本框的内容.(完成)还可以通过"其他控件"中的SHOCKWAVE FLASH OBJECT 实
现PPT中加入FLASH。
3. 轻松隐藏部分幻灯片
对于制作好的powerpoint幻灯片,如果你希望其中的部分幻灯片在放映时不显示出来,
我们可以将它隐藏。方法是:在普通视图下,在左侧的窗口中,按 Ctrl,分别点击要
隐藏的幻灯片,点击鼠标右键弹出菜单选“隐藏幻灯片”。如果想取消隐藏,只要选中
相应的幻灯片,再进行一次上面的操作即可。
4.在PPT演示文稿内复制幻灯片
要复制演示文稿中的幻灯片,请先在普通视图的“大纲”或“幻灯片”选项中,选择... 阅读全帖
S**I
发帖数: 15689
33
来自主题: JobHunting版 - Microsoft interview question
#include
#include
void reverseStr(char str[], int begin, int end){

char temp;

while(end > begin){
temp = str[begin];
str[begin++] = str[end];
str[end--] = temp;
}
}
void reverseWord(char str[]){

int begin = 0, end = 0, length = (int) strlen(str) - 1;

reverseStr(str, begin, length);

while(end < length){

if(str[end] != ' '){

begin = end;

while(end... 阅读全帖
c******t
发帖数: 391
34
来自主题: JobHunting版 - 明天A家onsite
感谢分享!
第一轮算括号的题,想到两种解法,用counter算左右括号数,以及压栈比较。
//For '(,)' only, use counter, left and right are half of length, e.g. for "
(())", the call
is validParen(2,2,"(())",0).
public static boolean validParen(int left, int right, String str, int
index){
if(left==0&&right==0)return true; //base case
if(str.length()%2==1)return false; //length must be even
if(left>right)return false;
if(left<0||right<0)return false;
if(str.charAt(index)=='(') return validParen(le... 阅读全帖
x*********1
发帖数: 23
35
来自主题: JobHunting版 - Z家programming assessment两题
我写了一个,大家帮看看, 谢谢啦, 请问楼主在哪找到的第二题的code,很想学习
一下
public long stringToLong(String str) {
if (str==null||str.length()==0){
return 0;
}

int sign=1;
Long sum=(long) 0;
int i=0;

while(i i++;
}

if (i==str.length()){
return 0;
}

if (str.charAt(i)=='+'){
... 阅读全帖
s********x
发帖数: 914
36
来自主题: JobHunting版 - 问一下OJ的Anagrams那道题
Time Limit Exceeded了
但感觉已经cache了intermediate result了
是不是这题assume只是26个小写英文字母呢,如果是的话,用array就更快?
贴一下code
class AnagramString {
boolean visited;
String str;
private Map map = null;

AnagramString(String s) {
this.str = s;
}

boolean isAnagram(String s) {
if (this.map == null) {
this.map = new HashMap(this.str.length());
for (int i = 0; i < this.str.length(); i++) {
char c =... 阅读全帖
c***r
发帖数: 280
37
来自主题: JobHunting版 - 请教个G题目
这个思路不错,我给转成了个c++代码如下。只是觉得你的code里面,最后一个if语句
,是否node->right = dfs(str);该移到if外,在一个else if 里面, 这个else if条件
是 else if (i 我转的C++如下:
int curr = 0;
Node* tree_dfs(string str ) {
if(curr >= str.length())
return NULL;
int i = curr;
while(i i++;
Node* node = new Node(str.substr(curr, i-curr));
curr = i+1;
if(i node->left = tree_dfs(str);
}
... 阅读全帖
c***r
发帖数: 280
38
来自主题: JobHunting版 - 请教个G题目
这个思路不错,我给转成了个c++代码如下。只是觉得你的code里面,最后一个if语句
,是否node->right = dfs(str);该移到if外,在一个else if 里面, 这个else if条件
是 else if (i 我转的C++如下:
int curr = 0;
Node* tree_dfs(string str ) {
if(curr >= str.length())
return NULL;
int i = curr;
while(i i++;
Node* node = new Node(str.substr(curr, i-curr));
curr = i+1;
if(i node->left = tree_dfs(str);
}
... 阅读全帖
t*********e
发帖数: 630
39
来自主题: Java版 - 这个有 bug 吗
boolean contains(String str, String substr) {

int strSize = str.length();
int subSize = substr.length();

if(str==null || substr==null){
return false;
}

if(subSize > strSize ){
return false;
}

int i =0;
while(i int j = 0;
int l = i;
while(j if(Character.toLowerCase(str.charAt(i)) == Charact... 阅读全帖
L***Q
发帖数: 508
40
来自主题: JobHunting版 - 问一个老的google面试题
假设str[0..n-1]表示整个字符串。基本思路是从str[0]开始,以每个位置作为substring的起点,从该位置往后直到substring能cover所有字符。那么最多就有n个起点。问题是,如何每次只用O(1)来确定substring长度。
我想可以用一个辅助数组记录每种字符出现在字符串中的位置。例如字符a出现在str[0]和str[20]两个地方。假设当前已经确定以str[0]为起点的substring长度。接下来应该确定以str[1]为起点的substring长度。该substring丢弃了str[0],所以必须要至少在str[20]结束,否则字符a没被cover。这就是基本思路。
我用一个例子解释整个算法。假设字符串包括0到9的数字,整个串如下
0247324596818329654001378
第一步扫描字符串,记录下每种字符位置,如下
0:0,19,20
1:11,21
2:1,5,14
3:4,13,22
4:2,6,18
5:7,17
6:9,16
7:3,23
8:10,12,24
9:8,15
第二步,以str[0]为起点,substring的终点在str[1... 阅读全帖
s*******f
发帖数: 1114
41
来自主题: JobHunting版 - 贡献两道的面试题

void GetAllComb_(char *str, vector *ret){
static vector vc;
if (!*str){
ret->push_back(string(vc.begin(), vc.end()));
return;
}
char *p = str + 1;
while (*p == *str)
++p;
GetAllComb_(p, ret);
for (int i = 0; i < p - str; ++i){
vc.push_back(*str);
GetAllComb_(p, ret);
}
while (!vc.empty() && vc.back() == *str)
vc.pop_back();
}
void GetAllComb(char *str, vector *ret){
if (!str)
re... 阅读全帖
s*******f
发帖数: 1114
42
来自主题: JobHunting版 - 贡献两道的面试题

void GetAllComb_(char *str, vector *ret){
static vector vc;
if (!*str){
ret->push_back(string(vc.begin(), vc.end()));
return;
}
char *p = str + 1;
while (*p == *str)
++p;
GetAllComb_(p, ret);
for (int i = 0; i < p - str; ++i){
vc.push_back(*str);
GetAllComb_(p, ret);
}
while (!vc.empty() && vc.back() == *str)
vc.pop_back();
}
void GetAllComb(char *str, vector *ret){
if (!str)
re... 阅读全帖
s**********e
发帖数: 326
43
贴个我之前写的代码,仿照java里面的源码写的
public static long stringToLong(String str) throws Exception{
if(!chekValid(str)){
throw new Exception("invalid input!!");
}
long limit;
boolean isNegative = false;
int curIndex = 0;
if(str.charAt(0) == '-'){
limit = Long.MIN_VALUE;
isNegative = true;
curIndex = 1;
}else{
limit = -1 * Long.MAX_VALUE;
}
long preLimit = limit/10;
... 阅读全帖
y****e
发帖数: 1012
44
来自主题: JobHunting版 - 问个anagram的问题
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
为啥string vector太大的时候结果是错的呢?
大家帮忙看看吧~~
谢谢!
#include
#include
#include
#include
#include
using namespace std;
class Solution {
public:
bool isAnagram(const string& s1, const string& s2) {
if (s1.length()!=s2.length()) return false;
int ct[256], nunique=0, ncomplete=0;
for (int i=0;i<256;i++){
ct[i... 阅读全帖
e*n
发帖数: 1511
45
【 以下文字转载自 Stock 讨论区 】
发信人: mitbbs2020 (bbc), 信区: Stock
标 题: Re: 【Stragety论坛】Trading with correlations.
发信站: BBS 未名空间站 (Sun Jul 25 19:52:04 2010, 美东)
用option当然可以,但不是必须,
用option可以得到更高的leverage,
但是缺点是散户量小,不容易实现market neutral。
比如1.37的hedge ratio,你要long 137和short 100个才能实现。
13和10就不那么好,而且不容易adjust,看附图就知道不怎么neutral的情况。
说stock不便于对冲,这点我不懂你什么意思。
这本身就是market neutral的strategy。
我测试过DISCA和AXP(paper account),追踪了至少2个月,涨跌幅从来没超过3%的,
但是这反过来说明这不是一对好的pair。
别的pair,没有任何leverage,一年可以做到100%以上的也有,如果我实际做stock到
这个%,
已经很满意了... 阅读全帖
j*****u
发帖数: 1133
46
写的比较乱,应该还有更clean的
static bool IsMatch(string str, string pattern)
{
if (str == null)
throw new ArgumentNullException("str");
if (string.IsNullOrEmpty(pattern))
throw new ArgumentException("pattern null or emptry.");
return MatchRec(str, pattern, 0, 0);
}
static bool MatchRec(string str, string pattern, int sIndex, int pIndex)
{
if (pIndex == pattern.Length)
{
return sIndex == str.Length;
}
char p = pattern[pIndex];
bool isStar = pIndex < pa... 阅读全帖
f*******t
发帖数: 7549
47
来自主题: JobHunting版 - large file的一道题
这个是很基本的数据结构,建议看一下wiki
我前几天实现了它,只有基本的insert和search,没有对查找child list进行优化,代
码贴出来供参考:
#include
#define MAX_LENGTH 64
using namespace std;
static char buff[MAX_LENGTH];
class Trie {
public:
Trie();
Trie(char value);
bool Insert(const char *str);
bool Search(const char *str) const;
void PrintAll(int pos) const;
private:
char _value;
Trie *_sibling;
Trie *_child;
};
Trie::Trie()
{
_value = 0;
_sibling = NULL;
_child = NULL;
}
Trie::Trie(char value)
... 阅读全帖
L***Q
发帖数: 508
48
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;
... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)