分享

UC 优视科技字符串算法面试题

 看风景D人 2014-06-15

1. 请写一个字符串转成驼峰的方法?

例如:border-bottom-color -> borderBottomColor

2. 查找字符串中出现最多的字符和个数?

如 sdsdsddssssssdd -> 字符最多的是s,出现恶9次

3. 如何给字符串加千分符?

如:18239423 -> 18,239,423

 

参考答案:
public class UC {
static void first() {
String s = “border-bottom-color”;
String[] array = s.split(“-”);
StringBuffer sb = new StringBuffer();
sb.append(array[0]);
for (int i = 1; i < array.length; i++) {
sb.append(array[i].substring(0, 1).toUpperCase()
+ array[i].substring(1, array[i].length()));
}
System.out.println(“border-bottom-color -> ” + sb.toString());
}

static void second() {
String param = “sdjksfssscfssdd”;
int history = 0;
char result = ‘a’;
for (int i = 0; i < param.length(); i++) {
char tc = param.charAt(i);
int pos = param.indexOf(tc);
int count = 0;
while (pos != -1) {
count++;
param = param.substring(pos + 1);
pos = param.indexOf(tc);
}
if (count > history) {
history = count;
result = tc;
}
}
System.out.println(“出现次数最多的字符是:” + result + “,总共出现了” + history + “次”);
}

static void three() {
StringBuffer temp = new StringBuffer();
temp.append(“3562123761″);
temp.reverse();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < temp.length(); i = i + 3) {
if ((i + 3) < temp.length()) {
sb.append(temp.substring(i, i + 3) + “,”);
} else {
sb.append(temp.substring(i));
}
}
System.out.println(“3562123761 -> ” + sb.reverse().toString());
}

public static void main(String[] args) {
// substring(num),截取num及num后面的字符串;substring(start,end),截取start及end之间的数据
first();
second();
three();

}

}

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多