自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 376. Wiggle Subsequence

本题是找出一个序列中的最长的摆动子序列For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not w

2017-11-30 10:53:03 110

原创 Add to List 371. Sum of Two Integers

不利用加减法实现两个数相加和题目一样,我用的是一个位运算,分为两个步骤: 1、输入 a,b 2、按照位把ab相加,不考虑进位,结果是 a xor b,即1+1 =0 0+0 = 0 1+0=1,进位的请看下面 3、计算ab的进位的话,只有二者同为1才进位,因此进位可以标示为 (a and b) 将 sum赋值给a,并将进位值相加,知道b为零时一定不产生进位,返回值sum。代

2017-11-27 11:04:55 100

原创 leetcode 368. Largest Divisible Subset

result记录当前列表的长度,pre记录达到最长的上一个数字的索引,最后将数值一个一个添加至list1的首端。返回list1.class Solution { public List largestDivisibleSubset(int[] nums) { if(nums.length==0){ return new Arra

2017-11-23 15:35:45 109

原创 LeetCode 367. Valid Perfect Square

本题不用内置方法求解平方,因为平方数可以分解为1+3+5+7·····利用这个性质即可。class Solution { public boolean isPerfectSquare(int num) { int tag = 1; while(num>=tag){ num = num-tag; tag = tag+2;

2017-11-21 11:22:02 104

原创 leetcode 365. Water and Jug Problem

水罐问题,输入x,y,z 看是否可以量出z容量的水本题为数学问题,找出x,y的最大公约数,如果可以被z整除,那么就可以量出,代码如下:class Solution { public boolean canMeasureWater(int x, int y, int z) { if(x+y<z) return false;

2017-11-21 11:05:10 90

原创 leetcode 357. Count Numbers with Unique Digits

找出所有不同的数字Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n.Example:Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤

2017-11-17 12:14:35 135

原创 LeetCode 350. Intersection of Two Arrays II

本题跟之前有所变化,要求找出两个数组中所有重复数字,包括重复的。我们用map记录值与次数,当次数小于1时,不记录,其余记录,代码如下:class Solution { public int[] intersect(int[] nums1, int[] nums2) { Map map = new HashMap(); List list = new A

2017-11-17 11:41:08 92

原创 leetcode 349. Intersection of Two Arrays

返回两个数组相交的数字利用set集合解决  代码如下现将一个数组写入set1,对于第二个数组,若其中的数字出现在1中,就将其写入2.最后将2中的数字写入a【】,返回即可class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set set1 = new Ha

2017-11-14 16:03:05 99

原创 leetcode 347. Top K Frequent Elements

本题要求返回前k个频率的数字For example,Given [1,1,1,2,2,3] and k = 2, return [1,2].我们的思路是构建一个map集合,将所有数字的出现次数统计并利用map的比较器进行排序,最后新建一个list1集合将结果返回;代码如下:class Solution { public List topKFrequent(int[]

2017-11-14 15:21:01 109

原创 LeetCode 345. Reverse Vowels of a String

翻转字符串中的元音字母class Solution { public String reverseVowels(String s) { if(s==null||s.length()==0) return s; String sss = "aeiouAEIOU"; char[] ss =s.toCharArray();

2017-11-14 11:34:29 101

原创 LeetCode 344. Reverse String

实现翻转字符串class Solution { public String reverseString(String s) { char[] ss =s.toCharArray(); int i = 0; int j = s.length()-1; while(i<j){ char tag = ss[i]

2017-11-14 10:56:00 86

原创 leetcode Integer Break 整数拆分

本题两种思路一种直接用动态规划求解维护一个tag数组,tag【1】=1,之后tag[i] =Math.max(tag[i],j*Math.max(tag[i-j], i-j));即可得到结果 代码如下:class Solution { public int integerBreak(int n) { int tag[] = new int[n+1];

2017-11-10 10:57:53 163

原创 leetcode 342. Power of Fou

本题找出4的幂方法是首先与num-1相与,得出为0说明为2的幂,再与0x55555555相与,找出只有在偶数位为1的数,即为所求代码如下:class Solution { public boolean isPowerOfFour(int num) { return num > 0 && (num&(num-1)) == 0 && (num & 0x5555555

2017-11-09 17:16:53 98

原创 leetcode 338. Counting Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num =

2017-11-09 15:46:03 106

原创 337. House Robber III

本题The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. Afte

2017-11-08 17:01:38 207

原创 LeetCode 334. Increasing Triplet Subsequence

本题Return true if there exists i, j, k such that arr[i] arr[j] arr[k] given 0 ≤ i j k ≤ n-1 else return false.判断是否有三个数满足如上条件,即数值大小顺序和下标大小顺序一致。我们维护两个常量a,ba来存储当前最小的数字,b存储最接近a的比a大的数字首先

2017-11-08 11:14:16 106

原创 LeetCode 331 Verify Preorder Serialization of a Binary Tree

本题是给一个字符串,让你判断其是否是一个二叉树序列,其中空节点用#表示。例如 _9_ / \ 3 2 / \ / \ 4 1 # 6/ \ / \ / \# # # # # #For example, the above binary tree can be serialized to the string "9,3,4

2017-11-08 10:22:58 182

原创 LeetCode 328. Odd Even Linked List

本题主要解决将一个链表中的偶数节点全放在奇数节点之后,并返回头结点。本题比较简单,新建一个odd节点,指向下一个奇数节点,even节点指向偶数节点,用节点evenhead记录偶数节点的头,当所有循环完成时,将最后一个奇数节点的next指向evenhead。代码如下:class Solution { public ListNode oddEvenList(ListNode hea

2017-11-07 17:07:25 88

原创 326. Power of Three

主要是判断一个数是不是3的次幂因为3这个数字比较特殊,是个质数,所以我们用int取值范围中最大的数值3^19去除以n,如果能整除,那么就是3的次幂。代码如下class Solution { public boolean isPowerOfThree(int n) { return(n>0&&Math.pow(3,19)%n==0); }}

2017-11-06 15:18:34 85

原创 324 wiggle sort

本题目要求是将给定的数组变为  来回交替的顺序。例如 123456   变为142536本题思路是将原数组排序后,分为两个数组,小数组所有的值都不大于大数组的值。然后为了确保取出的值是来回摆动的,我们依次从小数组末尾和大数组末尾取值。class Solution { public void wiggleSort(int[] nums) { int []

2017-11-06 10:44:39 108

原创 LeetCode 322 Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money

2017-11-02 11:47:36 75

原创 LeetCode 319 Bulb Switcher

题目   灯泡初始状态为off每一轮将是n的倍数的开关调转状态。求最后灯泡是on的个数。思路:由于 1 = 1*1                    2 = 2*1  1*2  3 = 3*1 1*3每个数每次分解都会改变状态,但由于分解的对称性,除了平方数,其余的都会回到初始状态,因为都翻转了偶数倍;所以结果就是返回n的平方根的整数位;代码很简单 如下:

2017-11-01 16:54:24 118

原创 LeetCode 318 Maximum Product of Word Lengths

本题是找出数组中最长的所有字母都不一样的两个单词,返回他们的长度乘积,不存在返回零。思路:新建一个数组,将其字母的值转化成相应位置二进制数值为1,并将他们相与,结果为0,就返回他们长度的乘机,时间复杂度为0(n2),空间复杂度为0(n)。代码如下:class Solution { public int maxProduct(String[] words) { i

2017-11-01 16:15:48 88

原创 leetcode 313 Super ugly number 解题报告

本题和之前的丑数不同,给出了不同质数的primes数组。本题解题思路为新建一个tag数组,ugly表示从小到大的丑数序列,tag表示此质数将和第几个丑数相乘,因为之前的丑数已经乘过并且写入了丑数序列中。先依次将数相乘得出第i个丑数的值,再将小于此丑数的tag下标加一,最后返回ugly【n-1】就是第n个丑数。代码如下:class Solution { public int nt

2017-10-24 19:55:24 170

原创 LeetCode 310 minimum height trees

本题采用的思路是不断地逐层的删除叶子节点,从而最后只剩下子节点的方法。本题的结果最后只能是有一个最小根节点或者两个,不可能有三个的情况存在。首先,我们将所有的节点的入度统计到表nums中,后用队列q将入度为一的节点统计至q中,然后对count进行判断,如果此时所有的点都入度为一,则此时q中的所有节点,有可能是一个或两个节点,都是最小根节点,写入list中。如果不是n,将所有的包含q中的节点的与

2017-10-24 14:47:59 139

原创 leetcode 309 best time to sell or buy stock

本题采用动态规划首先,我们知道,股票可分为两个状态,卖出状态和买入状态未持股状态包含在第i天的时候,股票处于sell状态或者处于冷却期,即cool.处于冷却期,利润为sell【i-1】,处于sell状态,利润为buy【i-1】+prices【i】买入状态包含在第i天,股票处于buy状态或者前一天处于买入状态,若今天买入,由于前一天不可卖出,则利润为sell【i-2】-prices【i】

2017-10-24 09:55:15 131

原创 LeetCode 307 Range Sum Query - Mutable(树状数组)

本题利用树状数组,若用其他方式,则会发生超时错误,树状数组的时间复杂度为o(logn)    树状数组的结构及实现见下网站http://www.cnblogs.com/zhangshu/archive/2011/08/16/2141396.html   本代码中的init,初始化了一个c数组,即树状数组,其实现为每次都把元素写入到其应有的位置,相当于初始化的时候,每次都将当前num

2017-10-23 15:34:15 127

原创 LeetCode 306 addtive number

addtive numer 主要是判断一个数是不是可加的,譬如1123,13417等都是爱的addtive numer首先,本算法可以采用递归方法或者迭代方法,判断函数为isTrue函数。首先选择数a,可以从第0位一直到一半的位数就足够   a      i位在选择数b,b   j位l为字符串总长度选完数a,b后,留下的位数l-j要大于i和j-i之中的最大值,道理简单易懂

2017-10-23 11:12:20 239

空空如也

空空如也

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

TA关注的人

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