1.分离字串:
通过键盘输入任意一个字符串序列,字符串可能包含多个子串,子串以空格分隔。请编写一个程序,自动分离出各个子串,并使用’,’将其分隔,并且在最后也补充一个’,’并将子串存储。如果输入“abc def gh i d”,结果将是abc,def,gh,i,d,
要求实现函数:
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO 的输入输出
示例
输入:“abc def gh i d”
输出:“abc,def,gh,i,d,”
具体代码如下:
/**
*DividStringTest.java
*/
package interview.huawei;
/**
* @author xuhongbin
* @data2014年9月19日下午3:50:58
*/
public class DividStringTest {
public static void DivideString(final char[] pInputStr, long lInputLen, char[] pOutputStr){
int index = 0;
for(char c: pInputStr){
if(' ' != c){
pOutputStr[index++] = c;
}else {
if(',' != pOutputStr[index -1]){//间隔出现多个' '时候只加一个','
pOutputStr[index++] = ',';
}
}
}
if(',' != pOutputStr[index -1]){//防止最后一位出现' '后添加两个','
pOutputStr[index] = ',';
}
}
/**
* @param args
*/
public static void main(String[] args) {
char[] inArr = "dsf sdfsf sdfxc erwer ss s ewr sd df f ".toCharArray();
char[] outArr = new char[inArr.length+1];
DivideString(inArr, inArr.length, outArr);
for(char c : outArr ){
System.out.print(c);
}
}
}
2.
题目描述:
将输入的一个单向链表,逆序后输出链表中的值。链表定义如下:
typedef struct tagListNode
{
int value;
struct tagListNode *next;
}ListNode;
要求实现函数:
void converse(ListNode **head);
【输入】head: 链表头节点,空间已经开辟好
【输出】head: 逆序后的链表头节点
【返回】无
【注意】只需要完成该函数功能算法,中间不需要有任何IO 的输入输出
具体代码如下:
/**
*HuaWeiLinkedList.java
*/
package interview.huawei;
/**
* @author xuhongbin
* @data2014年9月19日下午4:23:25
*/
public class HuaWeiNode {
public int value;
public HuaWeiNode next;
public boolean hasNext(){
return this.next !=null;
}
/**
* @return the value
*/
public int getValue() {
return value;
}
/**
* @param value the value to set
*/
public void setValue(int value) {
this.value = value;
}
/**
* @return the next
*/
public HuaWeiNode getNext() {
return next;
}
/**
* @param next the next to set
*/
public void setNext(HuaWeiNode next) {
this.next = next;
}
/**
* @param value
* @param next
*/
public HuaWeiNode(int value, HuaWeiNode next) {
this.value = value;
this.next = next;
}
public HuaWeiNode(int value) {
this.value = value;
this.next = next;
}
/**
*
*/
public HuaWeiNode() {
this.value = 0;
this.next = null;
}
}
/**
*HuaWeiLinkedList.java
*/
package interview.huawei;
/**
* @author xuhongbin
* @data2014年9月19日下午4:26:41
*/
public class HuaWeiLinkedList {
public HuaWeiNode head;
public HuaWeiNode weiba;
/**
* @return the head
*/
public HuaWeiNode getHead() {
return head;
}
/**
* @param head the head to set
*/
public void setHead(HuaWeiNode head) {
this.head = head;
}
/**
* @return the weiba
*/
public HuaWeiNode getWeiba() {
return weiba;
}
/**
* @param weiba the weiba to set
*/
public void setWeiba(HuaWeiNode weiba) {
this.weiba = weiba;
}
public HuaWeiLinkedList(int i){
head = new HuaWeiNode(i,null);
weiba = head;
}
public void add(HuaWeiNode node){
weiba.setNext(node);
this.setWeiba(node);
}
}
/**
*LinkedListReverse.java
*/
package interview.huawei;
import java.util.Iterator;
import java.util.LinkedList;
/**
* @author xuhongbin
* @data2014年9月19日下午4:18:30
*/
public class HWLL {
public static void reverse(HuaWeiLinkedList ll){
HuaWeiNode head = ll.head;
HuaWeiNode node = head;
while(null != node){
System.out.println(node.value);
node = node.next;
}
HuaWeiNode nextNode;
node = head;
HuaWeiNode thisNode = node.next;//每次将该节点加入
head.next = null;
while(null != thisNode){
nextNode = thisNode.next;
thisNode.next = head;
head = thisNode;
thisNode = nextNode;
}
System.out.print("reversed linkedList: ");
node = head;
while(null != node){
System.out.println(node.value);
node = node.next;
}
}
public static void main(String[] args) {
HuaWeiNode node = new HuaWeiNode(1, null);
HuaWeiLinkedList ll = new HuaWeiLinkedList(1);
for(int i = 2;i < 10; i++){
ll.add(new HuaWeiNode(i));
}
reverse(ll);
}
}