一道精炼字符串笔试题及队列试题

  1. 请完成函数,该函数输入一个纯英文字符串,请打印出该字符串中每个字符(区分大小写)出现的次数,并按照出现的次数从大到小排列,如输入“asisaB”,则打印出a=2,s=2,i=1,B=1。
    注:要求不能使用如Map,List等集合类。


    2.使用Java实现阻塞队列BlockQueue,请插入数据调用Offer,如果已满则等待。获取数据调用take,如果队列为空,则等待。请完成该类

    望详细指点迷津

[code="java"]public class Test9
{
/**
* 该函数输入一个纯英文字符串,输入出
* a=2,s=2,i=1,B=1。
*
* @param in
* 目标字符串
* @return String
*/
public static String println(String in)
{
// 由于不能用集合类,所以定义一个String个数的数组
Object[] objs = new Object[in.length()];

    // 遍历
    for (int i = 0; i < in.length(); i++)
    {
        // 取出单个字符
        char c = in.charAt(i);
        // 开关变量(标识是否相同)
        int temp = 0;

        // 在字符数组里查找是否有相同的字符已经保存了 
        for (int j = 0; j < objs.length; j++)
        {
            // 非空判断,因为定义的数组长度太大,肯定会有null值
            if (objs[j] == null)
            {
                continue;
            }
            // 取出字符对象
            Param param = (Param) objs[j];

            // 是否相同的
            if (param.getC() == c)
            {
                // 相同的数量加1
                param.setCount(param.getCount() + 1);

                // 不再产生新的对象
                temp = 1;
                break;
            }
        }
        if (temp == 1)
        {
            // 这个字符已经存在,并且已经叠加,不在产生新对象
            objs[i] = null;
            continue;
        }
        // 有一个新的字符
        Param param = new Param(c);
        objs[i] = param;
    }

    // 排序,这个自己看
    for (int i = 0; i < objs.length; i++)
    {
        if (objs[i] == null)
        {
            continue;
        }
        Param param = (Param) objs[i];
        for (int j = i; j < objs.length; j++)
        {
            if (objs[j] == null)
            {
                continue;
            }
            Param param1 = (Param) objs[j];

            if (param.getCount() < param1.getCount())
            {
                Param temp = param;
                objs[i] = param1;
                objs[j] = temp;
            }
        }
    }
    // 拼接
    StringBuffer result = new StringBuffer();
    for (int i = 0; i < objs.length; i++)
    {
        if (objs[i] == null)
        {
            continue;
        }
        result.append((Param) objs[i]).append(",");
    }
    return result.toString();
}

public static void main(String[] args)
{
    System.out.println(println("asisaiiiiBb"));
}

}

class Param
{
/**
* 字符
*/
private char c;

/**
 * 个数
 */
private int count;

public Param()
{
}
public Param(char c)
{
    this(c, 1);
}
public Param(char c, int count)
{
    this.c = c;
    this.count = count;
}
public char getC()
{
    return c;
}
public void setC(char c)
{
    this.c = c;
}
public int getCount()
{
    return count;
}
public void setCount(int count)
{
    this.count = count;
}
@Override
public String toString()
{
    return c + "=" + count;
}

}[/code]

是作业题吧?

  1. 开足够大的数组,顺序读,按下标更新。 最后排序输出

[code="java"]
import java.util.*;

public class CharCount implements Comparable {
private int count = 0;
private char ca;

public void add() {
    count++;
}

public CharCount() {
    ca = 'a';
}

public CharCount(char in) {
    ca = in;
}

@Override
public String toString() {
    if (ca > 'Z' && ca < 'a' || count==0)
        return "";
    return ca + "=" + count + ",";
}

public int compareTo(Object o) {
    if (o instanceof CharCount) {
        CharCount to = (CharCount) o;
        if (this.count == to.count)
            return 0;
        return this.count > to.count ? -1 : 1;
    }
    throw new RuntimeException("----,can compare");
}

public static void main(String args[]) {
    String test = "asdfascxcvsadqweryAFdDFWETDFSFWTGSDGSHJDFGSDFS";
    int chsLength = (int) ('z' - 'A');
    CharCount[] chs = new CharCount[chsLength];
    for (int i = 0; i < chsLength; i++) {
        chs[i] = new CharCount((char) ('A' + i));
    }
    char[] tChs = test.toCharArray();
    for (int i = 0; i < tChs.length; i++) {
        chs[tChs[i] - 'A'].add();
    }
    Arrays.sort(chs);
    for (CharCount c : chs) {
        System.out.print(c);
    }
}

}
[code]