今日头条一二题我做的答案分享一下
发布于 2017-03-30 21:40 3092 次浏览 0 赞 最后一次编辑是 2017-03-30 21:46 来自 笔试面试  

//第一题,比较简单,没写注释

#include <iostream>

using namespace std;


int main()

{

    int n; //数组长度

    cin>>n;


    int *num = new int[n];

    for(int i=0; i<n; i++)

    {

        cin>>num[i];

    }

    int f,l;

    int first = 0;

    int last = 0;

    int i,j;

    int flag = 0;

    int maxLen = 0;

    int maxN = 0;

    for(first=0; first<n-1; first++)

    {

        flag = 0; //上升

        last = first + 1;

        maxN = 0;

        while(last < n)

        {

            if(flag == 0) {

                if(num[last] > num[last-1])

                {

                    last++;

                } else

                {

                    maxN = last;

                    last++;

                    flag = 1;

                }

            } else

            {

                 if(num[last] < num[last-1] && last - 2 != first)

                {

                    last++;

                } else

                {

                    break;

                }

            }

        }

       // cout<<first<<" "<<last<<";   ";


        if(last - first > maxLen && last - 1 != maxN && maxN != 0)

        {

            maxLen = last - first;

            f = first;

            l = last - 1;

        }


    }

    if(maxLen > 2 && n > 2)

    {

        cout<<f<<" "<<l;

    }else

    {

        cout<<-1<<" "<<-1;

    }



    return 0;

}

//第二题

package play;


import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;


public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int n = in.nextInt(); //N个句子

int m = in.nextInt(); //m个查询句子

String zhan = in.nextLine();  //用于接收两个整数后面的换行

List<String> s = new ArrayList<>();  //n个句子

List<String> q = new ArrayList<>();  //m个查询句子

int[] word = new int[n];  //每个句子的单词数目

for(int i=0; i<n; i++) {

s.add(in.nextLine());

word[i] = 0;

}

for(int i=0; i<m; i++) {

q.add(in.nextLine());

}

String[][] str = new String[n][32];  //存储n个句子拆分的单词,每句最多32个单词

for(int i=0; i<n; i++) {

word[i] = s.get(i).split(" ").length;

str[i] = s.get(i).split(" ");

}

int j,k,l;

int count;  //当前句子对应的单词个数

int max = 0;  //所有对应的单词个数最大值

int flag = 0;  //对应单词最多的那句话的标号

for(int i=0; i<m; i++) {

String[] qury = q.get(i).split(" ");  //将查询句子拆分为单词

max = 0;

flag = 0;

for(k=0; k<n; k++) { //n个句子遍历

count = 0;//当前句子对应的单词个数置0

for(j=0; j<qury.length; j++)  //查询句子单词遍历

for(l=0; l<word[k]; l++) {//遍历当前句子的单词

if(str[k][l].equalsIgnoreCase(qury[j])) {

count++;  //相同则加一

break;   //跳出循环,从查询句子的下一单词开始

}

}

if(count > max) {  //如果当前句子的匹配单词数大于以前遍历过的句子匹配单词数目的最大值

max = count;  //更新最大值

flag = k;//对应单词最多的那句话的标号置为当前句子标号

}

}

System.out.println(s.get(flag));  //输出盘匹配当前查询句子单次最多的句子

}

}

}

输入

6 3

An algorithm is an effective method that can be expressed within a finite amount of space and time

Starting from an initial state and initial input the instructions describe a computation

That when executed proceeds through a finite number of successive states

Eventually producing output and terminating at a final ending state

The transition from one state to the next is not necessarily deterministic

Some algorithms known as randomized algorithms incorporate random input

Next to the transition

Wormhole infinite time and space

The transition from one state to the next is not necessarily deterministic

输出

The transition from one state to the next is not necessarily deterministic

An algorithm is an effective method that can be expressed within a finite amount of space and time

The transition from one state to the next is not necessarily deterministic


8 条回复

第二题用用Scala来写也就两行。。。

2017-03-30 22:05

第二题能AC不

2017-03-30 22:08

一道都没AC的飘过~

2017-04-01 11:08


import java.util.*;


public class Main{

public static void main(String[] args) {

long startTime = System.currentTimeMillis();//获取当前时间

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int m = sc.nextInt();

sc.nextLine();//nextLine()方法返回的是Enter键之前的所有字符,它是可以得到带空格的字符串的。

List<String> lstString = new ArrayList<String>();

for (int i = 1; i <=m + n; i++) {

lstString.add(sc.nextLine());

}

List<String> lstend = new ArrayList<String>();


lstend = GetLst(lstString, n, m);


for (int i = 0; i < lstend.size(); i++) {

System.out.println(lstend.get(i));

}

long endTime = System.currentTimeMillis();

System.out.println("程序运行时间:"+(endTime-startTime)+"ms");

}


private static List<String> GetLst(List<String> lstString, int n, int m) {

// 把每行的句子拆分然后放到HashSet里面,前n个是查询库,后m个是被查询库

List<HashSet<String>> setList = new ArrayList<HashSet<String>>();// 所有HashSET放到一个集合中,一共n+m个

//setList.remove(0);

for (int i = 0; i <lstString.size(); i++) {

HashSet<String> stringSet = new HashSet<String>();// 把每行句子拆分的单词放到set集合,使其不重复

String[] s1 = lstString.get(i).split(" ");

stringSet.add(s1[0].toLowerCase());

for (int j = 1; j < s1.length; j++) {

stringSet.add(s1[j]);

}

setList.add(stringSet);

}

int[][] arraymn = new int[m][n];// 用来计数

for (int j = setList.size() - m; j < setList.size(); j++) {// 后m个被查询库

for (int i = 0; i < setList.size() - m; i++) {// 查询库

// tList.get(i).

Iterator<String> iter = setList.get(j).iterator();// setList.get(j)第j个HashSet

while (iter.hasNext()) {// 拿出第n个句子里单词匹配

String x = iter.next();

if (setList.get(i).contains(x)) {

arraymn[j - n][i]++;// 每有一个包含就计数一次,得到一个二维计数数组

}


}

}

}

List<String> lstEnd=new ArrayList<String>();

for (int i = 0; i < arraymn.length; i++) {

int max=0;int k=0;//用来获取最大值的下标

for (int j = 0; j < arraymn[i].length; j++) {

if(arraymn[i][j]>max)//拿到最大值的下标

{

max=arraymn[i][j];

k=j;

}

}

lstEnd.add(lstString.get(k));

}

return lstEnd;

}

}


2017-04-01 14:17
1

第二题HashSet,HashMap都没用,代码质量不咋的啊

2017-04-01 14:20

你第一题时间复杂度为O(n^2)我发一个 O(n)的


import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int[] array=new int[n];

for (int i = 0; i < n; i++) {

array[i] = sc.nextInt();// 输入十个数组

}

List<Integer> lst=new ArrayList<Integer>();

lst=GetIndex(array ,n);

if(lst.get(1)==-1)

{

System.out.println(-1);

}

else

{

System.out.println(lst.get(0)+" "+lst.get(1));

}

}


private static List<Integer> GetIndex(int[] array ,int n) {

int max=0;

int start=-1;

int end=-1;

List<Integer> updown=new ArrayList<Integer>();//将谷底点放入集合

if(array[1]>array[0])

updown.add(array[0]);//当第二项大于第一项添加进去

for (int i = 1; i <n-1; i++) {

if ((array[i]<array[i-1])&&(array[i]<array[i+1])) {

updown.add(i);//1-n-1之间波谷添加进去

}

}

if(array[n-1]<array[n-2])

updown.add(n-1);//当最后一项小于倒数第二项

for (int i = 0; i < updown.size()-1; i++) {

if((updown.get(i+1)-updown.get(i))>max)

{

max=updown.get(i+1)-updown.get(i);

start=updown.get(i);

end=updown.get(i+1);

}


}

List<Integer> lst=new ArrayList<Integer>();

lst.add(start);

lst.add(end);

return lst;

}


}


2017-04-01 14:25
1
acmcoderJuXPaz9T 回复 acmcoderCPAh3GI5

谢谢分享

2017-04-01 16:43

第一题通过率20%多,送分题。  找函数极小值之间的差的最大值 ,也就10行 O(n)

2017-04-01 16:43
1
添加回复
回到顶部