LeetCode
wsy2846513
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
0042_Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1],原创 2017-08-15 22:10:01 · 221 阅读 · 0 评论 -
0009_Palindrome Number
c++class Solution {public: bool isPalindrome(int x) { if(x 0) { return false; } if( x % 10 == 0 && x > 0) { return false;原创 2017-03-31 19:20:41 · 231 阅读 · 0 评论 -
0010_Regular Expression Matching
JAVA方法一使用递归的方式进行判断,写了近1小时,最后显示超时。。。 public boolean isMatch(String s, String p) { if (s.length() == 0 ) { return equivalentNull(p); } if (s.length() !=原创 2017-05-22 22:12:17 · 235 阅读 · 0 评论 -
0011_Container With Most Water
求两条线和X轴围城的容器的最大容积,两条线中间的其他线段可以无视。JAVA方法一由于没有考虑可以无视中间的线段,因此下述方法当中间的线段比两端的线段高时,将产生错误的结果,例如数组【1,2,1】正常应该返回2,即两端的1和X轴围城容器,但是下述代码返回1,以为无论从左还是从右开始,都是线段1、线段2和X轴围城容器。public int maxArea(int[] hei原创 2017-05-24 20:06:06 · 189 阅读 · 0 评论 -
0012_Integer to Roman
JAVA果然智商捉襟见肘。。看了题目后,反反复复想了几个晚上但是一直没动手,一直没有合适的方法。 最初想要从1开始一直加到NUM,后来一想这还需要对字符串进行删除操作,实在是太麻烦了。在今天睡觉前,想稍微写写看,没想到在刚开始写的时候,一下子思路就打开了,不就是那么几个有限的规则嘛,只要按照规则用IF语句判断一下就可以了呀。。。最初是想从前卫开始,每一位都不断的进行判断,在准备些百位的时候又原创 2017-05-30 22:04:36 · 243 阅读 · 0 评论 -
0013_Roman to Integer
Java设置一个hashmap,字符为键值,数字为值,从左向右读取字符串中的字符,若后一位的取值比前一位的大,说明需要做减法,否则做加法。O(N)的时间复杂度,但是排名在后1/4。。。public class Solution { public int romanToInt(String s) { HashMap map = new HashMap();原创 2017-06-04 21:19:48 · 237 阅读 · 0 评论 -
0014_Longest Common Prefix
求一个字符串数组中的最长前缀。JAVA方法一直接暴力解决,最差时间复杂度O(N2)O(N^2) 没想到竟然AC了。。而且效率排名还在一般左右。。。public class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length == 0){原创 2017-06-04 22:12:50 · 193 阅读 · 0 评论 -
0016_3Sum Closest
JAVA在看过0015_3Sum的解题思路之后,本题作为练习,采用同样的思路进行运算。即使在看过3Sum之后,写代码时仍然有多处边界判断不准确,导致溢出或死循环。。。不过这种方法的效率还可以,大概排在前1/3左右。public class Solution { public int threeSumClosest(int[] nums, int target) {原创 2017-06-07 21:46:36 · 214 阅读 · 0 评论 -
0017_Letter Combinations of a Phone Number
JAVA方法一要求数字对应的全部字母的组合,直接递归就可以了,不过效率不够高,大概排在中间的位置,要考虑优化。而且在写代码的时候,对substring,list的应用不熟,记不清关键字的拼写。。尴尬。。public class Solution { public List letterCombinations(String digits) { List原创 2017-06-08 22:35:32 · 241 阅读 · 0 评论 -
0008_String to Integer (atoi)
题意//在数字前出现空格,略过。 “ 123” 返回123 //正负号只能有一个。+-123 返回0 //正负号后紧跟数字。+ 123 返回0 //在数字后出现非数字字符,扔掉该非数字字符及其后续字符。123f34 返回123 //若超过最大INT或小于最小INT,返回最大INT或最小INT。C++class Solution {public: int m原创 2017-03-30 22:18:37 · 218 阅读 · 0 评论 -
0007_Reverse Integer
C++class Solution {public: bool overFlow(double x) { if(x > INT_MAX || x < INT_MIN) { return true; } return false; } int reverse(int x)原创 2017-03-30 20:46:33 · 174 阅读 · 0 评论 -
0152_Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest pr原创 2018-01-16 20:46:14 · 273 阅读 · 0 评论 -
0001_Two Sum
C++:O(N)class Solution {public: vectorint> twoSum(vectorint>& nums, int target) { vectorint>::iterator it; vectorint> result; mapint,int> tempMap; for(it =原创 2017-03-25 09:52:32 · 198 阅读 · 0 评论 -
0002_Add Two Numbers
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */C++:按照正常思路写的代码,但是由于没有函数调用,因此代原创 2017-03-25 10:23:26 · 229 阅读 · 0 评论 -
0003_Longest Substring Without Repeating Characters
C++感谢praveen_uchiha分享class Solution {public: int lengthOfLongestSubstring(string s) { int currentLength = 0; int max = 0; int index = 0; string shortestStrin原创 2017-03-25 10:57:52 · 273 阅读 · 0 评论 -
0004_Median of Two Sorted Arrays_未完成
C++class Solution {public: double findMedianSortedArrays(vectorint>& nums1, vectorint>& nums2) { int totalNumber = nums1.size() + nums2.size(); vectorint> tempNum; vec原创 2017-03-25 10:59:59 · 184 阅读 · 0 评论 -
0030_Substring with Concatenation of All Words_未完成
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and wi原创 2017-07-05 23:11:15 · 366 阅读 · 0 评论 -
0005_Longest Palindromic Substring
C++:class Solution {public: string longestPalindrome(string s) { int head,tail,maxLength = 0; string * result = nullptr; for(int i = 0; i < s.length(); ++i) {原创 2017-03-25 11:02:34 · 201 阅读 · 0 评论 -
0006_ZigZag Conversion
c++class Solution {public: string convert(string s, int numRows) { //n = numRows,通过画图找规律可知: //对于第i行,dist = 2*n-2*i-2, cycle = 2*n-2,同时,输出的下标一次为 //0*cycle+i, 0*cycl原创 2017-03-27 20:35:12 · 209 阅读 · 0 评论 -
0018_4Sum
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. Note: The solution set原创 2017-06-12 20:19:40 · 234 阅读 · 0 评论 -
0019_Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list原创 2017-06-14 21:04:07 · 173 阅读 · 0 评论 -
0039_Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from原创 2017-07-31 22:38:43 · 386 阅读 · 0 评论 -
0034_Search for a Range
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm’s runtime complexity must be in the order of O(log n).If the targ原创 2017-07-25 21:38:52 · 236 阅读 · 0 评论 -
0035_Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2017-07-25 21:57:21 · 209 阅读 · 0 评论 -
0036_Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’. A partially filled sudok原创 2017-07-26 22:20:27 · 204 阅读 · 0 评论 -
0037_Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character ‘.’.You may assume that there will be only one unique solution. A sudoku puzzle…原创 2017-07-30 16:19:53 · 225 阅读 · 0 评论 -
0038_Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following:111211211111221 1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off原创 2017-07-30 23:13:22 · 222 阅读 · 0 评论 -
0040_Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combination原创 2017-08-15 21:16:12 · 234 阅读 · 0 评论 -
0041_First Missing Positive
Given an unsorted integer array, find the first missing positive integer.For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant spa原创 2017-08-15 21:45:57 · 199 阅读 · 0 评论 -
0031_Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible o原创 2017-07-10 23:44:39 · 219 阅读 · 0 评论 -
0029_Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.JAVA方法一控制好符号及边界条件,直接考虑循环使用被除数减去除数,统计循环次数即为结果,果然超时。方法二由于不能直接进行乘除和取余操作,所以直原创 2017-07-05 22:05:17 · 273 阅读 · 0 评论 -
0020_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 all valid bu原创 2017-06-14 22:38:48 · 362 阅读 · 0 评论 -
0021_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.JAVA直接将两个链表拼接成一个,时间复杂度O(N)O(N)但是效率排名在后1/4,而效率最高的方法竟然是原创 2017-06-21 19:54:05 · 162 阅读 · 0 评论 -
0022_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-06-21 22:44:22 · 179 阅读 · 0 评论 -
0023_Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.JAVA方法一可以把输入看做一个二维数组,数组的每一行都是递增的,因此每次只找第一列中最小的元素放入结果链表的尾部,然后将该行后续的数字前移。结果超时/** * Defini原创 2017-06-26 22:31:41 · 329 阅读 · 0 评论 -
0024_Swap Nodes in Pairs
Given 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 use only constant space. You m原创 2017-06-27 21:54:29 · 196 阅读 · 0 评论 -
0025_Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number of原创 2017-06-28 21:39:37 · 228 阅读 · 0 评论 -
0026_Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with c原创 2017-06-28 21:46:09 · 228 阅读 · 0 评论 -
0027_Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.The原创 2017-06-28 22:29:59 · 216 阅读 · 0 评论 -
0033_Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in t原创 2017-07-25 20:41:11 · 209 阅读 · 0 评论
分享