蔚来笔试 2022/2/23
选择题
排序算法时间复杂度,智力题,操作系统,网络ip等,代码运行结果
多选题
场景题
编程题
第一题
int数字转成string汉语读法的字符串。十百千 分别用 s b q 表示。
11 --- s1 表示十一
301 --- 3b01 表示三百零一
50001 --- 5w01 表示五万零一
5000100 --- 5bw01b 表示五百万零一百
12345678 --- 1q2b3s4w5q6b7s8 表示一千二百三十四万五千六百七十八
我的代码如下,过了百分之九十多,不知道剩下的还有那些,实在想不出。我的想法是,这个数字最多八位,前四位和后四位其实都差不多,只不过前四位转完之后加个w而已罢了。因此重点在于如果对一个四位数进行转换,注意一些特殊情况,不断完善代码,基本就行。
public String chinese_number (int n) {
String num = n +"";
// 每四位进行转换,万位无非加了个w
String result="";
if (num.length() > 4) {
String wan = num.substring(0, num.length() - 4);
String transfer1 = transfer(wan);
result += transfer1 + "w";
String ge = num.substring(num.length() - 4);
String transfer2 = transfer(ge);
result += transfer2;
} else {
String transfer = transfer(num);
result += transfer;
}
return result;
}
private String transfer(String num) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < num.length(); i++) {
if (num.charAt(i) == '0') {
if (i == num.length() - 1) {
String s = sb.toString();
int index = s.length()-1;
while (index >=0 &&s.charAt(index) == '0') {
sb.deleteCharAt(index);
index--;
}
break;
}
String s = sb.toString();
if (s.length() ==0 || s.length()>0 &&s.charAt(s.length() - 1) != '0') {
sb.append("0");
}
continue;
}
int wei = num.length() - i;
if (wei == 4) {
sb.append(num.charAt(i) + "q");
} else if (wei == 3) {
sb.append(num.charAt(i) + "b");
} else if (wei == 2) {
if (num.charAt(i) == '1' && num.length() == 2) {
sb.append("s");
} else {
sb.append(num.charAt(i) + "s");
}
} else {
sb.append(num.charAt(i));
}
}
return sb.toString();
} 第二题
力扣原题,就是合并两链表,就是两链表分别表示两个数字,就出这两个链表的和,生成一个新的链表。
public ListNode addTwoNumbers (ListNode l1, ListNode l2) {
int t = 0;
ListNode dummy = new ListNode(-1);
ListNode cur;
cur = dummy;
while (l1 != null || l2 != null) {
int l1Val = l1 == null ? 0 : l1.val;
int l2Val = l2 == null ? 0 : l2.val;
int sum = l1Val + l2Val + t;
t = sum / 10 == 0 ? 0 : 1;
sum = sum % 10;
cur.next = new ListNode(sum);
cur = cur.next;
l1 = l1 == null ? null : l1.next;
l2 = l2 == null ? null : l2.next;
}
if (t == 1) {
cur.next = new ListNode(1);
}
return dummy.next;
} 第三题
给你一个数组,这个数组中有重复的数字,求去重后的数组。注意结果的顺序
1 1 2 2 3 3 3 7 5
1 2 3 7 5
可能有其他解法,我就暴力了 :smile:
public int[] unique (int[] list) {
// write code here
HashSet<Integer> set = new HashSet<>();
List<Integer> result = new ArrayList<>();
for (int i : list) {
if (!set.contains(i)) {
result.add(i);
set.add(i);
}
}
return result.stream().mapToInt(Integer::intValue).toArray();
} #蔚来笔试##蔚来汽车##笔经#