diff --git "a/Week 07/id_053/[58]\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.java" "b/Week 07/id_053/[58]\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.java" new file mode 100644 index 000000000..b00e10233 --- /dev/null +++ "b/Week 07/id_053/[58]\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.java" @@ -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) diff --git "a/Week 07/id_053/[709]\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215.java" "b/Week 07/id_053/[709]\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215.java" new file mode 100644 index 000000000..69cba2ce1 --- /dev/null +++ "b/Week 07/id_053/[709]\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215.java" @@ -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) diff --git "a/Week 07/id_053/[746]\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.java" "b/Week 07/id_053/[746]\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.java" new file mode 100644 index 000000000..b878c1afc --- /dev/null +++ "b/Week 07/id_053/[746]\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.java" @@ -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)