1.买卖股票的最佳时机
121. 买卖股票的最佳时机 - 力扣(LeetCode)
class Solution {public int maxProfit(int[] prices) {int minprice=Integer.MAX_VALUE;int maxprofit=0;for(int i=0;i<prices.length;i++){if(prices[i]<minprice){minprice=prices[i];}else if(prices[i]-minprice>maxprofit){maxprofit=prices[i]-minprice;}}return maxprofit;}
}
2.买卖股票的最佳时机2
122. 买卖股票的最佳时机 II - 力扣(LeetCode)
给你一个整数数组
prices
,其中prices[i]
表示某支股票第i
天的价格。在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。
返回 你能获得的 最大 利润 。
方法一:动态规划
class Solution {public int maxProfit(int[] prices) {int n = prices.length;int dp0 = 0, dp1 = -prices[0];for (int i = 1; i < n; ++i) {int newDp0 = Math.max(dp0, dp1 + prices[i]);int newDp1 = Math.max(dp1, dp0 - prices[i]);dp0 = newDp0;dp1 = newDp1;}return dp0;}
}
方法二:贪心
class Solution {public int maxProfit(int[] prices) {int ans = 0;int n = prices.length;for (int i = 1; i < n; ++i) {ans += Math.max(0, prices[i] - prices[i - 1]);}return ans;}
}
3.验证回文串
125. 验证回文串 - 力扣(LeetCode)
方法一:在原字符串上直接判断
class Solution {public boolean isPalindrome(String s) {int n = s.length();int left = 0, right = n - 1;while (left < right) {while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {++left;}while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {--right;}if (left < right) {if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {return false;}++left;--right;}}return true;}
}
4.最长连续序列
给定一个未排序的整数数组
nums
,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。请你设计并实现时间复杂度为
O(n)
的算法解决此问题。
方法一:哈希表
class Solution {public int longestConsecutive(int[] nums) {Set<Integer> num_set = new HashSet<Integer>();for (int num : nums) {num_set.add(num);}int longestStreak = 0;for (int num : num_set) {if (!num_set.contains(num - 1)) {int currentNum = num;int currentStreak = 1;while (num_set.contains(currentNum + 1)) {currentNum += 1;currentStreak += 1;}longestStreak = Math.max(longestStreak, currentStreak);}}return longestStreak;}
}
5.求根节点到叶节点数字之和
129. 求根节点到叶节点数字之和 - 力扣(LeetCode)
方法一:深度优先搜索
思路与算法
深度优先搜索是很直观的做法。从根节点开始,遍历每个节点,如果遇到叶子节点,则将叶子节点对应的数字加到数字之和。如果当前节点不是叶子节点,则计算其子节点对应的数字,然后对子节点递归遍历。
class Solution {public int sumNumbers(TreeNode root) {return dfs(root, 0);}public int dfs(TreeNode root, int prevSum) {if (root == null) {return 0;}int sum = prevSum * 10 + root.val;if (root.left == null && root.right == null) {return sum;} else {return dfs(root.left, sum) + dfs(root.right, sum);}}
}
方法二:广度优先搜索
class Solution {public int sumNumbers(TreeNode root) {if (root == null) {return 0;}int sum = 0;Queue<TreeNode> nodeQueue = new LinkedList<TreeNode>();Queue<Integer> numQueue = new LinkedList<Integer>();nodeQueue.offer(root);numQueue.offer(root.val);while (!nodeQueue.isEmpty()) {TreeNode node = nodeQueue.poll();int num = numQueue.poll();TreeNode left = node.left, right = node.right;if (left == null && right == null) {sum += num;} else {if (left != null) {nodeQueue.offer(left);numQueue.offer(num * 10 + left.val);}if (right != null) {nodeQueue.offer(right);numQueue.offer(num * 10 + right.val);}}}return sum;}
}