算法
海口字典
是否现在越无奈,过去越实在
展开
-
leetcode 1072. 按列翻转得到最大值等行数
题目链接https://leetcode-cn.com/problems/flip-columns-for-maximum-number-of-equal-rows/题目看起来让人摸不着头脑,其实看懂题目就会变得特别的简单了。1、首先该函数返回的最小的值应该是12、两行数字,经过对一些列的反转,最后都达到这一行的值的相同。那么这两行数字的关系有以下两种情况: 1).两行的数...原创 2019-10-21 21:43:18 · 266 阅读 · 0 评论 -
leetcode-1182-与目标颜色间的最短距离
题目链接在这里:https://leetcode-cn.com/problems/shortest-distance-to-target-color/题目大意给你一个数组 colors,里面有 1、2、 3 三种颜色。我们需要在 colors 上进行一些查询操作 queries,其中每个待查项都由两个整数 i 和 c 组成。现在请你帮忙设计一个算法,查找从索引 i 到具有目标颜色 c 的...原创 2019-10-21 02:24:10 · 367 阅读 · 0 评论 -
Leetcode copy list with random pointer 拷贝带有随机指针的链表
题目描述A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.题目大意:给定一个链表,每个节点包含一个另外的随机的指...原创 2018-10-29 23:38:41 · 105 阅读 · 0 评论 -
Leetcode word break ii 单词拆分
题目描述Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens ="c...原创 2018-10-27 19:58:14 · 130 阅读 · 0 评论 -
次数出现为1的数
题目描述有一组存放 ID 的数据。并且 ID 取值为 0 - (N-1) 之间,其中只有一个 ID 出现的次数为 1,其他的 ID 出现的次数都等于 2,问如何找到这个次数为 1 的 ID ?解法一:巧用数组下标我的第一想法便是采用下标法来解决,把 ID 作为数组 arr 的下标,在遍历 ID 的过程中,用数组记下每个 ID 出现的次数,即每次遍历到 ID = n,则 arr[n]++...原创 2019-03-13 22:32:06 · 329 阅读 · 0 评论 -
single-number-ii leetcode
Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using ...转载 2019-03-13 23:12:17 · 80 阅读 · 0 评论 -
Leetcode Gas Station(加油站)
Leetcode Gas Station题目描述思路代码题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。思路我们首先要知道能走完整个...原创 2019-03-20 23:28:40 · 154 阅读 · 0 评论 -
Leetcode clone-graph 克隆图
题目描述图的遍历方法有两种:深度优先和广度优先。解法一:深度优先搜索用一个哈希表hashMap来记录已经克隆了的节点,深度优先遍历的递归函数实现如下:(1)如果node节点本身为null,直接返回null。(2)如果hashMap中已经存在了node.label对应的节点,直接返回该节点即可。(3)如果hashMap中还没有存在node.label对应的节点,新建一个节点,其labe...原创 2019-03-21 13:58:05 · 183 阅读 · 0 评论 -
1~n中1出现的次数《剑指offer》
转载自:从1到n整数中1出现的次数:O(logn)算法1. 解题思路考虑将n的十进制的每一位单独拿出讨论,每一位的值记为weight。1) 个位从1到n,每增加1,weight就会加1,当weight加到9时,再加1又会回到0重新开始。那么weight从0-9的这种周期会出现多少次呢?这取决于n的高位是多少,看图:以534为例,在从1增长到n的过程中,534的个位从0-9...转载 2019-03-18 18:59:30 · 161 阅读 · 0 评论 -
Leetcode linked-list-cycle 判断链表是否有环
题目描述 Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?给定一个链表,判断链表中否有环。补充: 你是否可以不用额外空间解决此题?思路:使用快慢指针,如果快指针和慢指针可以相遇,则证明该链表...原创 2018-10-23 17:52:01 · 167 阅读 · 0 评论 -
Leetcode linked-list-cycle-ii 判断链表存在环
题目描述 Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?给定一个链表,返回环开始的结点,如果没有环存在则返回NULL。接着:能...原创 2018-10-23 17:34:57 · 164 阅读 · 0 评论 -
evaluate-reverse-polish-notation(逆波兰表达式的值的计算)
题目描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Some examples: ["2", "1", "+...原创 2018-10-19 14:08:30 · 196 阅读 · 0 评论 -
LeetCode:max-points-on-a-line(位于一条线上的点的最大个数)Java实现
题目描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.给定二维平面上的n个点,请你找出位于一条直线上的点的最大个数。题目解析暴力求解:使用两层循环,注意第二层循环里面考虑相同的点,使用变量same来进行保存。在第二层循环中...原创 2018-10-19 15:06:03 · 228 阅读 · 0 评论 -
LeetCode Sort-List Java
题目描述 Sort a linked list in O(n log n) time using constant space complexity.题目解析归并排序,使用fast和slow指针的方式来将链表一分为二。/** * Definition for singly-linked list. * class ListNode { * int val; *...原创 2018-10-19 16:59:15 · 119 阅读 · 0 评论 -
Leetcode insertion-sort-list 插入排序 Java
题目描述 Sort a linked list using insertion sort.对链表进行插入排序题目解析插入排序的方法是首先构造出一个与head值相同的结点res,比较该结点的值和head后面结点cur的值,若小于head,则cur指向res,res赋值为cur。否则将cur插入到res后面的结点中。代码如下/** * Definition for ...原创 2018-10-19 17:37:10 · 183 阅读 · 0 评论 -
Leetcode binary-tree-postorder 二叉树后续遍历
题目描述 Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3 return[3,2,1].Note: Recursive solutio...原创 2018-10-19 20:32:35 · 96 阅读 · 0 评论 -
Leetcode word break 分词造句
题目描述Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens ="leetcode",dict =["leet", ...原创 2018-10-25 12:19:28 · 501 阅读 · 1 评论 -
Leetcode binary-tree-preorder- traversal 二叉树的前序遍历 Java
题目描述 Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3 return[1,2,3].Note: Recursive solution...原创 2018-10-23 11:59:42 · 155 阅读 · 0 评论 -
Leetcode reorder-list 链表重排序
题目描述 Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, r...原创 2018-10-23 16:45:30 · 123 阅读 · 0 评论 -
mininum-depth-of-binary-tree
题目描述Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.该题目的解法就是寻找离根结点最近的叶子节点解法1(递归):Jav...原创 2018-10-18 17:28:39 · 206 阅读 · 0 评论