优路科技笔面试题

笔试题:

1.独一无二的次数:

给你一个数组,计算每个数出现的次数,如果每个数组返回的数都是独一无二的就返回 true 相反则返回的 flase

  1. var arr = [1,2,3,4,5,6];
  2. function count(arr) {
  3. var result = [];
  4. var obj = {};
  5. arr.forEach(function (item) {
  6. if (!obj[item]) {
  7. obj[item] = 1;
  8. } else {
  9. obj[item] ++;
  10. }
  11. });
  12. for (var prop in obj) {
  13. if (obj.hasOwnProperty(prop)) {
  14. result.push(obj[prop]);
  15. }
  16. }
  17. return result.length === arr.length;
  18. }
  19. console.log(count(arr))

2.字符串压缩

给你个字符串,利用反复出的字符串的 例如 aaabbbdddddfff 转化为a3b3d4f3
假设只包含大小写字母

  1. function zipStr(str) {
  2. var reg = /(\w)(\1*)/g
  3. console.log(str.replace(reg, function ($, $1, $2) {
  4. console.log($1, $2);
  5. return $1 + ($2.length + 1)
  6. }))
  7. }

3.删除给定值val

  1. 给你一个链表 1>2>3>6>4>5>6 删除 val=6 得到 1>2>3>4>5>
  1. function deleteNode(node, targetVal) {
  2. var p = node;
  3. var result = node;
  4. if (p.val === targetVal && p.next) {
  5. result = p.next;
  6. } else if(p.val === targetVal) {
  7. result = null;
  8. }
  9. if (p.next.val === targetVal && p.next.next) {
  10. p.next = p.next.next;
  11. } else if (p.next.val === targetVal) {
  12. p.next = null;
  13. }
  14. while(p.next.next) {
  15. if (p.next.val === targetVal) {
  16. p.next = p.next.next;
  17. }
  18. p = p.next;
  19. }
  20. if (p.next.val === targetVal) {
  21. p.next = p.next.next ? p.next.next : null;
  22. }
  23. return result;
  24. }