自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(31)
  • 资源 (1)
  • 收藏
  • 关注

原创 Leetcode 50 Pow(x, n) 两种方式求解

原题链接: http://oj.leetcode.com/problems/powx-n/ 这道题是一道数值计算的题目,因为指数是可以使结果变大的运算,所以要注意越界的问题。如同我在Sqrt(x)这道题中提到的,一般来说数值计算的题目可以用两种方法来解,一种是以2为基进行位处理的方法,另一种是用二分法。这道题这两种方法都可以解,下面我们分别介绍。第一种方法在Divide Two Integers使...

2018-05-31 22:30:34 131

原创 Leetcode 50 Pow(x, n) 两种方式求解

原题链接: http://oj.leetcode.com/problems/powx-n/ 这道题是一道数值计算的题目,因为指数是可以使结果变大的运算,所以要注意越界的问题。如同我在Sqrt(x)这道题中提到的,一般来说数值计算的题目可以用两种方法来解,一种是以2为基进行位处理的方法,另一种是用二分法。这道题这两种方法都可以解,下面我们分别介绍。第一种方法在Divide Two Integers使...

2018-05-31 22:27:29 101

原创 Leetcode 50 Pow(x, n) 两种方式求解

原题链接: http://oj.leetcode.com/problems/powx-n/ 这道题是一道数值计算的题目,因为指数是可以使结果变大的运算,所以要注意越界的问题。如同我在Sqrt(x)这道题中提到的,一般来说数值计算的题目可以用两种方法来解,一种是以2为基进行位处理的方法,另一种是用二分法。这道题这两种方法都可以解,下面我们分别介绍。第一种方法在Divide Two Integers使...

2018-05-31 22:14:05 209

转载 LeetCode --- 29. Divide Two Integers(位运算)(异或)

题目链接:Divide Two IntegersDivide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.这道题的要求是在不使用乘法、除法、取模运算的前提下实现两个整数相除。如果溢出,返回MAX_INT。这道题的直接思路是用被除数不断减去...

2018-05-31 17:28:07 127

原创 leetcode 227: Basic Calculator II

class Solution {public: //利用堆栈实现基本运算,对给定的字符串进行轮训,如果是数字则进行循环累积,直到不为数字,则考虑如何将数字压入堆栈。 //如果之前记录的flag操作符是+或者-号,则将该数字压入到堆栈中,如果是操作符是*或者/号,则将栈顶元素取出之后与当前数字进行运算,将运算结果保存到堆栈中。最后堆栈中所保存的数字之间均为+运算关系,则只需要将堆...

2018-05-30 20:26:25 114

转载 LeetCode91:Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total n...

2018-05-30 10:34:58 118

原创 LeetCode(49)Group Anagrams(查找同构字符串)(int->string 用方法 to_string())

题意理解:将给定字符串分组,要求同一组的字符串由相同字符组成,最终,将各组字符串按字典序输出;题目分析:1. 将字符串先进行排序,然后通过字符串比较来判定是否为同一组;2. 将分组后的字符串集合再进行排序,从而满足题目的字典序要求;3. 在分组的过程中需要进行查找,本题中使用了hash的思想,但C++的STL中没有hash容器,解题代码中使用map来完成hash功能(map由红黑数实现,效率比很实...

2018-05-30 10:23:56 153

原创 【LeetCode】28. Implement strStr()

首先要做一些判断,如果子字符串为空,则返回0,如果子字符串长度大于母字符串长度,则返回-1。然后我们开始遍历母字符串,我们并不需要遍历整个母字符串,而是遍历到剩下的长度和子字符串相等的位置即可,这样可以提高运算效率。然后对于每一个字符,我们都遍历一遍子字符串,一个一个字符的对应比较,如果对应位置有不等的,则跳出循环,如果一直都没有跳出循环,则说明子字符串出现了,则返回起始位置即可,代码如下:cla...

2018-05-29 22:13:02 179

转载 LeetCode(22)Generate Parentheses(输出可能的合法表达式)(递归进行,在不同的条件可以加左括号,右括号)(像二叉树中序遍历)

题目Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:“((()))”, “(()())”, “(())()”, “()(())”, “()()()”分析给...

2018-05-29 21:23:50 426

转载 【LeetCode】17. Letter Combinations of a Phone Number

Letter Combinations of a Phone NumberGiven a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is...

2018-05-29 20:44:12 132

原创 LeetCode 14 Longest Common Prefix(最长公共前缀)(String)

思路优化,本来是把第一个字符串的长度作为初始预估prefix值。后来看到别人写的,还是觉得提前循环一遍,找到最短的字符串的长度作为prefix的长度预估值。双层循环:外层循环,用下图横着做,开始都是str[j][0]---str[j][1]---str[j][minlen-1]内层循环,用下图竖着做,大家都是第一个字符,看看一样吗,不一样则break。                     如果...

2018-05-29 19:38:31 321

转载 LeetCode 8:String to Integer (atoi)

题目的大概意思是:按照atoi函数的转换原则,将输入的字符串转换成整型数。其实就是自行实现atoi函数。这道题难度等级:简单需要注意的是以下几点:但是怎么觉得不止这几种情况啊,这些情况我都考虑且避免了。还是AC不掉,暂时没有找到一个详细的转换原则。网上找到的代码如下:class Solution {public: int myAtoi(string str) { ...

2018-05-29 09:59:40 114

转载 LeetCode127—Word Ladder(我觉得还挺难的,比如如何判断邻居节点,怎样记录路径长度,感觉有点像DP)

Word LadderGiven two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter can be changed at a timeEach intermediate...

2018-05-22 12:35:58 930

原创 LeetCode 102. Binary Tree Level Order Traversal 103二叉树分层Z字形遍历

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7],3/ \9 20/ \15 7return its lev...

2018-05-22 10:13:16 95

转载 Leetcode 207 &210 Course Schedule I II 课程表

题目:课程表There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as ...

2018-05-21 11:12:46 400

原创 LeetCode 116 Populating Next Right Pointers in Each Node

Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }12345  Populate each next pointer to point to its next right node. If t...

2018-05-20 20:41:46 110

原创 leetcode 104 Maximum Depth of Binary Tree二叉树求深度

求二叉树深度递归方法:class Solution {public: int maxDepth(TreeNode* root) { if(NULL == root) return 0; int depth_l = maxDepth(root->left); int depth_r = maxDepth(...

2018-05-20 16:54:04 93

转载 【LeetCode】101. Symmetric Tree 中序遍历,分支遍历,二叉树

101. Symmetric TreeTotal Accepted: 103742 Total Submissions: 306773 Difficulty: EasyGiven a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this bina...

2018-05-20 15:59:17 131

原创 LeetCode 98. Validate Binary Search Tree

方法1:class Solution {public: bool dfsleft(TreeNode *root,int value){ if(root==NULL) return true; if(root->val>=value) return false; return dfsleft(root->left,value...

2018-05-20 12:04:10 96

原创 leetcode 328 Odd Even Linked List(调整链表使得奇数位置的元素位于偶数位置元素之前)

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.    You should try to do it in...

2018-05-19 21:18:16 562

原创 LeetCode 237 : Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with ...

2018-05-19 20:53:42 283

原创 LeetCode 206. Reverse Linked List(单链表的逆置) LeetCode234_PalindromeLinkedList (判断是否为回文链表)

单链表的逆置class Solution {public: ListNode* reverseList(ListNode* head) { ListNode *p1,*p2,*p3; if(head==NULL||head->next==NULL) return head; p1=head; p2=p1->nex...

2018-05-19 19:00:15 151

原创 LeetCode 160 Intersection of Two Linked Lists 求两个链表的交点

A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3第一种:分别遍历两个链表,得到分别对应的长度。然后求长度的差值,把较长的那个链表向后移动这个差值的个数,然后一一比较即可。/**...

2018-05-19 18:47:38 104

转载 Leetcode 138. Copy List with Random Pointer(一个单链表包含一个指向任意结点或者null的结点指针,返回这个链表的深拷贝)

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.方法1:按照原链表next的顺序依次创建节点,并处理好新链表的next指针,...

2018-05-19 17:13:32 198

原创 LeetCode 21 Merge Two Sorted Lists(合并两个已排序的链表)(Linked List)

递归版:确实简单很多,但是还需要练习。class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (l1 == NULL) { return l2; } if (l2 == NULL) { ...

2018-05-19 12:16:01 207

原创 LeetCode 19 : Remove Nth Node From End of List

第一种方法:先遍历一遍取得长度,然后长度减去n就知道要删除正着数第几个节点,注意,我这里一直卡在,想找到要删除的第len-n个,只能循环cur=cur->next;len-n-1遍,才能将cur指针刚好指在要删除的节点的前一个。所以就会有两种情况会不进 for(int i=1;i<=len-n-1;i++) cur=cur->next;循环,第一种要删除的...

2018-05-19 11:53:36 121

转载 白话经典算法系列之五 归并排序的实现(讲的真好)

       归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。首先考虑下如何将将二个有序数列合并。这个非常简单,只要从比较二个数列的第一个数,谁小就先取谁,取了后就在对应数列中删除这个数。然后再进行比较,如果有数列为空,那直接将另一个数列的数据依次取出即可。[cpp] view plain copy//将有序数组a[]...

2018-05-12 19:30:27 120

转载 排序算法之 插入排序、希尔(shell)排序 及其时间复杂度和空间复杂度

     有一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,但要求插入后此数据序列仍然有序,这个时候就要用到一种新的排序方法——插入排序插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为O(n^2)。是稳定的排序方法。插入算法把要排序的数组分成两部分:第一部分包含了这个数组的所有元素,但将最后...

2018-05-12 17:29:50 4945

转载 排序算法之 快速排序 及其时间复杂度和空间复杂度

       快速排序是排序算法中效率相对较高的,但使用的人却是比较少,大家一般信手拈来的排序算法就是冒泡排序。因为冒泡排序主观,容易理解,而快速排序使用到了递归,大家可能就有点不知所措了。算法分析        快速排序由C. A. R. Hoare在1962年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对...

2018-05-12 16:30:23 1663 1

原创 LeetCode(13) RomanToInteger

方法1:(弄错了两个len,下回要小心)class Solution {public: int romanToInt(string s) { char tmpchar[]={'I','V','X','L','C','D','M'}; int tmpint[]={1,5,10,50,100,500,1000}; map<char,int...

2018-05-03 18:03:42 62

原创 [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For ...

2018-05-03 17:14:42 131

响应式网页编写经典入门教程(老外写的).zip

很好,自己看的,初学者不好找的,总结下给大家分享。

2017-10-29

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除