![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 50
corpsepiges
https://github.com/corpsepiges
展开
-
Algorithms—202.Happy Number
思路:用map存储过程中出现的每一个数,重复返回false,1返回truepublic class Solution { public boolean isHappy(int n) { String s=String.valueOf(n); Map map=new HashMap(); map.put(s, "存在");原创 2015-06-21 22:58:40 · 272 阅读 · 0 评论 -
Algorithms—42.Trapping Rain Water
public class Solution { public int trap(int[] height) { if (height.length<=1) { return 0; } int a=0; int h=height[a]; /** * 求出最高峰 */ for (int i = 1原创 2015-06-19 22:30:45 · 274 阅读 · 0 评论 -
Algorithms—123.Best Time to Buy and Sell Stock III
思路:限制交易两次。首先,求出单次交易最大利润,然后,只有2种可能,与此次交易无关,那么不在左侧就在右侧,再求一次,或者是与此次交易有关,那么在此次交易的日期限制内找出一段利润最低(负值)。比较左侧,右侧,中间的负值的绝对值的大小。这段代码写的比较丑,凑合的看吧。sum3是中间的负值。public class Solution { public int maxProfit(int[原创 2015-06-22 01:04:13 · 264 阅读 · 0 评论 -
Median of Two Sorted Arrays
public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { boolean flag=true;int a=nums1.length/2;int b=nums2.length/2;int c=Math.abs(a-b);int d=0;原创 2015-06-02 09:50:19 · 495 阅读 · 0 评论 -
Algorithms—27.Remove Element
public class Solution { public int removeElement(int[] nums, int val) { int end=nums.length;int k=0;for (int i = 0; i if (nums[i]==val) {for (int j = i; j k=nums[j];nums[j]=nu原创 2015-06-02 16:41:51 · 384 阅读 · 0 评论 -
Algorithms—70.Climbing Stairs
思路:爬n阶台阶,有2种方法,先爬n-1阶,或者先爬n-2阶。n=1时,只有1种,n=2时有2种。递归计算public class Solution { public int climbStairs(int n) { if (n<=2) { return n; }else { int[] sk=new int[n]; sk[0]=1; sk[原创 2015-06-04 00:16:14 · 305 阅读 · 0 评论 -
Algorithms—219.Contains Duplicate II
思路,首先将数组nums中前k个数放入map中查看是否有相同的,如果在,返回true;如果没有,添加;然后,检查后面的每一个数是否在map中,如果在,返回ture;如果没有,添加,并删除map中最先添加的那个;public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) {原创 2015-06-03 21:50:12 · 271 阅读 · 0 评论 -
Algorithms—34.Search for a Range
public class Solution { public int[] searchRange(int[] nums, int target) { int begin=0; int end=nums.length-1; int a=(begin+end)/2; int[] answer=new int[2]; while (nums[a]!=target)原创 2015-06-03 10:08:01 · 392 阅读 · 0 评论 -
Algorithms—35.Search Insert Position
public class Solution { public int searchInsert(int[] nums, int target) { int begin=0; int end=nums.length-1; int a=(begin+end)/2; if (target<nums[0]) { return 0; } if (target原创 2015-06-03 10:18:41 · 354 阅读 · 0 评论 -
Algorithms—36.Valid Sudoku
public class Solution { public boolean isValidSudoku(char[][] board) { //验证每行每列有没有重复数据 for (int i = 0; i < 9; i++) { Map cross=new HashMap(); Map vertical=new HashMap(); for (int j =原创 2015-06-03 14:17:09 · 269 阅读 · 0 评论 -
Algorithms—13.Roman to Integer
package leetcode;public class RomantoInt {public static void main(String[] args) {String s="MCDLXXVI";int a;int b;int c;int d;String sa="";String sb="";String sc="";String sd原创 2015-06-02 11:33:01 · 384 阅读 · 0 评论 -
Algorithms—74.Search a 2D Matrix
思路:非常简单,有序数组找target,中间开始判断,唯一需要注意的就是各种数组超界问题。public class Solution { public boolean searchMatrix(int[][] matrix, int target) { int begin=0; int end=matrix.length-1; int t=end/2;原创 2015-06-24 13:53:37 · 280 阅读 · 0 评论 -
Algorithms—77.Combinations
思路:反复添加元素。直到需要返回的list中的每个list长度都符合k。public class Solution { public List> combine(int n, int k) { List> answerList=new ArrayList>(); for (int i = 1; i <=n; i++) { List list=new原创 2015-06-25 10:12:28 · 251 阅读 · 0 评论 -
Algorithms—75.Sort Colors
思路:从左开始查看,找到非0数,标记,从右开始查看,查看到0,标记,交换,然后从左标记开始继续,以此类推,可使得所有的0集中在数组的左端,同理可得答案。public class Solution { public void sortColors(int[] nums) { int t=0; int b=0; int e=nums.length-1; boolean flag=t原创 2015-06-24 17:09:30 · 373 阅读 · 0 评论 -
Algorithms—118.Pascal's Triangle
思路:按照杨辉三角形定义去做。public class Solution { public List> generate(int numRows) { List> answerList = new ArrayList>(); for (int i = 0; i < numRows; i++) { List list = new ArrayList(); for (i原创 2015-06-26 09:09:57 · 280 阅读 · 0 评论 -
Algorithms—200.Number of Islands
思路,遍历二维char数组,判断其是否在map中,如果不在map中,且值为‘1’;那么将计数+1,并将点加入map中,然后递归查询周围4个点(考虑边界)情况将public class Solution { public int numIslands(char[][] grid) { int il = 0; Map map = new HashMap(); for (int i原创 2015-06-04 17:10:54 · 248 阅读 · 0 评论 -
Algorithms—119.Pascal's Triangle II
思路:限制了空间复杂度,我选择了使用map来逐层覆盖的方法。public class Solution { public List getRow(int rowIndex) { Map map=new HashMap(); for (int i = 0; i <=rowIndex; i++) { map.put(i, 1); //反向计算防止值在使原创 2015-06-26 09:50:28 · 268 阅读 · 0 评论 -
Algorithms—134.Gas Station
思路:每个加油站提供的油减去下一段路上耗费的油,生成新的数组。循环数组长度2倍的量,找出其中一段长度为数组长且每部的剩余油量都不小于0的;public class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int[] n = new int[gas.length]; for (int i原创 2015-06-26 14:19:32 · 263 阅读 · 0 评论 -
Algorithms—136.Single Number
思路:异或计算。0^a=a;a^b=b^a。public class Solution { public int singleNumber(int[] nums) { int answer=0; for (int i = 0; i < nums.length; i++) { answer^=nums[i]; } return answer;原创 2015-06-26 15:10:15 · 275 阅读 · 0 评论 -
Algorithms—125.Valid Palindrome
思路:判断一个字符串是否是回文很简单,题目就是把给的字符串空格和标点符号去掉,大写小写转化成同等的比较,我选择的是大写转小写。public class Solution { public boolean isPalindrome(String s) { if (s.length()<=1) { return true; } char[] c=s.to原创 2015-06-26 10:59:39 · 309 阅读 · 0 评论 -
Algorithms—153.Find Minimum in Rotated Sorted Array
思路:二分法查找,观察样例,更改判断条件即可。耗时:324ms。处于中游水准。public class Solution { public int findMin(int[] nums) { int begin=0; int end=nums.length-1; int half=(begin+end)/2; whil原创 2015-06-29 14:16:49 · 325 阅读 · 0 评论 -
Algorithms—151.Reverse Words in a String
思路:思路很简单,反着读,非空格暂存,读到空格拼接。难点在于前后空格省略,中间空格合并为一个。只是优化的难点,不是实现的难点。耗时:440ms。处于下游水准,代码较渣。public class Solution { public String reverseWords(String s) { if (s.length()<1) { return s; } Stri原创 2015-06-29 13:33:01 · 454 阅读 · 0 评论 -
Algorithms—154.Find Minimum in Rotated Sorted Array II
思路:按照题目提示思考了下,假设一个长度为n的数组由(n-1)个3和1个1构成,那么这个1有可能出现在任何位置,所以时间复杂度为O(n),所以直接遍历了。耗时:404ms。处于下游,显然还有更快的方法。public class Solution { public int findMin(int[] nums) { int a=nums[0]; for (int原创 2015-06-29 14:34:49 · 298 阅读 · 0 评论 -
Algorithms—33.Search in Rotated Sorted Array
思路:没什么好的思路,分各种情况讨论。public class Solution { public int search(int[] nums, int target) { if (nums.length==1) { if (nums[0]==target) { return 0; }else { return -1; } } int be原创 2015-07-07 17:56:31 · 237 阅读 · 0 评论 -
Algorithms—38.Count and Say
思路:掌握规则,逐次去读书,循环N次即可。public class Solution { public String countAndSay(int n) { if (n == 1) { return "1"; } String answer = "1"; for (int i = 0; i < n - 1; i++) { answer = new Solu原创 2015-07-08 09:04:47 · 247 阅读 · 0 评论 -
Algorithms—41.First Missing Positive
思路:要求时间复杂度线性,空间复杂度常量。不能使用计数排序。只能遍历查看然后交换位置。找到每个值正确的位置,然后再遍历查询看到第一个不对应的就是缺省值public class Solution { public int firstMissingPositive(int[] nums) { for (int i = 0; i < nums.length; i++) { i原创 2015-07-08 09:47:48 · 371 阅读 · 0 评论 -
Algorithms—17.Letter Combinations of a Phone Number
public class Solution { public List letterCombinations(String digits) { //存放答案的list List list=new ArrayList(); //建立map存放手机按键 Map> map=new HashMap>(); List list2=ne原创 2015-06-10 19:37:16 · 216 阅读 · 0 评论 -
Algorithms—171.Excel Sheet Column Number
思路:曾经和朋友讨论过这个问题,其实本质上是26进制,把Z当做A0就可以了。耗时:344ms。中游水准。public class Solution { public int titleToNumber(String s) { int[] a=new int[s.length()]; boolean flag=false; for (int i =原创 2015-07-01 17:22:13 · 261 阅读 · 0 评论 -
Algorithms—168.Excel Sheet Column Title
思路:同171题,记住Z=A0即可。耗时:204ms,中上游。public class Solution { public String convertToTitle(int n) { List list=new ArrayList(); while (n!=0) { list.add(n%26); n/=26; } boo原创 2015-07-01 18:04:01 · 270 阅读 · 0 评论 -
Algorithms—22.Generate Parentheses
public class Solution { public List generateParenthesis(int n) { List list = new ArrayList(); String a = "("; list.add(a); for (int i = 1; i <= 2 * n - 1; i++) { int size = list.size();原创 2015-06-10 20:22:46 · 421 阅读 · 0 评论 -
Algorithms—57.Insert Interval
思路:题目不难,就是要考虑好各种情况。/** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Interval(int s, int e) { start = s原创 2015-07-09 19:15:06 · 291 阅读 · 0 评论 -
Algorithms—60.Permutation Sequence
思路:自认为想了一个还是比较精巧的方法,因为是只招一个,首先把所有的数字放入list中,取一个删一个。取数的规则为,k-1/每个数字控制的范围。public class Solution { public String getPermutation(int n, int k) { int s=1; List list=new ArrayList();原创 2015-07-09 20:21:08 · 293 阅读 · 0 评论 -
Algorithms—67.Add Binary
思路:按照二进制加法规则来。public class Solution { public String addBinary(String a, String b) { if (a.equals("0")) { return b; } if (b.equals("0")) { return a; } if (a.length()<b原创 2015-07-09 21:04:31 · 265 阅读 · 0 评论 -
Algorithms—50.Pow(x, n)
思路:为了降低耗时要考虑各种情况直接return。public class Solution { public double myPow(double x, int n) { if (n==0) { return 1.0; } boolean flag=true; if (n<0) { n=0-n; flag=false; }原创 2015-07-09 09:21:53 · 547 阅读 · 0 评论 -
Algorithms—56.Merge Intervals
思路:原来以为很简单,结果发现会出现同一个点原地开始原地结束的情况。先遍历查询,然后把所有的坐标装入map中,value的值代表对应的属性,然后读取。/** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start =原创 2015-07-09 15:09:51 · 209 阅读 · 0 评论 -
Algorithms—53.Maximum Subarray
思路:最大子数组public class Solution { public int maxSubArray(int[] nums) { int sum=0; int answer=nums[0]; for (int i = 0; i < nums.length; i++) { sum+=nums[i]; if (sum>an原创 2015-07-09 11:15:47 · 259 阅读 · 0 评论 -
Algorithms—58.Length of Last Word
思路:倒着查。public class Solution { public int lengthOfLastWord(String s) { if (s.length()==0) { return 0; } int e=-1; for (int i = s.length()-1; i >=0; i--) { if (!s.原创 2015-07-09 19:40:12 · 307 阅读 · 0 评论 -
Algorithms—59.Spiral Matrix II
思路:上下左右各写一个方法public class Solution { public int[][] generateMatrix(int n) { int[][] answer = new int[n][n]; // 顶部 int t = 0; // 底部 int b = n - 1; // 左边 int l = 0; // 右边 int r =原创 2015-07-09 20:03:06 · 264 阅读 · 0 评论 -
Algorithms—73.Set Matrix Zeroes
思路:首先是O(m+n)空间复杂度的方法,新开辟空间,然后遍历所有的点,如果有那个点的值为0,则将其横纵坐标的值记录进去。然后是空间复杂度为常数的。即把这些横纵坐标记录在原数组中,改为0即可。但是需要注意的是2点,1.在查询前先判断(x,0)和(0,y)两条线上是否存在0的点。2.重新遍历赋值的时候值修改除上述2条线的内部结构。3.再根据1中查询的结果决定是否修改这2条线。public cla原创 2015-07-09 21:38:32 · 329 阅读 · 0 评论 -
Algorithms—46.Permutations
思路:排列组合public class Solution { public List> permute(int[] nums) { List> answerList=new ArrayList>(); for (int i = 0; i < nums.length; i++) { int size=answerList.size();原创 2015-06-11 15:54:53 · 291 阅读 · 0 评论