創新工場筆試

1將鏈表中的所有元素爲奇數的節點移到元素爲偶數節點的前面,並使奇數之間順序反轉,偶數之間順序反轉。


示例:

交換前鏈表的順序           交換後鏈表的順序

4→5→7→1→6   ==>  1→7→5→6→4

1                ==>  1                   (鏈表僅含一個元素)

2→1            ==>  1→2 

                   ==>                        (鏈表爲空)

 

C/C++:

鏈表節點定義爲:

struct node {

struct node *next;

int value;

};

struct node *swap(struct node *list);

Java:

鏈表節點定義爲:

class Node {

public Node next;

public int value

}

Node swap(Node list)

 

注意點和要求如下:

1. swap函數要求對節點的指針/引用進行操作(不得創建任何新的鏈表節點)

2. 不得使用任何庫函數/API,如需使用類似功能請自行實現

3. 不得將鏈表轉化爲其他類型數據結構再進行交換,如數組等

 

2給一個正整數 n, 找到若干個完全平方數(比如1, 4, 9, ... )使得他們的和等於 n。你需要讓平方數的個數最少。

給出 n = 12, 返回 3 因爲 12 = 4 + 4 + 4

給出 n = 13, 返回 2 因爲 13 = 4 + 9

第一題:

class Node

{

    public Node next;

    publicintvalue;

//  public Node(int value,Node next)

//  {

//      this.next=next;

//      this.value = value;

//  }

}

publicclass Factory

{

    publicstaticvoid main(String[] args)

    {

//      Node a1 = new Node(4,null);

//      Node a2 = new Node(5,null);

//      Node a3 = new Node(7,null);

//      Node a4 = new Node(1,null);

//      Node a5 = new Node(6,null);

//      a1.next=a2;

//      a2.next=a3;

//      a3.next=a4;

//      a4.next=a5;

//      a1 = swap(a1);

//      print(a1);

    }

    publicstatic Node swap(Node list)

    {

        if(list == null)

            return list;

        if(list.next==null)

            return list;

        Node first = list;

        Node ou = null,head2 = null;

        Node ji = null,head1 = null;

        Node next=null;

        int i=0,j=0;

        while(first!=null)

        {

            if(first.value%2!=0)

            {

                i++;

                if(i==1)

                {

                    ji=first;

                    head1 =ji;

                }

                else

                {

                    ji.next=first;

                    ji=ji.next;

                }

            }

            else

            {

                j++;

                if(j==1)

                {

                    ou=first;

                    head2 =ou;

                }

                else

                {

                    ou.next=first;

                    ou=ou.next;

                }

            }

            first = first.next;

        }

        ji.next=null;

        ou.next=null;

        next=head1=swap2(head1);

        head2=swap2(head2);

        while(head1.next!=null)

            head1=head1.next;

        head1.next=head2;

        return next;

    }

    publicstatic Node swap2(Node list)

    {

        Node head =list;

        Node p = list.next;

        Node next =p;

        head.next=null;

        while(p!=null)

        {

            next = p.next;

            p.next=head;

            head =p;

            p=next;

        }

        return head;

    }

    publicstaticvoid print(Node list)

    {

        while(list!=null)

        {

            System.out.print(list.value+" ");

            list=list.next;

        }

    }

   

}

第二題:

//import java.util.Scanner;

publicclass Factory2

{

    publicstaticvoid main(String[] args)

    {

//      Scanner sc = new Scanner(System.in);

//      while(sc.hasNext())

//      {

//          System.out.println(compute(sc.nextInt()));

//      }

    }

    publicstaticint compute(int N)

    {

             int[] a = newint[N+1];

             a[0] = 1;a[1] = 1;

             if(N==1)

             return 1;

             if(sqrt(N))

             return 1;

             for(int i=2;i<=N;++i)

             {

                 if(sqrt(i))

                 {

                     a[i]=1;

                     continue;

                 }

                 a[i] = a[i-1]+1;

                 for(int j=i-1;j>=i/2;--j)

                 {

                     if(a[i]>=(a[j]+a[i-j]))

                       a[i]=(a[j]+a[i-j]);

                 }

             }

             return a[N];

    }

    publicstaticboolean sqrt(int n)

    {

            for(int i=1;i<=Math.sqrt((double)n);i++)

            {

                 if(i*i == n)

                      returntrue;

            }

            returnfalse;

    }

//  public static void print(int[] a)

//  {

//      for(int i=0;i<a.length;i++)

//      {

//              System.out.print(a[i]+"");

//      }

//      System.out.println();

//  }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章