随笔分类 -  leetcode

主要记录leetcode刷题过程中的解题思路
摘要:题目描述: superPow(int a, int[] b),b是一个int数组,每个元素都是正的个位数,组合起来表示一个正整数,例如b=[1,2,3]表示123,求解a^b mod 1337. 思路描述: 本题的难点是当a和b足够大时会造成溢出,因此应考虑其他算法来实现。 理论支持(转幂算法): 阅读全文
posted @ 2016-11-04 19:39 沧浪少年 阅读(467) 评论(0) 推荐(0) 编辑
摘要:题目描述: 自己实现pow(double x, int n)方法 实现思路: 考虑位运算。考虑n的二进制表示形式,以n=51(110011)为例,x^51 = x^1*x^2*x^16*x^32,因此每次将n无符号右移一位,并将x取当前值的平方,如果n右移后末位 为1,则将res*x。考虑特殊情况, 阅读全文
posted @ 2016-11-04 15:58 沧浪少年 阅读(391) 评论(0) 推荐(0) 编辑
摘要:质数的判断 埃拉托斯特尼筛法: 算法的过程如下图所示: 我们从2开始遍历到根号n,先找到第一个质数2,然后将其所有的倍数全部标记出来,然后到下一个质数3,标记其所有倍数,依次类推,直到根号n,此时数组中未被标记的数字就是质数。 对于本题,即可采用上述判断质数的方法。 阅读全文
posted @ 2016-09-07 22:04 沧浪少年 阅读(185) 评论(0) 推荐(0) 编辑
摘要:题目要求: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 解题思路 阅读全文
posted @ 2016-08-10 16:18 沧浪少年 阅读(212) 评论(0) 推荐(0) 编辑
摘要:题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 解题思路分析: 前 阅读全文
posted @ 2016-07-03 11:44 沧浪少年 阅读(291) 评论(0) 推荐(0) 编辑
摘要:二叉树的最大深度: 递归的分别获取左右子树的深度,返回较大的深度,每次加1即可。 代码: 二叉树的最小深度: 设置一个深度计数变量 每次获取二叉树的一层结点,依次遍历该层的结点,第一次遇到叶子结点就返回,此时的深度是最小深度。 阅读全文
posted @ 2016-06-08 15:29 沧浪少年 阅读(1844) 评论(0) 推荐(0) 编辑
摘要:题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For 阅读全文
posted @ 2016-06-08 15:22 沧浪少年 阅读(195) 评论(0) 推荐(0) 编辑
摘要:题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level 阅读全文
posted @ 2016-06-08 11:24 沧浪少年 阅读(712) 评论(0) 推荐(0) 编辑
摘要:题目: Given a binary tree Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to  阅读全文
posted @ 2016-06-07 15:29 沧浪少年 阅读(429) 评论(0) 推荐(0) 编辑
摘要:题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return 阅读全文
posted @ 2016-06-03 22:25 沧浪少年 阅读(909) 评论(0) 推荐(0) 编辑
摘要:题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique 阅读全文
posted @ 2016-06-03 17:01 沧浪少年 阅读(576) 评论(0) 推荐(0) 编辑
摘要:题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains 阅读全文
posted @ 2016-06-03 16:26 沧浪少年 阅读(186) 评论(0) 推荐(0) 编辑
摘要:题目要求: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. 给出一棵二叉树,其中有两个结点交换了位置,要求找出这两个 阅读全文
posted @ 2016-06-03 15:52 沧浪少年 阅读(214) 评论(0) 推荐(0) 编辑
摘要:树节点定义: 递归建立二叉树: 1、先序遍历 遍历方式:根节点-->左节点-->右节点 递归先序遍历: 非递归遍历: 对于任意一个结点p 1)访问结点p,并将p入栈 2)将p变为p的左孩子结点,如果p的不为空,循环至 1); 否则弹出当前栈顶使用p接收,将p变为p的右孩子结点; 3)当p结点为nul 阅读全文
posted @ 2016-06-02 18:01 沧浪少年 阅读(398) 评论(0) 推荐(0) 编辑
摘要:题目要求: * 给定字符串,求解最长回文子串 * 字符串最长为1000 * 存在独一无二的最长回文字符串 求解思路: * 回文字符串的子串也是回文,比如P[i,j](表示以i开始以j结束的子串)是回文字符串, * 那么P[i+1,j-1]也是回文字符串。这样最长回文子串就能分解成一系列子问题了。 * 阅读全文
posted @ 2016-05-31 17:26 沧浪少年 阅读(1778) 评论(0) 推荐(0) 编辑
摘要:题目需求: 输入一个字符串,输出对应的int值 特殊处理: 输入: null 输出:0 输入: "a122" 输出:0 输入: " 1233" 输出:1233 输入: " -123" 输出:-123 输入: "+123" 输出:123 输入: " 123a233" 输出:123 输入: "-123 阅读全文
posted @ 2016-05-31 15:51 沧浪少年 阅读(159) 评论(0) 推荐(0) 编辑
摘要:题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity 思路1: 依次归并排序,首先归并前两个,然后归并完成的链表依次和剩下的链表进行归并排序 时间复杂 阅读全文
posted @ 2016-05-30 20:17 沧浪少年 阅读(575) 评论(0) 推荐(0) 编辑
摘要:题目要求: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' valu 阅读全文
posted @ 2016-05-28 11:59 沧浪少年 阅读(917) 评论(0) 推荐(0) 编辑
摘要:1、链表中是否有环 如果链表中存在环的话,则遍历链表时无法通过观察指针是否为null来判断链表是否结束。 判断链表中是否存在环,需要引入快慢指针(slow 和 fast),slow每次走一步,fast每次走两步, 如果slow和fast会相遇,则说明链表中存在环,否则不存在。 2、链表中环的入口结点 阅读全文
posted @ 2016-05-27 15:39 沧浪少年 阅读(171) 评论(0) 推荐(0) 编辑
摘要:对于O(1)的空间复杂度要求,不能对链表进行复制等操作,双指针法对处理该类问题比较有效。 同时由于链表头结点的特殊性,可以考虑引入一个空的头结点来辅助操作。 阅读全文
posted @ 2016-05-26 17:39 沧浪少年 阅读(1192) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示