zoukankan      html  css  js  c++  java
  • 算法练习--微软面试题前25题

    1.transform from binary search tree into doubly linked list.

    rule:can not create any new node,just change the "pointer".
        
      10
      / 
     6 14
     /  / 
    4 8 12 16
        
    transformed into:
    4=6=8=10=12=14=16。
        
    data structure of binary search tree:
     struct BSTreeNode
    {
      int m_nValue; // value of node
      BSTreeNode *m_pLeft; // left child of node
      BSTreeNode *m_pRight; // right child of node
    };

    ------------------------------------------------------------

    using inorder traversal to traverse the tree,at the same time,add the node to the list.

    ------------------------------------------------------------

    2write a function called min to get the smallest value of a stack.time complexity of min,push and pop are all O(1).

     1 public class MinStack{
     2     public static void main(String[] args){
     3         /**TEST CODE**/
     4     }
     5     private static Stack<Integer> stack = new Stack<Integer>();
     6     private static Stack<Integer> mini_stack = new Stack<Integer>();
     7     public static void push(Integer value) {
     8         stack.push(value);
     9         if(mini_stack.isEmpty())
    10             mini_stack.push(value);
    11         else if( value < mini_stack.peek())
    12             mini_stack.push(value);
    13     }
    14     public static Integer pop(){
    15         if(stack.isEmpty())
    16             return null;
    17         Integer temp = stack.pop();
    18         if(temp == mini_stack.peek())
    19             mini_stack.pop();
    20         return temp;
    21     }
    22     public static void min(){
    23         System.out.print(mini_stack.peek());
    24     }
    25 }

    ------------------------------------------------------------

    3.求子数组的最大和
    题目:
    输入一个整形数组,数组里有正数也有负数。
    数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。
    求所有子数组的和的最大值。要求时间复杂度为O(n)。

    例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,和最大的子数组为3, 10, -4, 7, 2,
    因此输出为该子数组的和18。

     1 public class MaxSubArray{
     2     public static void main(String[] args){
     3         int[] arr = {1, -2, 3, 10, -4, 7, 2, -5};
     4         System.out.println(get_max_subarray(arr));
     5     }
     6     public static int get_max_subarray(int[] arr){
     7         int now_max = 0 ,max =  0;
     8         for(int i = 0; i < arr.length; i++){
     9             now_max += arr[i];
    10             if(now_max  < 0)
    11                 now_max = 0;
    12             if(now_max > max)
    13                 max= now_max;
    14         }
    15         return max;
    16     }
    17 }

    ------------------------------------------------------------

    4.在二元树中找出和为某一值的所有路径
    题目:输入一个整数和一棵二元树。
    从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
    打印出和与输入整数相等的所有路径。
    例如 输入整数22和如下二元树
      10   
      /    
      5 12   
      /    
      4 7
    则打印出两条路径:10, 12和10, 5, 7。

     1 public class FixedValueBranch
     2 {
     3     /**
     4      * @author 无 名
     5      * @date 2015-12-17
     6      * @Description:查找给定和值得二叉树路径
     7      */
     8     public static void main(String[] args)
     9     {
    10         /** TEST CODE **/
    11     }
    12     private final static Integer DEMAND_SUM_VALUE = 22; 
    13     public static void print_path(BinaryTreeNode parent,Integer sum,String path)
    14     {
    15         Integer value = parent.getValue();
    16         sum += value;
    17         path += "/" + String.valueOf(value);
    18         if(null == parent.getLeft() &&  null == parent.getRight()
    19                  && sum== DEMAND_SUM_VALUE)
    20                 System.out.println(path);
    21         else
    22         {
    23             if(null != parent.getLeft())
    24                 print_path(parent.getLeft(),sum,path);
    25             if(null != parent.getRight())
    26                 print_path(parent.getRight(),sum,path);
    27         }
    28     }
    29 }

    5.查找最小的k个元素
    题目:输入n个整数,输出其中最小的k个。
    例如输入1,2,3,4,5,6,7和8这8个数字,则最小的4个数字为1,2,3和4。

     1 public class GetMinimals
     2 {
     3     /** 
     4      * @author            无           名
     5      * @date                 2015/12/17
     6      * @Description: 求出n个数中最小的k个数
     7      */
     8     public static void main(String[] args)
     9     {
    10         /**TEST CODE**/
    11     }
    12     public static void get_minimals(int[] arr,int k){
    13         int[] mins = new int[k];
    14         int max_flag = 0;
    15         for(int i = 0; i < arr.length; i++){
    16             if(i < k)
    17             {
    18                 mins[i] = arr[i];     //initialize mins[]
    19                 if(mins[i] > mins[max_flag])
    20                     max_flag = i;
    21             }
    22             else{
    23                 for(int j = 0; j < k; j++)
    24                     if(arr[i] < mins[j])
    25                         mins[max_flag] = arr[i]; //exchenge the min value with max value of mins[k]
    26                 for(int j =0; j < k; j++)        //find the max value of mins[k]
    27                     if(mins[j] > mins[max_flag])
    28                         max_flag = j;
    29             }
    30         }
    31         for(int i:mins)
    32              System.out.print(i + " ");
    33     }
    34 }

     第6题
    ------------------------------------
    腾讯面试题:   
    给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数   
    要求下排每个数都是先前上排那十个数在下排出现的次数。   
    上排的十个数如下:   
    【0,1,2,3,4,5,6,7,8,9】

    初看此题,貌似很难,10分钟过去了,可能有的人,题目都还没看懂。   

    举一个例子,   
    数值: 0,1,2,3,4,5,6,7,8,9   
    分配: 6,2,1,0,0,0,1,0,0,0   
    0在下排出现了6次,1在下排出现了2次,   
    2在下排出现了1次,3在下排出现了0次....   
    以此类推..   

    昨天,用c++实现了此题。(*^__^*)   



    第7题
    ------------------------------------
    微软亚院之编程判断俩个链表是否相交
    给出俩个单向链表的头指针,比如h1,h2,判断这俩个链表是否相交。
    为了简化问题,我们假设俩个链表均不带环。

    问题扩展:
    1.如果链表可能有环列?
    2.如果需要求出俩个链表相交的第一个节点列?



    第8题
    ------------------------------------
    此贴选一些 比较怪的题,,由于其中题目本身与算法关系不大,仅考考思维。特此并作一题。
    1.有两个房间,一间房里有三盏灯,另一间房有控制着三盏灯的三个开关,这两个房间是 分割开的,
    从一间里不能看到另一间的情况。
    现在要求受训者分别进这两房间一次,然后判断出这三盏灯分别是由哪个开关控制的。
    有什么办法呢?

    2.你让一些人为你工作了七天,你要用一根金条作为报酬。金条被分成七小块,每天给出一块。
    如果你只能将金条切割两次,你怎样分给这些工人?

    3  ★用一种算法来颠倒一个链接表的顺序。现在在不用递归式的情况下做一遍。
      ★用一种算法在一个循环的链接表里插入一个节点,但不得穿越链接表。
      ★用一种算法整理一个数组。你为什么选择这种方法?
      ★用一种算法使通用字符串相匹配。
      ★颠倒一个字符串。优化速度。优化空间。
      ★颠倒一个句子中的词的顺序,比如将“我叫克丽丝”转换为“克丽丝叫我”,实现速度最快,移动最少。
      ★找到一个子字符串。优化速度。优化空间。
      ★比较两个字符串,用O(n)时间和恒量空间。
      ★假设你有一个用1001个整数组成的数组,这些整数是任意排列的,但是你知道所有的整数都在1到1000(包括1000)之间。此外,除一个数字出现两次外,其他所有数字只出现一次
    。假设你只能对这个数组做一次处理,用一种算法找出重复的那个数字。
    如果你在运算中使用了辅助的存储方式,那么你能找到不用这种方式的算法吗?
      ★不用乘法或加法增加8倍。现在用同样的方法增加7倍。


    第9题
    -----------------------------------
    判断整数序列是不是二元查找树的后序遍历结果
    题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。
    如果是返回true,否则返回false。
    例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:
      8
      / 
      6 10
      /  / 
      5 7 9 11
    因此返回true。
    如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。



    第10题
    ---------------------------------
    翻转句子中单词的顺序。
    题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。
    为简单起见,标点符号和普通字母一样处理。
    例如输入“I am a student.”,则输出“student. a am I”。

     1 public class SentenceReversal {
     2     /**
     3      * @author              无       名
     4      * @date                2015/12/19
     5      * @description 翻转句子单词
     6      */
     7     public static void main(String[] args){
     8         /**TEST CODE**/
     9         String str_input = "I will be the King.";
    10         String[] sub_strs = str_input.split(" ");
    11         int len = sub_strs.length;
    12         String[] target_strs = new String[len];
    13         for(int i = len-1; i >= 0; i--){
    14             target_strs[len - i -1] = sub_strs[i];
    15         }
    16         for(int i = 0; i < len; i++){
    17             System.out.print(target_strs[i] + " ");
    18         }
    19     }
    20 }

    第11题

    ------------------------------------
    求二叉树中节点的最大距离...

    如果我们把二叉树看成一个图,
    父子节点之间的连线看成是双向的,
    我们姑且定义"距离"为两节点之间边的个数。
    写一个程序,
    求一棵二叉树中相距最远的两个节点之间的距离。


    第12题
    题目:求1+2+…+n,
    要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。

    转载于http://blog.csdn.net/shoulinjun/article/details/20564095




    第13题:
    题目:输入一个单向链表,输出该链表中倒数第k个结点。链表的倒数第0个结点为链表的尾指针。
    链表结点定义如下:   
    struct ListNode
    {
      int m_nKey;
      ListNode* m_pNext;
    };

    引自:http://blog.csdn.net/philofly/article/details/4852392


    第14题:
    题目:输入一个已经按升序排序过的数组和一个数字,
    在数组中查找两个数,使得它们的和正好是输入的那个数字。
    要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。
    例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4和11。

     1     public static void get_two_num(int[] arr,int target_value){
     2         int r_pointer = arr.length - 1;            //pointed to the stack of small values
     3         int l_pointer = 0;                                     //pointed to the stack of big values
     4         while(true){
     5             if(l_pointer == r_pointer){
     6                 System.out.print("do not exit");
     7                 break;
     8             }
     9             if(arr[r_pointer] + arr[l_pointer] < target_value)
    10                 l_pointer++;
    11             else if(arr[r_pointer] + arr[l_pointer] > target_value)
    12                 r_pointer--;
    13             else{
    14                 System.out.print(arr[l_pointer] +"+"+ arr[r_pointer]+"="+ target_value);
    15                 break;
    16             }
    17         }

    第15题:求二叉树镜像

    即在转换后的二元查找树中,左子树的结点都大于右子树的结点。
    用递归和循环两种方法完成树的镜像转换。   
    例如输入:
      8
      / 
      6 10
     / /
    5 7 9 11

    输出:
      8
      / 
     10 6
     / /
    11 9 7 5

    定义二元查找树的结点为:
    struct BSTreeNode // a node in the binary search tree (BST)
    {
      int m_nValue; // value of node
      BSTreeNode *m_pLeft; // left child of node
      BSTreeNode *m_pRight; // right child of node
    };

     1     /** 
     2      *@author           无                 名
     3      *@date                2015/12/25
     4      * @Description:二叉树镜像转换
     5      */
     6     public static void tree_turn(BinaryTreeNode root){
     7         Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();
     8         queue.add(root);
     9         while(!queue.isEmpty()){
    10             BinaryTreeNode node = queue.poll();
    11             reset_childs(node);
    12             if(null != node.getLeft())
    13                  queue.add(node.getLeft());
    14             if(null != node.getRight())
    15                 queue.add(node.getRight());
    16         }
    17     }
    18     public static void reset_childs(BinaryTreeNode parent){
    19         BinaryTreeNode temp = parent.getLeft();
    20         parent.setLeft(parent.getRight());
    21         parent.setRight(temp);
    22     }

    第16题:
    题目(微软):
    输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。   
    例如输入
      8
      / 
     6 10
    /  / 
    5 7 9 11

    输出8 6 10 5 7 9 11。

     1     /**
     2      * @author 无名
     3      * @date 2015/12/21
     4      * @Description: 层次遍历
     5      */
     6     public static void level_traverse(BinaryTreeNode root)
     7     {
     8         if(null == root)
     9             return;
    10         Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();
    11         queue.add(root);
    12         while (!queue.isEmpty())
    13         {
    14             BinaryTreeNode temp = queue.peek().getLeft();
    15             if (null != temp)
    16                 queue.add(temp);
    17             temp = queue.peek().getRight();
    18             if (null != temp)
    19                 queue.add(temp);
    20             System.out.print(queue.poll().getValue() + " ");
    21         }
    22     }

    第17题:
    题目:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。   
    分析:这道题是2006年google的一道笔试题。
    entity:

     1 package com.entity;
     2 
     3 public class ChIntNode {
     4     private char ch;
     5     private int occur_num;
     6     public char getCh() {
     7         return ch;
     8     }
     9     public void setCh(char ch) {
    10         this.ch = ch;
    11     }
    12     public int getOccur_num() {
    13         return occur_num;
    14     }
    15     public void setOccur_num(int occur_num) {
    16         this.occur_num = occur_num;
    17     }
    18 }

    algorithm:

     1 package com.micro;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import com.entity.ChIntNode;
     7 
     8 public class Question17 {
     9     /**
    10      * @author           无        名
    11      * @date             2015/12/20
    12      */
    13     public static void main(String[] args) {
    14         char[] arr = {'a','b','a','c','c','d','e','f','f'};
    15         get_the_only_char(arr);
    16     }
    17     public static void get_the_only_char(char[] arr){
    18         if(null == arr || 0==arr.length)
    19             return;
    20         List<ChIntNode> list =  new ArrayList<ChIntNode>(); 
    21         for(int i = 0; i < arr.length;i++){
    22             if(list.isEmpty()){
    23                 ChIntNode cin = new ChIntNode();
    24                 cin.setCh(arr[i]);
    25                 cin.setOccur_num(1);
    26                 list.add(cin);
    27             }
    28             Boolean flag = Boolean.TRUE;
    29             for(ChIntNode cin:list){
    30                 if(arr[i] == cin.getCh()){
    31                     cin.setOccur_num(cin.getOccur_num()+1);
    32                     flag = Boolean.FALSE;
    33                 }
    34             }
    35             if(flag){
    36                 ChIntNode cin = new ChIntNode();
    37                 cin.setCh(arr[i]);
    38                 cin.setOccur_num(1);
    39                 list.add(cin);
    40             }
    41         }
    42         for(ChIntNode cin: list){
    43             if(1 == cin.getOccur_num()){
    44                 System.out.print(cin.getCh());
    45                 break;
    46             }
    47         }
    48     }
    49 }

    第18题:
    题目:n个数字(0,1,…,n-1)形成一个圆圈,从数字0开始,
    每次从这个圆圈中删除第m个数字(第一个为当前数字本身,第二个为当前数字的下一个数字)。
    当一个数字删除后,从被删除数字的下一个继续删除第m个数字。
    求出在这个圆圈中剩下的最后一个数字。
    July:我想,这个题目,不少人已经 见识过了。

    #include<stdio.h>  
    #include<malloc.h>  
    #define NULL 0  
    #define LEN sizeof(struct Josephus)  
    struct Josephus  
    {  
        int num;  
        struct Josephus *next;  
    };  
    void main()  
    {  
        printf("约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在");  
        printf("一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开");  
        printf("始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。");  
        printf("
    ");  
        int m,n,k;  
        int i;  
        printf("Please input the values of m,n,k.");  
        scanf("%d,%d,%d",&m,&n,&k);  
        struct Josephus *p0,*p,*head;  
        head=NULL;  
        for(i=0;i<n;i++)  
        {  
            p=(struct Josephus*)malloc(LEN);  
            p->num=i;  
            if(head==NULL)  
            {  
                head=p;  
                p0=p;  
            }  
            else  
            {  
                p0->next=p;  
                p0=p;  
            }  
        }  
        p->next=head;  
        p=head;  
        for(i=0;i<k;i++)  
        {  
            p0=p;  
            p=p->next;  
        }  
        while(p->next!=p)  
        {  
            for(i=0;i<m-1;i++)  
            {  
                p0=p;  
                p=p->next;  
            }  
            p0->next=p->next;  
            printf("被删除的元素:%4d ",p->num);  
            p=p0->next;  
        }  
        printf("
    最后被删除的元素是:%4d",p->num);  
    }  



    第19题:
    题目:定义Fibonacci数列如下:   
      / 0 n=0
    f(n)= 1 n=1
       f(n-1)+f(n-2) n=2

    输入n,用最快的方法求该数列的第n项。
    分析:在很多C语言教科书中讲到递归函数的时候,都会用Fibonacci作为例子。
    因此很多程序员对这道题的递归解法非常熟悉,但....呵呵,你知道的。。


    第20题:
    题目:输入一个表示整数的字符串,把该字符串转换成整数并输出。
    例如输入字符串"345",则输出整数345。

     1 #include<stdio.h>
     2 #include<math.h>
     3 /**
     4 *@author 无  名
     5 *@date  2015/12/25
     6 */
     7 bool str_to_int(char* str_inpt)
     8 {
     9     if(NULL == str_inpt)
    10         return false;
    11     char* p_char = str_inpt;
    12     int final_num = 0;
    13     int digit = 0;   int flag;
    14     if(*p_char == '-')
    15     {
    16         p_char++;
    17         flag = -1;
    18     }
    19     else if(*p_char == '+')
    20     {
    21         p_char++;
    22         flag = 1;
    23     }
    24     while(*(p_char+1) != '')
    25     {
    26         p_char++;
    27         digit++;
    28     }
    29     for(int i = 0; i <= digit; i++)
    30     {
    31         //判断是否有非法输入
    32         if(*p_char < '0' || *p_char > '9')
    33             return false;
    34         int lastbit = *p_char - '0';
    35         final_num += lastbit*pow(10,i);
    36         p_char--;
    37     }
    38     if(flag == -1)
    39         final_num = -final_num;
    40     printf("%d",final_num);
    41     return true;
    42 }
    43 
    44 void main()
    45 {
    46       char str[] = "-78767345";
    47       str_to_int(str);
    48 }


    第21题
    2010年中兴面试题
    编程求解:
    输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数,
    使其和等于 m ,要求将其中所有的可能组合列出来.

     1     /**
     2      * @author   无      名
     3      * @date      2015/12/19
     4      */
     5     public static void main(String[] args) {
     6         /** TEST CODE **/
     7         System.out.println("1到25个数字中和为35的全部数列");
     8         select_mins(35, 20, new ArrayList<Integer>());
     9     }
    10     public static void select_mins(int total, int n, List<Integer> arr) {
    11         if (0 == total) {
    12             System.out.println(arr);
    13             return;
    14         }
    15         if (total < 0 || n < 0)
    16             return;
    17         select_mins(total, n - 1, arr);
    18         List<Integer> new_arr = new ArrayList<Integer>(arr);
    19         new_arr.add(n);
    20         select_mins(total - n, n - 1, new_arr);
    21     }
    22 }

    第22题:
    有4张红色的牌和4张蓝色的牌,主持人先拿任意两张,再分别在A、B、C三人额头上贴任意两张牌,
    A、B、C三人都可以看见其余两人额头上的牌,看完后让他们猜自己额头上是什么颜色的牌,
    A说不知道,B说不知道,C说不知道,然后A说知道了。
    请教如何推理,A是怎么知道的。
    如果用程序,又怎么实现呢?



    第23题:
    用最简单, 最快速的方法计算出下面这个圆形是否和正方形相交。"   
    3D坐标系 原点(0.0,0.0,0.0)
    圆形:
    半径r = 3.0
    圆心o = (*.*, 0.0, *.*)

    正方形:
    4个角坐标;   
    1:(*.*, 0.0, *.*)
    2:(*.*, 0.0, *.*)
    3:(*.*, 0.0, *.*)
    4:(*.*, 0.0, *.*)



    第24题:
    链表操作,
    (1).单链表就地逆置,
    (2)合并链表

     1     public static void reverse(Node root){
     2         Node n1,n2,n3;
     3         n1 = root;
     4         n2 = root.getNext();
     5         if(null == n2)
     6             return;
     7         n3 = n2.getNext();
     8         n1.setNext(null);
     9         if(null == n3){
    10             n2.setNext(n1);
    11             return;
    12         }
    13         while(null != n3.getNext()){
    14             n2.setNext(n1);
    15             n1 = n2; n2 = n3;
    16             n3 = n3.getNext();
    17         }
    18         n3.setNext(n2);
    19         n2.setNext(n1);
    20     }

    第25题:
    写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
    功能:
    在字符串中找出连续最长的数字串,并把这个串的长度返回,
    并把这个最长数字串付给其中一个函数参数outputstr所指内存。
    例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,
    outputstr所指的值为123456789

  • 相关阅读:
    DataSet数据导出为Excel文档(每个DataTable为一个Sheet)
    K2 Blackpearl 4.6.8 安装步骤详解
    解决未能从程序集xxx中加载类型System.ServiceModel.Activation.HttpModule的问题
    将博客搬至CSDN
    PMS-授权中心
    如何从现有版本1.4.8升级到element UI2.0.11
    Maven私有仓库: 发布release版本报错:Return code is: 400, ReasonPhrase: Repository does not allow upd ating assets: maven-releases.
    spring boot + dubbo开发遇到过的异常
    java,javascript中的url编码
    SpringBoot favicon.ico
  • 原文地址:https://www.cnblogs.com/rixiang/p/5000230.html
Copyright © 2011-2022 走看看