[LeetCode]Codes of Easy Problem
来源:互联网 发布:善良的男人大结局知乎 编辑:程序博客网 时间:2024/05/22 12:57
2016.2.5
231. Power of Two
[https://leetcode.com/problems/power-of-two/]
package Q231;public class Solution { public static boolean isPowerOfTwo(int n) { if(n<=0) return false; while(n!=1){ if(n!=(n/2)*2) return false; n=n/2; } return true; }// public static void main(String[] args) {// System.out.print(isPowerOfTwo(0));// }}
183. Customers Who Never Order My Submissions Question
[https://leetcode.com/problems/customers-who-never-order/]
select Name as Customers from Customers where Customers.Id not in (select distinct(CustomerId) from Orders);
2016.2.7
151. Reverse Words in a String
[https://leetcode.com/problems/reverse-words-in-a-string/]
package Q151;public class Solution { public static String reverseWords(String s) { String ans = ""; String tmp = ""; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ' ') { if (tmp.length() > 0) { if (ans.length() > 0) ans = tmp + " " + ans; else ans = tmp; } tmp = ""; } else { tmp += s.charAt(i); } } if (tmp.length() > 0) { if (ans.length() > 0) ans = tmp + " " + ans; else ans = tmp; } return ans; }// public static void main(String[] args) {// String a = " aasdaafdf";// System.out.println(reverseWords(a));// }}
237. Delete Node in a Linked List
[https://leetcode.com/problems/delete-node-in-a-linked-list/]
package Q237;public class Solution { public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public void deleteNode(ListNode node) { if(node==null) return; node.val=node.next.val; node.next=node.next.next; }// public static void main(String[] args) {//// }}
2016.2.13
160. Intersection of Two Linked Lists
[https://leetcode.com/problems/intersection-of-two-linked-lists/]
package Q160;public class Solution { static class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if(headA==null||headB==null) return null; ListNode tmp=new ListNode(-1); int lenA=0,lenB=0; tmp=headA; while(tmp!=null){ tmp=tmp.next; lenA++; } tmp=headB; while(tmp!=null){ tmp=tmp.next; lenB++; } while(lenA>lenB){ headA=headA.next; lenA--; } while(lenA<lenB){ headB=headB.next; lenB--; } while(headA!=headB){ headA=headA.next; headB=headB.next; } return headA; } public static void main(String[] args) { }}
2016.2.21
263. Ugly Number
[https://leetcode.com/problems/ugly-number/]
package Q263;public class Solution { public static boolean isUgly(int num) { if(num==0) return false; while(num!=1){ if(num%2!=0&&num%3!=0&&num%5!=0) return false; if(num%2==0) num=num/2; if(num%3==0) num=num/3; if(num%5==0) num=num/5; } if(num==1) return true; return true; } public static void main(String[] args) { System.out.print(isUgly(2*3*5*2+1)); }}
2016.2.23
175. Combine Two Tables
[https://leetcode.com/problems/combine-two-tables/]
SELECT Person.FirstName, Person.LastName, Address.City, Address.State from Person LEFT JOIN Address on Person.PersonId = Address.PersonId;
2016.2.29
26. Remove Duplicates from Sorted Array
[https://leetcode.com/problems/remove-duplicates-from-sorted-array/]
package q26;public class Solution { public static int removeDuplicates(int[] nums) { if (nums.length < 1) return 0; int duplicates = 0; int tmp = nums[0]; int j=1; for (int i = 1; i < nums.length; i++) { if (tmp == nums[i]) duplicates++; else { tmp = nums[i]; nums[j] = nums[i]; j++; } } return nums.length-duplicates; }// public static void main(String[] args) {// int[] a = { 1, 1,2 };// System.out.println(removeDuplicates(a));// for(int i=0;i<a.length;i++){// System.out.print(a[i]+" ");// }// // }}
226. Invert Binary Tree
[https://leetcode.com/problems/invert-binary-tree/]
package q226;//import q297.Codec_TLE;import TreeNode.TreeNode;public class Solution { public static TreeNode invertTree(TreeNode root) { if(root==null) return root; TreeNode tmp=null; tmp=root.left; root.left=root.right; root.right=tmp; if(root.left!=null) invertTree(root.left); if(root.right!=null) invertTree(root.right); return root; }// public static void main(String[] args) {// // TreeNode root=Codec_TLE.deserialize("1,null,2,3");// root=invertTree(root);// System.out.println(Codec_TLE.serialize(root));// // }}
2016.3.1
303. Range Sum Query - Immutable
[https://leetcode.com/problems/range-sum-query-immutable/]
package q303;public class NumArray { public int[] Mynums; public NumArray(int[] nums) { Mynums=new int[nums.length]; int tmp=0; for(int i=0;i<nums.length;i++){ tmp=tmp+nums[i]; Mynums[i]=tmp; } } public int sumRange(int i, int j) { if(i-1>=0) return Mynums[j]-Mynums[i-1]; return Mynums[j]; }// public static void main(String[] args) {// int[] nums={1,2,3,4,5,6,7};// NumArray numArray = new NumArray(nums);// System.out.println(numArray.sumRange(0, 1));// System.out.println(numArray.sumRange(1, 2));//// }}
2016.3.2
228. Summary Ranges
[https://leetcode.com/problems/summary-ranges/]
package q228;import java.util.ArrayList;import java.util.List;public class Solution { public static List<String> summaryRanges(int[] nums) { List<String> ans= new ArrayList<String>(); if(nums.length<1) return ans; int begin=nums[0],end=nums[0]; String tmp=""; for(int i=1;i<nums.length;i++){ if(nums[i]==end+1) end=nums[i]; else{ if(begin!=end){ tmp=begin+"->"+end; ans.add(tmp); } else{ ans.add(begin+""); } begin=nums[i]; end=nums[i]; } } if(begin!=end){ tmp=begin+"->"+end; ans.add(tmp); } else{ ans.add(begin+""); } return ans; } public static void main(String[] args) { int[] a={1,4,5,6,7,9}; System.out.print(summaryRanges(a).toString()); }}
2016.3.3
27. Remove Element
[https://leetcode.com/problems/remove-element/]
package q27;public class Solution { public static int removeElement(int[] nums, int val) { if (nums.length < 1) return 0; int sum=0; for(int i=0;i<nums.length;i++){ if(nums[i]!=val){ nums[sum]=nums[i]; sum++; } } return sum; }// public static void main(String[] args) {// int[] a = { 1, 2, 3, 4 };// System.out.print(a.length + " " + removeElement(a, 2));// }}
2016.3.4
21. Merge Two Sorted Lists
[https://leetcode.com/problems/merge-two-sorted-lists/]
package q021;public class Solution { static class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode head = null; ListNode tmp = null; while (l1 != null && l2 != null) { if (l1.val <= l2.val) { if (head == null) { head = l1; tmp = l1; } else { tmp.next = l1; tmp = tmp.next; } l1 = l1.next; } else { if (head == null) { head = l2; tmp = l2; } else { tmp.next = l2; tmp = tmp.next; } l2 = l2.next; } } while (l1 != null) { if (head == null) { head = l1; tmp = l1; } else { tmp.next = l1; tmp = tmp.next; } l1 = l1.next; } while (l2 != null) { if (head == null) { head = l2; tmp = l2; } else { tmp.next = l2; tmp = tmp.next; } l2 = l2.next; } return head; }//// public static void main(String[] args) {// // TODO Auto-generated method stub// ListNode a1 = new ListNode(1);// ListNode a2 = new ListNode(2);// ListNode a3 = new ListNode(3);// a1.next = a2;// a2.next = a3;//// ListNode ans = mergeTwoLists(null, a1);// while (ans != null) {// System.out.print(ans.val + " ");// ans = ans.next;// }//// }}
104. Maximum Depth of Binary Tree
[https://leetcode.com/problems/maximum-depth-of-binary-tree/]
package q104;import TreeNode.*;public class Solution { public int maxDepth(TreeNode root) { if(root==null) return 0; return 1+Math.max(maxDepth(root.left), maxDepth(root.right)); }}
2016.3.5
125. Valid Palindrome
[https://leetcode.com/problems/valid-palindrome/]
package q125;public class Solution { public static boolean isPalindrome(String s) { int i = 0; int j = s.length() - 1; while (i < j) { char a = s.charAt(i); char b = s.charAt(j); if (!Character.isLetter(a) && !Character.isDigit(a)) i++; else if (!Character.isLetter(b) && !Character.isDigit(b)) j--; else { if (Character.toLowerCase(a) != Character.toLowerCase(b)) return false; i++; j--; } } return true; }// public static void main(String[] args) {// System.out.print(isPalindrome(""));//// }}
2016.3.7
202. Happy Number
[https://leetcode.com/problems/happy-number/]
package q202;public class Solution { public boolean isHappy(int n) { String tmp = n + ""; while (n != 4) { tmp = n + ""; n = 0; for (int j = 0; j < tmp.length(); j++) { n += (tmp.charAt(j) - '0') * (tmp.charAt(j) - '0'); } if (n == 1) return true; } return false; } // public static void main(String[] args) { // Solution s=new Solution(); // for(int i=1;i<Integer.MAX_VALUE;i++){ // System.out.println(i+" "+s.isHappy(i)); // } // // }}
67. Add Binary
[https://leetcode.com/problems/add-binary/]
package q067;public class Solution { public String addBinary(String a, String b) { String ans = ""; int i = a.length() - 1; int j = b.length() - 1; boolean add = false; boolean p, q; int tmp = 0; while (i >= 0 && j >= 0) { tmp=0; p = a.charAt(i) - '0' == 1; q = b.charAt(j) - '0' == 1; if (p & !q & !add | !p & q & !add | !p & !q & add | p & q & add) tmp = 1; add = p & q & !add | p & !q & add | !p & q & add | p & q & add; ans = tmp + ans; i--; j--; } while (i >= 0) { p = a.charAt(i) - '0' == 1; tmp=0; if (p & !add | !p & add) tmp = 1; add = p & add; ans = tmp + ans; i--; } while (j >= 0) { p = b.charAt(j) - '0' == 1; tmp=0; if (p & !add | !p & add) tmp = 1; add = p & add; ans = tmp + ans; j--; } if(add) ans=1+ans; return ans; }// public static void main(String[] args) {// Solution s = new Solution();// String a = "1010";// String b = "1011";// System.out.print(s.addBinary(a, b));//// }}
2016.3.8
19. Remove Nth Node From End of List
[https://leetcode.com/problems/remove-nth-node-from-end-of-list/]
package q019;public class Solution { class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public ListNode removeNthFromEnd(ListNode head, int n) { ListNode tmp = head; ListNode now = head; ListNode n_now = head; for (int i = 0; i < n; i++) { n_now = n_now.next; } while (n_now != null) { tmp = now; now = now.next; n_now = n_now.next; } if (tmp != now) tmp.next = now.next; else return head.next; return head; }}
2016.3.9
38. Count and Say
[https://leetcode.com/problems/count-and-say/]
package q038;public class Solution { public String countAndSay(int n) { if (n < 1) return "1"; String ans = "1"; String tmp = ""; n--; char now; int nowCount; while (n > 0) { now = ans.charAt(0); nowCount = 1; for (int i = 1; i < ans.length(); i++) { if (ans.charAt(i) == now) nowCount++; else { tmp += Integer.toString(nowCount)+now; now = ans.charAt(i); nowCount = 1; } } tmp += Integer.toString(nowCount)+now; ans = tmp; tmp = ""; n--; } return ans; }// public static void main(String[] args) {// Solution s = new Solution();// for (int i = 1; i < 5; i++)// System.out.println(s.countAndSay(i));// }}
6. ZigZag Conversion
[https://leetcode.com/problems/zigzag-conversion/]
package q006;public class Solution { public String convert(String s, int numRows) { if(numRows<=1) return s; int k=0; int next=1; String[] lines=new String[numRows]; String ans=""; for(int i=0;i<numRows;i++){ lines[i]=""; } for(int i=0;i<s.length();i++){ if(k==0) next=1; if(k==numRows-1) next=-1; lines[k]+=s.charAt(i); k+=next; } for(int i=0;i<numRows;i++){ ans+=lines[i]; } return ans; }// public static void main(String[] args) {// Solution s=new Solution();// // System.out.println(s.convert("ab", 1));// }}
2016.4.3
165. Compare Version Numbers
[https://leetcode.com/problems/compare-version-numbers/]
package q165;public class Solution { public int compareVersion(String version1, String version2) { String[] v1 = version1.split("[.]"); String[] v2 = version2.split("[.]"); int i = 0; while (i < v1.length && i < v2.length) { if (Integer.valueOf(v1[i]) < Integer.valueOf(v2[i])) return -1; else if (Integer.valueOf(v1[i]) > Integer.valueOf(v2[i])) return 1; i++; } while (i < v1.length) { if (Integer.valueOf(v1[i]) > 0) return 1; i++; } while (i < v2.length) { if (Integer.valueOf(v2[i]) > 0) return -1; i++; } return 0; } public static void main(String[] args) { Solution s = new Solution(); System.out.print(s.compareVersion("1.1", "1.2.1")); }}
223. Rectangle Area
[https://leetcode.com/problems/rectangle-area/]
package q223;public class Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int left = Math.max(A, E); int right = Math.min(G, C); int bottom = Math.max(F, B); int top = Math.min(D, H); int area3 = 0; if (right > left && top > bottom) area3 = (right - left) * (top - bottom); int area1 = (C - A) * (D - B); int area2 = (G - E) * (H - F); return area1 - area3 + area2; }}
328. Odd Even Linked List
[https://leetcode.com/problems/odd-even-linked-list/]
package q328;public class Solution { class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public ListNode oddEvenList(ListNode head) { ListNode odd = null; ListNode even = null; ListNode oddhead = null; ListNode evenhead = null; int tmp = 1; while (head != null) { if (tmp % 2 != 0) { if (odd == null) { odd = head; oddhead = head; } else { odd.next = head; odd = odd.next; } } else { if (even == null) { even = head; evenhead = head; } else { even.next = head; even = even.next; } } head = head.next; tmp++; } if(even!=null) even.next=null; if(odd!=null) odd.next = evenhead; return oddhead; }}
2016.4.5
203. Remove Linked List Elements
[https://leetcode.com/problems/remove-linked-list-elements/]
package q203;public class Solution { class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public ListNode removeElements(ListNode head, int val) { if (head == null) return head; ListNode a, b; a = head; b = head.next; while (b != null) { if (b.val == val) a.next = b.next; else a = b; b = b.next; } if (head.val == val) return head.next; return head; }}
2016.4.6
205. Isomorphic Strings
[https://leetcode.com/problems/isomorphic-strings/]
package q205;import java.util.Hashtable;public class Solution { public boolean isIsomorphic(String s, String t) { Hashtable<Character, Character> h1 = new Hashtable<Character, Character>(); Hashtable<Character, Character> h2 = new Hashtable<Character, Character>(); for (int i = 0; i < s.length(); i++) { char c1 = s.charAt(i); char c2 = t.charAt(i); if (!h1.containsKey(c1) && !h2.containsKey(c2)) { h1.put(c1, c2); h2.put(c2, c1); } else { if (h1.containsKey(c1) && h1.get(c1) != c2) return false; if (h2.containsKey(c2) && h2.get(c2) != c1) return false; } } return true; }}
2016.4.7
24. Swap Nodes in Pairs
[https://leetcode.com/problems/swap-nodes-in-pairs/]
package q024;class ListNode { int val; ListNode next; ListNode(int x) { val = x; }}public class Solution { public ListNode swapPairs(ListNode head) { if (head == null) return head; if (head.next == null) return head; ListNode h2 = head.next; head.next = swapPairs(h2.next); h2.next = head; return h2; }}
2016.4.10
66. Plus One
[https://leetcode.com/problems/plus-one/]
package q066;public class Solution { public int[] plusOne(int[] digits) { int n = digits.length; int i; int tmp = 1; int[] ans; for (i = n - 1; i >= 0; i--) { if (tmp == 1) { digits[i] += 1; tmp = 0; if (digits[i] == 10) { digits[i] = 0; tmp = 1; } } } if (i < 0 && tmp == 1) { ans = new int[n + 1]; ans[0] = 1; for (int j = 0; j < n; j++) { ans[j + 1] = digits[j]; } } else { ans = new int[n]; for (int j = 0; j < n; j++) { ans[j] = digits[j]; } } return ans; }}
2016.4.12
112. Path Sum
[https://leetcode.com/problems/path-sum/]
package q112;import TreeNode.*;public class Solution { private boolean finded; public boolean hasPathSum(TreeNode root, int sum) { helper(root, sum); return finded; } public void helper(TreeNode root, int sum) { if (root == null) return; if (sum == root.val && root.left == null && root.right == null) finded = true; helper(root.left, sum - root.val); helper(root.right, sum - root.val); }}
2016.4.14
118. Pascal’s Triangle
[https://leetcode.com/problems/pascals-triangle/]
package q118;import java.util.ArrayList;import java.util.List;public class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> ans = new ArrayList<List<Integer>>(); List<Integer> tmp = new ArrayList<Integer>(); if (numRows <= 0) return ans; int k = 0; for (int i = 0; i <= numRows; i++) { for (int j = 0; j < i; j++) { if (j == 0) tmp.add(1); else if (j == i - 1) tmp.add(1); else { k = ans.get(i - 1).get(j - 1); k += ans.get(i - 1).get(j); tmp.add(k); } } ans.add(new ArrayList<Integer>(tmp)); tmp.clear(); } ans.remove(0); return ans; }}
2016.4.16
70. Climbing Stairs
[https://leetcode.com/problems/climbing-stairs/]
package q070;public class Solution { public int climbStairs(int n) { if (n < 1) return 0; int[] dp = new int[n + 1]; dp[0] = 1; dp[1] = 1; for (int i = 2; i <= n; i++) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n]; }}
232. Implement Queue using Stacks
[https://leetcode.com/problems/implement-queue-using-stacks/]
package q232;import java.util.Stack;class MyQueue { private Stack<Integer> s1 = new Stack<Integer>(); private Stack<Integer> s2 = new Stack<Integer>(); // Push element x to the back of queue. public void push(int x) { if (s2.isEmpty()) s1.push(x); else { if (s1.isEmpty()) s1.push(x); else s2.push(x); } } // Removes the element from in front of queue. public void pop() { if (s1.isEmpty()) { changeS1S2(); s1.pop(); changeS1S2(); } else { changeS1S2(); s2.pop(); changeS1S2(); } } // Get the front element. public int peek() { int tmp; if (s1.isEmpty()) { changeS1S2(); tmp = s1.peek(); changeS1S2(); } else { changeS1S2(); tmp = s2.peek(); changeS1S2(); } return tmp; } // Return whether the queue is empty. public boolean empty() { return s1.isEmpty() && s2.isEmpty(); } private void changeS1S2() { int tmp; if (!s1.isEmpty()) { while (!s1.isEmpty()) { tmp = s1.pop(); s2.push(tmp); } } else { while (!s2.isEmpty()) { tmp = s2.pop(); s1.push(tmp); } } }}
2016.4.17
9. Palindrome Number
[https://leetcode.com/problems/palindrome-number/]
package q009;public class Solution { public boolean isPalindrome(int x) { if (x < 0) return false; if (x != 0 && x % 10 == 0) return false; int tmp = 0; while (x > tmp) { tmp = tmp * 10 + x % 10; x /= 10; } return (x == tmp || x == tmp / 10); }}
- [LeetCode]Codes of Easy Problem
- Array of Leetcode: EASY
- 【leetcode】Power Of Two[easy]
- An Easy Problem Of BFS(queue stack)
- [Leetcode 191, Easy] Number of 1 Bits
- [Leetcode 58, easy] Length of Last Word
- [Leetcode 231, Easy] Power of Two
- 【LeetCode】(231)Power of Two(Easy)
- 【LeetCode】(58)Length of Last Word(Easy)
- <LeetCode><Easy>231 Power of Two
- <LeetCode><Easy> 191 Number Of 1 Bit
- <LeetCode><Easy> 58 Length of Last Word
- <LeetCode><Easy> 326. Power of Three
- LeetCode 231. Power of Two --Easy
- 【Leetcode】Number of 1 Bits(easy)
- Leetcode 231. Power of Two (Easy) (cpp)
- Leetcode 326. Power of Three (Easy) (cpp)
- Leetcode 342. Power of Four (Easy) (cpp)
- 为什么直接把javascript的代码写进html就行但是调用js文件就不行
- uva 10859 树形dp
- HDOJ1238 Substrings
- junit4X系列--Runner解析
- Spark On Yarn彻底解密
- [LeetCode]Codes of Easy Problem
- 基于OpenWrt的PPTP插件开启防火墙转发
- 日志管理
- {A} + {B}
- 数据结构(5)--栈的定义以及相关操作的实现
- c++n连环
- Linux下程序单例模式的保证机制:/var/run/*.pid
- junit4X系列--Statement
- jQuery2(JQuery实现onload)