2019互联网笔试题编程答案部分

顺时针打印M*n的矩阵的数字 用空格隔开,

输入如

3

3

1 2 3 4 5 6 7 8 9

static int[] spiralOrder(int[][] arr) {
        int [] res = new int[arr.length*arr[0].length];
        int count = 0;
        if(arr == null || arr.length == 0)
            return null;
        int startX = 0, startY = 0;
        int endX = arr[0].length - startX - 1, endY = arr.length - startY - 1;
        while(startX * 2 < arr[0].length && startY * 2 < arr.length) {
           for(int i = startX; i <= endX; ++i) {
               res[count++] = arr[startY][i];
           }
           if(startY < endY) {
              for(int i = startY + 1; i <=endY; ++i) {
                 res[count++] = arr[i][endX];
              }
           }  
           if(startX < endX && startY < endY) {
               for(int i = endX - 1; i >= startX; --i) {
                  res[count++] = arr[endY][i];
               }
           }
           if(endY > (startY + 1) && startX < endX) {
               for(int i = endY - 1; i > startY; --i) {
                   res[count++] = arr[i][startX];
               }
           }
          startX++;
          startY++;
          endX--;
          endY--;
       }
       return res;
    }

Java replace 源码

    public String replace(char oldChar, char newChar) {
        if (oldChar != newChar) 
        {
            int len = value.length;//value就是一个数组,这里得到string字符串的长度
            int i = -1;
            char[] val = value; /* avoid getfield opcode */

            while (++i < len)      //先确保字符串中有旧的字符
            {
                if (val[i] == oldChar) 
                {break;}
            }

            if (i < len) 
            {
                char buf[] = new char[len];
                for (int j = 0; j < i; j++) 
                {
                    buf[j] = val[j];
                }
                while (i < len) {
                    char c = val[i];
                    buf[i] = (c == oldChar) ? newChar : c;
                    i++;
                }
                return new String(buf, true);
            }
        }
        return this;
    }

阿里

import java.util.Scanner;

public class Main{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int result=getResult(n-1,n)+getResult(n-2,n)+getResult(n-3,n);
        System.out.println(result);
    }
    public static int getResult(int n,int m)
    {
        if(n==0) return 1;
        if(n==1 || n==2) return 1;
        if(n==m-1)
        {
            return getResult(n-2,n)+getResult(n-3,n);
        }
        else
        {
            return getResult(n-1,n)+getResult(n-2,n)+getResult(n-3,n);
        }
    }
}

网易有道

1-n栋房子标号相差1 为相邻,k栋房子已经有住户,你需要所住的房子两边都有住户,求最小的可能符合的和最大符合房子数

###---最小为0 #-#-# -最大为2

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int i = 0; i < t; i++) {
            int min = 0, max;
            int n = in.nextInt();
            int k = in.nextInt();
            if(n % 2 == 0) {
                if(k > n / 2) {
                    max = n - k;
                } else {
                    max = k - 1;
                }
            } else {
                if(k > n / 2 + 1) {
                    max = n - k;
                } else {
                    max = k - 1;
                }
            }
            if(n < 3) {
                max = 0;
            }
            if(max < 0) {
                max = 0;
            }
            System.out.println(min + " " + max);
        }
    }
}

头条第三次笔试

1,给定字符串输出字符串中不重复的最长子串的长度 例如:输入‘acddw’ 输出3  输入‘bbbb’ 输出 1

思路:两层循环,从头开始遍历,如果没有重复则用temp不断添加记录不重复的子串,有重复则立即停止,子串放到res_list,最后排序得到最长子串。

def solution(one_str):
    res_list=[]
    length=len(one_str)
    for i in range(length):
        tmp=one_str[i]
        for j in range(i+1, length):
            if one_str[j] not in tmp:
                tmp+=one_str[j]
            else:
                break
        res_list.append(tmp)
    res_list.sort(lambda x,y:cmp(len(x),len(y)))
    return res_list[-1]
ss=solution(s)
print len(ss)

猜你喜欢

转载自blog.csdn.net/a1084958096/article/details/82559748
今日推荐