当前位置:网站首页>一些编码Tips
一些编码Tips
2022-06-10 12:42:00 【51CTO】
递归控制
如何证明递归函数正确执行?
- 数学归纳法中的数学/自然语言<-->程序语言
递归书写方法
- 严格定义递归函数作用,包括参数,返回值,Side-effct
- 先一般,后特殊
- 每次调用必须缩小问题规模
- 每次问题规模缩小程度必须为1
链表创建
Head -->1-->2-->3-->4-->5-->null
为何面试喜欢问链表(单向)
- 容易理解
- 代码难写
- 通过链表本身考察代码的能力
链表反转
列出所有组合(side effect)
- combinations([1, 2, 3, 4], 2):
- [1, 2]、[1、3]、[1、4]、[2、3]、[2、4]、[3、4]
递归缺点:
- 函数调用开销
- stack overflow
- 问题规模:n million 栈大小
循环控制
递归 --> 非递归
- 一般化的方法仍需要使用栈
- 代码复杂,不根本解决问题
循环不变式(loop invariant)
- 是一句断言定义各变量所满足的条件
-
Var a, b; -
While(){
}
循环书写方法
- 定义循环不变式,并在循环体每次结束后保持循环不变式
- 先一般,后特殊
- 每次必须向前推进循环不变式中涉及的变量值
- 每次推进的规模必须为1
链表反转
链表中delete_if
- 去重
- 头节点没有previous怎么办?
- 特殊处理
- 增加虚拟头节点
边界控制
例如:二分查找 在二序数组中查找元素k,返回k所在下标 binarySearch([1, 2, 10, 15, 100], 15) == 3
二分查找思路:
- 规定要查找的值k可能在的数组arr内下标区间a, b
- 计算区间a,b的中间点m
- 若k<arr[m],将区间缩小为a, m。继续二分查找
- 若k>arr[m],将区间缩小为m, b。继续二分查找
数据结构
树 -- 重点与难点
在白板上写程序:白板、纸笔、Word文档、记事本 修改不便;缩进不便;对齐困难
心里不抵触;
先思考后写;
不要惧怕修改/重写
回顾
列表:
- 数组--随机访问
- 链表
- 队列、栈-- 访问有讲究
树:
- 二叉树
- 搜索树
- 堆/优先队列
栈/队列/优先队列
Map<K, V> / Set<K>
- HashMap / HashSet --> K.hashCode()
- TreeMap / TreeSet -- > K implements Comparable
图:
- 无向图
- 有向图
- 有向无环图
- 图的算法--复杂,面试一般不出算法题
- 深度优先遍历
- 广度优先遍历
- 拓扑排序
- 最短路径/最小生成树
数学归纳法 -- 用在编码上
用于证明断言对所有自然数成立
- 证明对于N=1成立
- 证明N>1时:如果对于N-1成立,那么对于N成立
数学归纳法法则: 求证:1+2+3+4+...+n=n(n+1)/2
- 1 = 1*2/2
- 如果1+2+3+...+(n-1) = (n-1)n/2
- 那么1+2+3+...+n = 1+2+3+...+(n-1)+n=(n-1)n/2+n = (n(n-1)+2n)/2=n(n+1)/2
边栏推荐
- Automatic Mapping of Tailored Landmark Representations for Automated Driving and Map Learning 论文阅读
- 百度程序员删库被判9个月,手机号一键解绑功能发布,推特再向马斯克妥协,今日更多大新闻在此...
- Comparison of two BigDecimal data types, addition, subtraction, multiplication and division, and formatting
- 从解读 BDC 自动生成的代码谈起,讲解 SAPGUI 的程序组成部分试读版
- list. Remove (index) returns false, removal failed
- 今天,一对情侣拿下香港最大电商IPO
- 'setbackgrounddrawable() 'is deprecated, setbackgrounddrawable is obsolete
- Performance test plan (plan) template
- Use soapUI tool to generate SMS interface code
- [spark] (task8) pipeline channel establishment in sparkml
猜你喜欢

ASP. Net using imagemap control to design navigation bar

Unity3d 使用URP渲染管线实现AR阴影(阴影投射再透明地面)

Use soapUI tool to generate SMS interface code

出海企业遇瓶颈 茄子科技(SHAREit Group)有话说

TIDB 初級課程體驗 8 (集群的管理維護, 添加一個TIKV節點)

JS click the button to slide to the left

使用SoapUI工具生成发送短信接口代码

KITTI 相关信息汇总

技术分享| 快对讲,全球对讲

Google proposed the super pre training model coca, and the accuracy of fine-tuning top-1 on Imagenet reached 91%! SOTA on multiple downstream tasks!
随机推荐
Practical cases, in-depth analysis
【NLP】NLP全路径学习推荐
Ant financial services Yang Jun: evolution of ant data analysis platform and application of data analysis methods
世贸组织MC12重启 议程重点关注全球经济复苏
【抬杠C#】如何实现接口的base调用
Binary XML file line 96: error inflating class & lt; unknown&gt;
由文件图形丢失,说明自己都不用自己开发的OFFICE
Yet Another Palindrome Partitioning
[spark] (task8) pipeline channel establishment in sparkml
实战案例,深入剖析
'getWidth()' is deprecated,'getHeight()' is deprecated
MySQL数据库(26):视图 view
Wei Lai: "pinches" the data and "pinches" the future
Vdo-slam: a visual dynamic object aware slam system paper reading
DynaSLAM II: Tightly-Coupled Multi-Object Tracking and SLAM 论文阅读
【深度学习】基于深度学习Autoencoder的信用卡欺诈异常检测,效果非常牛逼
KITTI 相关信息汇总
Stereo Vision-based Semantic 3D Object and Ego-motion Tracking for Autonomous Driving 论文阅读
change system time
Offer has been made, advanced learning