1. 找出n个字符串中出现次数最多的字符串。
C/C++:
char* find(char **data,int n);
Java:
String find(String data[]);
说明:
1. data是字符串数组,n是数组中字符串的个数,返回值为出现次数最多的字符串。
2. 若结果有多个,返回任意一个即可
3. 不得使用任何库函数/API,如需使用类似功能, 请自行实现
4. 算法效率尽可能高,尽量少的使用内存空间
5. 必须要有代码注释和算法说明。
C/C++:
char* find(char **data,int n);
Java:
String find(String data[]);
说明:
1. data是字符串数组,n是数组中字符串的个数,返回值为出现次数最多的字符串。
2. 若结果有多个,返回任意一个即可
3. 不得使用任何库函数/API,如需使用类似功能, 请自行实现
4. 算法效率尽可能高,尽量少的使用内存空间
5. 必须要有代码注释和算法说明。
例如:data里面的数据是{“paper”,”cup”,”book”,”cup”,”pen”,”book”}。n = 6。返回结果为”cup”或”book”。
2.将链表中的所有元素为奇数的节点移到元素为偶数节点的前面,并保证奇数之间顺序不变,偶数之间顺序不变。
示例:
交换前链表的顺序 交换后链表的顺序
4→5→3→1→2 ==> 5→3→1→4→2
1 ==> 1 (链表仅含一个元素)
2→1 ==>1→2
==> (链表为空)
C/C++:
链表节点定义为:
struct node {
struct node *next;
int value;
};
struct node *swap(struct node *list);
Java:
链表节点定义为:
class Node {
public Node next;
public int value
}
Node swap(Node list)
注意点和要求如下:
1. swap函数要求对节点的指针/引用进行操作(不得创建任何新的链表节点)
2. 不得使用任何库函数/API,如需使用类似功能, 请自行实现
3. 不得将链表转化为其他类型数据结构再进行交换,如数组等
1.
package com.yyp.job.tuyayidong;
/**
* Created by up on 2015/11/4.
*/
public class MaxCountWord {
private TrieNode root = new TrieNode();// 字典树的根节点
private int max;// 统计出现的最大次数
private String maxWord;// 出现最大次数的字符串
protected class TrieNode {
protected int words;// 统计从根到该节点的单词出现的个数
protected TrieNode[] edges;// 存放该节点的子节点
public TrieNode() {
this.words = 0;
edges = new TrieNode[26];// 题目对于字符没有做出限制,这里默认全是小写字符
for (int i = 0; i < edges.length; i++) {
edges[i] = null;
}
}
}
// 向字典树中添加单词
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 -'a';
if (vertex.edges[index] == null) { // 构建节点的路径
vertex.edges[index] = new TrieNode();
}
addWord(vertex.edges[index], word.substring(1), wordcount);
}
}
// 返回出现次数最大的单词
public String maxCountWord() {
return maxWord;
}
public static void main(String args[])
{
MaxCountWord trie = new MaxCountWord();
String[] data ={"paper","cup","book","cup","pen","book"};
for (int i = 0; i < data.length; i++) {
trie.addWord(data[i]);
}
System.out.println(trie.maxCountWord());
}
}
2.
package com.yyp.job.tuyayidong;
/**
* Created by up on 2015/11/3.
*/
class Node {
public Node next;
public int value;
public Node(int value){
this.value=value;
}
}
public class LinkListTest {
public static void main(String[] args) {
Node node1 = new Node(4);
Node node2 = new Node(5);
Node node3 = new Node(3);
Node mode4 = new Node(1);
Node node5 = new Node(2);
node1.next = node2;
node2.next = node3;
node3.next = mode4;
mode4.next = node5;
LinkListTest main = new LinkListTest();
Node node = main.swap(node1);
while (node != null) {
System.out.println(node.value);
node = node.next;
}
}
/**
* 链表元素的交换方法 奇数的节点移到元素为偶数节点的前面
* 查找尾元素,确定程序的结束点,找到第一个奇数元素,并将该元素之前的偶数放到尾部
* @param node
* @return
*/
public Node swap(Node node) {
if(node == null){
return null;
}
if(node.next == null){
return node;
}
Node end = node;//尾元素
Node prev = null;//指针移动的前一个节点
Node curr = node;//指针移动的当前节点
/**
* 循环,查找尾节点
*/
while (end.next != null) {
end = end.next;
}
Node newEnd = end;// 新的尾节点,不断的存放接收的偶数元素。
// 将第一个奇数前的偶数放到链尾
while (curr.value % 2 == 0 && curr != end) {
newEnd.next = curr;
curr = curr.next;
newEnd.next.next = null;
newEnd = newEnd.next;
}
// 元素是奇数
if (curr.value % 2 != 0) {
/* 头结点为第一个奇数 */
node = curr;
while (curr != end) {
if ((curr.value) % 2 != 0) {//奇数
prev = curr;
curr = curr.next;
} else {
// 将pre指向后一个节点
prev.next = curr.next;
curr.next = null;
newEnd.next = curr;// 将当前的偶数放到链尾
// 链尾后移
newEnd = curr;
// 继续判断
curr = prev.next;
}
}
} else {
prev = curr;
}
// 尾节点的特殊处理
if ((end.value) % 2 == 0) {
prev.next = end.next;
end.next = null;
newEnd.next = end;
}
return node;
}
}