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

    • 菜鸟教程 (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
      • Q206: 反转链表
      • Q217: 存在重复元素
      • Q237: 删除链表中的节点
      • Q242: 有效的字母异位词
      • Q283: 移动零
      • Q290: 单词规律
    • Question :301 - 400
  • 程序员内功
  • Leetcode
2021-12-18
目录

Question :201 - 300

# Question :201 - 300

# Q206: 反转链表 (opens new window)

public ListNode reverseList(ListNode head) {
  if(head == null || head.next == null) {
    return head;
  }
  ListNode node = reverseList(head.next);
  head.next.next = head;
  head.next = null;
  return node;
}

# Q217: 存在重复元素 (opens new window)

public boolean containsDuplicate(int[] nums) {
  if (nums.length < 2) {
    return false;
  }
  Set<Integer>  set = new HashSet<>();
  for(int num : nums) {
    if(!set.add(num)){
      return true;
    }
  }
  return false;
}

# Q237: 删除链表中的节点 (opens new window)

public void deleteNode(ListNode node) {
  node.val = node.next.val;
  node.next = node.next.next;
}

# Q242: 有效的字母异位词 (opens new window)

public boolean isAnagram(String s, String t) {
  if (s.length() != t.length()){
    return false;
  }

  int[] arr1 = new int[26], arr2 = new int[26];
  char[] sChars1 = s.toCharArray(), sChars2 = t.toCharArray();
  for (char c : sChars1) {
    arr1[c - 'a'] += 1;
  }
  for (char c : sChars2) {
    arr2[c - 'a'] += 1;
  }
  for (int i = 0; i < arr1.length; i++) {
    if(arr1[i] != arr2[i]) {
      return false;
    }
  }
  return true;
}

# Q283: 移动零 (opens new window)

public void moveZeroes(int[] nums){
  if (nums == null || nums.length <= 1){
    return;
  }
  int index = 0;
  for (int i = 0; i < nums.length; i++) {
    if (nums[i] != 0){
      nums[index] = nums[i];
      index++;
    }
  }
  for (int i = index; i < nums.length; i++) {
    nums[i] = 0;
  }
}

# Q290: 单词规律 (opens new window)

public boolean wordPattern(String pattern, String s) {
  String[] splitArr = s.split(" ");
  if(pattern.length() != splitArr.length) {
    return false;
  }
  String[] patLetterArr = new String[26];
  Set<String> set = new HashSet<>();
  for(int i = 0; i < pattern.length(); i++) {
    int index = pattern.charAt(i) - 'a';
    String patWord = patLetterArr[index];
    String word = splitArr[i];
    if (patWord == null){
      if(set.contains(word)) {
        return false;
      }
      set.add(word);
      patLetterArr[index] = word;
    }else if (!patWord.equals(word)){
      return false;
    }
  }	
  return true;
}
上次更新: 5/21/2023, 11:34:33 PM
Question :301 - 400

Question :301 - 400→

最近更新
01
2025
01-15
02
Elasticsearch面试题
07-17
03
Elasticsearch进阶
07-16
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式