51信用卡校园招聘编程题2107.10.18日
第一题:n个数字串找出出现次数最多的数字串,如果有个,按照字典顺序,输出最小的那个。(注意内存使用) 解题思路:要保证内存不超,建议使用字典树。 网上找的代码,没来的及提交。没输出最小的那个。字典树理解不深入,求大神帮忙改改程序,输出出现次数最多的那个最小的字符串。
import java.util.Scanner;
/**
* Created by tzy on 2017/10/18
* n个小写数字串中出现次数最多的数字串.(如果存在多个,按照字典顺序输出最小的那个。)
*/
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
Main trie = new Main();
while (scanner.hasNext()){
int n=scanner.nextInt();
for (int i = 0; i <n ; i++) {
String string=scanner.next().trim();
trie.addWord(string);
}
System.out.println(trie.maxCountWord());
}
}
private TrieNode root = new TrieNode();// 字典树的根节点
private int max;// 统计出现的最大次数
private String maxWord;// 出现最大次数的字符串
// 返回出现次数最大的单词
public String maxCountWord() {
return maxWord;
}
// 向字典树中添加单词
public void addWord(String word) {
addWord(root, word, word);// 第二个word是个冗余参数,为了记录增加的单词
}
private void addWord(TrieNode vertex, String word, String wordcount) {
if (word.length() == 0) {
vertex.words++;
if (max <=vertex.words) {
max = vertex.words;
maxWord = wordcount;
}
} else {
char c = word.charAt(0);
//c = Character.toLowerCase(c);
int index = c -'0';
if (vertex.edges[index] == null) { // 构建节点的路径
vertex.edges[index] = new TrieNode();
}
addWord(vertex.edges[index], word.substring(1), wordcount);
}
}
protected class TrieNode {
protected int words;// 统计从根到该节点的单词出现的个数
protected TrieNode[] edges;// 存放该节点的子节点
public TrieNode() {
this.words = 0;
edges = new TrieNode[10];// 题目对于字符没有做出限制,这里默认全是数字
for (int i = 0; i < edges.length; i++) {
edges[i] = null;
}
}
}
}
第二题:leetcode上是整数,确定一下小数点的位置就行。
import java.util.Scanner;
/**
* Created by tzy on 2017/10/18.
* leetcode上是整数,确定一下小数点的位置就行。
*/
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
while (scanner.hasNext()){
String numl=scanner.next();
String num2=scanner.next();
System.out.println(multiply(numl,num2));
}
}
private static String multiply(String num1, String num2) {
//每个数的小数点后有几位。
int dian_leng=0;
int dian_1=num1.indexOf('.');
int dian_2=num2.indexOf('.');
if (dian_1>0)
dian_leng+=num1.length()-1-dian_1;
if (dian_2>0)
dian_leng+=num2.length()-1-dian_2;
num1=num1.replace(".","");
num2=num2.replace(".","");
int len1 = num1.length();
int len2 = num2.length();
int[] product = new int[len1 + len2];
for (int i = len1 - 1; i >= 0; i--) {
for (int j = len2 - 1; j >= 0; j--) {
int index = len1 + len2 - i - j - 2;
product[index] += (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
product[index + 1] += product[index] / 10;
product[index] %= 10;
}
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = product.length - 1; i >= 0; i--) {
if (stringBuilder.length() == 0 && product[i] == 0)
continue;
stringBuilder.append(product[i]);
}
//确定小数点位置。
if (stringBuilder.length()<=dian_leng){
StringBuilder temp=new StringBuilder("0.");
for (int i = 0; i <dian_leng-stringBuilder.length() ; i++) {
temp.append(0);
}
stringBuilder.insert(0,temp);
}
else
stringBuilder.insert(stringBuilder.length()-dian_leng,".");
return stringBuilder.toString();
}
}
#Java工程师##算法工程师#
