资讯专栏INFORMATION COLUMN

AO Rettiwt

Zoom / 2050人阅读

摘要:

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

Solution

</>复制代码

  1. public class Solution {
  2. public int krakenCount(int m, int n) {
  3. if (m == 0 || n == 0) return 0;
  4. if (m == 1 || n == 1) return 1;
  5. int[][] dp = new int[m][n];
  6. for (int i = 0; i < m; i++) dp[i][0] = 1;
  7. for (int j = 0; j < n; j++) dp[0][j] = 1;
  8. for (int i = 1; i < m; i++) {
  9. for (int j = 1; j < n; j++) {
  10. dp[i][j] = dp[i-1][j] + dp[i][j-1] + dp[i-1][j-1];
  11. }
  12. }
  13. return dp[m-1][n-1];
  14. }
  15. }
Minimum Genetic Mutation Solution

Backtracking

</>复制代码

  1. public class Solution {
  2. public int minMutation(String start, String end, String[] bank) {
  3. Queue q = new LinkedList<>();
  4. q.offer(start);
  5. int count = 0;
  6. char[] genes = new char[]{"A","C","G","T"};
  7. Set set = new HashSet<>();
  8. for (String s: bank) set.add(s);
  9. while (!q.isEmpty()) {
  10. int size = q.size();
  11. for (int i = 0; i < size; i++) {
  12. String pre = q.poll();
  13. for (int j = 0; j < pre.length(); j++) {
  14. for (char gene: genes) {
  15. StringBuilder sb = new StringBuilder(pre);
  16. sb.setCharAt(j, gene);
  17. String cur = sb.toString();
  18. if (end.equals(cur) && set.contains(end)) return count+1;
  19. else if (set.contains(cur)) {
  20. set.remove(cur);
  21. q.offer(cur);
  22. }
  23. }
  24. }
  25. }
  26. count++;
  27. }
  28. return -1;
  29. }
  30. }

BFS

</>复制代码

  1. public class Solution {
  2. public int minMutation(String start, String end, String[] bank) {
  3. if (start == null || end == null || bank == null || bank.length == 0 || start.length() != end.length()) return -1;
  4. return helper(start, end, bank, new ArrayList(), 0);
  5. }
  6. public int helper(String start, String end, String[] bank, List path, int count) {
  7. int min = -1;
  8. if (start.equals(end)) return 0;
  9. if (count >= end.length()) return min;
  10. for (String gene: bank) {
  11. if (!path.contains(gene) && isNext(start, gene)) {
  12. path.add(gene);
  13. int res = helper(gene, end, bank, path, count++);
  14. if (res != -1) min = Math.min(Integer.MAX_VALUE, res+1);
  15. path.remove(gene);
  16. }
  17. }
  18. return min;
  19. }
  20. public boolean isNext(String s1, String s2) {
  21. if (s1 == null || s2 == null || s1.length() != s2.length()) return false;
  22. int diff = 0;
  23. for (int i = 0; i < s1.length(); i++) {
  24. if (s1.charAt(i) != s2.charAt(i) && ++diff > 1) return false;
  25. }
  26. return true;
  27. }
  28. }
Longest Phrases in a Tweet Maximum Size Subarray Sum equals or less than K Using Queue

</>复制代码

  1. public class Solution {
  2. public int maxSubArrayLen(int[] nums, int k) {
  3. if (nums == null || nums.length == 0) return 0;
  4. Queue q = new LinkedList<>();
  5. int sum = 0, max = 0;
  6. for (int num: nums) {
  7. if (sum+num <= k) {
  8. q.offer(num);
  9. sum+=num;
  10. }
  11. else {
  12. while (sum+num > k) {
  13. max = Math.max(max, q.size());
  14. int pre = q.poll();
  15. sum-=pre;
  16. }
  17. sum+=num;
  18. q.offer(num);
  19. max = Math.max(max, q.size());
  20. }
  21. }
  22. max = Math.max(max, q.size());
  23. return max;
  24. }
  25. }
Brute Force

</>复制代码

  1. public class Solution {
  2. public int maxSubArrayLen(int[] nums, int k) {
  3. if (nums == null || nums.length == 0) return 0;
  4. int len = nums.length;
  5. int[] sum = new int[len+1];
  6. sum[0] = 0;
  7. for (int i = 1; i <= len; i++) {
  8. sum[i] = sum[i-1]+nums[i-1];
  9. }
  10. int max = 0;
  11. for (int i = 0; i < len; i++) {
  12. //if (sum[i] <= k) max = Math.max(max, i);
  13. for (int j = i+1; j <= len; j++) {
  14. if (sum[j]-sum[i] <= k) max = Math.max(max, j-i);
  15. }
  16. }
  17. return max;
  18. }
  19. }
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

Solution

</>复制代码

  1. public class Solution {
  2. public static String emailMask(String email) {
  3. StringBuilder sb = new StringBuilder();
  4. sb.append("E:");
  5. sb.append(email.charAt(0));
  6. sb.append("*****");
  7. int lastIndex = email.lastIndexOf("@")-1;
  8. sb.append(email.substring(lastIndex));
  9. return sb.toString();
  10. }
  11. public static String phoneMask(String phone) {
  12. StringBuilder sb = new StringBuilder();
  13. sb.append("P:");
  14. boolean hasCode = false;
  15. if (phone.charAt(0) == "+") {
  16. hasCode = true;
  17. phone = phone.substring(1);
  18. }
  19. if (phone.charAt(0) == "(") phone = phone.substring(1);
  20. String delimiters = "D";
  21. String[] nums = phone.trim().split(delimiter);
  22. if (hasCode) sb.append("+");
  23. int n = nums.length;
  24. for (int i = 0; i < n-1; i++) {
  25. int len = nums[i].length();
  26. for (int i = 0; i < len; i++) sb.append("*");
  27. sb.append("-");
  28. }
  29. sb.append(nums[n-1]);
  30. return sb.toString();
  31. }
  32. public static void main(String args[] ) throws Exception {
  33. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  34. String input;
  35. while ((input = br.readLine()) != null){
  36. //String input = br.readLine();
  37. String[] inputs = input.trim().split(":");
  38. if (inputs[0].trim().equals("E")) System.out.println(emailMask(inputs[1].trim()));
  39. else if (inputs[0].trim().equals("P")) System.out.println(phoneMask(inputs[1].trim()));
  40. }
  41. br.close();
  42. }
  43. }
First Unique Character in a String

</>复制代码

  1. public class Solution {
  2. public int firstUniqChar(String s) {
  3. if (s == null || s.length() == 0) return -1;
  4. int[] dict = new int[26];
  5. for (int i = 0; i < s.length(); i++) {
  6. dict[s.charAt(i)-"a"]++;
  7. }
  8. for (int i = 0; i < s.length(); i++) {
  9. if (dict[s.charAt(i)-"a"] == 1) return i;
  10. }
  11. return -1;
  12. }
  13. }
Tweet Recommendation Hacking Time Apache Log Success Rates Evaluate Expression Tree Timeseries Data Aggregation SQL Student/Department

</>复制代码

  1. SELECT d.DEPT_NAME, COUNT(s.STUDENT_ID) as STUDENT_COUNT
  2. FROM Departments (AS) d
  3. LEFT JOIN Students (AS) s on d.DEPT_ID = s.DEPT_ID
  4. GROUP BY d.DEPT_ID
  5. ORDER BY STUDENT_COUNT DESC, d.DEPT_NAME;
ORDERS

</>复制代码

  1. SELECT o.customerNumber AS customer
  2. FROM ORDERS AS o
  3. GROUP BY customerNumber
  4. ORDER BY count(orderNumber) DESC
  5. limit 1;

OR

</>复制代码

  1. SELECT customerNumber
  2. FROM ORDERS
  3. WHERE ROWNUM <= 1
  4. GROUP BY customerNumber
  5. ORDER BY COUNT(orderNumber) DESC;
Investments in 2012

</>复制代码

  1. SELECT ROUND(SUM(TIV_2012), 2)
  2. FROM Insurance
  3. WHERE Insurance.PID IN
  4. (SELECT PID
  5. FROM Insurance I1, Insurance I2
  6. WHERE I1.TIV_2011 = I2.TIV_2011 AND I1.PID != I2.PID)
  7. AND Insurance.PID NOT IN
  8. (SELECT I1.PID
  9. FROM Insurance I1, Insurance I2
  10. WHERE I1.LAT = I2.LAT AND I1.LON = I2.LON AND I1.PID != I2.PID);
Employee/Department

</>复制代码

  1. SELECT d.Name, COUNT(e.ID) AS ID_COUNT
  2. FROM Department (AS) d
  3. LEFT JOIN Employee (AS) e
  4. ON(WHERE) e.DEPT_ID = d.DEPT_ID
  5. GROUP BY d.Name
  6. ORDER BY ID_COUNT DESC, NAME
Parent/Child/Tree

</>复制代码

  1. SELECT Id, CASE
  2. WHEN M IS NULL THEN "Leaf"
  3. WHEN P_id IS NULL THEN "Root"
  4. ELSE "Inner"
  5. END AS TypeNode
  6. FROM
  7. (
  8. SELECT DISTINCT hijo.*, padre.P_id AS M
  9. FROM Tree hijo
  10. LEFT JOIN Tree padre
  11. ON (hijo.Id = padre.P_id)
  12. )
  13. ORDER BY Id;

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69783.html

相关文章

  • 作用域链

    摘要:作用域链作用域就近原则在写下声明就能确定的,叫做词法作用域词法作用域可以确定是哪个,但不能确定的值关于作用域链,浏览器内部究竟发生了什么例子声明前置调用调用例子声明前置调用调用例子声明前置调用调用例子,,,,,,声明前置调用调用调用 作用域链 作用域:就近原则在写下声明就能确定的,叫做词法作用域 var a = 1 function bar(){ var a = 2 conso...

    zgbgx 评论0 收藏0
  • JavaScript-变量提升(AO、GO)编译、闭包

    摘要:是一个解释型语言上明确的说,是一个轻量级的解释型的面向对象的将函数视为一级公民的语言。全局代码在执行的时候,先是变量提升,在全局作用域内添加属性,然后是函数以函数声明创建的函数提升,再是代码执行。那么,很显然,闭包其实就是一个函数。 JavaScript是一个解释型语言 MDN上明确的说,JavaScript是一个轻量级的、解释型的、面向对象的、将函数视为一级公民的语言。 那么,既然j...

    plokmju88 评论0 收藏0
  • JavaScript的预编译过程与作用域

    摘要:词法作用域是一种静态作用域这个例子的结果按静态作用域来分析执行函数,先从函数内部查找是否有局部变量,如果没有,就根据书写的位置,查找上面一层的代码,也就是等于,所以结果会打印。静态作用域,决定的是作用域链的顺序。 博客原文地址:https://finget.github.io/2018/03/01/javascriptPrecompile/看不明白的地方欢迎提问,有理解的不对的地方希望...

    ziwenxie 评论0 收藏0
  • js 词法分析,词法作用域

    摘要:引擎会在代码执行前进行词法分析,所以事实上,运行分为此法分析和执行两个阶段。词法作用域所谓词法作用域是说,其作用域为在定义时词法分析时就确定下来的,而并非在执行时确定。 先来看个常见的面试题如下: var a = 10; function test(){ alert(a); //undefined var a = 20; alert(a); //20 } te...

    2450184176 评论0 收藏0

发表评论

0条评论

Zoom

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<