leetcode

LeetCode 208 - Implement Trie (Prefix Tree)

此生再无相见时 提交于 2020-02-29 17:20:57
一、问题描述 D e s c r i p t i o n : Implement a trie with insert, search, and startsWith methods. N o t e : You may assume that all inputs are consist of lowercase letters a-z . 二、解题报告 什么是trie树,trie树有哪些应用,怎么实现trie树,请看《 Trie树的应用与实现 》。 直接上代码: class TrieNode { public: bool iskey; // 标记该节点是否代表关键字 TrieNode *children[26]; // 各个子节点 TrieNode() { iskey = false; for(int i=0; i<26; ++i) children[i] = NULL; } }; class Trie { public: Trie() { root = new TrieNode(); } // 插入一个单词到trie树中 void insert(string s) { TrieNode* node = root; for(int i=0; i<s.size(); ++i) { if(node->children[s[i]-'a'] == NULL) { node-

LeetCode:Reverse Nodes in k-Group

不羁岁月 提交于 2020-02-29 06:19:16
1、题目名称 Reverse Nodes in k-Group(分组翻转链表) 2、题目地址 https://leetcode.com/problems/reverse-nodes-in-k-group 3、题目内容 英文: 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 left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nodes itself may be changed. Only constant memory is allowed. 中文: 给出一个链表,以k个元素为一组,对各组内元素进行翻转。忽略最后不够k的元素的部分。可以只对节点中的值进行交换,空间复杂度须控制在O(1)。 举例: 给出链表:1->2->3->4->5 当 k = 2 时,翻转后的链表为:2->1->4->3->5 当 k = 3 时,翻转后的链表为:3->2->1->4->5 4、解题方法 对这个问题

LeetCode 四数之和

时光怂恿深爱的人放手 提交于 2020-02-28 21:51:22
文章目录 四数之和 题目 解题思路 代码实现 实现结果 四数之和 题目来源: https://leetcode-cn.com/problems/4sum 前言:本题的主要思路,与 LeetCode | 15. 三数之和 此题的思路相似,可以借鉴思考。下面的链接内容是往期关于 LeetCode | 15. 三数之和 的解法。 LeetCode 三数之和 题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。 注意: 答案中不可以包含重复的四元组。 示例: 给定数组 nums = [ 1, 0, -1, 0, -2, 2 ] ,和 target = 0。 满足要求的四元组集合为: [ [ -1, 0, 0, 1 ] , [ -2, -1, 1, 2 ] , [ -2, 0, 0, 2 ] ] 解题思路 思路:数组排序和双指针; 先对特殊情况进行判断,若是 nums 为空(或None) 或者 nums 数组的长度 nl 小于 4,返回空列表; 先对数组进行排序; 返回的结果列表中不会有重复的四元组。在这个前提下,对数组进行遍历(从 0 开始,取 nl -3 个数, 最终需取 4 个元素,且不重复,取到第 nl -

算法面试题合集

戏子无情 提交于 2020-02-28 15:32:58
题目都来着leetcode 新的题都会在这更新 1、程序员面试经典 leetcode刷面试题(面试题01合集) ; leetcode刷面试题(面试题02合集) ; 2、剑指offer leetcode面试题41. 数据流中的中位数 ; 3、周赛 leetcode14双周赛 ; LeetCode第12场双周赛 ; leetcode第149周赛 ; leetcode第7双周赛 ; 第9场双周赛 ; leetcode第8场双周赛 ; leetcode第152周赛 ; leetcode第150周赛 ; leetcode第6双周赛 ; leetcode第146周赛 ; leetcode第147周赛 ; 4、个人总结 位运算在判断重复时候的运用 ; 动态规划总结与示例 ; 位运算在判断重复时候的运用 ; 来源: CSDN 作者: 孤竹彧 链接: https://blog.csdn.net/qq_33321609/article/details/104553224

Leetcode 113. 路径总和 II(Path Sum II)

大憨熊 提交于 2020-02-28 13:51:02
Leetcode 113.路径总和 II 1 题目描述( Leetcode题目链接 )   给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。说明: 叶子节点是指没有子节点的节点。 示例: 给定如下二叉树,以及目标和 sum = 22 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 返回: [ [ 5 , 4 , 11 , 2 ] , [ 5 , 8 , 4 , 5 ] ] 2 题解   与 112题路径总和 相同的做法,深度优先搜索解决该问题 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution : def pathSum ( self , root : TreeNode , sum : int ) - > List [ List [ int ] ] : if not root : return [ ] retv = [ ] def DFS ( stack , node , curr_sum ) : if not node . left and not node .

【LeetCode】 14. Longest Common Prefix 最长公共前缀(Easy)(JAVA)

左心房为你撑大大i 提交于 2020-02-27 23:59:22
【LeetCode】 14. Longest Common Prefix 最长公共前缀(Easy)(JAVA) 题目地址: https://leetcode.com/problems/longest-common-prefix/ 题目描述: Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string “”. Example 1: Input: ["flower","flow","flight"] Output: "fl" Example 2: Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. Note: All given inputs are in lowercase letters a-z. 题目大意 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 “”。 解题方法 直接循环遍历看当前字符的 i 为是否都相同即可 class Solution { public

Leetcode 589.N叉树的前序遍历(N-ary Tree Preorder Traversal)

夙愿已清 提交于 2020-02-27 15:17:59
Leetcode 589.N叉树的前序遍历 1 题目描述( Leetcode题目链接 )   给定一个 N 叉树,返回其节点值的前序遍历。例如,给定一个 3叉树 : 返回其前序遍历: [1,3,5,6,2,4]。 说明: 递归法很简单,你可以使用迭代法完成此题吗? 2 题解   用栈的数据结构,方法同 二叉树的前序遍历 。 """ # Definition for a Node. class Node: def __init__(self, val=None, children=None): self.val = val self.children = children """ class Solution : def preorder ( self , root : 'Node' ) - > List [ int ] : if not root : return [ ] retv = [ ] stack = [ root ] while stack : root = stack . pop ( ) retv . append ( root . val ) stack . extend ( root . children [ : : - 1 ] ) return retv 来源: CSDN 作者: 就叫昵称吧 链接: https://blog.csdn.net/qq

LeetCode All in One 题目讲解汇总(持续更新中...)

本秂侑毒 提交于 2020-02-27 13:50:31
Given string S and a dictionary of words words , find the number of words[i] that is a subsequence of S . Example : Input: S = "abcde" words = ["a", "bb", "acd", "ace"] Output: 3 Explanation: There are three words in words that are a subsequence of S: "a", "acd", "ace". Note: All words in words and S will only consists of lowercase letters. The length of S will be in the range of [1, 50000] . The length of words will be in the range of [1, 5000] . The length of words[i] will be in the range of [1, 50] . 这道题给了我们一个字符串S,又给了一个单词数组,问我们数组有多少个单词是字符串S的子序列。注意这里是子序列,而不是子串,子序列并不需要连续。那么只要我们知道如何验证一个子序列的方法

LeetCode——1. Two Sum

▼魔方 西西 提交于 2020-02-27 03:59:33
一.题目链接: https://leetcode.com/articles/two-sum/ 二.题目大意:   给定一个int型数组A和int值a,要求从A中找到两个数,使得这两个数值的和为a;返回结果为一个数组,该数组存储的为这两个数在数组A中的下标。(题目假设结果是唯一的) 三.题解   1.该题目首先最容易想到的就是暴力破解,只需要两个循环分别遍历数组;这样的话,时间复杂度为O(N 2 ),空间复杂度为O(1),代码如下:    int* twoSum(int* nums, int numsSize, int target) { int *rs = malloc(sizeof(int)*2); int i = 0,j = 0; for(i = 0; i < numsSize; i++) for (j = i + 1; j < numsSize; j++) { if(nums[i] + nums[j] == target) { rs[0] = i; rs[1] = j; return rs; } } return rs; }   2.由于O(N 2 )的时间复杂度效率太低,有没有更好的方法?我们只需想方设法优化第二个for循环即可(第一个for循环一般无法优化,因为该程序至少要遍历一次);可以考虑利用map来进行查询,代码如下:    vector<int> twoSum

LeetCode All in One 题目讲解汇总(持续更新中...)

天涯浪子 提交于 2020-02-27 01:01:07
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/" , => "/home" path = "/a/./b/../../c/" , => "/c" click to show corner cases. Corner Cases: Did you consider the case where path = "/../" ? In this case, you should return "/" . Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/" . In this case, you should ignore redundant slashes and return "/home/foo" . 这道题让简化给定的路径,光根据题目中给的那一个例子还真不太好总结出规律,应该再加上两个例子 path = "/a/./b/../c/" , => "/a/c"和path = "/a/./b/c/" , => "/a/b/c", 这样我们就可以知道中间是"."的情况直接去掉,是".."时删掉它上面挨着的一个路径