├── README.md ├── Problem 226.java ├── Problem122.java ├── Problem557.java ├── Leetcode 406.java ├── 404 leetcode.java ├── Leetcode 405.java ├── Problem 409.java ├── Problem228.java ├── Problem637.java ├── Problem154.java └── Problem-227.java /README.md: -------------------------------------------------------------------------------- 1 | List of problem solved on Leetcode 2 | problem 226 and problem 227,228 3 | problem 154,problem 122 4 | my leetcode 5 | Day 2 6 | problem 319,477,519 7 | Day 3 m,48, 8 | solve 405,404,406,637,635,409 9 | 10 | -------------------------------------------------------------------------------- /Problem 226.java: -------------------------------------------------------------------------------- 1 | //code 2 | class Solution { 3 | public TreeNode invertTree(TreeNode root) { 4 | if (root == null) 5 | return null; 6 | 7 | TreeNode left = root.left; 8 | TreeNode right = root.right; 9 | root.left = invertTree(right); 10 | root.right = invertTree(left); 11 | return root; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Problem122.java: -------------------------------------------------------------------------------- 1 | //Best time to Buy and sell problem II 2 | class Solution { 3 | public int maxProfit(int[] prices) { 4 | int sell = 0; 5 | int hold = Integer.MIN_VALUE; 6 | 7 | for (final int price : prices) { 8 | sell = Math.max(sell, hold + price); 9 | hold = Math.max(hold, sell - price); 10 | } 11 | 12 | return sell; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Problem557.java: -------------------------------------------------------------------------------- 1 | //Reverse ab word in string problem 2 | class Solution { 3 | public: 4 | string reverseWords(string s) { 5 | int i = 0; 6 | int j = 0; 7 | 8 | while (i < s.length()) { 9 | while (i < j || i < s.length() && s[i] == ' ') 10 | ++i; 11 | while (j < i || j < s.length() && s[j] != ' ') 12 | ++j; 13 | reverse(s.begin() + i, s.begin() + j); 14 | } 15 | 16 | return s; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode 406.java: -------------------------------------------------------------------------------- 1 | //leetcode 406 2 | class Solution { 3 | public int[][] reconstructQueue(int[][] people) { 4 | List ans = new ArrayList<>(); 5 | 6 | Arrays.sort(people, Comparator.comparingInt((int[] person) -> - person[0]) 7 | .thenComparingInt((int[] person) -> person[1])); 8 | 9 | for (final int[] person : people) 10 | ans.add(person[1], person); 11 | 12 | return ans.stream().toArray(int[][] ::new); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /404 leetcode.java: -------------------------------------------------------------------------------- 1 | //find left partvsum of tree 2 | class Solution { 3 | public int sumOfLeftLeaves(TreeNode root) { 4 | if (root == null) 5 | return 0; 6 | 7 | int ans = 0; 8 | 9 | if (root.left != null) { 10 | if (root.left.left == null && root.left.right == null) 11 | ans += root.left.val; 12 | else 13 | ans += sumOfLeftLeaves(root.left); 14 | } 15 | ans += sumOfLeftLeaves(root.right); 16 | 17 | return ans; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Leetcode 405.java: -------------------------------------------------------------------------------- 1 | //leetcode 405 problem 2 | class Solution { 3 | public String toHex(int num) { 4 | if (num == 0) 5 | return "0"; 6 | 7 | final char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', 8 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; 9 | StringBuilder sb = new StringBuilder(); 10 | 11 | while (num != 0) { 12 | sb.append(hex[num & 0xf]); 13 | num >>>= 4; 14 | } 15 | 16 | return sb.reverse().toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Problem 409.java: -------------------------------------------------------------------------------- 1 | //leetcode 409 problem solution in java 2 | class Solution { 3 | public int longestPalindrome(String s) { 4 | int ans = 0; 5 | int[] count = new int[128]; 6 | 7 | for (final char c : s.toCharArray()) 8 | ++count[c]; 9 | 10 | for (final int freq : count) 11 | ans += freq % 2 == 0 ? freq : freq - 1; 12 | 13 | final boolean hasOddCount = Arrays.stream(count).anyMatch(freq -> freq % 2 == 1); 14 | return ans + (hasOddCount ? 1 : 0); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Problem228.java: -------------------------------------------------------------------------------- 1 | //this code using java 2 | class Solution { 3 | public List summaryRanges(int[] nums) { 4 | List ans = new ArrayList<>(); 5 | 6 | for (int i = 0; i < nums.length; ++i) { 7 | final int begin = nums[i]; 8 | while (i + 1 < nums.length && nums[i] == nums[i + 1] - 1) 9 | ++i; 10 | final int end = nums[i]; 11 | if (begin == end) 12 | ans.add("" + begin); 13 | else 14 | ans.add("" + begin + "->" + end); 15 | } 16 | 17 | return ans; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Problem637.java: -------------------------------------------------------------------------------- 1 | //this is leetcode problem 637 solved in java 2 | class Solution { 3 | public List averageOfLevels(TreeNode root) { 4 | List ans = new ArrayList<>(); 5 | Queue q = new ArrayDeque<>(List.of(root)); 6 | 7 | while (!q.isEmpty()) { 8 | long sum = 0; 9 | final int size = q.size(); 10 | for (int i = 0; i < size; ++i) { 11 | TreeNode node = q.poll(); 12 | sum += node.val; 13 | if (node.left != null) 14 | q.offer(node.left); 15 | if (node.right != null) 16 | q.offer(node.right); 17 | } 18 | ans.add(sum / (double) size); 19 | } 20 | 21 | return ans; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Problem154.java: -------------------------------------------------------------------------------- 1 | //find first missing positive 2 | class Solution { 3 | public int firstMissingPositive(int[] nums) { 4 | final int n = nums.length; 5 | 6 | // Correct slot: 7 | // nums[i] = i + 1 8 | // nums[i] - 1 = i 9 | // nums[nums[i] - 1] = nums[i] 10 | for (int i = 0; i < n; ++i) 11 | while (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) 12 | swap(nums, i, nums[i] - 1); 13 | 14 | for (int i = 0; i < n; ++i) 15 | if (nums[i] != i + 1) 16 | return i + 1; 17 | 18 | return n + 1; 19 | } 20 | 21 | private void swap(int[] nums, int i, int j) { 22 | final int temp = nums[i]; 23 | nums[i] = nums[j]; 24 | nums[j] = temp; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Problem-227.java: -------------------------------------------------------------------------------- 1 | //simple calculator in java 2 | class Solution { 3 | public: 4 | int calculate(string s) { 5 | stack nums; 6 | stack ops; 7 | 8 | for (int i = 0; i < s.length(); ++i) { 9 | const char c = s[i]; 10 | if (isdigit(c)) { 11 | int num = c - '0'; 12 | while (i + 1 < s.length() && isdigit(s[i + 1])) { 13 | num = num * 10 + (s[i + 1] - '0'); 14 | ++i; 15 | } 16 | nums.push(num); 17 | } else if (c == '+' || c == '-' || c == '*' || c == '/') { 18 | while (!ops.empty() && compare(ops.top(), c)) 19 | nums.push(calculate(pop(ops), pop(nums), pop(nums))); 20 | ops.push(c); 21 | } 22 | } 23 | 24 | while (!ops.empty()) 25 | nums.push(calculate(pop(ops), pop(nums), pop(nums))); 26 | 27 | return nums.top(); 28 | } 29 | 30 | private: 31 | int calculate(char op, int b, int a) { 32 | switch (op) { 33 | case '+': 34 | return a + b; 35 | case '-': 36 | return a - b; 37 | case '*': 38 | return a * b; 39 | case '/': 40 | return a / b; 41 | } 42 | throw; 43 | } 44 | 45 | // Returns true if priority(op1) >= priority(op2). 46 | bool compare(char op1, char op2) { 47 | return op1 == '*' || op1 == '/' || op2 == '+' || op2 == '-'; 48 | } 49 | 50 | char pop(stack& ops) { 51 | const char op = ops.top(); 52 | ops.pop(); 53 | return op; 54 | } 55 | 56 | int pop(stack& nums) { 57 | const int num = nums.top(); 58 | nums.pop(); 59 | return num; 60 | } 61 | }; 62 | --------------------------------------------------------------------------------