LeetCode – Longest Absolute File Path (Java)

https://leetcode.com/problems/longest-absolute-file-path/ Java Solution 1 class Node{ int level; int len; public Node(int lev, int len){ this.level = lev; this.len = len; } }   public class Solution { public int lengthLongestPath(String input) { if(input==null||input.length()==0) return 0;   int max=0;   String[] arr = input.split("\n");   Stack<Node> stack = new Stack<Node>();   for(int i=0; i<arr.length; i++){ … Read more

LeetCode – Lexicographical Numbers (Java)

Given an integer n, return 1 – n in lexicographical order. For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000. Java Solution – DFS public List<Integer> lexicalOrder(int n) { int c=0; int t=n; while(t>0){ c++; t=t/10; }   ArrayList<Integer> … Read more

LeetCode – Rearrange String k Distance Apart (Java)

Given a non-empty string str and an integer k, rearrange the string such that the same characters are at least distance k from each other. All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string “”. Example: str = “aabbcc”, k = 3 Result: … Read more

LeetCode – Design Twitter (Java)

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your design should support the following methods: postTweet(userId, tweetId): Compose a new tweet. getNewsFeed(userId): Retrieve the 10 most recent tweet ids in the user’s news feed. … Read more

LeetCode – Count Numbers with Unique Digits (Java)

Given a non-negative integer n, count all numbers with unique digits, x, where x is from 0 to 10^n-1. Java Solution public int countNumbersWithUniqueDigits(int n) { int[] arr = new int[n+1]; arr[0]=1; // x can be 0   for(int i=1; i<=n; i++){ arr[i]=9; for(int j=9; j>=9-i+2; j–){ arr[i] *= j; } }   int result … Read more

LeetCode – Nested List Weight Sum II (Java)

Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list — whose elements may also be integers or other lists. Different from the previous question where weight is increasing from root to leaf, now the weight is … Read more

LeetCode – Plus One Linked List (Java)

Given a non-negative number represented as a singly linked list of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. Example: Input: 1->2->3 Output: 1->2->4 Java Solution public ListNode plusOne(ListNode head) { ListNode h2 = reverse(head);   ListNode p=h2;   while(p!=null){ … Read more

LeetCode – Design Phone Directory (Java)

Design a Phone Directory which supports the following operations: get: Provide a number which is not assigned to anyone. check: Check if a number is available or not. release: Recycle or release a number. Java Solution 1 public class PhoneDirectory { int max; HashSet<Integer> set; LinkedList<Integer> queue;   /** Initialize your data structure here @param … Read more

LeetCode – Shuffle an Array (Java)

Shuffle a set of numbers without duplicates. Java Solution How we make sure each the probability of each element get shuffled is very similar to the streaming random problem. The algorithm is straightforward to understand, but the question is why it works. To have a working shuffle algorithm, every element in the array results in … Read more