zoukankan      html  css  js  c++  java
  • 高盛oa

    一道题根本不会,抄答案过了。一道自己写,莫名其妙出现了不会的bug。最后交了暴力解,过了5/7。估计要跪。

    总结:

    缺点:做过的不熟练。没做过的题不会。一个陌生的小bug也de不出来。

    措施:多总结还是有好处的。坚持刷tag题有必要,绝对能慢慢增强实力,很重要。

    指定方向的dp: 改变i - 1 j - 1就行了 dp[j] = Math.min(dp[i+1][j-1], dp[i+1][j], dp[i+1][j+1]) + matrix[j];

    加下一行不行,不能处理第一行的时候。该成每次加上一行,这样第一行就可以被初始化了。lc 64。

    public class Solution {
        /*
         * @param grid: a list of lists of integers
         * @return: An integer, minimizes the sum of all numbers along its path
         */
        public int minPathSum(int[][] grid) {
            //corner case
            if (grid == null || grid.length == 0) {
                return -1;
            }
            if (grid[0] == null || grid[0].length == 0) {
                return -1;
            }
            //intialization
            int m = grid.length;
            int n = grid[0].length;
            int[][] f = new int[m][n];
            f[0][0] = grid[0][0];//
            for (int i = 1; i < m; i++) {
                f[i][0] = f[i - 1][0] + grid[i][0];
            }
            for (int j = 1; j < n; j++) {
                f[0][j] = f[0][j - 1] + grid[0][j];
            }
            //top down
            for (int i = 1; i < m; i++) {
                for (int j = 1; j < n; j++) {
                    f[i][j] = grid[i][j] + Math.min(f[i - 1][j],f[i][j - 1]);
                }
            }
            //result
            return f[m - 1][n - 1];
        }
    }

    [学生迟到问题]

    Q2(MEDIUM): greatest lateness,不知道地里有没有这题的面经,给一个2d-vector的string,每一行格式是 [日期, 姓名, 开课时间, 到课时间],输出整个输入里累计“相对”迟到时间最长的同学的姓名, 如果学生早于开课时间absolute lateness算0,相对迟到时间是说同学当天绝对迟到时间和当天所有人绝对迟到时间的平均值的差,如果小于的话就算0,如果结果一样按姓名alphabetical输出

    不知道存两个哈希表的好处

    map.keySet().size()用于找size

    每次i都要重新存声明为int

    自己造轮子,写了半天,终于貌似好像过了:

    // package whatever; // don't place package name!
    
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    
    
    class Solution {
      //method, need parameter, there is return 
        public String latestStudent(String[][] students) {
            //initialization
          HashMap<String, Integer> dayToTime = new HashMap<String, Integer>();
          HashMap<String, Integer> nameToTime = new HashMap<String, Integer>();
          HashMap<String, Integer> dayToAverageLateTime = new HashMap<String, Integer>();
          int count = 0;
          
          //put time to map
          for (int i = 1; i < students.length; i++) {
            if (students[i][0] == students[i - 1][0]) {
              count++;
            }else {
              int averageLateTime = dayToTime.get(students[i - 1][0]) / count;
              count = 0;
              dayToAverageLateTime.put(students[i - 1][0], averageLateTime);
              break;
            }
            int time = Integer.valueOf(students[i][3]) - Integer.valueOf(students[i][2]);
            if (time > 0) {
              dayToTime.put(students[i][0], time);
            }else
              dayToTime.put(students[i][0], 0); 
          }
          
          int countOfAverage = dayToAverageLateTime.keySet().size();
          int max = 0;
          
          //put student into the map, and find the max time
            for (int i = 0; i < students.length; i++) {
              int averageLateTime = dayToAverageLateTime.getOrDefault(students[i][0], 0);
              int absoluteTime = Integer.valueOf(students[i][3]) - Integer.valueOf(students[i][2]);
              if ((absoluteTime - averageLateTime)> 0) {
                nameToTime.put(students[i][1], absoluteTime - averageLateTime);
                max = Math.max(max, absoluteTime - averageLateTime);
              }
            }
          
          //find the max student
          int countOfStudents = nameToTime.keySet().size();
          for (String name : nameToTime.keySet()) {
            if (nameToTime.get(name) == max)
              System.out.println("max = " + max);
              return name;
          }
          return null;
        }
    }
    
    class MyCode {
      public static void main (String[] args) {
        String[][] students = {{"1", "A", "540", "570"}, {"1", "B", "540", "543"}, {"1", "C", "540", "530"}, {"2", "A", "540", "580"}, {"2", "B", "540", "580"}, {"2", "C", "540", "595"}};
        Solution answer = new Solution();
        System.out.println("name = " + answer.latestStudent(students));
      }
    }
    View Code

    右端对齐:sb.append(" ");

    // package whatever; // don't place package name!
    
    import java.io.*;
    
    class MyCode {
      public static void main (String[] args) {
        String str1 = "abc";
        String str2 = "def";
        String str3 = str1 + "
    " + str2;
        System.out.println(str3);
      }
    }

    是把前面的删掉

    // package whatever; // don't place package name!
    
    import java.io.*;
    
    class MyCode {
      public static void main (String[] args) {
        String str1 = "abc";
        String str2 = "def";
        String str3 = "gji";
        String str4 = "jkl";
        String str = str1 + "
    " + str2 + "
    " + str3 + "
    " + str4;
        System.out.println(str);
      }
    }
    转大写要用.toUpperCase()

    学生填志愿问题:可以用 comparable结构
    class Student implements Comparable<Student>{
            int score;
            int index;
            public Student(int score, int index) {
                this.score = score;
                this.index = index;
            }
            @Override
            public int compareTo(Student o) {
                if(this.score - o.score == 0) {
                    return this.index - o.index;
                }
                // TODO Auto-generated method stub
                return this.score-o.score;
            }
        }
    
    

    慈善数组每次分钱给最少的:

    用pq,然后也可以用比较结构

    class Orgnization implements Comparable<Orgnization>{
            String name;
            double amount;
             
            public Orgnization(String name) {
                this.name = name;
                this.amount = 0;
            }
             
            @Override
            public int compareTo(Orgnization o) {
                if(this.amount - o.amount == 0) {
                    return this.name.compareTo(o.name);
                }
                return (int) (this.amount - o.amount);
            }

    有符号的算式版reverse words in a string:用一个变量isnumber来控制,true就append

    public class ReverseAlgebraExpression {
     
        public static void main(String[] args) {
                String test1 = "1*2.4+9.6-23.89";
                String answer1 = "23.89-9.6+2.4*1";
                String test2 = "1*2.4+-9.6-23.89";
                String answer2 = "23.89--9.6+2.4*1";
                String test3 = "-1*2.4+-9.6-23.89";
                String answer3 = "23.89--9.6+2.4*-1";
                test1(test1, answer1, 1);
                test1(test2, answer2, 2);
                test1(test3, answer3, 3);
                 
    //          String t1 = "My keyboard is broken!";
    //          String a1 = "My keyboRD IS BROKEN!";
    //          String t2 = ""Baa, Baa!" said the sheep";
    //          String a2 = ""B, B!" sID THE SHEEP";
    //          test2(t1, a1, 1);
    //          test2(t2, a2, 2);
                 
        }
         
        public static void test1(String a, String b, int i) {
            String output = null;
            output = rae(a);
            if(b.equals(output)) {
                System.out.println("Passed case "+i);
            } else {
                System.out.println("Incorrect "+i+": " + output);
            }
        }
         
         
        public static void test2(String a, String b, int i) {
            String output = null;
            output = capLock(a);
            if(b.equals(output)) {
                System.out.println("Passed case "+i);
            } else {
                System.out.println("Incorrect "+i+": " + output);
            }
        }
         
        public static String rae(String expression) {
            boolean isNumber = true;
            StringBuilder output = new StringBuilder();
            if(expression.length() == 0) return output.toString();
            StringBuilder buffer = new StringBuilder();
            buffer.append(expression.charAt(0));
            for(int i = 1; i<expression.length(); i++) {
                char cur = expression.charAt(i);
                if(isNumber && (Character.isDigit(cur) || cur == '.')) {
                    buffer.append(cur);
                    continue;
                }
                if(isNumber && (cur=='+' || cur == '-' || cur == '*' || cur == '/')) {
                    output.insert(0, buffer);
                    buffer = new StringBuilder();
                    buffer.append(cur);
                    isNumber = false;
                    continue;
                }
                if(!isNumber && (cur=='+' || cur == '-' || cur == '*' || cur == '/')) {
                    output.insert(0, buffer);
                    buffer = new StringBuilder();
                    buffer.append(cur);
                    isNumber = true;
                    continue;
                }
                if(!isNumber && Character.isDigit(cur)) {
                    output.insert(0, buffer);
                    buffer = new StringBuilder();
                    buffer.append(cur);
                    isNumber = true;
                }
            }
            output.insert(0, buffer);
            return output.toString();
        }
         
        public static String capLock(String input) {
            boolean capLock = false;
            StringBuilder output = new StringBuilder();
            for(char c: input.toCharArray()) {
                if(c == 'a' || c == 'A') {
                    capLock = !capLock;
                    continue;
                }
                if(capLock) {
                    output.append(Character.toUpperCase(c));
                } else {
                    output.append(c);
                }
            }
            return output.toString();
        }
    }

     4 Whole Minute Dilemma有图
    给一串歌曲的时间,求里面有多少 pair 可以相加变成整分钟
    例如[40, 20, 60] 结果就是 1,必须是 pair,60 自己不能算一个. Waral 博客有更多文章,
    我的理解是同一首歌不能重复组成 pair 的
    思路是把 时间%60 和 出现的次数 作为键值对存进到一个 map 里面 然后遍历 map 找互补的:

    就是twosum的hashmap的变形

    世界杯概率: 保留两位小数

    String a = String.format("%.2f", result) 一般只能长小数转为短小数

     pq的写法:用comparator

    PriorityQueue<Map.Entry<Character, Integer>> pq = new PriorityQueue(
                new Comparator<Map.Entry<Character, Integer>>() {
                    public int compare(Map.Entry<Character, Integer> a, Map.Entry<Character, Integer> b) {
                        return b.getValue() - a.getValue();
                    }
                } 
            );

    getOrDefault(Object,V)允许调用者在代码语句中规定获得在map中符合提供的键的值,否则在没有找到提供的键的匹配项的时候返回一个“默认值”。

     

     

  • 相关阅读:
    Deployment of VC2008 apps without installing anything
    用MT.exe将exe中的manifest文件提取出来和将manifest文件放入exe中
    Golang快速入门
    8个优质的编程学习网站
    免费学编程!10个全球顶尖的编程在线自学网站
    7个在线学习C++编程的最佳途径
    为什么多数游戏服务端是用 C++ 来写
    学习游戏服务器开发必看,C++游戏服务器开发常用工具介绍
    Ambari——大数据平台的搭建利器(一)
    Python爬虫项目整理
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9568738.html
Copyright © 2011-2022 走看看