自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(311)
  • 收藏
  • 关注

原创 github学习手记

1. 安装配置安装;配置$ git config --global user.name "Your Name"$ git config --global user.email "email@example.com"$ ssh-keygen -t rsa -C "your_email@youremail.com"成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pu...

2019-08-09 10:03:34 159

原创 108. Convert Sorted Array to Binary Search Tree

1、题目描述给一个升序数组,将它生成平衡BST。 2、思路最中间的元素作为根节点,左边的元素生成左子树,右边生成右子树。 3、代码  TreeNode* sortedArrayToBST(vector<int>& nums) { int n = nums.size(); if(n==0) ...

2018-12-26 01:53:48 102

原创 109. Convert Sorted List to Binary Search Tree

1、题目描述输入一个有序链表,将其转换成平衡的BST。 2、思路如果链表中节点数量为奇数,则刚好取所有节点的中点,用这个数字来生成树的根节点。左右两边可以划分为两个长度相等的链表,分别生成树的左子树和右子树。如果链表中节点数量为偶数,则取中线靠前的一个位置的节点,让它当根节点。左边的链表长度比右边的少1,分别生成树的左子树和右子树。时间复杂度O(nlogn)。 ...

2018-12-26 01:53:31 115

原创 112. Path Sum

1、题目描述给出一个二叉树和一个target,判断有没有一条从根节点到叶子节点的路径,使得路径上的节点数值之和等于target。 2、思路BFS:每次放入queue的都是从根节点到当前节点的节点数值之和。如果当前有左右节点,则将其分别加上左右节点数值,放入queue,若当前节点是叶子节点,则判断当前和是否是target。 DFS:初始值sum为root->val,...

2018-12-26 01:53:07 82

原创 114. Flatten Binary Tree to Linked List

1、题目描述把一个二叉树转换成链表。 2、思路转换成的链表形式是 根节点-左子树形成的链表-右子树形成的链表递归。 3、代码  void flatten(TreeNode* root) { if(!root) return; else root = helper(root); } Tree...

2018-12-26 01:52:46 60

原创 115. Distinct Subsequences

1、题目描述输入字符串s和t,有多少种方式让s删删减减形成t。 2、思路动态规划。具体看代码吧。 3、代码  int numDistinct(string s, string t) { int l1=s.size(),l2=t.size(); if(l1<l2) return 0; int dp[l1+1...

2018-12-26 01:52:28 95

原创 118. Pascal's Triangle

1、题目描述输入行数n,形成帕斯考三角形。 2、思路观察特点,模拟。 3、代码  vector<vector<int>> generate(int numRows) { vector<vector<int>> ans; if(numRows==0) return ans; ...

2018-12-26 01:52:08 66

原创 119. Pascal's Triangle II

1、题目描述输入k,返回xxx三角形第k行。2、思路模拟。3、代码  vector<int> getRow(int rowIndex) { vector<int> ans; ans.push_back(1); for(int i=1;i<=rowIndex;i++){ ...

2018-12-26 01:51:49 65

原创 120. Triangle

1、题目描述给一个由数组组成的数组,三角形的,返回从顶到底的最短路径和。 2、思路一层层更新。更新每一层走到每个位置的最小值。返回最后一行的最小值。 3、代码  int minimumTotal(vector<vector<int>>& triangle) { int n = triangle.size()...

2018-12-26 01:51:28 72

原创 695. Max Area of Island

1、题目描述由0和1组成的二维矩阵表示水和陆地,返回最大的陆地面积。 2、思路DFS,记录面积大小。 3、代码  int x[4]={0,0,-1,1}; int y[4]={-1,1,0,0}; int maxAreaOfIsland(vector<vector<int>>& grid) { ...

2018-12-26 01:51:09 77

原创 203. Remove Linked List Elements

1、题目描述输入一个单链表,和一个val,返回删除链表中的val元素的新链表。 2、思路开一个新链表,遍历链表,如果节点值是val,就跳过这个节点,否则就连接这个节点。 3、代码  ListNode* removeElements(ListNode* head, int val) { ListNode* p = new ListNode(-1...

2018-12-26 01:50:45 75

原创 201. Bitwise AND of Numbers Range

1、题目描述输入m和n,求m到n范围内所有数字做与运算,得到的结果。2、思路naive:把m到n全都与运算,复杂度O(n);法2:复杂度O(1);从最末位开始,找到m和n的前半部分完全相同的位置。也就是说,删除了a位的末位数字,得到新的m(n)。将m右移a位得到答案。3、代码 int rangeBitwiseAnd(int m, int n) {

2018-05-29 16:46:44 97

原创 202. Happy Number

1、题目描述输入一个数字判断它是不是开心数字。开心数字的定义是把一个数的每一位的平方相加,得到新的数字,重复这个过程,如果得到1,则是开心数字。2、思路用hash table保存已经产生过的数字,如果新产生的数字已经在hash table里了,说明它将一直循环下去,所以返回false。若产生的新数字是1,则返回true。3、代码 bool isHapp

2018-05-29 16:46:19 153

原创 204. Count Primes

1、题目描述输入n,返回小于n的数字中,有多少个质数。2、思路声明长度为n布尔型队列,初始化为false,即都是质数。首先可以知道,除了2以外,比2大的偶数都是合数。所以计数君sum = 1【表示统计了2】。再从3开始,对于每个质数,都把它的小于n的所有倍数的队列值设为true。每个质数也都要计数,最后返回计数君。3、代码 int coun

2018-05-29 16:45:51 103

原创 205. Isomorphic Strings

1、题目描述输入字符串s和t,判断他们是否是同构的。也就是说字母对应时一对一的。2、思路哈希表保存对应关系,集合表示那些元素已经作为值存在过了,或者说它是值域。3、代码 bool isIsomorphic(string s, string t) { mapm; sets1; int l = s.size(

2018-05-29 16:45:38 85

原创 207. Course Schedule

1、题目描述输入n表示有n们课,输入一组pair,表示在上first这门课之前必须要上second这门课,问有没有合法上课顺序。2、思路拓扑排序。数组d用来记录没门课有几门先修课。v表示连接表,用来表示vi是哪些课的先修课。声明集合s,用来保存当前可以上的课。把d = 0的课都存入s,一门门上s中的课,每上一门课,都要把它的v中的课的d减1,如果这些课d为0,则加入s。

2018-05-29 16:45:27 108

原创 210. Course Schedule II

1、题目描述输入课程数n,课程先修对,返回一种可能的上课顺序。不存在返回空数组。2、思路拓扑排序。3、代码 vector findOrder(int n, vector>& p) { vector ans; int m = p.size(); vectord(n,0); vector > v(

2018-05-29 16:45:16 112

原创 215. Kth Largest Element in an Array

1、题目描述给一个无序的数组,和k,返回第k大的元素数值。2、思路priority_queue3、代码 int findKthLargest(vector& nums, int k) { priority_queueq; for(int i=0;i<nums.size();i++) q.pus

2018-05-29 16:44:55 80

原创 718. Maximum Length of Repeated Subarray

1、题目描述输入两个数组a和b,找出最长的相同子数组(subarray)。2、思路动态规划。dp[i][j]:由a的第i位,b的第j位向前的子数组,最长的相同长度。ans = max(dp[i][j]).if(a[i-1]==b[j-1]) dp[i][j] = dp[i-1][j-1]+1;dp[i][j]=0, otherwise.要注意subarray

2018-05-29 16:44:31 122

原创 211. Add and Search Word - Data structure design

1、题目描述设计一个数据结构,可以储存单词,查找单词。找单词的时候“.”表示任意a-z的字母。2、思路map>,key为字符串的长度,value为加入的长度为key的字符串。add操作,将字符串插入到对应长度的map中。search操作,提取出对应长度的字符串数组。在这个数组中找到有没有匹配的,有true,没有false。3、代码class Wo

2018-05-29 16:44:11 305

原创 231. Power of Two

1、题目描述输入n,判断它是不是2的power。2、思路如果是2的次方,那么它的2进制表示应该是1,后面跟着很多0.用取出最后面1位1的方法,即n&(n-1),如果n&(n-1)等于0,则说明它是2的次方。否则不是。3、代码 bool isPowerOfTwo(int n) { if(n<=0) return false;

2018-05-29 16:43:58 79

原创 326. Power of Three

1、题目描述判断n是不是3的次方。2、思路3、代码class Solution {public: bool isPowerOfThree(int n) { if(n==1) return true; if(n<=0) return false; if(n%3!=0)

2018-05-22 09:58:55 71

原创 238. Product of Array Except Self

1、题目描述输入一个数组nums,返回一个数组,返回数组每个位置的值为nums数组中除去该位置的所有位置的乘积。2、思路不可以用除法,时间复杂度要O(n)。那么为了求ans[i]可以将其分为左右两部分。也就是一共要遍历数组两次,第一次让每个ans[i]都乘以i的左半部分的数字;第二次让每个ans[i]都乘以i右半部分的数字。3、代码 vecto

2018-05-22 09:58:36 180

原创 628. Maximum Product of Three Numbers

1、题目描述输入一个数组,数组中任取3个数,使得乘积最大,返回这个乘积。2、思路数组排序。最大的可能就两种。三个最大数求乘积,两个最小数和一个最大数的乘积。3、代码 int maximumProduct(vector& nums) { sort(nums.begin(),nums.end()); int n = nums.s

2018-05-22 09:58:18 68

原创 560. Subarray Sum Equals K

1、题目描述输入一个数组,一个k,返回有多少个subarray的和等于k。2、思路遍历数组,求遍历到的位置的前面所有数的和。将这个和记录到map中,map的value是这个和出现的次数。ans加上个map中sum-k的次数。3、代码 int subarraySum(vector& nums, int k) { mapm;

2018-05-22 09:57:56 106

原创 713. Subarray Product Less Than K

1、题目描述输入数组(positive)和一个k,求数组中有多少个subarray乘积是小于k的。2、思路碰到subarray问题,有几个方案:1、动归【求连续最大什么的】 2、two pointers【大概都是正的,要达到某一目标k,连续的区间最小】 3、map【有正有负,有目标k】这里用two pointers。遍历数组,求前i个数字的乘积,如果大于k,则移动l

2018-05-22 09:57:42 101

原创 152. Maximum Product Subarray

1、题目描述输入一个有正有负的数组,返回subarray乘积最大的值。2、思路动归。dp1保存着以每个元素结尾的正的最大乘积,没有正的就是0;dp2保存着以每个元素结尾的负的最小乘积,没有负的就是0;遍历数组,对于数字是正还是负,有不同的状态转移方程。遍历dp1,找出最大值,如果最大值是0,则可能是数组元素全是负的,再去dp2里找出最大值。3、代

2018-05-22 09:57:25 64

原创 241. Different Ways to Add Parentheses

1、题目描述输入一个字符串,是一个没有括号的表达式,返回给它加上括号后计算的所有答案的可能。2、思路分治。遍历字符串,每找到一个运算符号,就把字符串分成左右两个部分,分别算出两个部分的答案的组数,两个数组中的数字一一组合,用运算符进行对应运算,并将其放入到答案数组中。如果字符串中不包含任何运算符号,那说明它就单纯是个数字,把这个数字直接仿佛答案数组中即可。返回答

2018-05-22 09:57:03 149

原创 239. Sliding Window Maximum

1、题目描述输入一个数组和一个k,k是移动窗口大小,向右移动窗口,找出每个窗口找那个的最大值。2、思路前找出前k个数的最大值,记录为m,并将其放入答案数组。移动窗口,若最左移除的元素正好等于m,则需要重新求窗口的最大值,将其记录进m,并放入答案;反之,更新m为当前元素和m的最大值,将m放入答案数组。3、代码  vector maxSlidingWi

2018-05-22 09:56:34 93

原创 155. Min Stack

1、题目描述设计stack,能返回其中的最小值。2、思路用一个数字m记录最小值,每次push都更新最小值,当pop时,如果栈顶是最小值,则需要更新m;否则直接弹出栈顶元素。3、代码class MinStack {public: /** initialize your data structure here. */ stacks;

2018-05-22 09:56:13 82

原创 228. Summary Ranges

1、题目描述输入一个有序的无重复数组,返回将连续数字合并后的表达方式。2、思路一点点合并吧。3、代码 vector summaryRanges(vector& nums) { vectorans; if(nums.size()==0) return ans; string s=to_s

2018-05-22 09:55:53 72

原创 669. Trim a Binary Search Tree

1. DescriptionGiven a binary search tree and the interval [L, R].Change the BST so that all elements lies in [L, R].2. SolutionIf the root's value is in the range [L, R], then trim its lef

2018-01-16 03:27:56 102

原创 668. Kth Smallest Number in Multiplication Table

1. DescriptionGiven three elements m, n and k. Find the k-th smallest number in the m*n Multiplication Table.2. SolutionBinary Search.The left point is 1, and the right point is m*n.Whil

2018-01-16 03:20:07 137

原创 667. Beautiful Arrangement II

1. DescriptionGiven two integers n and k. Construct a list which contains 1 to n and the diffs between the neighbor elements has exactly k distinct integers.2. SolutionUse two points to trav

2018-01-16 02:48:11 133

原创 526. Beautiful Arrangement

1. DescriptionGiven an integer N, find the number of beautiful arrangements can construct by integers from 1 to N.We define a beautiful arrangement as an array that is constructed by these N n

2018-01-16 02:36:09 123

原创 665. Non-decreasing Array

1. DescriptionGiven an array, find whether it could become non-descreasing by modifying at most 1 element.2. SolutionTraverse the array, if there is an element bigger that the element next t

2018-01-16 01:06:06 183

原创 662. Maximum Width of Binary Tree

1. DescriptionFind the maximum width of a binary tree.2. SolutionBFS.In every level of the binary tree. Find the first unnull node and the last unnull node. The distance of the two nodes i

2018-01-16 00:29:29 106

原创 661. Image Smoother

1. DescriptionUpdate a 2D matrix with the average number of every gird's 8 surrounding cells and itself.2. SolutionMock.3. Code int x[8]={0,0,1,1,1,-1,-1,-1}; int y[8]={1,-1,1

2018-01-16 00:22:37 146

原创 658. Find K Closest Elements

1.DescriptionGiven a sorted array, and two integers k and x. Find the k closest elements to x in the array.2. SolutionUse binary search to find the closest element to x in the array,and fi

2018-01-16 00:17:55 134

原创 547. Friend Circles

1. DescriptionGiven a N*N matrix representing the relationship between every two students in the class.Find the total number of friend circles in the class.2. SolutionDFS / Union Finddfs

2018-01-15 23:32:13 68

空空如也

空空如也

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

TA关注的人

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