leetcode从零单排
hantianxiangKKK
THE WIND RISES
展开
-
203. Remove Linked List Elements
public class Solution { public ListNode removeElements(ListNode head, int val) { ListNode fakeHead = new ListNode(-1); fakeHead.next = head; ListNode curr = head, p原创 2016-02-29 10:19:14 · 115 阅读 · 0 评论 -
9. Palindrome Number
判断回文串 第一个和最后一个比public class Solution { public boolean isPalindrome(int x) { String a=Integer.toString(x); if(a.length() for(int i=0,j=a.length()-1;i if(a原创 2016-02-17 00:42:02 · 147 阅读 · 0 评论 -
112. Path Sum
public class Solution { public boolean hasPathSum(TreeNode root, int sum) { if(root==null)return false; if(root.left==null&&root.right==null&&root.val==sum)return true;原创 2016-02-17 00:41:11 · 157 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
public class Solution { public int removeDuplicates(int[] nums) { int cnt=0; if(nums.length==0)return cnt; if(nums.length==1)return 1; for(int i=0;i原创 2016-02-16 01:53:13 · 237 阅读 · 0 评论 -
27. Remove Element
public class Solution { public int removeElement(int[] nums, int val) { int cnt=0; for(int i=0;i if(nums[i]!=val){ nums[cnt]=nums[i];原创 2016-02-16 01:52:45 · 149 阅读 · 0 评论 -
198. House Robber
子问题每次选不选第一个public class Solution { public int rob(int[] nums) { int a=0; int b=0; for(int i=0;i if(i%2==0){ a=Math.max(a+nums[i],b);原创 2016-02-14 01:16:03 · 146 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal
public class Solution { public List> levelOrder(TreeNode root) { List>res=new ArrayList>(); levelHelper(res,root,0); return res; } public void levelHelper(Lis原创 2016-02-15 01:51:22 · 161 阅读 · 0 评论 -
107. Binary Tree Level Order Traversal II
public class Solution { public List> levelOrderBottom(TreeNode root) { List>res=new ArrayList>(); levelHelper(res,root,0); return res; } public void levelHelp原创 2016-02-15 01:50:42 · 166 阅读 · 0 评论 -
101. Symmetric Tree
树相关的问题一般都是递归public class Solution { public boolean isSymmetric(TreeNode root) { if(root==null)return true; return isMirror(root.left,root.right); } public boolean is原创 2016-02-14 01:10:23 · 251 阅读 · 0 评论 -
110. Balanced Binary Tree
递归方法 public class Solution { private boolean ans=true; public boolean isBalanced(TreeNode root) { maxDepth(root); return ans; } public int maxDepth(TreeNode roo原创 2016-02-14 01:08:55 · 224 阅读 · 0 评论 -
232. Implement Queue using Stacks
class MyQueue { // Push element x to the back of queue. Stack s1 = new Stack Stack s2 = new Stack public void push(int x) { s1.push(x); } // Removes the原创 2016-02-13 00:16:07 · 197 阅读 · 0 评论 -
225. Implement Stack using Queues
class MyStack { Queue q = new LinkedList(); // Push element x onto stack. public void push(int x) { q.add(x); } // Removes the element on top of the stack.原创 2016-02-17 00:42:41 · 277 阅读 · 0 评论 -
111. Minimum Depth of Binary Tree
public class Solution { public int minDepth(TreeNode root) { if(root==null)return 0; int l=minDepth(root.left); int r=minDepth(root.right); if(l==0||r==0)retur原创 2016-02-19 01:35:12 · 144 阅读 · 0 评论 -
290. Word Pattern
public class Solution { public boolean wordPattern(String pattern, String str) { String[]strs=str.split(" "); if(pattern.length()!=strs.length)return false; Mapmap=new原创 2016-02-29 10:18:55 · 144 阅读 · 0 评论 -
299. Bulls and Cows
public class Solution { public String getHint(String secret, String guess) { int bulls=0; int cows=0; int [] nums=new int [10]; for(int i=0;i int原创 2016-02-29 10:18:37 · 140 阅读 · 0 评论 -
20. Valid Parentheses
public class Solution { public boolean isValid(String s) { Stack stack = new Stack(); for(int i = 0; i if(s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) ==原创 2016-02-26 00:03:33 · 221 阅读 · 0 评论 -
205. Isomorphic Strings
public class Solution { public boolean isIsomorphic(String s, String t) { if(s == null || s.length() HashMap map = new HashMap(); for(int i = 0 ; i char a原创 2016-02-26 00:02:36 · 211 阅读 · 0 评论 -
58. Length of Last Word
public class Solution { public int lengthOfLastWord(String s) { return s.trim().length()-s.trim().lastIndexOf(" ")-1; }}原创 2016-02-26 00:02:03 · 142 阅读 · 0 评论 -
190. Reverse Bits
public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int result = 0; for (int i = 0; i result += n & 1; n >>>=1; //原创 2016-02-26 00:01:27 · 185 阅读 · 0 评论 -
223. Rectangle Area
public class Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int s1=(D-B)*(C-A); int s2=(G-E)*(H-F); int l=Math.max(A,E);原创 2016-02-25 23:59:07 · 190 阅读 · 0 评论 -
219. Contains Duplicate II
public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { Mapmap=new HashMap for(int i=0;i if(!map.containsKey(nums[i])){原创 2016-02-25 23:58:37 · 163 阅读 · 0 评论 -
36. Valid Sudoku
public class Solution { public boolean isValidSudoku(char[][] board) { List>rl=new ArrayList>(); List>cl=new ArrayList>(); List>sl=new ArrayList>();原创 2016-02-25 23:58:01 · 159 阅读 · 0 评论 -
88. Merge Sorted Array
public class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int cnt=m+n-1; int i=m-1; int j=n-1; while(i>=0&&j>=0){原创 2016-02-19 01:36:03 · 191 阅读 · 0 评论 -
326. Power of Three
用math.log求3为底的对数public class Solution { public boolean isPowerOfThree(int n) { double res = Math.log(n)/Math.log(3); return Math.abs(res - Math.rint(res)) }}原创 2016-02-13 00:08:39 · 209 阅读 · 0 评论 -
231. Power of Two
一直除2public class Solution { public boolean isPowerOfTwo(int n) { if(n==0)return false; if(n==1)return true; if(n%2==1)return false; while(n%2==0){原创 2016-02-13 00:10:34 · 131 阅读 · 0 评论 -
100. Same Tree
判断两个树的左右孩子是不是相等,迭代验证子树。public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p==null&&q==null){return true;} if(p==null||q==null){return false;}原创 2016-02-10 23:16:59 · 126 阅读 · 0 评论 -
283. Move Zeroes
用一个变量记录非零的个数并且移位,最后后面的都补零。public class Solution { public void moveZeroes(int[] nums) { int k=0; for(int i=0;i if(nums[i]!=0){ nums[k]=nums[i];原创 2016-02-10 23:15:31 · 141 阅读 · 0 评论 -
226. Invert Binary Tree
翻转二叉树 用迭代实现public class Solution { public TreeNode invertTree(TreeNode root) {if(root==null) return null; TreeNode temp=root.left; root.left=root.right; root.righ原创 2016-02-10 23:14:20 · 144 阅读 · 0 评论 -
237. Delete Node in a Linked List
删除结点 引用指向下一个结点public class Solution { public void deleteNode(ListNode node) { if(node==null)return; node.val=node.next.val; node.next=node.next.next; }}原创 2016-02-10 23:13:08 · 147 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
深度优先搜索,迭代求子树深度public class Solution { public int maxDepth(TreeNode root) { if(root==null){ return 0; } int left=maxDepth(root.left); int right=m原创 2016-02-10 23:07:37 · 143 阅读 · 0 评论 -
258. Add Digits
num大于10,%取余求个位数 除10取十位数。public class Solution { public int addDigits(int num) { while(num>=10){ num=num%10+num/10; } return num; }}原创 2016-02-10 23:03:57 · 139 阅读 · 0 评论 -
292. Nim Game
取完后的n是4个倍数就可以public class Solution { public boolean canWinNim(int n) { return n%4!=0; }}原创 2016-02-10 23:00:32 · 154 阅读 · 0 评论 -
36. Valid Sudoku
9个横行9个竖行9个3*3的方格各设置一个set。对每个方格遍历,“。”继续,数字的话判断哪个格的横竖行有没有重复的数字。方格判断用的是i/3*3 + j/3把9*9的格分成public class Solution { public boolean isValidSudoku(char[][] board) { List>rl=new ArrayList>原创 2016-02-10 01:33:35 · 157 阅读 · 0 评论 -
290. Word Pattern
把str分成数组后每个pattern的字幕应该对应一个单词。pattern = "abba", str = "dog dog dog dog" should return false.每个单词还应该对应一个字母。用map记录字母和单词用set记录是否多个字母对应一个单词。public class Solution { public boolean wordPa原创 2016-02-10 01:25:11 · 256 阅读 · 0 评论 -
1. Two Sum
public class Solution { public static int[] twoSum(int[] nums, int target) { Mapmap=new HashMap int []ans=new int[2]; for(int i=0;i if(map.containsKey(nums[i]))原创 2016-02-10 01:14:54 · 153 阅读 · 0 评论 -
242. Valid Anagram
验证两个单词是不是字母重组的单词,sort排序,然后比较。很多类似的问题都可以先排序看看,有序是一个很好的条件。public class Solution { public boolean isAnagram(String s, String t) { char[] ss=s.toCharArray(); char[] tt=t.toCharA原创 2016-02-10 23:18:29 · 140 阅读 · 0 评论 -
171. Excel Sheet Column Number
类似于26进制,A是1,Z是26。用进制的方法计算。public class Solution { public int titleToNumber(String s) { char[]h=s.toCharArray(); int sum=0; for(int i=0;i sum=sum*26+(h[i]原创 2016-02-10 23:24:53 · 152 阅读 · 0 评论 -
263. Ugly Number
一个数是235的倍数 先除2 然后3 然后5 public class Solution { public boolean isUgly(int num) { if(num==0){ return false; } if(num==1){ return true;原创 2016-02-12 00:41:52 · 159 阅读 · 0 评论 -
83. Remove Duplicates from Sorted List
如果第i个和i+1的值相等 next跳过下一个。public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head==null){ return null; } ListNode node=head;原创 2016-02-12 00:39:34 · 135 阅读 · 0 评论 -
70. Climbing Stairs
动态规划 最后一步走1步还是两步public class Solution { public int climbStairs(int n) { if(n==1){ return 1; } if(n==2){ return 2; } int l原创 2016-02-12 00:37:40 · 211 阅读 · 0 评论