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

    • 菜鸟教程 (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
      • Q122*: 买卖股票的最佳时机 II
      • Q125: 验证回文串
      • Q136: 只出现一次的数字
      • Q189*: 轮转数组
    • Question :201 - 300
    • Question :301 - 400
  • 程序员内功
  • Leetcode
2021-12-27
目录

Question :101 - 200

# Question :101 - 200

# Q122*: 买卖股票的最佳时机 II (opens new window)

public static int maxProfit(int[] prices) {
	int profit = 0;
	for (int i = 1; i < prices.length; ++i) {
		profit += Math.max(0, prices[i] - prices[i - 1]);
	}
	return profit;
}

# Q125: 验证回文串 (opens new window)

public boolean isPalindrome(String s) {

  if(s.isEmpty()) {
    return true;
  }

  char[] charArray = s.toCharArray();
  int i = 0, y = charArray.length - 1;
  while ( i < y){
    char c1 = validateChar(charArray[i]);
    if(c1 == 0) {
      ++i;
      continue;
    }
    char c2 = validateChar(charArray[y]);
    if(c2 == 0) {
      --y;
      continue;
    }
    if(c1 != c2) {
      return false;
    }
    ++i;
    --y;
  }

  return true;
}

private char validateChar(char ch) {
  if(ch >= 'A' && ch <= 'Z') {
    return (char)(ch+'a'-'A');
  }else if((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <='9')) {
    return ch;
  }else {
    return 0;
  }
}

# Q136: 只出现一次的数字 (opens new window)

public static int singleNumber(int[] nums) {
  int n = nums[0];
  for (int i = 1; i < nums.length; ++i) {
    n = n ^ nums[i];
  }
  return n;
}

# Q189*: 轮转数组 (opens new window)

public void rotate(int[] nums, int k) {
	int n = nums.length;
	k %= n;
	reverse(nums, 0, n - 1);
	reverse(nums, 0, k - 1);
	reverse(nums, k, n - 1);
}

private void reverse(int[] arr,int start,int end) {
	while(start < end) {
		int temp = arr[start];
		arr[start++] = arr[end];
		arr[end--] = temp;
	}
}
上次更新: 5/21/2023, 11:34:33 PM
Question :201 - 300

Question :201 - 300→

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