leetcode
cyj083
这个作者很懒,什么都没留下…
展开
-
455. Assign Cookies
tag为贪心的第一题。https://leetcode.com/problems/assign-cookies/#/description思路:先给两个数组排个序,然后比较cookie数组比need数组大的数字有多少。public class Solution { public int findContentChildren(int[] g, int[] s) {原创 2017-04-07 11:22:39 · 124 阅读 · 0 评论 -
【回头再看看】141. Linked List Cycle
https://leetcode.com/problems/linked-list-cycle/#/description这个题目要判断链表里是否有回路。总的来说用两个指针,slow走的慢,每次只走一步,fast走得快,每次走两步,如果有环,slow就会和fast碰到。。参考一下这个吧,以后再认真看看……………………http://blog.sina.com.cn/s/blog_6f原创 2017-04-05 11:06:27 · 132 阅读 · 0 评论 -
【还没做,待定】198. House Robber
https://leetcode.com/problems/house-robber/#/description原创 2017-04-05 10:36:44 · 141 阅读 · 0 评论 -
326. Power of Three
https://leetcode.com/problems/power-of-three/#/description要求不用循环和递归判断一个数字是否是3的次方。思路:3^19=1162261467,用1162261467 % 这个数字,如果没有余数,说明是3的次方~public class Solution { public boolean isPowerOfThree(i原创 2017-04-05 10:29:59 · 193 阅读 · 0 评论 -
202. Happy Number
https://leetcode.com/problems/happy-number/#/description问题是判断一个输入的数字是否是happy number,规则看题目吧,翻译无能。。思路就是将一个数字拆分成各个位数上的数字,然后进行求和,判断。用到了map,最近只会用map。。因为如果是false,要判断是否是出现过的数字,不然会死循环Map map = new HashM原创 2017-04-05 10:24:03 · 175 阅读 · 0 评论 -
268. Missing Number
https://leetcode.com/problems/missing-number/#/description找到从0开始的等差数列中缺失的一项。思路:求和,然后用理论上应该得到的数列和减去实际上得到的数组和,就是缺失的数字。public class Solution { public int missingNumber(int[] nums) { i原创 2017-04-05 10:03:26 · 136 阅读 · 0 评论 -
350. Intersection of Two Arrays II
https://leetcode.com/problems/intersection-of-two-arrays-ii/#/description很简单的问题,求两个集合的交集。写的有点蠢了。。先a了再说初步思路是,将两个集合各自遍历一遍,映射一下元素,然后将映射后的结果进行对比。遇到的一个问题是,在取map元素数据的时候,要对map里的数据进行比较,用==不能比较,只能用equals原创 2017-04-04 17:56:59 · 155 阅读 · 0 评论 -
238. Product of Array Except Self
https://leetcode.com/problems/product-of-array-except-self/#/description计算数组中除本身以外的所有数字之积要求:不允许用除法;O(n)思路:将乘法拆开,分为该数字的前一部分和后一部分。那么开两个数组,forword表示从前往后的乘积,backword表示从后往前的乘积。最后将两个相乘就得到结果。public原创 2017-04-04 12:02:56 · 130 阅读 · 0 评论 -
387. First Unique Character in a String
https://leetcode.com/problems/first-unique-character-in-a-string/#/description寻找字符串中第一个只出现一次的字符的位置思路:先计算每个字符出现的次数,获得第一个只出现一次的字符,遍历字符串,得到该位置。public class Solution { public int firstUniqChar(原创 2017-04-04 11:25:43 · 215 阅读 · 0 评论 -
283. Move Zeroes
将数组中所有的零元素移到数组末尾。思路:寻找 0元素,移动到末尾。public class Solution { public void moveZeroes(int[] nums) { int count = 0; int i,j; for(i = 0;i if(nums[i] == 0){原创 2017-04-04 10:56:36 · 144 阅读 · 0 评论 -
136. Single Number
https://leetcode.com/problems/single-number/#/description所有数字都出现两次,仅有一个数字出现一次,找出这个数字。要求:线性时间;不占用其他内存。思路:xora xor a = 0a xor 0 = apublic class Solution { public int singleNumber(int[原创 2017-04-04 10:30:06 · 112 阅读 · 0 评论 -
371. Sum of Two Integers
https://leetcode.com/problems/sum-of-two-integers/#/description两个整数求和,不能用到加号和减号。思路:比特运算,用and表示进位,用xor表示不进位的结果,累加起来。public class Solution { public int getSum(int a, int b) { int c原创 2017-04-04 10:28:18 · 144 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
计算字符串中的最长不重复子串用到map,一次遍历if (s.length()==0) return 0; HashMap map = new HashMap(); int max=0; for (int i=0, j=0; i if (map.containsKey(s.charAt(i))){原创 2017-03-24 15:44:44 · 120 阅读 · 0 评论 -
leetcode第一题
https://leetcode.com/problems/two-sum/#/descriptiono(n)的方法,用到了map。public class Solution { public int[] twoSum(int[] nums, int target) { int[] result = new int[2];//哈希原创 2017-03-23 11:20:30 · 149 阅读 · 0 评论 -
55. Jump Game
https://leetcode.com/problems/jump-game/#/description这道题是说,给定一个数组,数组中每个元素表示当前位置最大能前进的数,最开始在第一个元素所在的位置,判断是否能到达终点。题目的tag是贪心算法,一开始我认为从前往后看,每一步走争取最大步数,能走多远就是多远。public class Solution { public bo原创 2017-04-07 16:33:21 · 151 阅读 · 0 评论 -
122. Best Time to Buy and Sell Stock II
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/#/description这个题允许股票进行多次买卖,目标是计算总的最大获益。基本思路是用贪心算法,只要能挣到钱(下一个数字比上一个数字大),就进行买卖。public class Solution { public int maxProfit(in原创 2017-04-07 16:09:23 · 105 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/#/description股票买入和卖出的最大差价,只允许交易一次,实际上就是计算数组中靠后的值和靠前的值最大差。public class Solution { public int maxProfit(int[] prices) { in原创 2017-04-07 15:52:46 · 134 阅读 · 0 评论 -
125. Valid Palindrome
https://leetcode.com/problems/valid-palindrome/#/description判断一个字符串是否是回文(正着和反着看一样)。思路,硬着头皮判断呗。不过要注意,标点符号不算在内,但是数字算在内。。public class Solution { public boolean isPalindrome(String s) {原创 2017-04-05 11:21:46 · 188 阅读 · 0 评论