如何写出一个bug free的code?

mac2024-12-16  23

首先必须记得 这个问题的输入是什么 输出是什么形式 这些必须要在全程牢牢记住。首先写出输入为空或者长度不足特殊情况: if (nums == null || nums.length < 2) { //特殊情况的处理 return new int[]{-1, -1};//初始化的时候放值 } 然后初始化输出: int[] res = new int[]{-1, -1}; 然后开始思考最一般的情况:先不管任何复杂度等等的限制 首先暴力解法能写出来一种解即可。 大的方向:可以逐个想一下算法思想:暴力穷举(BFS DFS 各种traversal, 双指针的双for),divide and conquer, greedy, recursion(一般递归与尾递归), DP / 或者想一下有没有什么有着特殊性质的数据结构可以帮到 小的方向:针对算法思想的一些小的技巧处理方式:单向双向双指针 ,快慢双指针, sliding window,排序预处理, dummy node,扫描线算法等等等等 之后还可以做进一步的补充。关于小的方向的进一步的补充以及详细的说明:参考资料:https://hackernoon.com/14-patterns-to-ace-any-coding-interview-question-c5bb3357f6edsliding window: the size of sliding window can change some times and some other times just remain constant. 使用场景: 1. the problem input is a linear data structure such as a linked list, array, or string. 2. find the longest/shortest substring, subarray or a desired value(which remains me of 2/3/4 sum)Two pointers or iterators: two pointers is often useful when searching pairs in a sorted array or linked list. and it often used in double direction or single direction. this technique can reduce the time complexity.fast and slow pointers(2x pointer to find the middle, and one pointer goes k first to find the kth last element), **appreciable scene:**1. the problem will deal with a loop in a linked list or array. 2. we you need to know the position of a certain element or the overall length of the linked list. this method often used in linkedlist because you can’t go back you pointer.merge Intervals: many question are involving with merge intervals. **common scenarios:**1. produce a list with only mutually exclusive intervals. 2. overlapping intervals. (I believe for those kind of questions we can use pre sort based greedy)Cyclic sort: the cyclic sort pattern iterates over the array one number at a time, and if the current number you are iterating is not at the correct index, you can swap it with the number at its correct index. you should try placing the number in its corrent index, and this will produce O(n^2) which is not optimal, hence the cyclic sort pattern. common scenario: 1. problems involving a sorted array with numbers in a given range(?). 2. if the problem asks you to find the missing/duplicate/smallest number in a sorted/rotated array.in place reversal of linked list: use prev, cur, temp pointer is solve this problem.Tree BFS: use recursion or queue(related to level order) can be used in problems like zigzag traversal.Tree DFS: use recursion or stack. if the node is not a leaf you need to do following thing: decide whether to process the current node now, or between processing two children or after processing both children. and if node is leaf, you should consider return to previous level.Two Heaps: ??? I never heard of this technique, it’s mainly about maintaining a max heap and a min heap to get the max or min element. common scenario: 1. implement PQ(other ways to implement PQ: array, linkedlist, BST) or scheduling. 2. problem is about you need to find the smallest/largest/median elements of a set.subsets: permutation and combination kind f questions, use BFS to handle those problems.Modified Binary search: whether you are given an array or linked list, or a matrix, if it is sorted, then Binary search might be a good idea. even if the element is not sorted, it’s index is always sorted(就像那道用抽屉原理二分法解的leetcode题)Top K elements: some problem will ask for you to find the top/smallest/frequent K elements. still, uses heap.K way merge: use and maintain a small heap instead of comparation to get the right order.topological sort: it used to find a linear ordering of elements that have dependencies on each other. common scenario: 1. the problem will deal with graphs that have no directed cycles. 2. the problem asked you to update all objects in a sorted order. 3 class schedule, have to follow a particular rules

选择有可能是解的一种 试着写出来。

在写的过程中 可能会遇到之前的各种没注意到的但是会让你痛苦万分的小细节,坚持下去。 在写的过程中再初始化其他要用到的数据结构或者变量,到时候可以做进一步处理。注意题干的各种要求 比如要求用const space 这通常意味着不能用特殊的数据结构, 所以也就意味着需要in place,通常会涉及到交换(或者是index与数值对应,不对应的话会进行交换)如果要求logn,通常说明要用递归,二分,DP等等关于时间复杂度限定为n的进一步说明,不是最多只能一个for,可以进行多个并行的for,而且for里面嵌套一个while循环语句仍然可以算作复杂度为n,见LC41List item

???:If we could maintain two heaps in the following way: A max-heap to store the smaller half of the input numbers A min-heap to store the larger half of the input numbers

最新回复(0)