创新工厂的笔试题目,大家一起来讨论一下。
1、把输入的字符串转置。比如输入“I love YOU too ”,则输出“too YOU love I”。 
2,输入一个整数,计算出比该整数大的最小的质数。
3,输入一个数组,生成一个二叉排序树。

解决方案 »

  1.   

    1.把字符串按照空格断开,然后倒序输出split()  for(int index = split().size -1;index >=0 ; index --){ ^^}
      

  2.   

    我也分享一些Java面试题吧 变态海量+专业
    分类特别全http://www.mianwww.com/html/category/it-interview/java
      

  3.   

    1用空格分隔成数组,然后将数组倒序,最后再将数组组成字符串
    2
    boolean issu(int index){
    for(int i=2;i<index;i++){
    if(index%i==0){
    return true;
    }
    }
    return false;
    }
    main(){
    int i=10;
    int j=i+1;
    while(!issu(j)){
    j++;
    }
    System.out.println(j);
    }
      

  4.   

    我只做第三个
    /**
     * 二叉排序树
     * 
     * @author July
     * 
     */
    public class T {
    public static void main(String[] args) {
    int[] data = { 8, 1, 7, 8, 5, 6, 4, 8, 9, 3, 0, 2 };
    Tree t = new Tree(null, null, data[0]);
    for (int i = 1; i < data.length; i++) {
    t.insert(data[i]);
    }
    t.print();
    }
    }class Tree {
    Tree left, right;
    int value; public Tree(Tree left, Tree right, int item) {
    this.left = left;
    this.right = right;
    this.value = item;
    } public void insert(int value) {
    Tree root = this;
    Tree node = new Tree(null, null, value);
    while (true) {
    if (root.value > value) {
    if (root.left == null) {
    root.left = node;
    return;
    }
    root = root.left;
    } else {
    if (root.right == null) {
    root.right = node;
    return;
    }
    root = root.right;
    }
    }
    } public void print() {
    if (left != null) {
    left.print();
    }
    System.out.println(value + " ");
    if (right != null) {
    right.print();
    }
    }
    }
      

  5.   

    1、把输入的字符串转置。比如输入“I love YOU too ”,则输出“too YOU love I”。 
     :把这个字符串压入栈,然后打出来就可以了(先进后出的原理)
      

  6.   

    我想了想 应该把我写的类Tree的名字改成TreeNode
      

  7.   

    第二题:
    int m 输入的那个整数
    int n ;比m大的最小质数public void printMinNumber()
    {
      for(int i = m + 1;;m++)
      {
          if(check(m))
          {
             n = m;
             System.out.print("比M大的最小质数n为:" + n);
          }
      }
    }
    public boolean check(int number)
    {
      for(int i = 2; i <= number / 2; i++)
      {
         if(number % i == 0)
         {
           return true;
         } 
      }
      return false;
    }
      

  8.   

    上面的printMinNumber() 方法中在打出n之后要return 不然是个死循环了
      

  9.   

    public boolean isPrime(int n) {
    if (n < 0)
    return isPrime(-n);
    if (n <= 1)
    return false;
    if (n == 2)
    return true;
    int sqrt = (int) Math.sqrt(n);
    for (int i = 2; i <= sqrt; i++)
    if (n % i == 0)
    return false;
    return true;
    }public int getMinPrime(int n) {
    while (!isPrime(++n));
    return n;
    }这个是我的第二题
      

  10.   

    第二题:
    public class MinPrime { public static void main(String[] args) {
    System.out.println(getMinPrime(13));
    }

    public static int getMinPrime(int digit){
    digit++;
    int half = (int)Math.sqrt(digit);
    int i = 2;
    for(;i<=half;i++){
    if(digit%i==0)
    return getMinPrime(digit++);
    }
    return digit;
    }
    }
      

  11.   

    重新发过public class T {
    public static void main(String[] args) {
    int[] data = { 8, 1, 7, 8, 5, 6, 4, 8, 9, 3, 0, 2 };
    TreeNode t = new TreeNode(data[0]);
    for (int i = 1; i < data.length; i++) {
    t.insert(data[i]);
    }
    t.print();
    }
    }class TreeNode {
    TreeNode left, right;
    int value; public TreeNode(int value) {
    this.value = value;
    } public void insert(int value) {
    TreeNode root = this;
    TreeNode node = new TreeNode(value);
    while (true) {
    if (root.value > value) {
    if (root.left == null) {
    root.left = node;
    return;
    }
    root = root.left;
    } else {
    if (root.right == null) {
    root.right = node;
    return;
    }
    root = root.right;
    }
    }
    } public void print() {
    if (left != null) {
    left.print();
    }
    System.out.println(value + " ");
    if (right != null) {
    right.print();
    }
    }
    }
      

  12.   

    之前李开复来我们学校做宣讲时说过创新工场面试不强调
    算法,google则强调算法,看来果真如此,题目蛮简单的
      

  13.   

    这个题其实就是对数组元素的排序呢,比起诸如冒泡、选择、插入等排序方法效率怎么样呢?
    还有 return 能否改成break,循环里用return 不太好吧,效果应该是一样的,跳出循环后,这个方法就结束,自然也就return了;
      

  14.   

    回楼上 这里break和return其实一样的 因为只有一层循环而且while后面没有语句了
    循环里用return从语法上来说是没完全没有问题的 有很多递归算法都有这种写法啊如果说排序效率的话 我想平均说来还是快速排序最好 如果数据量很小可以选择插入排序
      

  15.   

    第一题 public static void function(String s) {
    String[] s1 = s.split("\\s");
    String s2 = "";
    for (int i = s1.length - 1; i >= 0; i--) {
    s2 += s1[i] + " ";
    }
    s2 = s2.substring(0, s2.length() - 1);
    System.out.println(s2);
    } public static void main(String[] args) {
    String s = "I love YOU too";
    function(s);
    }
      

  16.   

    感觉第二题还是打表比较快
    private static int biggerPrime(int num){
    int count = 0;
    int[] tag = new int[2*num];
    int[] p = new int[num];

    for(int i=0; i<tag.length; i++){
    tag[i] = 0;
    }

    for(int i=2; i<2*num; i++){
    if(tag[i] == 0){
    if(i > num) return i;
    p[count++] = i;
    }
            for(int j=0; (j < count && i*p[j] < 2*num && (p[j] <= tag[i] || tag[i] == 0)); j++)
             tag[i*p[j]]=p[j]; 
    }
    return -1;
    }