设为首页 加入收藏

TOP

UC 优视科技字符串算法面试题
2014-10-19 00:08:30 来源: 作者: 【 】 浏览:36
Tags:科技 字符串 算法 试题

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();


}


}





】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇请用带么写一个算法测试回环字符.. 下一篇算法面试题组合整理

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: