How to insert the “logical and” (&) to MySQL database?
You may try to escape the “&” character, but it doesn’t work.
Given the URL of an image, you can download it by using the following Java code. It download the image and save the image using the original file name. The key is to use InputStream to read image and use OutputStream to write to a file. public static void saveImage(String imageUrl) throws IOException { URL … Read more
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Analysis The following cases should be considered for this problem: 1. null or empty string 2. white spaces 3. +/- … Read more
Problem: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) … Read more
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. Analysis The key to solve this problem is defining a Comparator first to sort the arraylist of Intevals. Java Solution public List<Interval> merge(List<Interval> intervals) { if(intervals == null || intervals.size()<=1){ return intervals; } Collections.sort(intervals, Comparator.comparing((Interval itl)->itl.start)); List<Interval> result … Read more
Implement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch(“aa”,”a”) return false isMatch(“aa”,”aa”) return true isMatch(“aaa”,”aa”) … Read more
How to insert the “logical and” (&) to MySQL database?
You may try to escape the “&” character, but it doesn’t work.
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Java Solution This problem can be converted to the problem of finding kth element, k is (A’s length + B’ Length)/2. If any of … Read more
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. Java Solution 1 – DFS HashMap<Node, Node> map = new HashMap<>(); public Node cloneGraph(Node node) { map.put(node, new Node(node.val, new ArrayList<>())); for(Node neighbor: node.neighbors){ if(map.containsKey(neighbor)){ map.get(node).neighbors.add(map.get(neighbor)); }else{ map.get(node).neighbors.add(cloneGraph(neighbor)); } } return map.get(node); }HashMap<Node, Node> map … Read more
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that only one letter can be changed at a time and each intermediate word must exist in the dictionary. For example, given: start = “hit” end = “cog” dict = [“hot”,”dot”,”dog”,”lot”,”log”] One shortest transformation … Read more
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. For example: [“2”, “1”, “+”, “3”, “*”] -> ((2 + 1) * 3) -> 9 [“4”, “13”, “5”, “/”, “+”] -> (4 + (13 / 5)) -> 6 … Read more
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. Java Solution 1 We can solve this problem by doing the following steps: copy every node, i.e., duplicate every node, and insert it … Read more
LeetCode – Reverse Integer: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 1. Naive Method We can convert the integer to a string/char array, reverse the order, and convert the string/char array back to an integer. However, this will require extra space for the string. It … Read more
Given a linked list, determine if it has a cycle in it. Analysis If we have 2 pointers – fast and slow. It is guaranteed that the fast one will meet the slow one if there exists a circle. The problem can be demonstrated in the following diagram: Java Solution public class Solution { public … Read more
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = “leetcode”, dict = [“leet”, “code”]. Return true because “leetcode” can be segmented as “leet code”. 1. Naive Approach This problem can be solve by … Read more
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 … Read more