- 博客(27)
- 收藏
- 关注
转载 [LeetCode 063] Unique Paths II
Unique Paths II左上角那个点:有obstacle,则为0;没有obstacle,则为1。最上一排:有obstacle,则为0;没有obstacle,则为左边元素的值。最左边一排:有obstacle,则为0;没有obstacle,则为上边元素的值。其它点:有obstacle,则为0;没有obstacle,则为上边元素和左边元素值得和。Implementation...
2016-02-18 15:39:00
113
转载 [LeetCode 056] Merge Intervals
Merge IntervalsImplementation/** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Interval(int s, int...
2016-02-18 15:06:00
122
转载 [LeetCode 049] Group Anagrams
Group Anagramsset a HashMap.The key is string with letters in lexicographic order.The value is index of list containing anagrams in an ArrayList.Implementationpublic class Solution { ...
2016-02-18 11:34:00
117
转载 [LeetCode 033] Search in Rotated Sorted Array
Search in Rotated Sorted ArrayImplementationpublic class Solution { public int search(int[] nums, int target) { int start = 0; int end = nums.length - 1; while (sta...
2016-02-18 05:35:00
97
转载 [LeetCode 024] Swap Nodes in Pairs
Swap Nodes in PairsImplementation/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public ...
2016-02-18 04:08:00
81
转载 [LeetCode 016] 3Sum Closest
3Sum Closest遍历数组nums,依次取出一个数nums[i],在i之后的数列中找两个数最接近target。第一个数固定,后两个数按类似2Sum处理。当sum小于target时,需要将左边的数向右移动。当sum大于target时,需要将右边的数向左移动。设置midDif存储最小的difference,不断的跟新midDif。设置result存储三个数的和,当mi...
2016-02-17 14:49:00
86
转载 [LeetCode 015] 3Sum
3Sum去重转化成2sum问题时,已经选过的数值可以跳过不再选求解2sum过程中,碰到满足条件的两个数值,之后与它俩个相同的可以直接跳过剪枝尚未求过数列的前三个数之和大于0,则整个求解过程可以结束取出一个数后,该数和最后两个数之和小于0的话,则跳过该数继续向后选取Implementationpublic class Solution { public ...
2016-02-17 13:33:00
99
转载 [LeetCode 013] Roman to Integer
Roman to Integer从后往前遍历字符串当前一个字符代表的数值比后一个小时,用最终结果减去这个数值否则,用最终结果加上这个数值Implementationpublic class Solution { public int romanToInt(String s) { if (s == null || s.length() == 0) ...
2016-02-17 12:59:00
85
转载 [LeetCode 008] String to Integer (atoi)
String to Integer (atoi)Implementationpublic class Solution { public int myAtoi(String str) { if (str == null || str.length() == 0) { return 0; } int sig...
2016-02-17 08:13:00
85
转载 [LeetCode 007] Reverse Integer
Reverse Integer判断是否overflow的部分注意:根据x的正负检测。根据result不准确,我们需要检测x还剩一位数的时候result是否overflowImplementationpublic class Solution { public int reverse(int x) { int result = 0; ...
2016-02-17 07:32:00
85
转载 [LeetCode 088] Merge Sorted Array
Merge Sorted Array由于是按大小顺序把nums2中的数字添加到nums1中,所以当nums2中的数字全部添加到nums1中后,添加便完成Implementationpublic class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { i...
2016-02-16 15:56:00
92
转载 [LeetCode 003] Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters设置两个pointer,start指向子串的起始位置,end指向子串的终止位置。设置一个HashMap,保存字符和该字符出现的在字符串中的位置。当HashMap中已经存在某个字符,并且该字符在字符串中出现的位置在start之后,说明出现了重复字符。更新最大子串的长度max。更新Has...
2016-02-16 15:06:00
96
转载 [LeetCode 001] Two Sum
Two Sum在HashMap中存储numbers[i]尚需要都少数值达到target,以及下表iImplementationpublic class Solution { public int[] twoSum(int[] numbers, int target) { HashMap<Integer, Integer> map = new ...
2016-02-16 13:07:00
84
转载 [LeetCode 002] Add Two Numbers
Add Two Numbers使用carry记录进位情况,初始化为0如果l1节点存在,累加到carry中。如果l2节点存在,累加到carry中。新节点值carry % 10下一个节点进位carry最后carry为1,新建一个值为1的节点Implementation/** * Definition for singly-linked list. * public...
2016-02-16 13:04:00
60
转载 [LeetCode 266] Palindrome Permutation
Palindrome Permutationpalindrome中,字符均是成对出现的(除了当字符串长度是单数时的中间字母)创建一个set对象遍历字符串,当遇到一个字符的时候检测set中有没有该字符。如果有则将该字符从set中删除否则,将该字符添加到set中最后检测set中元素的个数个数小于等于1时,说明满足条件。Implementationpublic c...
2016-02-15 11:37:00
115
转载 [LeetCode 050] Pow(x, n)
Pow(x, n)n == 0时,结果为1n > 0时,结果是x ^ n否则,为上述结果的倒数if n是odd\({x}^{n} = {x}^{\frac{n}{2}}\times{x}^{\frac{n}{2}}\times{x} \)if n是even\({x}^{n} = {x}^{\frac{n}{2}}\times{x}^{\frac{n}{2}} \)I...
2016-02-15 09:16:00
94
转载 [LeetCode 013] Roman to Integer
从后往前遍历字符串当前一个字符代表的数值比后一个小时,用最终结果减去这个数值否则,用最终结果加上这个数值Implementationpublic class Solution { public int romanToInt(String s) { if (s == null || s.length() == 0) return ...
2016-02-15 08:45:00
58
转载 [LeetCode 172] Factorial Trailing Zeroes
2 * 5可以的到一个102的个数远比5的个数要多,所以我们需要计算出n!中有几个5从1开始算,每5个数会出现一个因子5 (n/5进行计算)从1开始算,每25个数会多出现一个因子5 (n/25进行计算)...Implementationpublic class Solution { public int trailingZeroes(int n) { ...
2016-02-15 08:12:00
174
转载 Quick Sort
数组中选择一个元素作为pivot点。根据pivot元素partition数组将数组中小于等于pivot的元素移动到pivot的左边。将数组中大于pivot的元素移动到pivot右边。在数组的合适位置插入pivot元素。pivot选取选取第一个元素选取最后一个元素(本文实现)随机选取元素partition实现取变量i记录小于或等于pivot元素的最后一个位...
2016-02-15 03:35:00
145
转载 [LeetCode 217] Contians Duplicate
Contains Duplicate IQuestion: 217. Contains DuplicateHashSetadd(): add an element into hashset, if it has the element return false, otherwise return true.Codepublic class Solution { ...
2016-02-03 08:57:00
70
转载 [LeetCode 061] Rotate List
QuestionGiven a list, rotate the list to the right by k places, where k is non-negative.ExampleGiven 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL....
2016-01-30 04:58:00
104
转载 [LeetCode 024] Swap Nodes in Pairs
QuestionGiven a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should u...
2016-01-25 16:21:00
84
转载 [LeetCode 319] Bulb Switcher
QuestionThere are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off o...
2016-01-25 14:46:00
167
转载 Java-protected的使用范围
protected的使用范围类NewObject中有protected修饰的方法或者属性,则:同一个包中:可在同一个包里的子类中实例化NewObject类获得对象,然后可用该对象访问protected修饰的方法或者属性,即.操作访问。可在同一个包里的非子类中实例化NewObject类获得对象,然后可用该对象访问protected修饰的方法或者属性。可在同一个包里的非子类...
2015-10-08 16:26:00
1346
转载 Swift-控制流
if语句不需要将正在检查的表达式放到括号内。if 1+1 == 2 { println("The math checks out")}所有if语句的主体都要放在大括号内。if(something) do_something();判断optional类型中是否有值,并赋值给另一个变量var conditionalString : String? = nil...
2015-08-10 17:22:00
86
转载 JavaScript入门
JavaScript的作用是为网页提供动态效果基础如何插入JS语法:<script type="text/javascript"> 代码</script>引用外部JS文件javascript代码写在.js文件中语法:<script src="js文件"></script>JS...
2015-02-02 05:50:00
144
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人