摘要:
Ways to complete Kraken Problem
Kraken is m*n grids on a rectangular board. From the top left to reach the bottom right corner while moving one grid at a time in either the down, right or down-right diagonal directions
Solutionpublic class Solution { public int krakenCount(int m, int n) { if (m == 0 || n == 0) return 0; if (m == 1 || n == 1) return 1; int[][] dp = new int[m][n]; for (int i = 0; i < m; i++) dp[i][0] = 1; for (int j = 0; j < n; j++) dp[0][j] = 1; for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { dp[i][j] = dp[i-1][j] + dp[i][j-1] + dp[i-1][j-1]; } } return dp[m-1][n-1]; } }Minimum Genetic Mutation Solution
Backtracking
public class Solution { public int minMutation(String start, String end, String[] bank) { Queueq = new LinkedList<>(); q.offer(start); int count = 0; char[] genes = new char[]{"A","C","G","T"}; Set set = new HashSet<>(); for (String s: bank) set.add(s); while (!q.isEmpty()) { int size = q.size(); for (int i = 0; i < size; i++) { String pre = q.poll(); for (int j = 0; j < pre.length(); j++) { for (char gene: genes) { StringBuilder sb = new StringBuilder(pre); sb.setCharAt(j, gene); String cur = sb.toString(); if (end.equals(cur) && set.contains(end)) return count+1; else if (set.contains(cur)) { set.remove(cur); q.offer(cur); } } } } count++; } return -1; } }
BFS
public class Solution { public int minMutation(String start, String end, String[] bank) { if (start == null || end == null || bank == null || bank.length == 0 || start.length() != end.length()) return -1; return helper(start, end, bank, new ArrayListLongest Phrases in a Tweet Maximum Size Subarray Sum equals or less than K Using Queue(), 0); } public int helper(String start, String end, String[] bank, List path, int count) { int min = -1; if (start.equals(end)) return 0; if (count >= end.length()) return min; for (String gene: bank) { if (!path.contains(gene) && isNext(start, gene)) { path.add(gene); int res = helper(gene, end, bank, path, count++); if (res != -1) min = Math.min(Integer.MAX_VALUE, res+1); path.remove(gene); } } return min; } public boolean isNext(String s1, String s2) { if (s1 == null || s2 == null || s1.length() != s2.length()) return false; int diff = 0; for (int i = 0; i < s1.length(); i++) { if (s1.charAt(i) != s2.charAt(i) && ++diff > 1) return false; } return true; } }
public class Solution { public int maxSubArrayLen(int[] nums, int k) { if (nums == null || nums.length == 0) return 0; QueueBrute Forceq = new LinkedList<>(); int sum = 0, max = 0; for (int num: nums) { if (sum+num <= k) { q.offer(num); sum+=num; } else { while (sum+num > k) { max = Math.max(max, q.size()); int pre = q.poll(); sum-=pre; } sum+=num; q.offer(num); max = Math.max(max, q.size()); } } max = Math.max(max, q.size()); return max; } }
public class Solution { public int maxSubArrayLen(int[] nums, int k) { if (nums == null || nums.length == 0) return 0; int len = nums.length; int[] sum = new int[len+1]; sum[0] = 0; for (int i = 1; i <= len; i++) { sum[i] = sum[i-1]+nums[i-1]; } int max = 0; for (int i = 0; i < len; i++) { //if (sum[i] <= k) max = Math.max(max, i); for (int j = i+1; j <= len; j++) { if (sum[j]-sum[i] <= k) max = Math.max(max, j-i); } } return max; } }Information Masking Example
(111)222-3456 --> --3456
+123(444)555-6789 --> +--*-6789
333 444 5678 --> --5678
(333)444-5678 --> --5678
jackandrose@gmail .com --> je@gmail .com
Solutionpublic class Solution { public static String emailMask(String email) { StringBuilder sb = new StringBuilder(); sb.append("E:"); sb.append(email.charAt(0)); sb.append("*****"); int lastIndex = email.lastIndexOf("@")-1; sb.append(email.substring(lastIndex)); return sb.toString(); } public static String phoneMask(String phone) { StringBuilder sb = new StringBuilder(); sb.append("P:"); boolean hasCode = false; if (phone.charAt(0) == "+") { hasCode = true; phone = phone.substring(1); } if (phone.charAt(0) == "(") phone = phone.substring(1); String delimiters = "D"; String[] nums = phone.trim().split(delimiter); if (hasCode) sb.append("+"); int n = nums.length; for (int i = 0; i < n-1; i++) { int len = nums[i].length(); for (int i = 0; i < len; i++) sb.append("*"); sb.append("-"); } sb.append(nums[n-1]); return sb.toString(); } public static void main(String args[] ) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input; while ((input = br.readLine()) != null){ //String input = br.readLine(); String[] inputs = input.trim().split(":"); if (inputs[0].trim().equals("E")) System.out.println(emailMask(inputs[1].trim())); else if (inputs[0].trim().equals("P")) System.out.println(phoneMask(inputs[1].trim())); } br.close(); } }First Unique Character in a String
public class Solution { public int firstUniqChar(String s) { if (s == null || s.length() == 0) return -1; int[] dict = new int[26]; for (int i = 0; i < s.length(); i++) { dict[s.charAt(i)-"a"]++; } for (int i = 0; i < s.length(); i++) { if (dict[s.charAt(i)-"a"] == 1) return i; } return -1; } }Tweet Recommendation Hacking Time Apache Log Success Rates Evaluate Expression Tree Timeseries Data Aggregation SQL Student/Department
SELECT d.DEPT_NAME, COUNT(s.STUDENT_ID) as STUDENT_COUNT FROM Departments (AS) d LEFT JOIN Students (AS) s on d.DEPT_ID = s.DEPT_ID GROUP BY d.DEPT_ID ORDER BY STUDENT_COUNT DESC, d.DEPT_NAME;ORDERS
SELECT o.customerNumber AS customer FROM ORDERS AS o GROUP BY customerNumber ORDER BY count(orderNumber) DESC limit 1;
OR
SELECT customerNumber FROM ORDERS WHERE ROWNUM <= 1 GROUP BY customerNumber ORDER BY COUNT(orderNumber) DESC;Investments in 2012
SELECT ROUND(SUM(TIV_2012), 2) FROM Insurance WHERE Insurance.PID IN (SELECT PID FROM Insurance I1, Insurance I2 WHERE I1.TIV_2011 = I2.TIV_2011 AND I1.PID != I2.PID) AND Insurance.PID NOT IN (SELECT I1.PID FROM Insurance I1, Insurance I2 WHERE I1.LAT = I2.LAT AND I1.LON = I2.LON AND I1.PID != I2.PID);Employee/Department
SELECT d.Name, COUNT(e.ID) AS ID_COUNT FROM Department (AS) d LEFT JOIN Employee (AS) e ON(WHERE) e.DEPT_ID = d.DEPT_ID GROUP BY d.Name ORDER BY ID_COUNT DESC, NAMEParent/Child/Tree
SELECT Id, CASE WHEN M IS NULL THEN "Leaf" WHEN P_id IS NULL THEN "Root" ELSE "Inner" END AS TypeNode FROM ( SELECT DISTINCT hijo.*, padre.P_id AS M FROM Tree hijo LEFT JOIN Tree padre ON (hijo.Id = padre.P_id) ) ORDER BY Id;
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/69783.html
摘要:是一個解釋型語言上明確的說,是一個輕量級的解釋型的面向對象的將函數視為一級公民的語言。全局代碼在執行的時候,先是變量提升,在全局作用域內添加屬性,然后是函數以函數聲明創建的函數提升,再是代碼執行。那么,很顯然,閉包其實就是一個函數。 JavaScript是一個解釋型語言 MDN上明確的說,JavaScript是一個輕量級的、解釋型的、面向對象的、將函數視為一級公民的語言。 那么,既然j...
摘要:詞法作用域是一種靜態作用域這個例子的結果按靜態作用域來分析執行函數,先從函數內部查找是否有局部變量,如果沒有,就根據書寫的位置,查找上面一層的代碼,也就是等于,所以結果會打印。靜態作用域,決定的是作用域鏈的順序。 博客原文地址:https://finget.github.io/2018/03/01/javascriptPrecompile/看不明白的地方歡迎提問,有理解的不對的地方希望...
摘要:引擎會在代碼執行前進行詞法分析,所以事實上,運行分為此法分析和執行兩個階段。詞法作用域所謂詞法作用域是說,其作用域為在定義時詞法分析時就確定下來的,而并非在執行時確定。 先來看個常見的面試題如下: var a = 10; function test(){ alert(a); //undefined var a = 20; alert(a); //20 } te...
閱讀 2618·2021-11-12 10:36
閱讀 2257·2021-08-23 09:47
閱讀 1675·2019-08-30 15:44
閱讀 1400·2019-08-30 14:10
閱讀 2241·2019-08-29 16:52
閱讀 2333·2019-08-29 16:40
閱讀 1582·2019-08-29 16:17
閱讀 2407·2019-08-26 13:21