58. 最後一個單詞的長度
59. 螺旋矩陣 II
60. 第k個排列
61. 旋轉鏈表
62. 不同路徑
----------------------------------分割線-----------------------------------------
58. 最後一個單詞的長度
Difficulty: 簡單
給定一個僅包含大小寫字母和空格 ' '
的字符串 s
,返回其最後一個單詞的長度。如果字符串從左向右滾動顯示,那麼最後一個單詞就是最後出現的單詞。
如果不存在最後一個單詞,請返回 0 。
**說明:**一個單詞是指僅由字母組成、不包含任何空格字符的 最大子字符串。
示例:
輸入: "Hello World"
輸出: 5
Solution:注意這種情況 "a "
class Solution {
public int lengthOfLastWord(String s) {
if(s == null || s.length()<=0) return 0;
int res = 0, len = s.length();
boolean flag = false;
for(int i=len-1; i>=0; i--){
if(s.charAt(i) == ' '){
if(flag) return res;
else continue;
}
else{
res++;
if(!flag) flag = true;
}
}
return res;
}
}
59. 螺旋矩陣 II
Difficulty: 中等
給定一個正整數 n,生成一個包含 1 到 n2 所有元素,且元素按順時針順序螺旋排列的正方形矩陣。
示例:
輸入: 3
輸出:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
Solution:固定四個,一層層的遍歷,注意(上和下)、(左和右)重複的情況
class Solution {
public int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
if(n == 0) return res;
else if(n == 1){
res[0][0] = 1;
return res;
}
int low, high;
low = 0;
high = n-1;
int x = 1;
while(x <= n*n){
for(int j=low; j<=high; j++){
res[low][j] = x;
x++;
}
for(int j=low+1; j<high; j++){
res[j][high] = x;
x++;
}
if(low != high){ //(上和下)、(左和右)重複的情況
for(int j=high; j>low; j--){
res[high][j] = x;
x++;
}
for(int j=high; j>low; j--){
res[j][low] = x;
x++;
}
}
low ++;
high--;
}
return res;
}
}
60. 第k個排列
Difficulty: 中等
給出集合 [1,2,3,…,_n_]
,其所有元素共有 n! 種排列。
按大小順序列出所有排列情況,並一一標記,當 n = 3 時, 所有排列如下:
"123"
"132"
"213"
"231"
"312"
"321"
給定 n 和 k,返回第 k 個排列。
說明:
- 給定 n 的範圍是 [1, 9]。
- 給定 _k _的範圍是[1, n!]。
示例 1:
輸入: n = 3, k = 3
輸出: "213"
示例 2:
輸入: n = 4, k = 9
輸出: "2314"
Solution:(遞歸)——把所有的情況寫成二叉樹的形式,依次確定每個值屬於哪支樹杈
class Solution {
public String getPermutation(int n, int k) {
StringBuilder builder = new StringBuilder();
int[] used = new int[n+1];
help(n, n, k, builder, used);
return builder.toString();
}
public void help(int n, int m, int k, StringBuilder builder, int[] used){
if(m <= 0 || k < 0) return ;
int jc = jieCheng(m-1); //有多少樹杈
int x = (k-1) / jc; // x就是位於第幾個樹杈
int count = -1;
for(int i=1; i<=n; i++){
if(used[i] == 0){
count++;
if(count == x){
builder.append(i);
used[i] = 1;
break;
}
}
}
help(n, m-1, k-x*jc, builder, used);
}
//計算n的階乘
public int jieCheng(int n){
int res = 1;
for(int i=2; i<=n; i++){
res *= i;
}
return res;
}
}
61. 旋轉鏈表
Difficulty: 中等
給定一個鏈表,旋轉鏈表,將鏈表每個節點向右移動 _k _個位置,其中 _k _是非負數。
示例 1:
輸入: 1->2->3->4->5->NULL, k = 2
輸出: 4->5->1->2->3->NULL
解釋:
向右旋轉 1 步: 5->1->2->3->4->NULL
向右旋轉 2 步: 4->5->1->2->3->NULL
示例 2:
輸入: 0->1->2->NULL, k = 4
輸出: 2->0->1->NULL
解釋:
向右旋轉 1 步: 2->0->1->NULL
向右旋轉 2 步: 1->2->0->NULL
向右旋轉 3 步: 0->1->2->NULL
向右旋轉 4 步: 2->0->1->NULL
Solution :就是把倒數第(k%len)個結點當做頭結點
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode rotateRight(ListNode head, int k) {
int len = 0;
ListNode node = head;
//找鏈表的長度
while(node != null){
len++;
node = node.next;
}
if(len <= 0) return head;
k = k%len; //避免循環重複的情況
if(k == 0) return head;
//尋找倒數第k+1個結點
node = head;
for(int i=0; i<len-k-1; i++){
node = node.next;
}
//res就是頭結點
ListNode res = node.next;
node.next = null;
node = res;
while(node.next != null){
node = node.next;
}
node.next = head;
return res;
}
}
62. 不同路徑
Difficulty: 中等
一個機器人位於一個 m x n 網格的左上角 (起始點在下圖中標記爲“Start” )。
機器人每次只能向下或者向右移動一步。機器人試圖達到網格的右下角(在下圖中標記爲“Finish”)。
問總共有多少條不同的路徑?
例如,上圖是一個7 x 3 的網格。有多少可能的路徑?
示例 1:
輸入: m = 3, n = 2
輸出: 3
解釋:
從左上角開始,總共有 3 條路徑可以到達右下角。
1\. 向右 -> 向右 -> 向下
2\. 向右 -> 向下 -> 向右
3\. 向下 -> 向右 -> 向右
示例 2:
輸入: m = 7, n = 3
輸出: 28
提示:
1 <= m, n <= 100
- 題目數據保證答案小於等於
2 * 10 ^ 9
Solution:動態規劃
class Solution {
int count = 0;
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(i==0 || j==0) dp[i][j] = 1;
else dp[i][j] = dp[i][j-1] + dp[i-1][j];
}
}
return dp[m-1][n-1];
}
}