FSharp-08-Array.Parallel
单线程计算
12[|0..100|] |>Array.map (fun x -> x*x) // 1/n
index
value
0
0
1
1
2
4
3
9
4
16
5
25
6
36
7
49
8
64
9
81
10
100
11
121
12
144
13
169
14
196
15
225
16
256
17
289
18
324
19
361
(81 more)
并行计算
12[|0..100|]|>Array.Parallel.map (fun x -> x*x) // 100%
index
value
0
0
1
1
2
4
3
9
4
16
5
25
6
36
7
49
8
64
9
81
10
100
11
121
12
144
13
169
14
196
15
225
16
256
17
289
18
324
19
361
...
算法导学-2-链表类型题目
移除链表元素LeetCode 203.移除链表元素
问题
给你一个链表的头节点head和一个整数val,请你删除链表中所有满足Node.val == val的节点,并返回新的头节点。
12输入:head = [1,2,6,3,4,5,6], val = 6输出:[1,2,3,4,5]
迭代删除
算法思想
用temp表示当前节点。
如果temp的下一个节点不为空且下一个节点的节点值等于给定的val,则需要删除下一个节点。删除下一个节点可以通过以下做法实现: temp.next = temp.next.next
如果temp的下一个节点的节点值不等于给定的val,则保留下一个节点,将temp移动到下一个节点即可。
当temp的下一个节点为空时,链表遍历结束,此时所有节点值等于val的节点都被删除。
由于链表的头节点head有可能需要被删除,因此创建哑节点dummyHead
令dummyHead.next=head,初始化temp=dummyHead,然后遍历链表进行删除操作。最终返回dummyHead.next即为删除操作后的头节点。
12345678910111 ...
数据结构-3-链表
引言:
方法论
对于笔试:一切为了时间复杂度,不用在乎空间复杂度
对于面试:时间复杂度放在首位,空间复杂度尽量最小
重要技巧
额外数据结构记录(哈希表等)
快慢指针
链表回文
方式1 利用快慢指针走到中点,将链表后一半放入栈
123456789101112131415161718192021222324252627282930313233343536// 方式1public boolean isPalindrome(Node head) {if (head == null || head.next == null) { return true;}// 定义栈Stack<Node> stack = new Stack<Node>();// 定义右指针Node right = head.next;// 定义当前指针Node cur = head;while (cur.next != null || cur.next.next != null) { // 慢指针步长为1 right = right ...
FSharp-07-Collections-Mutable
Array创建操作
步长为1的数组
1234let a = [|0..10|]printfn $"%A{a}"[|0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10|]
步长为2的数组
1234let a = [|1..2..10|]// [|1; 3; 5; 7; 9|]printfn $"%A{a}"a
index
value
0
1
1
3
2
5
3
7
4
9
步长为-2的数组
12let a=[|10.. -2 ..0|]a
index
value
0
10
1
8
2
6
3
4
4
2
5
0
创建数组的其他方式
12345let arr123 = [| 1 2 3 |]arr123
index
value
0
1
1
2
2
3
12let arrOfSquares = [| for i in 0 .. 10 -> i*i ...
FSharp-06-Function_Advance
Recursive Function
Fibonacci
recursion123456// Fibonacci 1 1 2 3 5 ...let rec f x = match x with | 1 -> 1 | 2 -> 1 | _ -> f (x-1) + f (x-2)
sum
conventional
12345let mutable x =0for i in [1..100] do x <- x+ ix// 5050
recursion
12345678let rec sum all result = match all with | head::tail -> sum tail (result+head) | [] -> resultsum [1..100] 0// 5050
exercise
123456789101112131415// 求1..100的乘积// example1 let mutable x = 1for i in [1..10] do ...
FSharp-05-Match_and_if_then
if then else1234567891011// definelet f x = if x % 2 = 0 then "Even" else "Odd"// invoke f 1 Odd
12345678let g x = if x =1 then "One" elif x=2 then "Two" elif x=3 then "Three" else "Other"// Otherg 11
练习: string -> int “one” -> 1 “two” -> 2 “three” -> 3 _ -> 0
match with1234567let f x = match (x % 2) with | 0 -> "Even" | 1 -> "Odd"f 2// ...











