58、Length of Last Word 最后一个单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' '
, return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given s = "Hello World"
,
return 5
.
public class Solution { public int lengthOfLastWord(String s) { if (s == null || s.length() == 0) return 0; int w_length = 0; // LastWord长度 boolean isLastWord = false; // 判断是否是最后一个单词,避免最后一个字母是' '的情况 // 注意从后往前遍历即可 for (int i = s.length() - 1; i >= 0; i--) { if (s.charAt(i) != ' ') { // 如果是普通字母,则lastWord长度递增 isLastWord = true; w_length++; } else { // 如果是' ',要注意判断该字母是否为最后一个字母,如果是,则跳过 if (isLastWord) break; } } return w_length; } }
相关问题:《 leetcode-54 Spiral Matrix 顺时针打印矩阵(《剑指offer》面试题20)》
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3
,
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
Subscribe to see which companies asked this question。
public class Solution { public int[][] generateMatrix(int n) { if (n < 0) return null; int[][] matrix = new int[n][n]; // 记录上下左右边界值 int left = 0; int right = n - 1; int top = 0; int bottom = n - 1; // 注意起始值为1 int count = 1; while ((left <= right) && (top <= bottom)) { // 往右走进行赋值 for (int j = left; j <= right; j++) matrix[top][j] = count++; ++top; // 更新边界值 // 向下走进行赋值 for (int i = top; i <= bottom; i++) matrix[i][right] = count++; --right; // 向左走进行赋值 for (int j = right; j >= left; j--) matrix[bottom][j] = count++; --bottom; // 向上走进行赋值 for (int i = bottom; i >= top; i--) matrix[i][left] = count++; ++left; } return matrix; } }
The set [1,2,3,…,n]
contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
public class Solution { public String getPermutation(int n, int k) { if ((n <= 0) || (n > 9) || (k <= 0) || (k > countN(n))) return ""; // 记录结果字符串 StringBuilder resBder = new StringBuilder(); // 记录当前数字集合中剩下的未使用数字 List<Integer> remainList = new ArrayList<>(); // 初始化remainList for (int i = 1; i <= n; i++) remainList.add(i); k--; while (n > 1) { int count = countN(n - 1); int index = k / count; // 添加结果数字 resBder.append(remainList.get(index)); // 更新,进行下一层循环 remainList.remove(index); k %= count; n--; } resBder.append(remainList.get(0)); return resBder.toString(); } // 计算每个数字的阶乘 private int countN(int n) { int result = 1; while (n > 0) { result *= n--; } return result; } }
public class Solution { public String getPermutation(int n, int k) { if ((n <= 0) || (n > 9)) return ""; // 先来获得相应的排列组合 ArrayList<String> list = getAllList(n); Collections.sort(list); // System.out.println(list); // 要注意判断k是否合法 return (k > list.size()) ? "" : list.get(k - 1); } private ArrayList<String> getAllList(int n) { ArrayList<String> list = new ArrayList<>(); if (n == 0) { list.add(""); } else { ArrayList<String> preList = getAllList(n - 1); // 往P(n-1)中添加字母 for (String word : preList) { // word中每个位置上添加数字n for (int i = 0; i <= word.length(); i++) { StringBuilder builder = new StringBuilder(word); builder.insert(i, n); list.add(builder.toString()); } } } return list; } public static void main(String[] args) { Solution solution = new Solution(); System.out.println(solution.getPermutation(5, 15)); } }