112. Path Sum
路径总和
Given the root
of a binary tree and an integer targetSum
, return true
if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum
.
A leaf is a node with no children.
Example 1:
Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.
Example 2:
Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: There two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.
Example 3:
Input: root = [], targetSum = 0
Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.
leetcode代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) return false;
if(root.left == null && root.right == null) return sum == root.val;
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
118. Pascal’s Triangle
帕斯卡三角
Given an integer numRows
, return the first numRows of Pascal’s triangle.
In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3Y9ZmRo5-1670866442793)(https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)]
Example 1:
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input: numRows = 1
Output: [[1]]
leetcode代码
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList();
List<Integer> row = new ArrayList();
for(int i = 0; i < numRows; i++) {
for(int j = row.size() - 1; j >= 1 ; j--) {
row.set(j, row.get(j) + row.get(j - 1));
}
row.add(1);
res.add(new ArrayList(row));
}
return res;
}
}
119. Pascal’s Triangle II
Given an integer rowIndex
, return the rowIndexth
(0-indexed) row of the Pascal’s triangle.
In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pd0XqeQw-1670866488083)(https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)]
Example 1:
Input: rowIndex = 3
Output: [1,3,3,1]
Example 2:
Input: rowIndex = 0
Output: [1]
Example 3:
Input: rowIndex = 1
Output: [1,1]
leetcode代码
class Solution {
public List<Integer> getRow(int k) {
Integer[] arr = new Integer[k + 1];
Arrays.fill(arr, 0);
arr[0] = 1;
for (int i = 1; i <= k; i++)
for (int j = i; j > 0; j--)
arr[j] = arr[j] + arr[j - 1];
return Arrays.asList(arr);
}
}
121. Best Time to Buy and Sell Stock
买卖股票的最佳时机
You are given an array prices
where prices[i]
is the price of a given stock on the ith
day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0
.
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Example 2:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
leetcode代码
class Solution {
public int maxProfit(int[] prices) {
int min = Integer.MAX_VALUE, max = 0;
for (int i = 0; i < prices.length; i++) {
min = Math.min(min, prices[i]);
max = Math.max(max, prices[i] - min);
}
return max;
}
}
125. Valid Palindrome
有效回文
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s
, return true
if it is a palindrome, or false
otherwise.
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Example 3:
Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.
leetcode代码
class Solution {
public boolean isPalindrome(String s) {
char[] c = s.toCharArray();
for (int i = 0, j = c.length - 1; i < j; ) {
if (!Character.isLetterOrDigit(c[i])) i++;
else if (!Character.isLetterOrDigit(c[j])) j--;
else if (Character.toLowerCase(c[i++]) != Character.toLowerCase(c[j--]))
return false;
}
return true;
}
}