算法
helloJeremy1996
这个作者很懒,什么都没留下…
展开
-
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 · 294 阅读 · 0 评论 -
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 · 250 阅读 · 0 评论 -
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 · 238 阅读 · 0 评论 -
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 · 208 阅读 · 0 评论 -
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 · 171 阅读 · 0 评论 -
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 · 197 阅读 · 0 评论 -
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 · 227 阅读 · 0 评论 -
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 · 217 阅读 · 0 评论 -
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 · 202 阅读 · 0 评论 -
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 · 259 阅读 · 0 评论 -
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 · 210 阅读 · 0 评论 -
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 · 228 阅读 · 0 评论 -
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 · 254 阅读 · 0 评论 -
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 · 259 阅读 · 0 评论 -
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 · 268 阅读 · 0 评论 -
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 · 249 阅读 · 0 评论 -
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 · 252 阅读 · 0 评论 -
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 · 326 阅读 · 0 评论 -
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 · 198 阅读 · 0 评论 -
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 · 206 阅读 · 0 评论 -
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 · 220 阅读 · 0 评论 -
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 · 206 阅读 · 0 评论 -
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 · 253 阅读 · 0 评论 -
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 · 262 阅读 · 0 评论 -
POJ 1004_Financial Management
...#include using namespace std;int main(){ float n,sum = 0; int i; for ( i = 0; i < 12; i++) { cin >> n; sum += n; } cout << "$"<<sum / 12.0 << endl; return 0;}原创 2016-01-05 21:31:09 · 386 阅读 · 0 评论 -
POJ1003_Hangover
水题...#include using namespace std;int main(){ float n,sum; int i; while (cin >> n&&n!=0){ for (i = 0, sum = 0; sum < n; i++){ sum += 1.0 / (2 + i); } cout <<i<<" card(s)" <<原创 2016-01-05 20:14:01 · 322 阅读 · 0 评论 -
uva_201Squares
From now on...开始刷题啦!希望2016一切顺利,哈哈。本题直接遍历所有可能的点,用h[][],v[][]两个数组存储题目所给的两点的关系,1代表这两点有线相连,0则没有。唯一的缺点是本题for循环太多了,由于数据量比较小所以也没什么关系就是了。不多说,直接上代码。原创 2016-01-02 19:35:27 · 271 阅读 · 0 评论 -
UVA 10881_Piotr's Ant
问题描述:在L cm长的杆子上有n只蚂蚁,蚂蚁的移动速度为1 cm/s,题目并告知这些蚂蚁的初始位置pos及将要移动的方向L or R。如果两只蚂蚁相遇则各自方向倒置,即往原移动方向相反的方向移动。求最终各个蚂蚁的位置及最终方向。解题方法:根据题目意思,终止时刻每只蚂蚁相对其他蚂蚁的位置是稳定的,即一只蚂蚁不会穿过另一只蚂蚁。我们可以假设把每只蚂蚁看作一样的,因为其最终时刻的位原创 2016-01-20 16:22:45 · 299 阅读 · 0 评论 -
最大子列和问题
解法1:直接枚举所有情况暴力求解问题O (n^3)解法2:在解法1的基础上改善了局部求和方法,使用递推和式代替每次累加求和O (n^2)解法3:采用分治法求解,将数列递归地一分为二,最大子列和就等于左边的最大子列和与右边的最大子列和与跨越中线的最大子列和的最大值。O (n logN)解法4:采用在线扫描方法,从起点开始往终点扫描,记录当前的子列和,如果为负数原创 2016-01-20 21:56:48 · 393 阅读 · 0 评论 -
UVALive 3708_Graveyard
题目描述:有个1000步距离的圆环,环上的两点之间的距离是一样的,及环上的点平分周长。题目给出初始点个数和将要加上点的个数,把初始的点调整下位置,然后把要加入点的接上,求总调整距离的最小值。解题方法:把圆环看成一维的坐标轴。计算原始点和加上附加点后所有的坐标数组P1,P2,对比P1中每个原始点两侧离P2中任意点最近的点,贪心的算出每个的最小调整距离,其之和就是结果。原创 2016-01-20 16:46:13 · 492 阅读 · 0 评论 -
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 · 274 阅读 · 0 评论 -
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 · 298 阅读 · 0 评论 -
二分查找算法
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 · 328 阅读 · 0 评论 -
POJ 1006_Biorhythms
本题没什么编程难点,主要就是采用了中国剩余定理解题。在《孙子算经》中有这样一个问题:“今有物不知其数,三三数之剩二(除以3余2),五五数之剩三(除以5余3),七七数之剩二(除以7余2),问物几何?”这个问题称为“孙子问题”,该问题的一般解法国际上称为“中国剩余定理”。下面说说我对该定理的理解。 首先确定3,5,7为俩俩互质的数,找出3和5的公倍数中最小的能使它%7=1的数,再找出3和7的原创 2016-01-07 12:52:57 · 347 阅读 · 0 评论 -
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 · 308 阅读 · 0 评论 -
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 · 213 阅读 · 0 评论 -
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 · 315 阅读 · 0 评论 -
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 · 212 阅读 · 0 评论 -
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 · 206 阅读 · 0 评论 -
字符串搜索--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 · 477 阅读 · 0 评论