`
testforvln
  • 浏览: 18930 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • superich2008: 1、去重可以用Set集合2、在排序后,相邻2个元素如果相同可以 ...
    4Sum
文章列表

Combinations

全排列 按理说很简单,可是用递归写,边界条件就还是难想清楚,sigh 主要是n<k和k==1的条件要想清楚 public class Solution { public ArrayList<ArrayList<Integer>> combine(int n, int k) { // Start typing your Java solution below // DO NOT write main() function ArrayList<ArrayList<Integer>> ...
还是递归 但是边界条件以及边界上的处理不容易搞清楚(一开始我就把target==0的情况漏掉了,然后又把target==0放在了target<0||candidates.length<=0的后面,导致candidates.length==0&&target==0的情况不会被target==0处理,唉,考虑不清楚) public class Solution { public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) { ...

climbing stairs

一开始觉得是简单的组合数学题,但是写完之后发现,首先组合数不是那么简单就能算好的,然后就是。。。。int对于up和down(分子和分母)太小了 囧啊 换成long好一点,还有错,然后再换成double,差不多了,但是有3个结果正好只差2,囧啊,改成float也不行,sigh public class Solution { public int climbStairs(int n) { // Start typing your Java solution below // DO NOT write main() function ...
I简单 直接递归就好 addAll函数很好用 /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public ArrayList<Integer> inorderTraversal(TreeNode root) { ...
啊 第一次直接过small和big测试 好爽!虽然主要是以前知道这个题目吧= = 以前一直想不清这个题目的算法,今天终于搞明白了。思路很简单:就是用i遍历第2到最后一天,看第i天卖最大的profit是多少。当然,这就需要维护一个前i项最小值的变量,很简单啦。这里还有一个问题,就是如果最大profit都小于0,那不如不卖。就像期货界里大家说的那样,你不交易,就已经超越80%的人了 = = 恩 代码就是这样。。。 public class Solution { public int maxProfit(int[] prices) { // Start typing y ...
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isBalanced(TreeNode root) { // Start typing your Java solution below ...
实验室和公司看了这么些时候代码 然后晚上回寝室写leetcode 确实有一种很不一样的感觉。 因为都是用java 大家懂的 用起来确实很方便,但是不知不觉会让程序员少想很多东西,更多的是设计和架构方面的东西。可是从基础代码就如此省心,写出来代码质量和效率可想而知。无怪乎邓公要鄙视我们这些只会写java的人了。。。 这几天写算法的体会就是代码结构的堆砌是一种思路,基本算法又是一种思路,并不能说设计就要比算法高一层次。好的算法不仅能开阔程序的思路,还能真正改变程序的 这里想到以前说“程序=数据结构+算法 ”,感觉确实是这样,数据结构不仅是说简单的队列、栈,而且指的是穿插在你程序中所有的变量、常量,甚 ...

Anagrams

这题实在是没懂它的意思。。。囧啊 import java.util.HashSet; public class Solution { public ArrayList<String> anagrams(String[] strs) { ArrayList<String> results = new ArrayList<String>(); HashSet<String> anas = new HashSet<String>(); HashMap<String, ...

Add Two Numbers

这题不难 直接上递归就行 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode ...

Add Binary

public String addBinary(String a, String b) { int x = Integer.parseInt(a, 2); int y = Integer.parseInt(b, 2); return Integer.toBinaryString(x + y); } 一开始直接用Integer.toBinaryString()和Integer.parseInt(String, int)方法直接写。小例子还好,可是大例子就过不了,只能强行写了= = public class Solution { public ...

4Sum

本来以为只要在3Sum外面再包一层循环就好了,可是。。。在Judge Large的时候还是超时了 呜呜呜 public class Solution { public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) { Arrays.sort(num); ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>() ...
这题的思路和3Sum一样,只是要考虑一下k在j+1之后是不是需要减1,因为不是0而是绝对值最小,所以有点难考虑= = 我就懒得去想直接没-1 汗啊 public class Solution { public int threeSumClosest(int[] num, int target) { Arrays.sort(num); int tempClose = Integer.MAX_VALUE; int tempsum = Integer.MAX_VALUE; for (int i = 0; i != n ...

3sum

一直没看清英文题目,导致各种WA!唉唉唉。。。 通过Judge Small后: public class Solution { public ArrayList<ArrayList<Integer>> threeSum(int[] num) { int length = num.length; ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>(); ArrayL ...
Global site tag (gtag.js) - Google Analytics