×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

P1 can be easily resolved with a recursive function; P2 only need a loop.

1. This problem can be easily resolved with a recursive function:

Set combination(Sequence s) {
Set result = empty set;
if( ! s.isEmpty()) {
Char c = s.head;
Sequence tail = s.tail;
Set temp = combination(tail);
retult = c union temp;
}
return result;
}

2. Let S be the sentence stored in an array of words;

Let W be the additional variable that can hold a word;
Let i = 1; and j = S.length; // we do not count i and j for they are scale variables
while( i < j ) {
W = S[i];
S[i] = S[j];
S[j] = W;
i = i+1;
j = j - 1;
}
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 最近参加了一个面试考试,其中考到几道关于算法的题目,感觉没答好, 所以拿出来跟大家请教请教.
    1. 设想有N个字母,请编个算法写出所以的组合
    比如 N=3, 有 a, b, c三个字母,则组合是ab,ac,bc,abc.
    当N=4, 则有a,b,c,d四个字母,组合为 ab,ac,ad,bc,bd,cd,abc,abd,bcd.
    2. 一句话,比如 “I love this game”, 编个算法把这句话变为“game this love I”.
    要求除了用一个变量外不能再占用别的内存空间。
    • 第一个算法是垃圾,不多说了。第二个的诀窍是group flipping:先是整个句子flip,然后是每个单词flip。
    • P1 can be easily resolved with a recursive function; P2 only need a loop.
      1. This problem can be easily resolved with a recursive function:

      Set combination(Sequence s) {
      Set result = empty set;
      if( ! s.isEmpty()) {
      Char c = s.head;
      Sequence tail = s.tail;
      Set temp = combination(tail);
      retult = c union temp;
      }
      return result;
      }

      2. Let S be the sentence stored in an array of words;

      Let W be the additional variable that can hold a word;
      Let i = 1; and j = S.length; // we do not count i and j for they are scale variables
      while( i < j ) {
      W = S[i];
      S[i] = S[j];
      S[j] = W;
      i = i+1;
      j = j - 1;
      }
      • 2, 先是整个句子flip,然后是每个单词flip
        • 多谢各位,我想问一下大家平常都怎么研究这些算法的 有什么书籍或者资料可以推荐吗?
          • google
    • 2.I think the purpose is to test your knowlege of using double-linked list
      • i don't think so.
    • 第一题我以前做过,链接
    • 第二题更简单吧,不知这样行不行,只用了一个临时变量 i, 没用第二个变量,C#
      string xx = "i love this game";
      for (int i = xx.Split(' ').Length - 1; i >= 0; i--)
      {
      Console.Write(xx.Split(' ')[i] + " ");
      }
      • you are really not on the right track at all. Usually, this kinda questions aim at testing whether people can think in a programmatical way.
        You really needs to think over these questions in algorithm patterns instead of benefitting from .NET features or STL. Though, in the really work, ,NET features and STL do help make things easier.

        In the extreme case, the interviewers can simply stop you by asking you not to use .NET features or they even ask you implement Split().
        • 俺知道你的意思,但只用一个变量......这是比较难(如果把循环内的临时变量也算上的话)