您的位置:首页 > 产品设计 > UI/UE

leetcode 58、Length of Last Word;59、Spiral Matrix II ;60、Permutation Sequence

2016-01-13 22:01 591 查看
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
.

二、问题分析:
Java里面用trim和spilt会很容易实现,这里采取遍历字符的一般方式进行操作;
只要注意从后往前遍历即可,遇到' '字符,首先要注意判断该字符是否是word的最后一个字符(也可能最后几个字符是连续的' '),如果是,则跳过;
如果不是,则返回计算的非' '字母长度即可;

三、代码:

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;
}
}

59、 Spiral Matrix II 构造螺旋矩阵

相关问题:《 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
,
You should return the following matrix:
[
[ 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

;
// 记录上下左右边界值
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;
}
}

60、Permutation Sequence 查找数字排列组合的第k个组合


问题描述:

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.

问题分析:
笨拙的解法:使用递归得出所有的排列组合,然后进行字典序排序,进而索引出第k个字符串;
规律的解法:由于只是找到第k个组合,没必要计算出所有的排列组合;
总结规律:以1,2,3,4,5为例;k = 25;记n = 5的排列组合为P(1,2,3,4,5)
1)可以看到 P(1,2,3,4,5)= {1 + P(2,3,4,5)} + {2 + P(1,3,4,5)} + {3 + P(1,2,4,5)} + {4 + P(1,2,3,5)} + {5
+ P(1,2,3,4)};
P(2,3,4,5)、 P(1,3,4,5)、P(1,2,4,5)、P(1,2,3,4)都有(5-1)!=24中排列组合,故k=25,必然不在{1 + P(2,3,4,5)}的集合中,而应该落在 {2
+ P(1,3,4,5)} 的集合中;即结果字符串的第一个字符应该为2;其后面的字符为{1,3,4,5}的组合;
2)而同理P(1,3,4,5)={1 + P(3,4,5)} + {3 + P(1,4,5)}
+ {4 + P(1,3,5)} +  {5 + P(1,3,4)};
P(3,4,5)等各有(4-1)!=6中排列组合;index = (k-1)/6 = 0;则结果落在{1 + P(3,4,5)}中,则第二个字符应该为1;

3)以此类推,最终找到所有的字符,组装成结果字符串;

代码:
解法一:

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));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: