
算法
icodebugs
爱写bug
展开
-
Leetcode 498:对角线遍历Diagonal Traverse(python3、java)
对角线遍历给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示。Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below im...原创 2019-06-05 14:20:16 · 225 阅读 · 0 评论 -
Leetcode 344:Reverse String 反转字符串(python、java)
Leetcode 344:Reverse String 反转字符串公众号:爱写bugWrite a function that reverses a string. The input string is given as an array of characters char[].Do not allocate extra space for another array, you must...原创 2019-06-30 16:32:31 · 287 阅读 · 0 评论 -
LeetCode 151:给定一个字符串,逐个翻转字符串中的每个单词 Reverse Words in a String
公众号:爱写bug(ID:icodebugs)翻转字符串里的单词Given an input string, reverse the string word by word.示例 1:输入: "the sky is blue"输出: "blue is sky the"示例 2:输入: " hello world! "输出: "world! hello"解释: 输入字符串...原创 2019-07-11 12:43:42 · 900 阅读 · 0 评论 -
LeetCode 203:移除链表元素 Remove LinkedList Elements
删除链表中等于给定值 val 的所有节点。Remove all elements from a linked list of integers that have value val.示例:输入: 1->2->6->3->4->5->6, val = 6输出: 1->2->3->4->5解题思路:两种方法,一种是迭代法,从第...原创 2019-07-18 10:16:15 · 154 阅读 · 0 评论 -
LeetCode 141:环形链表 Linked List Cycle
给定一个链表,判断链表中是否有环。为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。Given a linked list, determine if it has a cycle in it.To represent a cycle in the given linked list, we use...原创 2019-07-16 10:15:24 · 166 阅读 · 0 评论 -
LeetCode 160: 相交链表 Intersection of Two Linked Lists
爱写Bug(ID:iCodeBugs)编写一个程序,找到两个单链表相交的起始节点。Write a program to find the node at which the intersection of two singly linked lists begins.如下面的两个链表**:**在节点 c1 开始相交。示例 1:输入:intersectVal = 8, listA...原创 2019-07-19 13:10:40 · 144 阅读 · 0 评论 -
LeetCode 328:奇偶链表 Odd Even Linked List
给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。请尝试使用原地算法完成。你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数。Given a singly linked list, group all odd nodes together followed by t...原创 2019-07-20 09:50:09 · 216 阅读 · 0 评论 -
LeetCode 2:两数相加 Add Two Numbers
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。You are given two non-empty linked lists representing two non-negative integers. The digits are st...原创 2019-07-21 12:21:18 · 180 阅读 · 0 评论 -
LeetCode 206:反转链表 Reverse Linked List
反转一个单链表。Reverse a singly linked list.示例:输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL进阶:你可以迭代或递归地反转链表。你能否用两种方法解决这道题?Follow up:A linked list can be reversed eith...原创 2019-07-22 10:32:21 · 153 阅读 · 0 评论 -
LeetCode 61:旋转链表 Rotate List
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。Given a linked list, rotate the list to the right by k places, where k is non-negative.示例 1:输入: 1->2->3->4->5->NULL, k = 2输出: 4->5->1-...原创 2019-07-26 10:51:43 · 157 阅读 · 0 评论 -
Leetcode 542:01 矩阵 01 Matrix
题目:给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。两个相邻元素间的距离为 1 。Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.The distance between two adjacent cells is 1.示例 1:输入:0 ...原创 2019-09-17 23:35:20 · 306 阅读 · 0 评论 -
LeetCode 733: 图像渲染 flood-fill
题目:有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间。An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).给你一个坐标 (sr, sc) 表示图...原创 2019-09-18 00:01:06 · 189 阅读 · 0 评论 -
LeetCode 841:钥匙和房间 Keys and Rooms
题目: 有 N 个房间,开始时你位于 0 号房间。每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间。 在形式上,对于每个房间 i 都有一个钥匙列表 rooms[i],每个钥匙 rooms[i][j] 由 [0,1,...,N-1] 中的一个整数表示,其中 N = rooms.length。 钥匙 rooms[i][j] = v 可以打开编号为 ...原创 2019-09-21 20:36:39 · 289 阅读 · 0 评论 -
哈希表(Hash Table)
概览: 简单来说,哈希表是一种依赖哈希函数组织数据,以达到常数级别时间复杂度,插入和搜索都非常高效的数据结构。两种哈系表:哈希集合是集合数据结构的实现之一,用于存储非重复值。哈希映射是映射 数据结构的实现之一,用于存储(key, value)键值对。大多数高级程序设计语言标准库里都内置了哈系表模板。1、哈希表的原理哈希表的关键思想是使用哈希函数将键映射到存储桶。更确切地说,...原创 2019-09-24 21:40:33 · 331 阅读 · 0 评论 -
LeetCode 706:设计哈希映射 Design HashMap
LeetCode 706:设计哈希映射 Design HashMap题目:不使用任何内建的哈希表库设计一个哈希映射具体地说,你的设计应该包含以下的功能put(key, value):向哈希映射中插入(键,值)的数值对。如果键对应的值已经存在,更新这个值。get(key):返回给定的键所对应的值,如果映射中不包含这个键,返回-1。remove(key):如果映射中存在这个键,删除这个...原创 2019-09-25 20:30:06 · 191 阅读 · 0 评论 -
LeetCode 705:设计哈希集合 Design HashSet
LeetCode 705:设计哈希集合 Design HashSet题目:不使用任何内建的哈希表库设计一个哈希集合具体地说,你的设计应该包含以下的功能add(value):向哈希集合中插入一个值。contains(value) :返回哈希集合中是否存在这个值。remove(value):将给定值从哈希集合中删除。如果哈希集合中没有这个值,什么也不做。Design a HashSe...原创 2019-09-25 20:30:25 · 381 阅读 · 0 评论 -
LeetCode 26:删除排序数组中的重复项 Remove Duplicates from Sorted Array
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。Given a sorted array nums, remove the duplicates in-place such that each element appear only once and re...原创 2019-07-15 10:22:30 · 159 阅读 · 0 评论 -
LeetCode 561:数组拆分 I Array Partition I
公众号: 爱写bug(ID:icodebugs)作者:爱写bugGiven an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for ...原创 2019-07-10 13:40:04 · 154 阅读 · 0 评论 -
Leetcode724:寻找数组的中心索引(java、python3)
寻找数组的中心索引给定一个整数类型的数组 nums,请编写一个能够返回数组**“中心索引”**的方法。我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和。如果数组不存在中心索引,那么我们应该返回 -1。如果数组有多个中心索引,那么我们应该返回最靠近左边的那一个。示例 1:输入: nums = [1, 7, 3, 6, 5, 6]输出: 3解释:...原创 2019-05-29 16:35:25 · 216 阅读 · 0 评论 -
Leetcode747至少是其他数字两倍的最大数
Leetcode747至少是其他数字两倍的最大数在一个给定的数组nums中,总是存在一个最大元素 。查找数组中的最大元素是否至少是数组中每个其他数字的两倍。如果是,则返回最大元素的索引,否则返回-1。Given an array of integers nums, write a method that returns the “pivot” index of this array.We de...原创 2019-05-29 22:04:33 · 158 阅读 · 0 评论 -
Leetcode加一 (java、python3)
加一给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。你可以假设除了整数 0 之外,这个整数不会以零开头。Given a non-empty array of digits representing a non-negative integer, plus one to the integer.The digit...原创 2019-06-03 22:37:07 · 133 阅读 · 0 评论 -
Leetcode54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。Example 1:Input:[...原创 2019-06-25 12:51:11 · 312 阅读 · 0 评论 -
LeetCode 118:Pascal's Triangle 杨辉三角
118:Pascal’s Triangle 杨辉三角Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.给定一个非负整数 *numRows,*生成杨辉三角的前 numRows 行。In Pascal’s triangle, each number is the sum o...原创 2019-06-25 12:53:00 · 225 阅读 · 0 评论 -
LeetCode 485:连续最大1的个数 Max Consecutive Ones(python java)
LeetCode 485:连续最大1的个数 Max Consecutive Ones给定一个二进制数组, 计算其中最大连续1的个数。Given a binary array, find the maximum number of consecutive 1s in this array.示例 1:输入: [1,1,0,1,1,1]输出: 3解释: 开头的两位和最后的三位都是连续1,所以...原创 2019-07-01 16:04:44 · 247 阅读 · 0 评论 -
LeetcCode 27:移除元素 Remove Element(python、java)
公众号:爱写bug给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。Given an array nums and a value val, remove all instances...原创 2019-07-02 13:18:24 · 204 阅读 · 0 评论 -
# Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和)公众号:爱写bug***(python、java)***Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0....原创 2019-06-27 15:55:59 · 272 阅读 · 0 评论 -
LeetCode 283:移动零 Move Zeroes
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.示例:输入: [0,1,0,3,1...原创 2019-07-13 11:50:33 · 177 阅读 · 0 评论 -
# Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bugWrite a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string "".编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。E...原创 2019-06-28 23:56:19 · 147 阅读 · 0 评论 -
LeetCode707:设计链表 Design Linked List
爱写bug (ID:iCodeBugs)设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。在链表类中实现这些功能:get(index):获取链表中第 in...原创 2019-07-14 11:10:00 · 244 阅读 · 0 评论 -
LeetCode 189:旋转数组 Rotate Array
公众号:爱写bug(ID:icodebugs)作者:爱写bug给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。Given an array, rotate the array to the right by k steps, where k is non-negative.示例 1:输入: [1,2,3,4,5,6,7] 和 k = 3输出: [5,6,7,...原创 2019-07-10 13:30:53 · 164 阅读 · 0 评论 -
周末了,围观知乎福利话题,放松一下
公众号:爱写bug(ID:iCodeBugs)前言:周末了,围观几个知乎福利话题:女生身材好是什么体验?:https://www.zhihu.com/question/328457531拥有一双大长腿是怎样的体验?:https://www.zhihu.com/question/297715922有个身材火辣的女朋友是怎样一种体验?:https://www.zhihu.com/ques...原创 2019-07-10 13:32:13 · 425 阅读 · 0 评论 -
LeetCode 28:实现strStr() Implement strStr()
公众号:爱写bug(ID:icodebugs)作者:爱写bug实现 strStr() 函数。给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。Implement strStr().Return the index of the first occurren...原创 2019-07-10 13:34:52 · 149 阅读 · 0 评论 -
LeetCode 118:杨辉三角 II Pascal's Triangle II
公众号:爱写bug(ID:icodebugs)作者:爱写bug给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal’s triangle.Note that the row index starts from ...原创 2019-07-10 13:36:17 · 148 阅读 · 0 评论 -
LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted
公众号:爱写bug(ID:icodebugs)作者:爱写bug给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2*。*说明:返回的下标值(index1 和 index2)不是从零开始的。你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。...原创 2019-07-10 13:37:57 · 178 阅读 · 0 评论 -
LeetCode 217:存在重复元素 Contains Duplicate
题目:给定一个整数数组,判断是否存在重复元素。Given an array of integers, find if the array contains any duplicates.如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。Your function should return true if any value appears...原创 2019-09-25 20:30:44 · 111 阅读 · 0 评论