高盛

  1. 学生会主席考前po答案, 你怎么想的,该怎么办.
  2. 如何找一个跟你不同的人的共同点.
  3. 你上课一个做的project 队友要走 你该怎么办
  4. 你的专业怎么选的,你现在如何看
  1. First Unique Character in a String
class Solution {
    public int firstUniqChar(String s) {
        int[] letters = new int[26];
        for(int i=0; i
  1. Climbing Stairs
class Solution {
    public int climbStairs(int n) {
        if(n==1) return 1; 
        if(n==2) return 2;
        int pre1 = 1;
        int pre2 = 2;
        int res = 0;
        for(int i=2; i
  1. Valid Anagram
class Solution {
    public boolean isAnagram(String s, String t) {
        if(s==null||t==null||s.length()!=t.length()) return false;
        int[] fre = new int[26];
        char[] ss = s.toCharArray();
        char[] tt = t.toCharArray();
        for(int i=0; i
高盛_第1张图片

如果包含其他字符和空格

class Solution {
    public static boolean isAnagram(String s, String t) {
      // to lower case
      s = s.toLowerCase();
      t = t.toLowerCase();
      // to char array
      char[] ss = s.toCharArray();
      char[] tt = t.toCharArray();
      // fre array
      int[] fre = new int[256];
      for(char c : ss){
        if(c>='1'&&c<='9'||c>='a'&&c<='z'){
          fre[c]++;
        }
      }
      for(char c : tt){
        if(c>='1'&&c<='9'||c>='a'&&c<='z'){
          fre[c]--;
        }
      }
      for(int i : fre){
        if(i!=0) return false;
      }
      return true;
  }
  public static void main(String[] args) {
    System.out.println(isAnagram("s.,%%0t$s#t@", "0 tt@!ss#"));
    System.out.println(isAnagram("sa", "ss"));
  }
}
高盛_第2张图片
class Solution {
  // time: 2n   space: n
  public static int countPairs(int k, int[] pairs) {
    if(pairs==null) return 0;
    // element, frequence
    Map map = new HashMap<>();
    int count = 0;
    for(int i : pairs) {
      map.put(i, map.getOrDefault(i, 0)+1);
    }
    for(int i : pairs){
      if(map.get(k-i)!=null) count+=map.get(k-i);
      // if i + i = k
      if(k%2==0 && i==k/2) count--;
    }
    return count / 2;
  }
  public static void main(String[] args) {
    int[] pairs = new int[] {2, 2, 5, 7, 3, 3, 1, 1};
    System.out.print(countPairs(4, pairs));
    int[] pairs2 = new int[] {};
    System.out.print(countPairs(5, pairs2));
    int[] pairs3 = null;
    System.out.print(countPairs(5, pairs3));
  }
}

找数组中第二小的元素

class Solution {
  // time: n  space: 1
  public static int secLargest(int[] nums) {
    int first=Integer.MIN_VALUE;
    int second=Integer.MIN_VALUE;
    for(int i : nums) {
      if(i>first){
        second = first;
        first = i;
      }
      else if(i>second && i!=first) second = i;
    }
    if(second==Integer.MIN_VALUE) return 0; // no second largest
    return second; 
  }

  public static void main(String[] args) {
    int[] nums = new int[] {2, 2, 5, 7, 3, 3, 1, 1};
    System.out.print(secLargest(pairs));
  }
}

给两个数组求点乘(dot product)

class Solution {
  // time: n  space: 1
  public static int dotProduct(int[] nums1, int[] nums2) {
    int sum = 0;
    if(nums1==null||nums2==null||nums1.length!=nums2.length) return sum;
    for(int i=0; i
  1. Reverse String
class Solution {
    public String reverseString(String s) {
        char[] ss = s.toCharArray();
        int p = 0;
        int q = ss.length-1;
        while(p

给一个字符串s,找出所有Unique的长度为k的substring,并排序。
比如caaab,k = 2,返回 aa, ab, ca. 不重复子串.
输入是一个string和一个substr长度,返回String[]所有unique substrs.

public class tyr {
  public static String[] subString(String s, int k) {
    if(s==null||s.length() set = new TreeSet<>();
    for(int i=0; i<=s.length()-k;i++) {
      set.add(s.substring(i, i+k));
    }
    String[] res = new String[set.size()];
    int i=0;
    for (String string : set) {
      res[i]=string;
      i++;
    }
    return res;
  }

  public static void main(String[] args) {
    String[] ss = subString("sabb", 3);
    for (String s : ss) {
      System.out.println(s);
    }
  }
}

给一个数组,每一项是学生名字和分数,学生名字可能有重复,找出所有考试平均分最高的分数。比如 [['bob', '88'], ['ted', '100'], ['ted', '20']],返回88
第二题一开始有个case没过,有点tricky,因为分数可能有负数,然后负数需要取floor,所以不能直接用int作除法,要double做除法,然后取floor以后再转成Int. 比如 -123 / 10 = -12,但应该要-13

  • Best Average Grade:input是String[][],两列,第一列是名字,第二列是分数。要计算每一个人的平均分数,返回最高的分数。注意要用double。
public class tyr {
  public static int bestAverageGrade(String[][] s) {
    Map map = new HashMap<>();
    // fre, total points
    for (String[] row : s) {
      if (map.containsKey(row[0])) {
        int[] grad = map.get(row[0]);
        grad[0] += 1;
        grad[1] += Integer.parseInt(row[1]);
        map.put(row[0], grad);
      } else {
        map.put(row[0], new int[]{1, Integer.parseInt(row[1])});
      }
    }
    int max = Integer.MIN_VALUE;
    for (int[] ave : map.values()) {
      int total = ave[1];
      int fre = ave[0];
      if(total>=0) max = Math.max(total/fre, max);
      else {
        Double res = (double)total/fre;
        max = Math.max((int)Math.floor(res), max);
      }
    }
    return max;
  }

  public static void main(String[] args) {
    String[][] ss = new String[][]{{"tod", "0"},{"tod", "0"},{"tod", "0"},
            {"tod", "0"},{"tod", "0"},{"tod", "0"},{"tod", "0"},{"tod",
            "0"},
            {"tod", "-3"},
            {"tod",
            "-120"}, {"anny",
            "-88888"}};
    System.out.println(bestAverageGrade(ss));
  }
}

Compress String(run length encoding): input: aaabbbccccc output: a3b3c5

class Solution {
    public int compress(char[] chars) {
        int anchor = 0, write = 0;
        for (int read = 0; read < chars.length; read++) {
            if (read + 1 == chars.length || chars[read + 1] != chars[read]) {
                chars[write] = chars[anchor];
                write++;
                if (read > anchor) {
                    for (char c: ("" + (read - anchor + 1)).toCharArray()) {
                        chars[write] = c;
                        write++;
                    }
                }
                anchor = read + 1;
            }
        }
        return write;
    }
}

你可能感兴趣的:(高盛)