
LeetCode_Easy
文章平均质量分 67
ALiTuTu
这个作者很懒,什么都没留下…
展开
-
【Leetcode】136-single-number【Java实现】【位操作】
看到要求的空间、时间复杂度完全没有头绪。只好看discussion好巧妙。。完全想不到。。。还是没有经验啊。。。。异或操作:相同为0,不同为1stem:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorith原创 2015-11-05 18:25:39 · 889 阅读 · 0 评论 -
【leetcode】【Easy】【455. Assign Cookies】【greedy】
problem linkcode:public class Solution { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s); int num=0; for(int i=0,原创 2017-01-25 21:16:25 · 478 阅读 · 0 评论 -
【leetcode】【Easy】【258. Add Digits】【Math】
题目:https://leetcode.com/problems/add-digits/Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3原创 2016-02-25 22:04:04 · 500 阅读 · 0 评论 -
【leetcode】【Easy】【283. Move Zeroes】【array】
problem linkword:in-place 在原位code:code1:计算某一非零位前面有多少个0,直接前移,然后给后面的赋值为0,有几个赋值几个。public class Solution { public void moveZeroes(int[] nums) { if(nums==null || nums.length==0)原创 2017-01-20 00:05:49 · 512 阅读 · 0 评论 -
【leetcode】【Easy】【226. Invert Binary Tree】【tree】
problem linkcode:code1:递归在leetcode上要比BFS快一点public class Solution { public TreeNode invertTree(TreeNode root) { if(root == null) return null; TreeNode tmp = root.left;原创 2017-01-19 23:30:34 · 415 阅读 · 0 评论 -
【leetcode】【Easy】【389. Find the Difference】【string】【bit manipulation】
problem linkcode1:将string变为char数组比第二种直接操作string使用charAt方法要快很多public class Solution { public char findTheDifference(String s, String t) { int sumS=0; int sumT=0; int原创 2017-01-18 16:56:00 · 419 阅读 · 0 评论 -
【leetcode】【Easy】【104. Maximum Depth of Binary Tree】【tree】
经典简单问题 problem linknote:(1)递归要有结束条件(2)此题可用DFS,BFS解题code:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode r原创 2017-01-18 16:34:48 · 416 阅读 · 0 评论 -
【leetcode】【Easy】【448. Find All Numbers Disappeared in an Array】【正负位标记】【Array】
problem linkcode:用负来标记出现过的,因为题目要求不能有extra space,复杂度为O(n),所以只能操作原数组,另外可以利用的是数组的下标。public class Solution { public List findDisappearedNumbers(int[] nums) { for(int i=0;i<nums.length;原创 2017-01-18 15:06:53 · 306 阅读 · 0 评论 -
【leetcode】【Easy】【463. Island Perimeter】
problem link:https://leetcode.com/problems/island-perimeter/code1:效率较高的两种解法,打败一半的人//查看右边和下边的邻居public class Solution { public int islandPerimeter(int[][] grid) { int islands = 0, ne原创 2017-01-18 00:19:57 · 352 阅读 · 0 评论 -
【leetcode】【Easy】【344. Reverse String】【string】【two pointers】
一个经典,基本的问题:翻转字符串problem link:https://leetcode.com/problems/reverse-string/note:(1)String类的方法substring(beginindex,endindex)两个参数如果相同返回的是空字符串(2)stringbuilder的replace方法和string的substring方法指针超原创 2017-01-17 15:04:30 · 385 阅读 · 0 评论 -
【leetcode】【Easy】【412. Fizz Buzz】
problem link:https://leetcode.com/problems/fizz-buzz/code:code1: 最简单的一种public class Solution { public List fizzBuzz(int n) { List res=new ArrayList(n); for(int i原创 2017-01-16 20:41:16 · 522 阅读 · 0 评论 -
【leetcode】【Easy】【461. Hamming Distance】【bit manipulation】
problem link:https://leetcode.com/problems/hamming-distance/code:效率从高到低的三种方法第一种方法,要知道java中int占用4个字节(char占用2个字节 reference:http://blog.youkuaiyun.com/witsmakemen/article/details/8974200),这种类型的题既原创 2017-01-14 18:31:24 · 468 阅读 · 0 评论 -
【Leetcode】202-Happy Number【Java实现】【Easy】
Your runtime beats 82.86% of java submissions.stem:Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any pos原创 2015-11-13 16:05:06 · 2201 阅读 · 0 评论 -
【leetcode】【Easy】【453. Minimum Moves to Equal Array Elements】【math】
problem link看着复杂,其实转换一下思维就可以了code:public class Solution { //n-1个元素加一相当于只有一个减去1,那么每次只让一个元素减一,直到所有元素都相同,这个相同的元素就是数组中最小的元素 public int minMoves(int[] nums) { int min=nums[0];原创 2017-01-25 21:47:57 · 627 阅读 · 0 评论