两道面试题(英文版)

来源:互联网 发布:大白菜会计软件 编辑:程序博客网 时间:2024/05/17 06:20

今天下午小陈给我发了两道面试题,我花了点时间研究了一下。
现在在这里总结:
题目一:
Problem Statement # 1

Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint sets A1 and A2 of n1 and n2 numbers, respectively. Let S1 and S2 denote the sums of all the numbers in A1 and A2, respectively. You are supposed to make the partition so that |n1 - n2| is minimized first, and then |S1 - S2| is maximized.

Input Specification:
Each input file contains one test case. For each case, the first line gives an integer N (2 <= N <= 10^5), and then N positive integers follow in the next line, separated by spaces. It is guaranteed that all the integers and their sum are less than 2^31.

Output Specification:
For each case, print in a line two numbers: |n1 - n2| and |S1 - S2|, separated by exactly one space.

Sample Input 1:
10
23 8 10 99 46 2333 46 1 666 555
Sample Output 1:
0 3611

Sample Input 2:
13
110 79 218 69 3721 100 29 135 2 6 13 5188 85
Sample Output 2:
1 9359

没错!当初看到是英文版的时候我也是懵逼的,但是谁叫我有金山词霸呢!这点小困难还是打不倒我的。
题目大意是说:给定一组数组,让我们写一个方法将它分为两个集合A1和A2,它们分别有n1和n2个元素,集合所有元素值加起来是S1和S2,,要求方法输出|n1-n2|和|S1-S2|的值,且n1-n2要尽可能的小,S1-S2要尽可能的大。over。

我的思路是这样的:既然n1-n2要尽可能的小,那么最小的结果是n1=n2,即|n1-n2 |=0;所以说要尽可能地将这组数组等分,但考虑到传入的数组中元素个数为奇数个的时候,是取不到n1=n2的,最多能取到n1-n2=1;
然后直接上代码理解吧!

package com.xiaochen;import java.util.ArrayList;import java.util.Comparator;import java.util.List;/* * @author sjia * @Date 2017年4月16日--下午6:05:04 */public class Problem {    public void ouput(int num,String str)    {        int s1=0;        int s2=0;        String[] strs=str.split(" ");        List<Integer> list=new ArrayList<Integer>();        List<Integer> a1=new ArrayList<Integer>();        List<Integer> a2=new ArrayList<Integer>();        for(String s:strs)        {            list.add(Integer.parseInt(s));        }        list.sort(new Comparator<Integer>() {            public int compare(Integer o1, Integer o2) {                return o2-o1;            }        });        //分两种情况,num为奇数和num为偶数        if(num%2==0)        {            //System.out.println("偶数");            for(int i=0;i<(num/2);i++)            {                s1+=list.get(i);                a1.add(list.get(i));            }            for(int i=list.size()-1;i>num/2-1;i--)            {                s2+=list.get(i);                a2.add(list.get(i));            }        }else if(num%2!=0)        {            //System.out.println("奇数");            for(int i=0;i<(num+1)/2;i++)            {                s1+=list.get(i);                a1.add(list.get(i));            }            for(int i=list.size()-1;i>(num-1)/2;i--)            {                s2+=list.get(i);                a2.add(list.get(i));            }        }        int nr=a1.size()-a2.size();        System.out.println("|n1-n2|="+nr);        int sr=s1-s2;        System.out.println("|s1-s2|="+sr);    }    public static void main(String[] args) {        Problem pro=new Problem();        pro.ouput(13, "110 79 218 69 3721 100 29 135 2 6 13 5188 85");    }}

题目二:
Problem Statement # 2

You are to write a program that takes a list of strings containing integers and words and returns a sorted version of the list.

The goal is to sort this list in such a way that all words are in alphabetical order and all integers are in numerical order. Furthermore, if the nth element in the list is an integer it must remain an integer, and if it is a word it must remain a word.

Input Specification:
The input will contain a single, possibly empty, line containing a space-separated list of strings to be sorted. Words will not contain spaces, will contain only the lower-case letters a-z. Integers will be in the range -999999 to 999999, inclusive. The line will be at most 1000 characters long.

Output Specification:
The program must output the list of strings, sorted per the requirements above. Strings must be separated by a single space, with no leading space at the beginning of the line or trailing space at the end of the line.

Sample Input 1:
1
Sample Output 1:
1

Sample Input 2:
car truck bus
Sample Output 2:
bus car truck

Sample Input 3:
8 4 6 1 -2 9 5
Sample Output 3:
-2 1 4 5 6 8 9

Sample Input 4:
car truck 8 4 bus 6 1
Sample Output 4:
bus car 1 4 truck 6 8

个人感觉第二题就是第一题的升级加强版,就是考排序嘛。
这题在思路上是很简洁明了的,但是在实现的过程中略显繁琐。
也是直接上代码吧!

package com.xiaochen;import java.util.ArrayList;import java.util.Comparator;import java.util.HashMap;import java.util.List;import java.util.Map;/* * @author sjia * @Date 2017年4月16日--下午6:21:08 */public class Problem2 {    //先将这个字符串中的字母和数字都分隔出来,put进一个map中    public static Map<String, List<String>> separate(String str) {        List<String> alp = new ArrayList<String>();        List<String> num = new ArrayList<String>();        Map<String, List<String>> map = new HashMap<String, List<String>>();        //用来临时存放时字母的string        String temp = null;        String[] strs = str.split(" ");        for (int i = 0; i < strs.length; i++) {            try {                temp = strs[i] + "+" + i;                int result = Integer.parseInt(strs[i]);                num.add(strs[i] + "+" + i);            } catch (Exception e) {                alp.add(temp);            }        }        map.put("alp", alp);        map.put("num", num);        return map;    }    // letter & number sort    public void InSort(String str)    {        Map<String,List<String>> map=separate(str);        //alpplus代表该集合中的元素是 "abc+1"这样的形式        List<String> alpplus=map.get("alp");        List<String> numplus=map.get("num");        List<String> alp=new ArrayList<String>();        List<String> num=new ArrayList<String>();        //这两个list用来存放对应list的index  即alpindex 存放alp的 index        List<String> alpindex=new ArrayList<String>();        List<String> numindex=new ArrayList<String>();        for(String s1:alpplus)        {            //注意,之前我是直接用"+"split的,没加转义的话会报错            String[] strs=s1.split("\\+");            alp.add(strs[0]);            alpindex.add(strs[1]);        }        for(String s2:numplus)        {            String[] strs=s2.split("\\+");            num.add(strs[0]);            numindex.add(strs[1]);        }        Comparator<String> comparator=new Comparator<String>() {            public int compare(String o1, String o2) {                return o1.compareTo(o2);            }        };        alpplus.sort(comparator);        numplus.sort(comparator);        alpindex.sort(comparator);        numindex.sort(comparator);        Map<String,String> result=new HashMap<String,String>();        for(int i=0;i<alp.size();i++)        {            result.put(alpindex.get(i), alp.get(i));        }        for(int i=0;i<num.size();i++)        {            result.put(numindex.get(i), num.get(i));        }        String res="";        for(int i=0;i<alp.size()+num.size();i++)        {            res=res+result.get(i+"")+" ";        }        System.out.println(res);    }    public static void main(String[] args) {        Problem2 problem2=new Problem2();        problem2.InSort("car truck 8 4 bus 6 1");    }}

吐槽一下小陈,用两道题打断了正在学习spring boot的我….
好了,我现在要滚去看我的spring boot 了.

最后,如果你有更好的做法,或者发现我有什么地方有错误的话,欢迎在评论里交流

0 0
原创粉丝点击