自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 8.12

k-生成树是指给定一个无向图,找到一个生成树,其中每个节点的度数不超过k。 首先要验证所找的生成树是否符合要求,只需要遍历每个点,检查其度数即可,时间复杂度为V+E,为多项式时间复杂度,因此k-生成树问题为NP问题。 这个问题可以由Rudrata环路问题归约。当k=2时,所要寻找的生成树其实就是一条经过所有顶点的路径,只不过相比Rudrata路径而言,这个生成树是没有回路的。那么Rudrata环

2017-07-10 13:06:56 188

原创 116. Populating Next Right Pointers in Each Node

题目描述Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If

2017-06-28 00:19:11 182

原创 147. Insertion Sort List

Sort a linked list using insertion sort. Subscribe to see which companies asked this question. 这是对链表进行排序,采取的方法是当遍历到前个节点的数值要大于后一个节点的数值的时候,使前一节点指向后一节点的后一节点,同时从root开始遍历到后一节点,这时候使后一节点指向前一节点,root的后一节点为上述的

2017-06-18 21:52:22 161

原创 75. Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0,

2017-06-11 20:35:58 162

原创 63. Unique Paths II

Follow up for “Unique Paths”: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid.

2017-06-03 09:41:00 211

原创 62. Unique Paths

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bot

2017-06-03 09:23:15 208

原创 124. Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path

2017-05-26 15:26:27 304

原创 105. Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Subscribe to see which companies asked this question. 根据树的前

2017-05-16 00:05:43 192

原创 103. Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tre

2017-05-15 21:55:09 166

原创 95. Unique Binary Search Trees II

Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1…n.For example, Given n = 3, your program should return all 5 unique BST’s shown below.1 3

2017-05-15 21:03:46 156

原创 99. Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a c

2017-05-15 13:51:11 152

原创 32. Longest Valid Parentheses

Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring. For “(()”, the longest valid parentheses substring is “()”, which h

2017-05-12 10:48:00 179

原创 46. Permutations

Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,

2017-05-09 22:00:17 83

原创 148. Sort List

Sort a linked list in O(n log n) time using constant space complexity. Subscribe to see which companies asked this question. 思路:采取归并排序的方法,将链表分为两个部分,分别将每一部分进行排序,再将两部分的结果进行整合,分开的方法为,设置两个指针,一个指针每次前进一步,另

2017-05-09 12:37:17 222

原创 57. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Exampl

2017-05-08 23:25:13 153

原创 241. Different Ways to Add Parentheses

Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *. Example 1 Input

2017-05-07 20:09:30 172

原创 349. Intersection of Two Arrays

问题描述:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].寻找两个数组的交集并且是只出现一次的,思路是利用JAVA中set的不可以包括两个重复元素的特性,第一次先将nums1放到一个一个set中,第二次再来寻找nums2中的元素,如果在set中,就将其放到最后的结果中,具体代码如下:public int[] intersection(in

2017-04-16 22:03:20 186

原创 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.Examples:Given “abcabcbb”, the answer is “abc”, which the length is 3.Given “bbbbb”, the answer is “b”, with the le

2017-04-04 10:56:20 297

原创 537. Complex Number Multiplication

这道题主要是实现复数的乘法(主要是实现题目的特殊要求) 利用string.split(“\+”)将字符串“+”分开得到复数的实部和虚部 (a1+a2*i)*(b1+b2*i)=(a1*b1-a2*b2)+(a1*b2+a2*b1)*i,但是当结果为负数时候,有特殊的形式“+-”,所以需数要进行判断正负,具体代码如下:public String complexNumberMultiply(Stri

2017-04-02 19:47:15 272

原创 345. Reverse Vowels of a String

本题要做的就是将一个单词中的元音字母的顺序颠倒,我的思路就是利用了stack的先入后出性,进行两遍遍历,第一遍遍历存入元音字母,第二遍遍历时候,是元音字母时候就stack冒出,不是的话就补充单词的下一个字母。下面是代码。public String reverseVowels(String s) { Stack<Character>set=new Stack<>();

2017-03-26 22:17:55 180

原创 69. Sqrt(x)

这里采用二分查找的办法计算一个数的二次根式。每次都采用折半查找看是否满足mid*mid==x,如果mid*mid 小于x,往后一半进行查找,相反向前一半进行查找,最后上代码:public int sqrt(int x) { int low=0; int high=x; while (low<=high) { int mid=(l

2017-03-19 22:21:36 240

原创 18. 4Sum

这道题和3Sum那道题十分类似,只是把3sum 中的0换成了target-num【i】,直接上代码。public List<List<Integer>> fourSum(int[] num, int target) { List<List<Integer>> array=new ArrayList(); if(num == null||num.length < 4)

2017-03-09 00:13:53 170

原创 16. 3Sum Closest

这道题和之前的3sum差不多,设置左右标兵left=i+1,right=len-1,这是选择和target最接近的sum,使用 Math.abs来计算,同时设定初始值error,循环遍历,直至找到最小的sum,代码如下:public static int threeSumClosest(int[] num, int target) { if (num==null||num.lengt

2017-03-08 23:42:50 164

原创 3Sum

先将数组进行排列,从i=0开始循环,每个遇见相等的数向前进一位,同时设立两个观察哨兵(left、right),当每一次有a+b+c=0时候添加一个list,同时,也是left和right遇见相等的数分别向前和向后进一位。具体代码如下:public static List<List<Integer>> threeSum(int[] nums) { List<List<Integer>>

2017-03-08 22:28:23 141

原创 43. Multiply Strings

这是一套实现大数的乘法,把大数的加法也加在里面了,首先来看! 这里十分清楚乘法的数学分析,在这里不多讲解,得到一个int【num1.length()+num2.length()】的数组; 然后进行加法运算,设置一个carrybit(进位数)=数组的数%10; 最后从第一个不为0的数放入string中。 最后考虑“0”的情况,string等于“0”应该用string.equal(“0”)表

2017-03-08 15:02:49 173

原创 38. Count and Say

解题思路:设置一个count来计数,ch为上次记录的字符,一共有n次操作,每次操作的输入字符为前一次产生的string,因此这里面使用到了一个二重for循环,代码如下:public static String countAndSay(int n) { String string="1"; for (int i = 0; i < n; i++) {

2017-03-08 13:57:48 147

原创 50. Pow(x, n)

这道题目是求x的n次方。 可以选择用n个x相乘,复杂度为O(n)(负数的-n个相乘再求倒数) 另外就是采取分治来算 分解为pow(x,n/2)*pow(x,n/2) 最后的复杂度应该为logn+logn即为O(logn) 代码如下:public double myPow(double x, int n) { if (n==0) { return 1

2017-03-06 23:55:27 147

原创 394. Decode String

这道题目要实现的是字符串按照输入的格式进行解码,有点类似RLE算法,但是更复杂一点,因为这里有嵌套的解码,主要做法如下: 1、利用stack的先入后出性,设置countstack和resstack分别存储数字和字符; 2、当遇见‘【’时候,先把res放入resstack中,再reset为“”,来存储下次的字符 3、当遇见‘】’时候,先把resstack中pop,再加上下次要重复的字符(pop

2017-02-27 20:20:29 200

原创 459. Repeated Substring Pattern

这道题目实际上是要实现判断字符串是否是由重复字符串单元组成,主要的做法有: 重复单元的长度要少于字符长度的一半,并且是字符串长度的一个约数。因此可以遍历一般长度到1的子字符串并且进行重复,最终判断和原字符串是否相等。最后附上代码:public static boolean repeatedSubstringPattern(String s) { int l=s.length();

2017-02-22 21:36:08 208

原创 520.Dectect Capital

第一次练手,就像上次上课时候林老师说的那样,要选择一些难度稍大,自己需要思考的题目,自己能力才能提升。 这道题目要实现的目的是判断是否满足下面条件 1)全部为大写 2)全部为小写 3)第一个字母为大写,其余字母为小写 我的做法是:分别将word中的大写字母和小写字母个数统计出来: 1)大写字母个数或者小写字母个数等于word长度时候,为true; 2)第一字母为大写并且大写字母个数为1

2017-02-22 20:49:39 392

空空如也

空空如也

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

TA关注的人

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