diff --git a/Java/palindrome-number.java b/Java/palindrome-number.java new file mode 100644 index 00000000..53332cfb --- /dev/null +++ b/Java/palindrome-number.java @@ -0,0 +1,19 @@ +class Solution { + public boolean isPalindrome(int x) { + String _x = x+""; // integer convert string + String rev = ""; // store reverse + int i = 0; + for(i = _x.length()-1; i>=0; i--){ + rev += _x.charAt(i); + } + + if(_x.compareTo(rev) == 0){ + // correct + return true; + } + else{ + return false; + } + + } +} diff --git a/Java/path-sum.java b/Java/path-sum.java new file mode 100644 index 00000000..526031d6 --- /dev/null +++ b/Java/path-sum.java @@ -0,0 +1,40 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public boolean hasPathSum(TreeNode root, int sum) { + if(root == null){ + // exception + return false; + } + if(root.left == null && root.right == null){ + if(sum == root.val){ + return true; + } + return false; + } + else{ + boolean left = false; + boolean right = false; + if(root.left != null){ + left = hasPathSum(root.left, sum-root.val); + } + if(root.right != null){ + right = hasPathSum(root.right, sum-root.val); + } + return left||right; + } + } +} diff --git a/README.md b/README.md index 4bf66a57..eb7e9551 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 1614 | [Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/) | [Java](./Java/max-nesting-depth-parentheses.java) | _O(n)_ | _O(1)_ | Easy | | | | 1374 | [Generate a String With Characters That Have Odd Counts](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/) | [Java](./Java/generate-a-string-with-characters-that-have-odd-counts.java) | _O(n)_ | _O(1)_ | Easy | | | | 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Java](./Java/buddy-strings.java) | _O(n)_ | _O(1)_ | Easy | | | +| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Java](./Java/palindrome-number.java) | _O(n)_ | _O(1)_ | Easy | | |
⬆️ Back to Top @@ -327,6 +328,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | ---- | ------------------------------------------------------------------------------------------- | -------------------------------------------------- | ----------- | ----------- | ---------- | --- | ---- | | 1463 | [Cherry Pickup II](https://leetcode.com/problems/cherry-pickup-ii/) | [C++](./C++/Cherry-Pickup-II.cpp) | _O(n \* m)_ | _O(n \* m)_ | Hard | DFS | | | 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [python](./Python/maximum-depth-of-binary-tree.py) | _O(n)_ | _O(n)_ | Easy | DFS | | +| 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [Java](./Java/path-sum.java) | _O(n)_ | _O(n)_ | Easy | DFS | |