
leetcode
新角度着想
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
刷题Leetcode:1两数和
Given an array of integers, returnindicesof the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the sa...原创 2019-08-19 14:58:39 · 349 阅读 · 0 评论 -
#61旋转链表medium
code1:使用快慢指针法,快指针先走k步,然后两个指针一起走,当快指针走到末尾时,慢指针的下一个位置是新的顺序的头结点,这样就可以旋转链表,注意边界条件:原链表为空时,直接返回NULL,还有就是当k大于链表长度和k远远大于链表长度时该如何处理,我们需要首先遍历一遍原链表得到链表长度n,然后k对n取余,这样k肯定小于nclass Solution { public ListNode...原创 2019-09-15 16:41:14 · 122 阅读 · 0 评论 -
#60第K个排列medium
#60mediumcode1:class Solution { public String getPermutation(int n, int k) { List<Integer> num = new ArrayList<>(); for (int i = 1; i <= n; i++) num.add(i);...原创 2019-09-15 16:24:43 · 115 阅读 · 0 评论 -
#58最后一个单词的长度easy
#58easycode1://String.trim()方法h是消去字符串前后的空格,中间的空客不消去class Solution { public int lengthOfLastWord(String s) { return s.trim().length()-s.trim().lastIndexOf(" ")-1; }}...原创 2019-09-15 16:23:50 · 108 阅读 · 0 评论 -
#57插入区间hard
class Solution { public int[][] insert(int[][] intervals, int[] newInterval) { List<int[]> res=new ArrayList<>(); int n=intervals.length,cur=0; while(cur<n &...原创 2019-09-15 16:23:00 · 141 阅读 · 0 评论 -
#56合并区间medium
code1:public int[][] merge(int[][] intervals) { List<int[]> res = new ArrayList<>(); if (intervals == null || intervals.length == 0) return res.toArray(new int...原创 2019-09-15 16:22:04 · 147 阅读 · 0 评论 -
#51N皇后hard
#51hardcode1:class Solution { //确定哪一列是否已经被皇后占据 int rows[]; //确定相应的主对角线是否被占据 //对于主对角线,有row-col=constant; int hills[]; //确定相应的对角线是否被占据 //对于副对角线有row+col=constant int dale...原创 2019-09-04 21:37:42 · 123 阅读 · 0 评论 -
52N皇后IImedium
#52hardcode1:原理同一篇class Solution { int res; public int totalNQueens(int n) { this.res=0; boolean[] cols=new boolean[n]; boolean[] diag=new boolean[2*n]; boole...原创 2019-09-04 21:36:57 · 99 阅读 · 0 评论 -
53最大子序和easy
#53easycode1:class Solution { public int maxSubArray(int[] nums) { int res=Integer.MIN_VALUE,curSum=0; for(int num:nums){ curSum=Math.max(num+curSum,num); r...原创 2019-09-04 21:36:02 · 109 阅读 · 0 评论 -
#54螺旋矩阵medium
#54medimcode1://注意up主right在初始化和进行下一圈更新时是不一样的哦//int right = column - 1 - round * 2;//因为第一圈第[0,0]元素提前插入(进入循环还未更新步就马上插入)//right = column - round * 2;//而后面每一圈第[0,0]元素都是在循环下一步才插入(本次循环是插入上一圈最后一个数)clas...原创 2019-09-04 21:34:45 · 92 阅读 · 0 评论 -
#55跳跃游戏medium
code1:贪心算法//从右向左迭代,对于每个节点我们检查是否存在一步跳跃可以到达 GOOD 的位置//(currPosition + nums[currPosition] >= leftmostGoodIndex)。//如果可以到达,当前位置也标记为 GOOD ,同时,这个位置将成为新的最左边的 GOOD 位置,//一直重复到数组的开头,如果第一个坐标标记为 GOOD 意味着可以从...原创 2019-09-04 21:33:16 · 135 阅读 · 0 评论 -
leetcode刷题:2两数相加
英文题目:You are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse orderand each of their nodes contain a single digit. Add the two numbers and r...原创 2019-08-19 16:55:13 · 152 阅读 · 0 评论 -
#62不同路径medium
code1:用动态规划 Dynamic Programming 来解,可以维护一个二维数组 dp,其中 dp[i][j] 表示到当前位置不同的走法的个数,然后可以得到状态转移方程为: dp[i][j] = dp[i - 1][j] + dp[i][j - 1],这里为了节省空间,使用一维数组 dp,一行一行的刷新也可以class Solution { public int uniqu...原创 2019-09-15 18:21:14 · 149 阅读 · 0 评论