随笔分类 -  LeetCode

LeetCode题解
摘要:有效的数独 判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。 上图是一个部分填充的有效的数独。 数独部分空格内已填入了数字,空白格用 '.' 表示。 示例 1: 示例 2: 说明: 一个有效的数独(部分已被填充)不一定是可解的。 只需要根据以上规则,验证已经填入 阅读全文
posted @ 2018-09-11 21:58 gremount 阅读(148) 评论(0) 推荐(0)
摘要:https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/28/ 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 说明: 此题有两个思 阅读全文
posted @ 2018-08-31 13:14 gremount 阅读(139) 评论(0) 推荐(0)
摘要:题意是把一个二叉树转换成一个链表,链表的元素顺序是二叉树的先序遍历结果。 最简单的做法就是先序遍历该二叉树,将所有经过的节点的指针都依次记录在一个vector里,完成后,在将vector里的节点指针的左孩子置为NULL,右孩子置为vector中的下一个指针。具体的代码和运行效果如下 这里用了多余的空 阅读全文
posted @ 2017-04-05 23:24 gremount 阅读(246) 评论(0) 推荐(0)
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maxim 阅读全文
posted @ 2017-04-02 14:00 gremount 阅读(174) 评论(0) 推荐(0)
摘要:Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeat 阅读全文
posted @ 2016-10-12 16:58 gremount 阅读(142) 评论(0) 推荐(0)
摘要:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possib 阅读全文
posted @ 2016-09-04 15:33 gremount 阅读(223) 评论(0) 推荐(0)
摘要:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then l 阅读全文
posted @ 2016-08-26 14:19 gremount 阅读(241) 评论(0) 推荐(0)
摘要:Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 两个字符串A和B, 在A中查找是否出现过B, 如 阅读全文
posted @ 2016-08-15 19:49 gremount 阅读(151) 评论(0) 推荐(0)
摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 该题是要求合并两 阅读全文
posted @ 2016-08-14 22:47 gremount 阅读(152) 评论(0) 推荐(0)
摘要:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这道题如果有第21题 合并两个链表的基础就会比较容易,具体合并链表的时候有两种思路 (1)如果k个li 阅读全文
posted @ 2016-08-14 22:19 gremount 阅读(169) 评论(0) 推荐(0)
摘要:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array whic 阅读全文
posted @ 2016-08-08 14:47 gremount 阅读(123) 评论(0) 推荐(0)
摘要:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. 阅读全文
posted @ 2016-08-08 11:07 gremount 阅读(151) 评论(0) 推荐(0)
摘要:Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: All root-to-leaf paths are: 阅读全文
posted @ 2016-07-31 16:33 gremount 阅读(114) 评论(0) 推荐(0)
摘要:112题目如下: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the giv 阅读全文
posted @ 2016-07-31 15:54 gremount 阅读(159) 评论(0) 推荐(0)
摘要:Implement atoi to convert a string to an integer. 题目分析: 题目本身很简单就是将一个字符串转化成一个整数,但是由于字符串的千差万别,导致在实现的时候细节非常多。需要注意的有以下一些: 1.字符串可能由一些空格开始,然后遇到一个正号或者负号,然后是正 阅读全文
posted @ 2016-03-07 23:15 gremount 阅读(205) 评论(0) 推荐(0)
摘要:leetcode网站原题如下:给一个非负数组,开始位置是下标为0的元素,数组的元素代表当前位置最多向前行进多少个位置,判断是否能到达数组的最后一个位置,能就回复true,不能就回复false. Given an array of non-negative integers, you are init 阅读全文
posted @ 2015-12-06 11:31 gremount 阅读(140) 评论(0) 推荐(0)
摘要:题目:算出二叉树的最大深度 解决方案:(1)BFS (2)DFS (1)BFS 一层一层往下搜索,一直找到最深的点,这里由于节点的val是没有用的,所以可以用来存储当前节点的深度,然后注意指针一定要初始化,不然在leetcode里可能会出现runtime error /** * Definition 阅读全文
posted @ 2015-11-14 21:18 gremount 阅读(124) 评论(0) 推荐(0)