From Wiki,
Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class.
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn’t one, return 0 instead. Note: The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range. Example 1: Given nums = [1, -1, 5, -2, 3], … Read more
Given two sparse matrices A and B, return the result of AB. You may assume that A’s column number is equal to B’s row number. 1. Naive Method We can implement Sum(A_ik * B_kj) -> C_ij as a naive solution. public int[][] multiply(int[][] A, int[][] B) { //validity check int[][] C = new int[A.length][B[0].length]; … Read more
A large portion of Java software development is using APIs from various libraries. From 10,000 open source Java projects, I extracted the frequency of API classes. The classes are either from Java standard library or from third-party libraries. Each class is counted once for each project. The list below shows the top 100.
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, … For example, given 3, output should be 3. Given 11, output should be 0. (The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, … is a … Read more
Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent. Example: Input: n = 1 Return: [“1:00”, “2:00”, “4:00”, “8:00”, “0:01”, “0:02”, “0:04”, “0:08”, “0:16”, “0:32”] Accepted Java Solution public List<String> readBinaryWatch(int num) { List<String> result = new ArrayList<String>(); for(int i=0; … Read more
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. Note: The length of num is less than 10002 and will be ≥ k. The given num does not contain any leading zero. Example 1: Input: num = “1432219”, k = … Read more
Given a 2d matrix, find a path from the top left corner to bottom right corner. Assume there exists at least one path, and you only need to find one valid path. You can move up, right, down, left at any position. For example, given [1, 0, 0, 0, 0] [1, 0, 1, 1, 1] … Read more
Given an encoded string, return it’s decoded string. For example, given “3[a2[b]]”, return “abbabbabb”. https://leetcode.com/problems/decode-string/ Java Solution The key to solve this problem is convert the string to a structured data structure and recursively form the return string. class Solution { public String decodeString(String s) { Stack<Exp> stack = new Stack<>(); Exp e = … Read more
Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times. Example 1: Input: s = “aaabb”, k = 3 Output: 3 The longest substring is “aaa”, as ‘a’ is repeated 3 times. Java Solution This problem … Read more
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, followed by its unicode code. For n-bytes character, the first n-bits are all one’s, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being … Read more
Design a Snake game that is played on a device with screen size = width x height. Play the game online if you are not familiar with the game. The snake is initially positioned at the top left corner (0,0) with length = 1 unit. You are given a list of food’s positions in row-column … Read more
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
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
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