函数编程-语法
函数编程-语法
表现形式
在 Java 语言中,lambda 对象有两种形式:lambda 表达式与方法引用
lambda 对象的类型是由它的行为决定的,如果有一些 lambda 对象,它们的入参类型、返回值类型都一致,那么它们可以看作是同一类的 lambda 对象,它们的类型,用函数式接口来表示
函数类型
练习:将 lambda 对象分类,见 PPT
函数接口的命名规律
带有 Unary 是一元的意思,表示一个参数
带有 Bi 或 Binary 是二元的意思,表示两个参数
Ternary 三元
Quatenary 四元
…
方法引用也是类似,入参类型、返回值类型都一致的话,可以看作同一类的对象,也是用函数式接口表示
六种方法引用
类名::静态方法名
如何理解:
函数对象的逻辑部分是:调用此静态方法
因此这个静态方法需要什么参数,函数对象也提供相应的参数即可
123456789101112131415161718192021public class Type2Test { public static void main(String[] args) { ...
函数式编程-缘起
道之伊始
宇宙初开之际,混沌之气笼罩着整个宇宙,一切模糊不清。
然后,盘古开天,女娲造人:日月乃出、星辰乃现,山川蜿蜒、江河奔流、生灵万物,欣欣向荣。此日月、星辰、山川、江河、生灵万物,谓之【对象】,皆随时间而化。
然而:日月之行、星汉灿烂、山川起伏、湖海汇聚,冥冥中有至理藏其中。名曰【道】,乃万物遵循之规律,亦谓之【函数】,它无问东西,亘古不变
作为设计宇宙洪荒的程序员
造日月、筑山川、划江河、开湖海、演化生灵万物、令其生生不息,则必用面向【对象】之手段
若定规则、求本源、追纯粹,论不变,则当选【函数】编程之思想
下面就让我们从【函数】开始。
什么是函数
什么是函数呢?函数即规则
数学上:
例如:
INPUT
f(x)
OUTPUT
1
?
1
2
?
4
3
?
9
4
?
16
5
?
25
…
…
…
f(x)=x2f(x) = x^2f(x)=x2 是一种规律, input 按照此规律变化为 output
很多规律已经由人揭示,例如 e=m⋅c2e = m \cdot c^2e=m⋅c2
程序设计中更可以自己去制定规律,一旦成 ...
附录
附录
参考文章
[^1]: “Definition of ALGORITHM”. Merriam-Webster Online Dictionary. Archived from the original on February 14, 2020. Retrieved November 14, 2019.
[^2]: Introduction to Algorithm 中文译作《算法导论》
[^3]: 主要参考文档 https://en.wikipedia.org/wiki/Binary_search_algorithm
[^4]: 图片及概念均摘自 Introduction to Algorithm 4th,3.1节,3.2 节
[^5]: 图片引用自 wikipedia linkedlist 条目,https://en.wikipedia.org/wiki/Linked_list
[^6]: 也称为 Pascal’s triangle https://en.wikipedia.org/wiki/Pascal’s_triangle
[^7]: 递归求解斐波那契数列的时间复杂度——几种 ...
股票问题
股票问题
Leetcode 121
123456789101112131415161718192021public class SharesI { static int maxProfit(int[] prices) { int i = 0; int j = 1; int max = 0; while (j < prices.length) { if (prices[j] - prices[i] > 0) { max = Math.max(max, prices[j] - prices[i]); j++; } else { i = j; j++; } } return max; } public sta ...
Leetcode 设计
Leetcode 设计
LRU 缓存-Leetcode 146
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495public class LRUCacheLeetcode146 { static class LRUCache { static class Node { Node prev; Node next; int key; int value; public Node(int key, int value) { this.key = key; t ...
Leetcode 字符串
Leetcode 字符串
indexOf-Leetcode 28
native string matching
123456789101112131415161718192021222324public class StrStrLeetcode28 { static int strStr(String haystack, String needle) { char[] text = haystack.toCharArray(); char[] pattern = needle.toCharArray(); int n = text.length; int m = pattern.length; for (int i = 0; i <= n - m; i++) { int j; for (j = 0; j < m; j++) { if (pattern[j] != text[i + j]) ...
Leetcode 单调队列和栈
Leetcode 单调队列和栈
单调递减队列
12345678910111213141516171819202122232425262728293031public class MonotonicStack<T extends Comparable<T>> { private final LinkedList<T> stack = new LinkedList<>(); public void push(T t) { while (!stack.isEmpty() && stack.peek().compareTo(t) < 0) { stack.pop(); } stack.push(t); } public void pop() { stack.pop(); } public T peek() { retur ...
Leetcode 双指针
Leetcode 双指针
下面是的题目都会涉及双指针,除此外,还有
Leetcode3 最长不重复子串,在 hash 表部分讲过了
快排中
二分中
…
移动零-Leetcode 283
123456789101112131415161718192021public class MoveZeroesLeetcode283 { static void moveZeroes(int[] nums) { int i = 0; int j = 0; while (j < nums.length) { if (nums[j] != 0) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; i++; } j++; } } ...
Backtracking Algorithm
Backtracking Algorithm
入门例子
12345678910111213141516public class Backtracking { public static void main(String[] args) { rec(1, new LinkedList<>()); } static void rec(int n, LinkedList<String> list) { if (n == 3) { return; } System.out.println("before:" + list); list.push("a"); rec(n + 1, list); list.pop(); System.out.println("after:" + list); ...
Divide and Conquer
Divide and Conquer
概述
分治思想
将大问题划分为两个到多个子问题
子问题可以继续拆分成更小的子问题,直到能够简单求解
如有必要,将子问题的解进行合并,得到原始问题的解
之前学过的一些经典分而治之的例子
二分查找
快速排序
归并排序
合并K个排序链表 - LeetCode 23
二分查找
1234567891011121314151617public static int binarySearch(int[] a, int target) { return recursion(a, target, 0, a.length - 1);}public static int recursion(int[] a, int target, int i, int j) { if (i > j) { return -1; } int m = (i + j) >>> 1; if (target < a[m]) { return ...