算法/oj
文章平均质量分 63
lscHacker
这个作者很懒,什么都没留下…
展开
-
LeetCode Insertion Sort List
题意:实现用链表写的插入排序解法:注意每次进行“选择”那一步操作的时候,要先把当前这个点给独立出来,就是把前后的next都给割断class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; }}public class Solution { public ListN原创 2015-01-27 16:50:16 · 245 阅读 · 0 评论 -
LeetCode Max Points on a Line
题意:给定一些二维坐标,求多少个点在同一条直线上解法:明显的极角排序,卡了几下,发现这道题目比uva 270多了几种情况。1、先说这道题目的极角排序:O(n)遍历一遍,枚举极角排序的中心点。然后构造向量,进行极角排序。排序结束,根据差乘判断(假设只有一个中心点,即任意两个点坐标都不同),有多少个点在一条直线上。即为返回值ret。2、题目可能有重复点。这个就需要我们把重复的点找出来。极角原创 2015-01-21 19:21:41 · 366 阅读 · 0 评论 -
LeetCode Majority Element, Excel Sheet Column Title
leetCode Excel Sheet Column Title:题意,给一个数字,保证大于0,然后给出这个数字对应的在Excel上面的列数原创 2014-12-23 09:03:10 · 685 阅读 · 0 评论 -
LeetCode Fraction to Recurring Decimal
lz才发现原来LeetCode的题目是会更新的,不错啊~不过中间有几个大了标记的题目,点进去之后没有办法阅读题目,略蛋碎(ms要买什么书?,大家有好的解决方法求教)回到这道题目,给出两个数字,分母和分子,求这个结果。结果要求:循环的数字要将循环节用括号包起来。先说几点:1、能够被整出的,要求分子分母divide greatest common dividor,分母的因子只有2和5,原创 2014-12-23 10:22:53 · 840 阅读 · 0 评论 -
LeetCode Maximum Product Subarray
题意:给定一个int,求解字串的最大product解法:dp咯因为只是一个求解字串的product,只要考虑正负和零,当前的max[i]只和三个有关系,A[i],A[i]*max[i-1]以及A[i]*min[i-1],那么这样我们不妨同时维护两个dp数组,max和min,分别表示以A[i]结尾的字串中最大值和最小值。状态转移方程如下max[i] = max( A[i], A[i]*m原创 2015-02-02 15:54:22 · 271 阅读 · 0 评论 -
LeetCode LRU Cache
题意:模拟实现LRU缓存,可以支持的功能有get(key)和set(key,value)解法:1、先来一个朴素的,自己模拟的双端队列,可惜TLE了,不过大家可以打打玩玩public class LRUCache { int capacity; node head = null, tail = null; public LRUCache(int capacity) { t原创 2015-01-30 10:25:36 · 450 阅读 · 0 评论 -
Leetcode Largest Number
题意:给一串数字,要求将这些数字组成一个数值最大的数字解法:懒一点的写法,直接将两者暴力组合之后判断大小,根据这个comparator写排序import java.util.Arrays;public class Solution { public String largestNumber(int[] num) { String ret = ""; NewString s[]原创 2015-01-15 10:21:29 · 438 阅读 · 0 评论 -
LeetCode Binary Tree Preorder Traversal & Binary Tree Postorder Traversal
Binary Tree Preorder Traversal题意:给一个树,求前序序列解法:……直接上代码吧public class Solution { List list; public List preorderTraversal(TreeNode root) { list = new LinkedList(); dfs(root); return list;原创 2015-02-04 17:27:59 · 289 阅读 · 0 评论 -
LeetCode Reorder List
题意:给定一个序列,将他进行重排序,下表如下:a0, an, a1, an-1, ...解法:用了一个很朴素的想法,就是将a0, a1, a2...和an, an-1, an-2...,最后合并public class Solution { public void reorderList(ListNode head) { ListNode cnt = head; int N =原创 2015-02-06 12:10:45 · 296 阅读 · 0 评论 -
LeetCode Linked List Cycle & Linked List Cycle II
Linked List Cycle题意:给定一个链表,求这个链表有无循环解法一:按照题目要求,不用额外空间做,我的想法是如果当前链表是a0->a1-> a2-> a3,那么不妨进行一次遍历,当前为temp,前一个为pre,后一个为next,然后将temp的next指针指向pre,这样一直遍历,如果最后跑到了head,就表示有环,否则无环。有一个bug是head是null的情况需要特判。原创 2015-02-06 12:45:44 · 436 阅读 · 0 评论 -
LeetCode Binary Search Tree Iterator
给了一个BST的根节点,要求写一个遍历类,将这棵树从小到大遍历一遍。我的做法是先把BST遍历一遍, 然后放到数组里,这样看起来似乎题目很简单。public class BSTIterator { int len = 0, index = 0; int[] arr = new int[1000000]; public BSTIterator(TreeNode root) {原创 2015-01-01 08:40:46 · 1246 阅读 · 0 评论 -
LeetCode Factorial Trailing Zeroes, Excel Sheet Column Number
Factorial Trailing Zeroes题意:给一个数n,问n!的末尾有几个0第一反应是0只有2、5相遇能出一个0,所以n有几个5的倍数似乎就是答案了。(n!有k个5,肯定有k个2,能凑出相对应的0)但是如果是像25的怎么办?他能凑出100,两个0,;所以前面单单5的倍数不够,还应该是5^i,将这个i加起来。代码如下:public class Solution {原创 2015-01-01 11:31:44 · 860 阅读 · 0 评论 -
LeetCode Word Break & Word Break II
Word Break题意:给定一个字符串str,和一个集合dict,问能否用集合当中的字符串(可以使用多次)拼接处str解法:动态规划dp[i]表示当前i前面的下标能否被拼接而成刚刚开始都会想到一个比较朴素的想法是for (int i=1; iwhile (ite.hasNext())……但是后来发现貌似不用遍历dict,而是枚举任意两个的起始点和终点,然后判断当前这个原创 2015-02-09 12:33:17 · 398 阅读 · 0 评论 -
LeetCode Compare Version Numbers, Maximum Gap, Find Peak Element, Intersection of Two Linked Lists
对的原创 2014-12-26 00:23:53 · 670 阅读 · 0 评论 -
LeetCode Sort List
题意:给定一个list的节点,对它进行排序解法:归并排序咯~直接上码class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; }}public class Solution { public ListNode sortList(ListNode head) {原创 2015-01-22 09:18:36 · 317 阅读 · 0 评论 -
LeetCode Min Stack
Min Stack题意:给定一个类和几个描述好的操作,实现它。是一个能够输出最小值的类。这道题目有点回到了ACM的感觉。明确以下几点:1、stack是一个和时间相关的2、某一个时刻的stack从top到i的最小值给划分的话,发现随着i从栈顶到栈底是依次递增的(有着单调性)认识到这么一个单调性的话就很好做了import java.util.Stack;class MinS原创 2015-01-02 15:40:42 · 353 阅读 · 0 评论 -
LeetCode Gas Station
题意:给定一个[0,n)的gas station,每个站可以加gas[i],然后才从每个站到下一个站需要cost[i]的gas cost,问从哪个节点可以绕转一圈解法:首先既然是可以转圈的,那么唯一不成立的情况就是gas和比cost和小,否则都能够“转”出结果来。可能略微抽象,大家仔细想想。证明我也不会囧……然后就是很明显的贪心了,到了一个gas station就把gas[i]加满原创 2015-03-19 10:55:45 · 257 阅读 · 0 评论 -
LeetCode Clone Graph
After I realizing that if I take an interview, I will need to discuss the problem in English, I begin to describe all my answers in English. And I hope this will also improve my written ability.Desc原创 2015-03-20 18:23:05 · 230 阅读 · 0 评论 -
LeetCode Single Number, Single Number II
Single Number题意:给定一个数组,出了一个int出现一次,其余大多int都出现了两次,求这个只出现一次的int解法:要求O(n),同时空间要求位常数。好像以前做过类似的,可以考虑异或操作,因为两个一样的数字异或操作的结果是0,那么遍历一遍,累计异或的结果和哪一个A[i]异或结果为0就是仅仅出现一次的数字。public class Solution { public原创 2015-03-09 15:42:57 · 370 阅读 · 0 评论 -
LeetCode Copy List with Random Pointer
题意:import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;// DeFfinition for singly-linked list with a random pointer.class RandomListNode { int label; RandomLis原创 2015-03-09 15:21:28 · 308 阅读 · 0 评论 -
LeetCode Sum Root Leaf Numbers
Description:Given a tree, each node with a value, and we need to calculate the sum of all the number from leaf to root.Solution:Just a dfs is enough./** * Definition for binary tree publi原创 2015-03-23 18:01:12 · 340 阅读 · 0 评论 -
LeetCode Surrounded Regions
Description:Solution:import java.util.LinkedList;public class Solution { int dir[][] = { { 1, -1, 0, 0 }, { 0, 0, 1, -1 } }; int n, m; public void solve(char[][] board) { n = board原创 2015-03-23 09:16:43 · 278 阅读 · 0 评论 -
LeetCode Palindrome Partitioning, Palindrome Partitioning II
Palindrome PartitioningDescription:Given a string, an we need to find out all the palindrome partition, and store them in a List.Solution:I think we can split this problem into原创 2015-03-22 15:23:36 · 361 阅读 · 0 评论 -
LeetCode Candy
题意:给定一个数组,rating[],然后要求是每个小伙伴给一些糖果,要求是,如果他比左边或者右边的小伙伴rating值大的,给他的candy就要多(潜意思就是如果少或者相等的rating就可以给一个糖果)。问最少一共要准备多少个candy。解法:想清楚这个题目的问题关键就行,下面是考编码能力了。就是相当于将一个rating数组给线段化了,是很多个v还是^这样的,‘或者干脆是单调的原创 2015-03-18 15:14:20 · 437 阅读 · 0 评论 -
LeetCode Longest Consecutive Sequence
Description:Given an unsorted array, and we need to find out the longest consecutive sequence. And the the complexity should be O(n).Solution:It is easy to solve this problem after sorting this原创 2015-03-24 15:00:32 · 212 阅读 · 0 评论 -
LeetCode 3Sum Closest
Description: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input原创 2015-06-18 23:18:00 · 245 阅读 · 0 评论 -
LeetCode Longest Palindromic Substring
Description:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.So原创 2015-05-27 20:23:08 · 333 阅读 · 0 评论 -
LeetCode Reverse Integer
Description:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questio原创 2015-06-01 09:20:37 · 222 阅读 · 0 评论 -
LeetCode String to Integer (atoi)
Description:Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the原创 2015-06-01 09:31:07 · 253 阅读 · 0 评论 -
LeetCode Palindrome Number
Description: Determine whether an integer is a palindrome. Do this without extra space.Solution:Since it is required that no extra space is used, so we only have to take out the corresponding原创 2015-06-01 09:36:40 · 280 阅读 · 0 评论 -
LeetCode Letter Combinations of a Phone Number
Description:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.原创 2015-06-20 11:49:37 · 293 阅读 · 0 评论 -
LeetCode Remove Nth Node From End of List
Description:Given a linked list, remove the nth node from the end of list and return its head.Solution:First get the length of the whole list, then we need to take out the (length-n)th one.原创 2015-06-20 17:54:43 · 347 阅读 · 0 评论 -
LeetCode Container With Most Water
Description:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai)原创 2015-06-01 15:12:13 · 262 阅读 · 0 评论 -
LeetCode Longest Common Prefix
Description:Write a function to find the longest common prefix string amongst an array of strings.Solution:This problem can be solved by comparing all other strings with the first one, compari原创 2015-06-01 15:55:05 · 236 阅读 · 0 评论 -
LeetCode Merge Two Sorted Lists
Description:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.Solution:Remember the very important原创 2015-06-20 18:18:00 · 286 阅读 · 0 评论 -
LeetCode 4Sum
Description:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.原创 2015-06-21 18:12:51 · 261 阅读 · 0 评论 -
LeetCode Valid Parentheses
Description:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()原创 2015-06-19 19:58:36 · 262 阅读 · 0 评论 -
LeetCode Implement strStr()
Description:Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Solution:Only one needle need to be checked. Or else we need to use an Aho-原创 2015-06-24 15:37:45 · 239 阅读 · 0 评论 -
LeetCode Generate Parentheses
Description:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.Solution:A dfs can solve this problem,We can imagine this problem as settin原创 2015-06-24 14:21:02 · 286 阅读 · 0 评论 -
LeetCode Swap Nodes in Pairs
Description:Given a linked list, swap every two adjacent nodes and return its head.Solution:We can get two list nodes each time, then just simply switch them. Remember that there are three 'ne原创 2015-06-24 14:55:32 · 280 阅读 · 0 评论