my solutions


  • 0

    leetcode warmup contest

    • 386
    public class Solution {
        public List<Integer> lexicalOrder(int n) {
            List<Integer> result = new ArrayList<>();
    
            int boundary = Math.min(n, 9);
            for (int j = 1; j <= boundary; j++) {
                result.add(j);
                nextRound(result, j, n);
            }
    
            return result;
        }
        
        public static void nextRound(List<Integer> result, int current, int n) {
            int count = 0;
            while (count < 10 && (current * 10 + count) <= n) {
                result.add(current * 10 + count);
                nextRound(result, current * 10 + count, n);
                count++;
            }
        }
    }
    
    • 387
    public class Solution {
        public int firstUniqChar(String s) {
            HashMap<Character, Integer> cache = new LinkedHashMap<>();
            HashMap<Character, Integer> location = new HashMap<>();
    
            for (int i = 0; i < s.length(); i++) {
                if (cache.get(s.charAt(i)) != null) {
                    cache.put(s.charAt(i), cache.get(s.charAt(i)) + 1);
                } else {
                    cache.put(s.charAt(i), 1);
                    location.put(s.charAt(i), i);
                }
            }
    
            for (Map.Entry<Character, Integer> entry : cache.entrySet()){
                if (entry.getValue().equals(1)) {
                    return location.get(entry.getKey());
                }
            }
    
            return -1;
        }
    }
    
    • 388
    public class Solution {
        public static int containsNTab(String line) {
            int i = 0;
            while (line.charAt(i) == '\t') {
                i++;
            }
            return i;
        }
    
        public int lengthLongestPath(String input) {
            Stack<String> store = new Stack<>();
            int max_length = 0; // length counts end with file
            int tmp_length = 0; // length counts end with dir
    
            int current_depth = 0;
            for (String line: input.split("\\n")){
                int tmp_depth = containsNTab(line);
                String tmp_name = line.substring(tmp_depth, line.length());
    
                if (tmp_depth <= current_depth && !store.isEmpty()){
                    for (int k = 0; k <= current_depth - tmp_depth; k++) {
                        String pop = store.pop();
                        tmp_length -= pop.length();
                        if (!pop.contains(".")) tmp_length--;
                    }
                }
    
                store.push(tmp_name);
                tmp_length += tmp_name.length();
                if (tmp_name.contains(".")) {
                    if (tmp_length > max_length) max_length = tmp_length;
                } else {
                    tmp_length += 1;
                }
    
                current_depth = tmp_depth;
            }
    
            return max_length;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.