- 博客(44)
- 资源 (1)
- 收藏
- 关注
原创 Leetcode 22. Generate Parentheses
题目:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()",
2017-04-24 15:22:38 323
原创 Leetcode 19. Remove Nth Node From End of List
题目:Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the e
2017-04-24 10:59:44 278
原创 Leetcode 11. Container With Most Water
题目: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) and (i,
2017-04-24 09:51:42 278
原创 Leetcode 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 rea
2017-03-26 11:09:45 289
原创 Leetcode 15. 3Sum
题目:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not contai
2017-03-25 16:03:23 267
原创 Leetcode 8. String to Integer (atoi)
题目:Implement atoi to convert a string to an integer.思路:编写相应规则即可.import ctypesclass Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int
2017-03-24 10:36:59 271
原创 LeetCode 79. Word Search
题目:Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or ver
2017-03-23 18:51:08 348
原创 leetcode 84. Largest Rectangle in Histogram
题目:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where
2017-03-01 10:28:18 277
原创 leetcode 91. Decode Ways
题目:A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the t
2017-02-28 17:28:44 325
原创 94. Binary Tree Inorder Traversal
题目:中序遍历二叉树思路:递归# Definition for a binary tree node.class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = Noneclass S
2017-02-27 11:23:40 215
原创 98. Validate Binary Search Tree
题目:判断是不是搜索二叉树思路:1.中序遍历的序列是严格递增的。2.节点的左子树上的值均小于该节点的值,右子树的值均大于该节点的值。1.class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = No
2017-02-27 10:50:30 225
原创 leetcode 88. Merge Sorted Array
题目:Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.思路:二分查找+插入class Solution(object): def merge(self, nums1, m, nums2, n): """
2017-02-25 15:06:19 240
原创 leetcode 69. Sqrt(x)
题目:求x的平方根思路:牛顿法,https://en.wikipedia.org/wiki/Newton%27s_methodclass Solution(object): def mySqrt(self, x): """ :type x: int :rtype: int """ if x=
2017-02-24 22:20:35 246
原创 leetcode 70. Climbing Stairs
题目:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?思路:动态规划,n可以看做前n-1步的解法
2017-02-24 21:58:57 222
原创 leetcode 66. Plus One
题目:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.T
2017-02-23 11:25:27 271
原创 leetcode 66. Plus One
题目:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.T
2017-02-23 11:23:27 260
原创 leetcode 53. Maximum Subarray
题目:Find the contiguous subarray within an array (containing at least one number) which has the largest sum. 最大子列和思路:在线搜索class Solution(object): def maxSubArray(self, nums): "
2017-02-23 11:02:33 224
原创 leetcode 53. Maximum Subarray
题目:Find the contiguous subarray within an array (containing at least one number) which has the largest sum. 最大子列和思路:在线搜索O(n)class Solution(object): def maxSubArray(self, nums):
2017-02-23 10:59:43 190
原创 leetcode 38. Count and Say
题目:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read
2017-02-22 11:05:33 219
原创 leetcode 28. Implement strStr()
题目:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.思路:模式匹配问题,即在原始串中寻找第一个匹配目标串的位置。利用KMP算法,分两步,1.求next数组 2.根据next
2017-02-22 10:41:25 244
原创 leetcode 26. Remove Duplicates from Sorted Array
题目:Total Accepted: 199594Total Submissions: 563740Difficulty: EasyContributors: AdmiGiven a sorted array, remove the duplicates in place such that each element appear only once and return
2017-02-21 12:19:04 239
原创 leetcode 21. Merge Two Sorted Lists
题目: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.思路:链表合并。1判空,2.找到头节点head,r=head,3.当l1,l2都不为空时逐个判断并下移指针,
2017-02-21 10:58:37 219
原创 Leetcode 20. Valid Parentheses
题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are
2017-02-20 10:00:24 224
原创 14. Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings.思路:找出最小长度的字符串,逐个子集判断,判断时可以使用set,最后判断set元素是否为1个即可。class Solution(object): def longestCommonPrefix(s
2017-02-20 09:36:30 269
原创 Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:Input: "babad"Output: "bab"Note: "aba" is also a valid answer.E
2016-11-20 23:21:16 230
原创 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 "
2016-11-19 22:35:08 335
原创 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link
2016-11-19 22:15:39 233
原创 Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example:Given nums =
2016-11-19 21:25:12 221
原创 字符串搜索--Rabin-Karp算法
该算法采用数字指纹的思想,欲求m位目标字符串 p 在文本T中相同子串的位置转化为求p的指纹fp与文本T中一个m位窗口子串的指纹ft的关系。有以下假设:1. 如果fp!=ft ,则p与该窗口子串不匹配2. 可以在O (m) 时间内计算p的指纹fp3. 可以在 O (1) 时间内比较fp 和 ft 的大小关系4. 可以在 O (1) 时间内利用前一个ft 计算 窗口位移一位后子串的
2016-02-03 16:21:03 499
原创 二分查找算法
1.二分法搜索指定元素在有序序列中的位置或待插入的位置.2.模拟STL里的下界和上界查找函数lower_bound()和upper_bound().#include #include #include #include #include #define MAX(a,b) (a)>(b)?(a):(b)using namespace std;int b
2016-01-22 16:33:46 356
原创 最大子列和问题
解法1:直接枚举所有情况暴力求解问题O (n^3)解法2:在解法1的基础上改善了局部求和方法,使用递推和式代替每次累加求和O (n^2)解法3:采用分治法求解,将数列递归地一分为二,最大子列和就等于左边的最大子列和与右边的最大子列和与跨越中线的最大子列和的最大值。O (n logN)解法4:采用在线扫描方法,从起点开始往终点扫描,记录当前的子列和,如果为负数
2016-01-20 21:56:48 410
原创 UVALive 3708_Graveyard
题目描述:有个1000步距离的圆环,环上的两点之间的距离是一样的,及环上的点平分周长。题目给出初始点个数和将要加上点的个数,把初始的点调整下位置,然后把要加入点的接上,求总调整距离的最小值。解题方法:把圆环看成一维的坐标轴。计算原始点和加上附加点后所有的坐标数组P1,P2,对比P1中每个原始点两侧离P2中任意点最近的点,贪心的算出每个的最小调整距离,其之和就是结果。
2016-01-20 16:46:13 510
原创 UVA 10881_Piotr's Ant
问题描述:在L cm长的杆子上有n只蚂蚁,蚂蚁的移动速度为1 cm/s,题目并告知这些蚂蚁的初始位置pos及将要移动的方向L or R。如果两只蚂蚁相遇则各自方向倒置,即往原移动方向相反的方向移动。求最终各个蚂蚁的位置及最终方向。解题方法:根据题目意思,终止时刻每只蚂蚁相对其他蚂蚁的位置是稳定的,即一只蚂蚁不会穿过另一只蚂蚁。我们可以假设把每只蚂蚁看作一样的,因为其最终时刻的位
2016-01-20 16:22:45 321
原创 UVA 11300_ Spreading the Wealth
把问题巧妙换成求解中位数问题,妙哉!#include #include #include #include #include #define MAX(a,b) (a)>(b)?(a):(b)using namespace std;const int MAXSIZE = 1000010;long long originCoins[MAXSIZE];lon
2016-01-19 21:08:58 295
原创 UVA11729_Commando War
基本的贪心算法之前把要求输出的Case xxx写成小写case,一直没发现..结果都是wa,气炸了。所以,做题一定要细心来#include #include #include #include #include #define MAX(a,b) (a)>(b)?(a):(b)using namespace std;struct TimePoint{ i
2016-01-19 15:26:04 317
原创 UVA 11292_Dragon of Loowater
题目意思:给定两组序列A和B,长度分别为n和m。对于A中的每个数据都要在B中找出大于或等于这个值的不同的值,并是这些至之和最小。解法:贪心+搜索.#include #include #include #include #include using namespace std;vector heads, knights;vector::iterator
2016-01-19 13:09:43 332
原创 POJ 1008_Maya Calendar
没什么算法技巧,理解题意,and convert the dates from the Haab calendar to the Tzolkin calendar. #include #include #include #include #include using namespace std;map Haab;void init(){ Haab.insert
2016-01-07 22:01:55 607
原创 POJ 1007_DNA Sorting
本题先求各个DNA序列的逆序数,再排序,这里是稳定排序(题目中说明了如果二者逆序数相同不改变相对位置)。So,这里用stable_sort(),not sort()。 之前一直wa,把输入方式getline(cin,string **)改为cin>>..结果AC了,that's strange !#include #include #include using namespace
2016-01-07 18:50:12 323
原创 POJ 1006_Biorhythms
本题没什么编程难点,主要就是采用了中国剩余定理解题。在《孙子算经》中有这样一个问题:“今有物不知其数,三三数之剩二(除以3余2),五五数之剩三(除以5余3),七七数之剩二(除以7余2),问物几何?”这个问题称为“孙子问题”,该问题的一般解法国际上称为“中国剩余定理”。下面说说我对该定理的理解。 首先确定3,5,7为俩俩互质的数,找出3和5的公倍数中最小的能使它%7=1的数,再找出3和7的
2016-01-07 12:52:57 368
原创 POJ 1005_I Think I Need a Houseboat
#include #include using namespace std;const float PI = 3.14159;int main(){ int N,NUM; float sum = 0, posX, posY, year, radius; cin >> N; NUM = 0; while (N--){ NUM++; cin >> posX >> posY
2016-01-05 22:17:51 284
hadoop.txt
2016-02-25
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人