Skip to content

Commit efb479c

Browse files
author
code.wang
committed
添加笔试题
1 parent 2fd3a8e commit efb479c

File tree

7 files changed

+221
-18
lines changed

7 files changed

+221
-18
lines changed

.vscode/launch.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

main.js

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,116 @@
44

55
// import './string/main'
66

7-
import './动态规划/main'
7+
// import './动态规划/main'
88

9-
// import './数组/main'
9+
// import './数组/main'
10+
11+
// import './查找/main'
12+
13+
(function(target){
14+
console.log(target)
15+
const result = [];
16+
let start = 1;
17+
let end = 2;
18+
let sum = start + end;
19+
while (start != end && end <= target) {
20+
if(sum === target){
21+
result.push( getSubSequence(start, end) )
22+
++b;
23+
sum += b;
24+
} else if(sum > target){
25+
sum -= start;
26+
++start
27+
} else {
28+
++end;
29+
// sum +=
30+
}
31+
}
32+
return result;
33+
})
34+
35+
function getSubSequence(start, end){
36+
const res = [];
37+
for (let i = start; i < end; i++){
38+
res.push(i)
39+
}
40+
return res;
41+
}
42+
43+
//动态规划最大和子序列
44+
var maxSubArray = function(nums) {
45+
// let ans = nums[0];
46+
// let sum = 0;
47+
// for (let i = 1; i < nums.length; i++) {
48+
// if(sum > 0){
49+
// sum += nums[i];
50+
// } else {
51+
// sum = nums[i];
52+
// }
53+
// ans = Math.max(ans, sum);
54+
// }
55+
// return ans
56+
const dp = [];
57+
dp[0] = nums[0];
58+
let max = nums[0];
59+
for (let i = 1; i< nums.length; i++) {
60+
dp[i] = Math.max(dp[i-1] + nums[i], nums[i]);
61+
max = Math.max(max, dp[i]);
62+
}
63+
return max;
64+
};
65+
66+
// console.log( maxSubArray([-2,1,-3,4,-1,2,1,-5,4]) )
67+
function isFlag (str) {
68+
const n = str.length - 1;
69+
for (let i = 0; i < n / 2; i++) {
70+
if(str[i] !== str[n - i]){
71+
return false;
72+
}
73+
}
74+
return true
75+
}
76+
77+
function longestPalindrome(str){
78+
let res = '';
79+
for (let i = 0; i < str.length; i ++){
80+
for (let j = i + 1; j < str.length + 1; j++){
81+
const s = str.substring(i, j);
82+
if(s.length < res.length) continue;
83+
if(isFlag(s) ){
84+
res = s;
85+
}
86+
}
87+
}
88+
return res
89+
}
90+
91+
// console.log( longestPalindrome("aacabdkacaa"))
92+
const dp = [];
93+
94+
/**
95+
* @param {number} n
96+
* @return {number}
97+
*/
98+
var fib = function(n) {
99+
if(n === 0) return 0;
100+
if( n === 1 ) return 1;
101+
if(fib.dp[n]) return fib.dp[n];
102+
fib.dp[n] = fib(n - 1) + fib(n - 2);
103+
return fib.dp[n];
104+
};
105+
fib.dp = [];
106+
107+
var tribonacci = function(n) {
108+
if(n === 0) return 0;
109+
if( n === 1 ) return 1;
110+
if( n === 2 ) return 1;
111+
if(tribonacci.dp[n]) return tribonacci.dp[n];
112+
tribonacci.dp[n] = tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3);
113+
return tribonacci.dp[n];
114+
};
115+
tribonacci.dp = [];
116+
117+
console.time()
118+
console.log( tribonacci(4) )
119+
console.timeEnd()

rk.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
null 没有对象也没有对象的引用
2+
undefined 有对象的引用,但是指向的对象不存在

查找/main.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
const search = (nums, target) => {
7+
let left = 0;
8+
let right = nums.length - 1;
9+
while(left <= right) {
10+
const mid = Math.floor((right + left) / 2);
11+
const midValue = nums[mid];
12+
if(midValue > target){
13+
right = mid - 1;
14+
} else if ( midValue < target){
15+
left = mid + 1;
16+
} else {
17+
return mid;
18+
}
19+
}
20+
return -1;
21+
};
22+
23+
// console.log(search([-1], -1));
24+
// 输入一个正整数target,输出所有和为target的连续正整数的序列,
25+
const test = () => {
26+
27+
}

笔试题/js基础/ces.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
console.log({}+[]); // [Object Object] 会调用toString方法
2+
{}+[];//[object Object]
3+
[]+{}; //[object Object]
4+
{} + {}; // "[object Object][object Object]"
5+
console.log([] == false) // true
6+
console.log({} == false) // false
7+
if([]){
8+
console.log([] == false)
9+
}
10+
// ???? 为什么在if语句中[]为true
11+
("b" + "a" + + "a" + "a").toLocaleLowerCase();
12+
0 == "0"
13+
Boolean("0") == Boolean(0) // true
14+
console.log(NaN == 0); // false
15+
console.log(NaN <= 0); //
16+
console.log(null <= 0);
17+
console.log(1 + null);
18+
var a ={value:1};
19+
var b ={value:2};
20+
console.log(a<=b);
21+
var obj={};
22+
var x = +obj.yideng?.name ?? 'test';
23+
console.log(x);
24+
var test="test";
25+
console.log(typeof test); // true
26+
console.log(test instanceof String); // true ???
27+
28+
Promise.resolve().then(() => {
29+
console.log(11);
30+
})
31+
.then(() => {
32+
console.log(12);
33+
})
34+
.then(() => {
35+
console.log(0);
36+
return Promise.resolve(5); })
37+
.then((r) => {
38+
console.log('🐻 ', r);
39+
});

链表/main.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { bianli } from "../utils/linkList";
22
import { deleteNode } from "./deleteLinkLIstItem";
33
import { ListNode } from "./myLinkList";
44
import { getIntersectionNode } from './leetcode160相交链表'
5+
import { removeElements } from "./移除链表元素";
56

67
const a = new ListNode(4)
78
const b = new ListNode(5)
@@ -33,4 +34,10 @@ export const mapArruyToLinklist = (arr) => {
3334

3435
const listA = [4,1,8,4,5], listB = [5,0,1,8,4,5];
3536

36-
console.log( getIntersectionNode( mapArruyToLinklist(listA), mapArruyToLinklist(listB) ))
37+
const listTest = mapArruyToLinklist([1, 7,7,7, 7])
38+
39+
const newList = removeElements(listTest, 7)
40+
// console.log(newList)
41+
bianli(newList)
42+
43+
// console.log( getIntersectionNode( mapArruyToLinklist(listA), mapArruyToLinklist(listB) ))

链表/移除链表元素.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
2+
// 输入:head = [1,2,6,3,4,5,6], val = 6
3+
// 输出:[1,2,3,4,5]
4+
5+
import { bianli } from "../utils/linkList";
6+
import { ListNode } from "./myLinkList";
7+
8+
/**
9+
* @param {ListNode} head
10+
* @param {number} val
11+
* @return {ListNode}
12+
*/
13+
// export var removeElements = function(head, val) {
14+
// if(head === null) return head;
15+
// head.next = removeElements(head.next, val);
16+
// return head.val === val ? head.next : head;
17+
// };
18+
19+
export var removeElements = function(head, val) {
20+
//创建一个虚拟头结点
21+
let dummyNode = new ListNode(val-1);
22+
dummyNode.next=head;
23+
let prev=dummyNode;
24+
//确保当前结点后还有结点
25+
while(prev.next!=null){
26+
if(prev.next.val==val){
27+
prev.next=prev.next.next;
28+
}else{
29+
prev=prev.next;
30+
}
31+
}
32+
return dummyNode.next;
33+
};

0 commit comments

Comments
 (0)