This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class ReverseWordsinaString { | |
| public static String reverseWords(String s) { | |
| String[] strs = s.split(" "); | |
| int n = strs.length; | |
| StringBuilder sb = new StringBuilder(); | |
| for (int i = n-1; i >= 0 ; i--) { | |
| if(!strs[i].equals("")) { | |
| if (sb.length() > 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class BinaryTreeUpsideDown { | |
| /** | |
| * Definition for a binary tree node. | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; | |
| * TreeNode right; | |
| * TreeNode(int x) { val = x; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class PaintHouse { | |
| public int minCost(int[][] costs) { | |
| int n = costs.length; | |
| if(n == 0) return 0; | |
| for (int i = 1; i < n; i++) { | |
| costs[i][0] = costs[i][0] + Math.min(costs[i-1][1], costs[i-1][2]); | |
| costs[i][1] = costs[i][1] + Math.min(costs[i-1][0], costs[i-1][2]); | |
| costs[i][2] = costs[i][2] + Math.min(costs[i-1][0], costs[i-1][1]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class Permutations { | |
| public List<List<Integer>> permute(int[] nums) { | |
| List<List<Integer>> result = new ArrayList<>(); | |
| List<Integer> l0 = new ArrayList<>(); | |
| l0.add(nums[0]); | |
| result.add(l0); | |
| for (int i = 1; i < nums.length; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class BSTIterator { | |
| /** | |
| * Definition for binary tree | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; | |
| * TreeNode right; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class InvertBinaryTree { | |
| /** | |
| * Definition for a binary tree node. | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; | |
| * TreeNode right; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| /** | |
| * Definition for a binary tree node. | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; | |
| * TreeNode right; | |
| * TreeNode(int x) { val = x; } | |
| * } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| /** | |
| * Definition for a binary tree node. | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; | |
| * TreeNode right; | |
| * TreeNode(int x) { val = x; } | |
| * } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public static int hammingWeight(int n) { | |
| int cnt = 0; | |
| while(n != 0){ | |
| if(0 != n%2){ //if(1 == n%2){ | |
| cnt++; | |
| } | |
| n = n >>> 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class StackList { | |
| Node top; | |
| void push(Object data){ | |
| Node t = new Node(data); | |
| t.next = top; | |
| top = t; | |
| } | |
| Object pop(){ |