avatar
d*4
1
问了一个就是dictionary的题:
给一个dictionary:“hello","world","opt","pot";
一个target: "pto",找出dictionary中的"opt" 和"pot"
avatar
d*4
2
第二题:
给一对票,分别代表出发站和终点站
b-c,c-d,a-b,d-e
排序:
a-b,b-c,c-d,d-e
avatar
j*8
3
这两题看着都还行阿,lz答得如何
avatar
l*a
4
第一题就是pre-processing找anagrams
第二题有点麻烦吧,如果有cycle咋算,另外给定输入一定有结果?

【在 j*****8 的大作中提到】
: 这两题看着都还行阿,lz答得如何
avatar
j*8
5
嗯,从给的例子上看好像都是 end > start
你提的这些点需要和面试官交流一下,

【在 l*****a 的大作中提到】
: 第一题就是pre-processing找anagrams
: 第二题有点麻烦吧,如果有cycle咋算,另外给定输入一定有结果?

avatar
w*5
6
第二题topological sorting?
avatar
l*a
7
基本这个意思,但是LZ条件说得不清楚

【在 w*****5 的大作中提到】
: 第二题topological sorting?
avatar
y*a
8
第一题:
List anagram(Set dict, String target) {
Map> m=new HashMap<>();
for (String s:dict) {
char[] cc=s.toCharArray();
Arrays.sort(cc);
String key=new String(cc);
if (!m.containsKey(key))
m.put(key, new ArrayList());
m.get(key).add(s);
}
char[]cc=target.toCharArray();
Arrays.sort(cc);
String key=new String(cc);
if (m.containsKey(key)) return m.get(key);
return new ArrayList();
}
第二题:
List topSort(String[] tickets) {
Map m=new HashMap<>();
String start=null;
Comparator c=String.CASE_INSENSITIVE_ORDER;
for (String s:tickets) {
String[] items=s.split("-");
if (start==null)start=items[0];
else start=c.compare(items[0], start)<0?items[0]:start;
m.put(items[0], items[1]);
}
List rel=new ArrayList<>();
for (int i=0;iString s=start,t=m.get(start);
rel.add(s+"-"+t);
start=t;
}
return rel;
}
avatar
h*g
9
第一题应该是找permutation吧?dictionary可以遍历么?

【在 l*****a 的大作中提到】
: 第一题就是pre-processing找anagrams
: 第二题有点麻烦吧,如果有cycle咋算,另外给定输入一定有结果?

avatar
m*p
10
如果测试你的代码,你会发现没有考虑有这种票:
b -> d,
b->c,
c->d,
d-> c

【在 y**********a 的大作中提到】
: 第一题:
: List anagram(Set dict, String target) {
: Map> m=new HashMap<>();
: for (String s:dict) {
: char[] cc=s.toCharArray();
: Arrays.sort(cc);
: String key=new String(cc);
: if (!m.containsKey(key))
: m.put(key, new ArrayList());
: m.get(key).add(s);

avatar
h*t
11
第一题用几个异或就出来了。
用不着用sort,hash,contains.add这种复杂的操作吧。

【在 y**********a 的大作中提到】
: 第一题:
: List anagram(Set dict, String target) {
: Map> m=new HashMap<>();
: for (String s:dict) {
: char[] cc=s.toCharArray();
: Arrays.sort(cc);
: String key=new String(cc);
: if (!m.containsKey(key))
: m.put(key, new ArrayList());
: m.get(key).add(s);

avatar
L*e
12
processing the dictionary seems unnecessary. The dictionary could be huge. I
think get the permutation of target and lookup the dict would be sufficient.

【在 y**********a 的大作中提到】
: 第一题:
: List anagram(Set dict, String target) {
: Map> m=new HashMap<>();
: for (String s:dict) {
: char[] cc=s.toCharArray();
: Arrays.sort(cc);
: String key=new String(cc);
: if (!m.containsKey(key))
: m.put(key, new ArrayList());
: m.get(key).add(s);

avatar
W*y
13
为什么第二题看不到了?

【在 d******4 的大作中提到】
: 问了一个就是dictionary的题:
: 给一个dictionary:“hello","world","opt","pot";
: 一个target: "pto",找出dictionary中的"opt" 和"pot"

avatar
h*e
14
题目不清,第一题,target和found string的 啥关系,
第二题, 找出的序列满足什么条件。
给的例子不具有代表性,多给几个例子阿。
avatar
d*4
15
第一题 找到所有字符相同顺序不同的串
第二题 把所有的票按起始站排序,不是按 a b c的顺序排序

【在 h*******e 的大作中提到】
: 题目不清,第一题,target和found string的 啥关系,
: 第二题, 找出的序列满足什么条件。
: 给的例子不具有代表性,多给几个例子阿。

avatar
m*k
16
会有false positive吧

【在 h*******t 的大作中提到】
: 第一题用几个异或就出来了。
: 用不着用sort,hash,contains.add这种复杂的操作吧。

avatar
m*k
17
how about
hashmap, sorted as key, words list as value?


【在 d******4 的大作中提到】
: 第一题 找到所有字符相同顺序不同的串
: 第二题 把所有的票按起始站排序,不是按 a b c的顺序排序

avatar
l*2
18
能具体说下怎么用异或做么?

【在 h*******t 的大作中提到】
: 第一题用几个异或就出来了。
: 用不着用sort,hash,contains.add这种复杂的操作吧。

avatar
j*3
19
mark,好像这两题都常见。。。

【在 d******4 的大作中提到】
: 问了一个就是dictionary的题:
: 给一个dictionary:“hello","world","opt","pot";
: 一个target: "pto",找出dictionary中的"opt" 和"pot"

avatar
h*t
20
#include
#include
using namespace std;
char XOR(string s)
{
char result = 0;
for(int i = 0; iresult ^= s[i];
return result;
}
int main()
{
char targetXOR = XOR("pto");
string dict[]={"hello", "world", "pot", "opt"};
char result = 0;
for (int i = 0; i{
result = XOR(dict[i]) ^ targetXOR;
if(!result)
cout << dict[i] <}
delete dict;
return 0;
}

【在 l*******2 的大作中提到】
: 能具体说下怎么用异或做么?
avatar
a*8
21
ran your program, found that XOR("hello") = 'b', XOR("world") = 'b'
but they are not anagrams
avatar
y*a
22

static List anagram(List dict, String target){
int[] st=new int[256], ex=new int[256];
for (char c:target.toCharArray()) ++ex[c];
List rel=new ArrayList<>();
for (String s:dict) {
Arrays.fill(st, 0);
for (char c:s.toCharArray())++st[c];
if (Arrays.equals(st, ex)) rel.add(s);
}
return rel;
}

static List sortTickets(Listtickets) {
String[][] ts=new String[tickets.size()][2];
for (int i=0;iArrays.sort(ts, new Comparator(){
@Override
public int compare(String[] o1, String[] o2) {
return o1[0].compareTo(o2[0]);
}
});
List rel=new ArrayList<>();
for (int i=0;ireturn rel;
}

【在 d******4 的大作中提到】
: 第一题 找到所有字符相同顺序不同的串
: 第二题 把所有的票按起始站排序,不是按 a b c的顺序排序

avatar
E*1
23
手机党输个Python精简版:
1.
res=[]
for item in dictionary:
if sorted(list(target))==sorted(list(item)):
res.append[item]
return res
2. 定义一个叫Ticket的class,里面含有start和end,人给的那一堆票就是Tickets =
[tix1, tix2, tix3……]
然后:
sorted(Tickets, key=lambda x: x.start)


【在 d******4 的大作中提到】
: 第一题 找到所有字符相同顺序不同的串
: 第二题 把所有的票按起始站排序,不是按 a b c的顺序排序

avatar
w*m
24
def q1(dictionary, target):
return [x for x in dictionary if sorted(x) == sorted(target)]
print q1(["hello","world","opt","pot"], 'pto')
def q2(input):
return sorted(input, key = lambda x: (x[0], x[2]))
print q2(['b-c','c-d','a-b','d-e'])
avatar
b*d
25
mark
avatar
u*r
26
delete 用错了吧?
而且这个异或不能保证出来的结果完全对吧?

【在 h*******t 的大作中提到】
: #include
: #include
: using namespace std;
: char XOR(string s)
: {
: char result = 0;
: for(int i = 0; i: result ^= s[i];
: return result;
: }

avatar
m*g
27
My solution in Java. Permutation first then search
public class FindInDictionary {

private static int numOfPerm;
private static int index;
private static String [] allPerms;

private static void swap (char[] a, int i, int j){
char c = a[i]; a[i]=a[j]; a[j]=c;
}

//recursive way of creating permutation
private static void permutation (char[] a, int n)
{
if (n==1)
{
// System.out.println(String.valueOf(a));
allPerms [index]=String.valueOf(a);
index ++;
return;
}
for (int i=0;iswap (a,i,n-1);
permutation (a,n-1);
swap (a,i,n-1);
}
}

private static int factorial (int n){
int fact = 1;
for (int i=1;i<=n;i++){
fact *= i;
}
return fact;
}
public static void perm (String s){
if (s==null)
return;
else
{
int n = s.length();
int index = 0;
numOfPerm = factorial(n);
allPerms = new String [numOfPerm];
index = 0;
char[] array = s.toCharArray();
permulation (array,n);
}
}

public static void main(String[] args) {
String [] dict = {"hello", "world", "pot", "opt"};
String target = "pto";
perm (target);
for (String targetStr : allPerms)
{
for (String dictStr : dict)
{
if (targetStr.equals(dictStr) )
System.out.println("match found in dictionary: "+dictStr
);
}
}
}
}
avatar
d*r
28
反例:“aaa”,“a”
这题复杂度比较低的做法是用26个素数做hash。

【在 h*******t 的大作中提到】
: #include
: #include
: using namespace std;
: char XOR(string s)
: {
: char result = 0;
: for(int i = 0; i: result ^= s[i];
: return result;
: }

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。