UC 优视科技字符串算法面试题 - 高飞网
4人看过

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

2014-05-20 01:19:09

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

}

}

 

 

 

还没有评论!
54.158.248.167