博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode Weekly Contest 147
阅读量:5069 次
发布时间:2019-06-12

本文共 2377 字,大约阅读时间需要 7 分钟。

1137. N-th Tribonacci Number

The Tribonacci sequence Tn is defined as follows: 

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

Given n, return the value of Tn.

 

Example 1:

Input: n = 4Output: 4Explanation:T_3 = 0 + 1 + 1 = 2T_4 = 1 + 1 + 2 = 4

Example 2:

Input: n = 25Output: 1389537

 

Constraints:

  • 0 <= n <= 37
  • The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1.

题目大意:和斐波那契数列差不多,简单。

思路:暴力模拟。

class Solution {    public int tribonacci(int n) {        if( n == 0 || n == 1 ) return n;        if( n == 2 ) return 1;        int sum = 0, three = 1, two = 1, one = 0;        for(int i=3; i<=n; i++) {            sum = three + two + one;            one = two;            two = three;            three = sum;        }        return sum;    }}
View Code

 

1138. Alphabet Board Path

On an alphabet board, we start at position (0, 0), corresponding to character board[0][0].

Here, board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"].

We may make the following moves:

  • 'U' moves our position up one row, if the square exists;
  • 'D' moves our position down one row, if the square exists;
  • 'L' moves our position left one column, if the square exists;
  • 'R' moves our position right one column, if the square exists;
  • '!' adds the character board[r][c] at our current position (r, c) to the answer.

Return a sequence of moves that makes our answer equal to target in the minimum number of moves.  You may return any path that does so.

 

Example 1:

Input: target = "leet"Output: "DDR!UURRR!!DDD!"

Example 2:

Input: target = "code"Output: "RR!DDRR!UUL!R!"

 

Constraints:

  • 1 <= target.length <= 100
  • target consists only of English lowercase letters.

题目大意:给你一个字母地图board,你最开始是在‘a’的位置,给你一个字符串,让你依次走过每个字母的位置,然后打印路径(随便一个路径就好)。

思路:暴力模拟,但是z有坑,z只能由u下去。

class Solution {    private static int m = 5;    public String alphabetBoardPath(String target) {        StringBuilder res = new StringBuilder();        char[] chars = target.toCharArray();        int len = target.length();        char last = 'a';        for(int i=0; i
last ) { int ch_o = ch - 'a'; int last_o = last - 'a'; x = ch_o/m - last_o/m; y = Math.abs(ch_o%m - last_o%m); for(int k=0; k
last ) { for(int k=0; k
View Code

 

转载于:https://www.cnblogs.com/Asimple/p/11258560.html

你可能感兴趣的文章
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>
iOS开发——缩放图片
查看>>
HTTP之URL的快捷方式
查看>>
满世界都是图论
查看>>
配置链路聚合中极小错误——失之毫厘谬以千里
查看>>
蓝桥杯-分小组-java
查看>>
Android Toast
查看>>
iOS开发UI篇—Quartz2D使用(绘制基本图形)
查看>>
docker固定IP地址重启不变
查看>>
桌面图标修复||桌面图标不正常
查看>>
JavaScript基础(四)关于对象及JSON
查看>>
JAVA面试常见问题之Redis篇
查看>>
jdk1.8 api 下载
查看>>
getElement的几中属性介绍
查看>>
HTML列表,表格与媒体元素
查看>>
雨林木风 GHOST_XP SP3 快速装机版YN12.08
查看>>
java对象的深浅克隆
查看>>
数据结构3——浅谈zkw线段树
查看>>
Introduction to my galaxy engine 2: Depth of field
查看>>
Python 3.X 练习集100题 05
查看>>