Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Week 07/id_053/[58]最后一个单词的长度.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。
//
// 如果不存在最后一个单词,请返回 0 。
//
// 说明:一个单词是指由字母组成,但不包含任何空格的字符串。
//
// 示例:
//
// 输入: "Hello World"
//输出: 5
//
// Related Topics 字符串



//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int lengthOfLastWord(String s) {
if (s == null || " ".equals(s)) return 0;
String[] split = s.split(" ");
return split.length == 0? 0:split[split.length - 1].length();
//调用系统函数很慢,以下是别人写的代码,后面再看
/*int end = s.length() - 1;
while(end >= 0 && s.charAt(end) == ' ') end--;
if(end < 0) return 0;
int start = end;
while(start >= 0 && s.charAt(start) != ' ') start--;
return end - start;*/

}
}
//leetcode submit region end(Prohibit modification and deletion)
40 changes: 40 additions & 0 deletions Week 07/id_053/[709]转换成小写字母.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串。
//
//
//
// 示例 1:
//
//
//输入: "Hello"
//输出: "hello"
//
// 示例 2:
//
//
//输入: "here"
//输出: "here"
//
// 示例 3:
//
//
//输入: "LOVELY"
//输出: "lovely"
//
// Related Topics 字符串



//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String toLowerCase(String str) {
if (str == null) return null;
char[] chs = str.toCharArray();
for (int i = 0;i < chs.length; i++){
if (chs[i] >= 'A' && chs[i] <= 'Z') {
chs[i] = (char)(chs[i] + 32);
}
}
return new String(chs);
}
}
//leetcode submit region end(Prohibit modification and deletion)
48 changes: 48 additions & 0 deletions Week 07/id_053/[746]使用最小花费爬楼梯.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始)。
//
// 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯。
//
// 您需要找到达到楼层顶部的最低花费。在开始时,你可以选择从索引为 0 或 1 的元素作为初始阶梯。
//
// 示例 1:
//
//
//输入: cost = [10, 15, 20]
//输出: 15
//解释: 最低花费是从cost[1]开始,然后走两步即可到阶梯顶,一共花费15。
//
//
// 示例 2:
//
//
//输入: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
//输出: 6
//解释: 最低花费方式是从cost[0]开始,逐个经过那些1,跳过cost[3],一共花费6。
//
//
// 注意:
//
//
// cost 的长度将会在 [2, 1000]。
// 每一个 cost[i] 将会是一个Integer类型,范围为 [0, 999]。
//
// Related Topics 数组 动态规划



//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minCostClimbingStairs(int[] cost) {
//dp[i] 表示走到第i个阶梯的最小花费
//dp[i] = min(dp[i-1],dp[i-2]) + cost[i]
if(cost.length == 2) return Math.min(cost[0],cost[1]);
int[] dp = new int[cost.length];
dp[0] = cost[0];
dp[1] = cost[1];
for(int i = 2;i < cost.length;i++) {
dp[i] = Math.min(dp[i-2],dp[i-1]) +cost[i];
}
return Math.min(dp[cost.length-1],dp[cost.length-2]);
}
}
//leetcode submit region end(Prohibit modification and deletion)