目录
1. Excel表列名称 🌟
2. 验证回文串 🌟
3. 路径总和 II 🌟🌟
🌟 每日一练刷题专栏 🌟
Golang每日一练 专栏
Python每日一练 专栏
C/C++每日一练 专栏
Java每日一练 专栏
1. Excel表列名称
给你一个整数 columnNumber
,返回它在 Excel 表中相对应的列名称。
例如:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
示例 1:
输入:columnNumber = 1 输出:"A"
示例 2:
输入:columnNumber = 28 输出:"AB"
示例 3:
输入:columnNumber = 701 输出:"ZY"
示例 4:
输入:columnNumber = 2147483647 输出:"FXSHRXW"
提示:
1 <= columnNumber <= 2^31 - 1
出处:
https://edu.csdn.net/practice/27308135
代码:
import java.util.*;
public class convertToTitle {
public static class Solution {
public String convertToTitle(int n) {
if (n <= 0) {
return "";
}
StringBuilder sb = new StringBuilder();
while (n > 0) {
n--;
sb.append((char) (n % 26 + 'A'));
n = n / 26;
}
return sb.reverse().toString();
}
}
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.convertToTitle(1));
System.out.println(s.convertToTitle(28));
System.out.println(s.convertToTitle(701));
System.out.println(s.convertToTitle(2147483647));
}
}
输出:
A
AB
ZY
FXSHRXW
2. 验证回文串
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
输入: "A man, a plan, a canal: Panama" 输出: true 解释:"amanaplanacanalpanama" 是回文串
示例 2:
输入: "race a car" 输出: false 解释:"raceacar" 不是回文串
提示:
1 <= s.length <= 2 * 10^5
- 字符串
s
由 ASCII 字符组成
出处:
https://edu.csdn.net/practice/27308136
代码:
import java.util.*;
public class isPalindrome {
public static class Solution {
public boolean isPalindrome(String s) {
StringBuffer str = new StringBuffer();
int len = s.length();
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (Character.isLetterOrDigit(c)) {
str.append(Character.toLowerCase(c));
}
}
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.isPalindrome("A man, a plan, a canal: Panama"));
System.out.println(s.isPalindrome("race a car"));
}
}
输出:
true
false
3. 路径总和 II
给你二叉树的根节点 root
和一个整数目标和 targetSum
,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 输出:[[5,4,11,2],[5,8,4,5]]
示例 2:
输入:root = [1,2,3], targetSum = 5 输出:[]
示例 3:
输入:root = [1,2], targetSum = 0 输出:[]
提示:
- 树中节点总数在范围
[0, 5000]
内 -1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
出处:
https://edu.csdn.net/practice/27308137
代码:
import java.util.*;
import java.util.LinkedList;
public class pathSum {
public final static int NULL = Integer.MIN_VALUE; //用NULL来表示空节点
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public static class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
if (root == null)
return new ArrayList<>();
List<List<Integer>> ans = new ArrayList<>();
if (root.val == sum && root.left == null && root.right == null) {
List<Integer> arr = new ArrayList<>();
arr.add(root.val);
ans.add(arr);
return ans;
}
List<List<Integer>> left = pathSum(root.left, sum - root.val);
List<List<Integer>> right = pathSum(root.right, sum - root.val);
for (List<Integer> list : left) {
list.add(0, root.val);
ans.add(list);
}
for (List<Integer> list : right) {
list.add(0, root.val);
ans.add(list);
}
return ans;
}
}
public static TreeNode createBinaryTree(Integer[] nums) {
Vector<Integer> vec = new Vector<Integer>(Arrays.asList(nums));
if (vec == null || vec.size() == 0) {
return null;
}
Queue<TreeNode> queue = new LinkedList<>();
TreeNode root = new TreeNode(vec.get(0));
queue.offer(root);
int i = 1;
while (!queue.isEmpty()) {
int size = queue.size();
for (int j = 0; j < size; j++) {
TreeNode node = queue.poll();
if (i < vec.size() && vec.get(i) != NULL) {
node.left = new TreeNode(vec.get(i));
queue.offer(node.left);
}
i++;
if (i < vec.size() && vec.get(i) != NULL) {
node.right = new TreeNode(vec.get(i));
queue.offer(node.right);
}
i++;
}
}
return root;
}
public static void main(String[] args) {
Solution s = new Solution();
Integer[] nums = {5,4,8,11,NULL,13,4,7,2,NULL,NULL,5,1};
int targetSum = 22;
TreeNode root = createBinaryTree(nums);
System.out.println(s.pathSum(root, targetSum));
}
}
输出:
[[5, 4, 11, 2], [5, 8, 4, 5]]
🌟 每日一练刷题专栏 🌟
✨ 持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/
Golang每日一练 专栏 | |
Python每日一练 专栏 | |
C/C++每日一练 专栏 | |
Java每日一练 专栏 |