万隆的笔记 万隆的笔记
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
  • 数据结构与算法

  • 设计模式

  • 编程方法论

  • 分布式设计与微服务理论

  • Leetcode

    • Question :1 - 100
    • Question :101 - 200
    • Question :201 - 300
    • Question :301 - 400
      • Q344: 反转字符串
      • Q350: 两个数组的交集 II
      • Q387: 字符串中的第一个唯一字符
  • 程序员内功
  • Leetcode
2021-12-19
目录

Question :301 - 400

# Question :301 - 400

# Q344: 反转字符串 (opens new window)

public void reverseString(char[] s) {
  for (int i = 0, j = s.length - 1; i < j; i++, j--) {
    char temp = s[i];
    s[i] = s[j];
    s[j] = temp;
  }
}

# Q350: 两个数组的交集 II (opens new window)

public int[] intersect(int[] nums1, int[] nums2) {
  Arrays.sort(nums1);
  Arrays.sort(nums2);
  int len1 = nums1.length, len2 = nums2.length;
  int i = 0, j = 0, c = 0;
  int[] result = new int[Math.max(len1, len2)];
  while (i < len1 && j < len2) {
    if (nums1[i] == nums2[j]) {
      result[c++] = nums1[i];
      ++i;
      ++j;
    } else if (nums1[i] > nums2[j]) {
      ++j;
    } else {
      ++i;
    }
  }
  return c > 0 ? Arrays.copyOf(result, c) : new int[0];
}

# Q387: 字符串中的第一个唯一字符 (opens new window)

public int firstUniqChar(String s) {
  int result = s.length();
  for(char c = 'a'; c <= 'z'; c++) {
    int index = s.indexOf(c);
    if(index != -1 && index == s.lastIndexOf(c)) {
      result = Math.min(result, index);
    }
  }
  return result == s.length() ? -1 : result;
}
上次更新: 5/21/2023, 11:34:33 PM
最近更新
01
2025
01-15
02
Elasticsearch面试题
07-17
03
Elasticsearch进阶
07-16
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式