From ccb02ba0d0edf2b3fe4a175850daa110f6a92064 Mon Sep 17 00:00:00 2001 From: gocoder Date: Tue, 8 Jan 2019 11:06:43 +0800 Subject: [PATCH] update #001 solution --- .gitignore | 3 +- README.md | 2 - .../BinaryTreeLevelOrderTraversal.java | 98 ----------------- .../BinaryTreeLevelOrderTraversalII.java | 100 ------------------ java/com.gocoder.leetcode/CountAndSay.java | 35 ------ java/com.gocoder.leetcode/PascalTriangle.java | 41 ------- .../PascalTriangleII.java | 28 ----- java/com.gocoder.leetcode/ReverseInteger.java | 34 ------ src/com/gocoder/leetcode/L0001.java | 33 ++++++ 9 files changed, 35 insertions(+), 339 deletions(-) delete mode 100644 java/com.gocoder.leetcode/BinaryTreeLevelOrderTraversal.java delete mode 100644 java/com.gocoder.leetcode/BinaryTreeLevelOrderTraversalII.java delete mode 100644 java/com.gocoder.leetcode/CountAndSay.java delete mode 100644 java/com.gocoder.leetcode/PascalTriangle.java delete mode 100644 java/com.gocoder.leetcode/PascalTriangleII.java delete mode 100644 java/com.gocoder.leetcode/ReverseInteger.java create mode 100644 src/com/gocoder/leetcode/L0001.java diff --git a/.gitignore b/.gitignore index 2ac0ce7..5e13f7b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ leetcode.iml -java/java.iml +/.idea/ +/bin/ diff --git a/README.md b/README.md index 19989aa..5efb64a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ Solution set for Leetcode online interview problems. -Code compiled by Java7 - https://oj.leetcode.com/problems/ diff --git a/java/com.gocoder.leetcode/BinaryTreeLevelOrderTraversal.java b/java/com.gocoder.leetcode/BinaryTreeLevelOrderTraversal.java deleted file mode 100644 index 7e990f7..0000000 --- a/java/com.gocoder.leetcode/BinaryTreeLevelOrderTraversal.java +++ /dev/null @@ -1,98 +0,0 @@ -import java.util.ArrayList; -import java.util.List; -import java.util.TreeMap; - -/** - * this is solution of Binary Tree Level Order Traversal - *

- * https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ - */ -public class BinaryTreeLevelOrderTraversal { - - public List> levelOrderBottom(TreeNode root) { - List> result = new ArrayList<>(); - if (root == null) { - return result; - } - - TreeMap> map = new TreeMap<>(); - int level = 0; - - List rootNode = new ArrayList<>(); - rootNode.add(root.val); - map.put(level, rootNode); - - generateNextLevelNodes(map, root, level + 1); - - for (Integer key : map.keySet()) { - result.add(map.get(key)); - } - - return result; - } - - private void generateNextLevelNodes(TreeMap> map, TreeNode node, - int level) { - List currentNode = map.get(level); - - if (currentNode == null) { - currentNode = new ArrayList<>(); - } - - - if (node.left != null) { - currentNode.add(node.left.val); - } - - if (node.right != null) { - currentNode.add(node.right.val); - } - - if (currentNode.size() != 0) { - map.put(level, currentNode); - } - - level++; - if (node.left != null) { - generateNextLevelNodes(map, node.left, level); - } - - if (node.right != null) { - generateNextLevelNodes(map, node.right, level); - } - } - - - - public static void main(String[] args) { - TreeNode root = new TreeNode(1); - root.left = new TreeNode(2); - root.right = new TreeNode(3); - - root.right.left = new TreeNode(4); - root.right.left.right = new TreeNode(5); - - // TreeNode root = new TreeNode(1); - // root.left = new TreeNode(2); - // root.right = new TreeNode(3); - // - // root.left.left = new TreeNode(4); - // root.right.right = new TreeNode(5); - System.out.println(new BinaryTreeLevelOrderTraversal().levelOrderBottom(root)); - } - - - static class TreeNode { - - int val; - - TreeNode left; - - TreeNode right; - - TreeNode(int x) { - val = x; - } - } - -} diff --git a/java/com.gocoder.leetcode/BinaryTreeLevelOrderTraversalII.java b/java/com.gocoder.leetcode/BinaryTreeLevelOrderTraversalII.java deleted file mode 100644 index 519ea0c..0000000 --- a/java/com.gocoder.leetcode/BinaryTreeLevelOrderTraversalII.java +++ /dev/null @@ -1,100 +0,0 @@ -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.TreeMap; - -/** - * this is solution of Binary Tree Level Order Traversal II - *

- * https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ - */ -public class BinaryTreeLevelOrderTraversalII { - - public List> levelOrderBottom(TreeNode root) { - List> result = new ArrayList<>(); - if (root == null) { - return result; - } - - TreeMap> map = new TreeMap<>(); - int level = 0; - - List rootNode = new ArrayList<>(); - rootNode.add(root.val); - map.put(level, rootNode); - - generateNextLevelNodes(map, root, level + 1); - - for (Integer key : map.keySet()) { - result.add(map.get(key)); - } - - Collections.reverse(result); - return result; - } - - private void generateNextLevelNodes(TreeMap> map, TreeNode node, - int level) { - List currentNode = map.get(level); - - if (currentNode == null) { - currentNode = new ArrayList<>(); - } - - - if (node.left != null) { - currentNode.add(node.left.val); - } - - if (node.right != null) { - currentNode.add(node.right.val); - } - - if (currentNode.size() != 0) { - map.put(level, currentNode); - } - - level++; - if (node.left != null) { - generateNextLevelNodes(map, node.left, level); - } - - if (node.right != null) { - generateNextLevelNodes(map, node.right, level); - } - } - - - - public static void main(String[] args) { - TreeNode root = new TreeNode(1); - root.left = new TreeNode(2); - root.right = new TreeNode(3); - - root.right.left = new TreeNode(4); - root.right.left.right = new TreeNode(5); - - // TreeNode root = new TreeNode(1); - // root.left = new TreeNode(2); - // root.right = new TreeNode(3); - // - // root.left.left = new TreeNode(4); - // root.right.right = new TreeNode(5); - System.out.println(new BinaryTreeLevelOrderTraversalII().levelOrderBottom(root)); - } - - - static class TreeNode { - - int val; - - TreeNode left; - - TreeNode right; - - TreeNode(int x) { - val = x; - } - } - -} diff --git a/java/com.gocoder.leetcode/CountAndSay.java b/java/com.gocoder.leetcode/CountAndSay.java deleted file mode 100644 index de3568d..0000000 --- a/java/com.gocoder.leetcode/CountAndSay.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * this is solution of Count and Say - *

- * https://oj.leetcode.com/problems/count-and-say/ - */ -public class CountAndSay { - - public String countAndSay(int n) { - String result = "1"; - if (n <= 0) { - return result; - } - - for (int i = 1; i < n; i++) { - int count = 1; - - StringBuilder tmp = new StringBuilder(); - for (int m = 0; m < result.length(); m++) { - if (m < result.length() - 1 && result.charAt(m) == result.charAt(m + 1)) { - count++; - } else { - tmp.append(count).append(result.charAt(m)); - count = 1; - } - } - result = tmp.toString(); - } - - return result; - } - - public static void main(String[] args) { - System.out.println(new CountAndSay().countAndSay(5)); - } -} diff --git a/java/com.gocoder.leetcode/PascalTriangle.java b/java/com.gocoder.leetcode/PascalTriangle.java deleted file mode 100644 index 3099393..0000000 --- a/java/com.gocoder.leetcode/PascalTriangle.java +++ /dev/null @@ -1,41 +0,0 @@ -import java.util.ArrayList; -import java.util.List; - - -/** - * this is solution of Pascal's Triangle - *

- * https://oj.leetcode.com/problems/pascals-triangle/ - */ -public class PascalTriangle { - - public List> generate(int numRows) { - List> result = new ArrayList<>(); - - if (numRows <= 0) { - return result; - } - - for (int m = 1; m <= numRows; m++) { - result.add(generateRow(m)); - } - return result; - } - - public List generateRow(int row) { - ArrayList last = new ArrayList<>(); - last.add(1); - - for (int i = 1; i < row; i++) { - for (int j = last.size() - 2; j >= 0; j--) { - last.set(j + 1, last.get(j) + last.get(j + 1)); - } - last.add(1); - } - return last; - } - - public static void main(String[] args) { - System.out.println(new PascalTriangle().generate(9)); - } -} diff --git a/java/com.gocoder.leetcode/PascalTriangleII.java b/java/com.gocoder.leetcode/PascalTriangleII.java deleted file mode 100644 index 0347239..0000000 --- a/java/com.gocoder.leetcode/PascalTriangleII.java +++ /dev/null @@ -1,28 +0,0 @@ -import java.util.ArrayList; -import java.util.List; - - -/** - * this is solution of Pascal's Triangle II - *

- * https://oj.leetcode.com/problems/pascals-triangle-ii/ - */ -public class PascalTriangleII { - - public List getRow(int rowIndex) { - ArrayList last = new ArrayList<>(); - last.add(1); - - for (int i = 0; i < rowIndex; i++) { - for (int j = last.size() - 2; j >= 0; j--) { - last.set(j + 1, last.get(j) + last.get(j + 1)); - } - last.add(1); - } - return last; - } - - public static void main(String[] args) { - System.out.println(new PascalTriangleII().getRow(9)); - } -} diff --git a/java/com.gocoder.leetcode/ReverseInteger.java b/java/com.gocoder.leetcode/ReverseInteger.java deleted file mode 100644 index 0e90ff8..0000000 --- a/java/com.gocoder.leetcode/ReverseInteger.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * this is solution of reverse-integer - *

- * https://oj.leetcode.com/problems/reverse-integer/ - */ -public class ReverseInteger { - - public int reverse(int x) { - int result = 0; - - if (x == Integer.MIN_VALUE) { - return result; - } - - int abs = Math.abs(x); - - while (abs != 0) { - int t = abs % 10; - - if ((Integer.MAX_VALUE - t) / 10 < result) { - return 0; - } - - abs /= 10; - result = result * 10 + t; - } - - return x > 0 ? result : -result; - } - - public static void main(String[] args) { - System.out.println(new ReverseInteger().reverse(-2147483412)); - } -} diff --git a/src/com/gocoder/leetcode/L0001.java b/src/com/gocoder/leetcode/L0001.java new file mode 100644 index 0000000..0c90086 --- /dev/null +++ b/src/com/gocoder/leetcode/L0001.java @@ -0,0 +1,33 @@ +package com.gocoder.leetcode; + +import java.util.Arrays; +import java.util.HashMap; + +/** + * 两数之和 + * + *

#001 https://leetcode-cn.com/problems/two-sum/ + * + * @author gocoder + */ +public class L0001 { + + public int[] twoSum(int[] nums, int target) { + int len = nums.length; + HashMap map = new HashMap<>(len); + for (int i = 0; i < len; ++i) { + if (map.containsKey(nums[i])) { + return new int[]{map.get(nums[i]), i}; + } + map.put(target - nums[i], i); + } + return null; + } + + public static void main(String[] args) { + L0001 solution = new L0001(); + int[] nums = new int[]{2, 7, 11, 15}; + int target = 9; + System.out.println(Arrays.toString(solution.twoSum(nums, target))); + } +}