780. 到达终点
2022.4.9 每日一题
题目描述
给定四个整数 sx , sy ,tx 和 ty,如果通过一系列的转换可以从起点 (sx, sy) 到达终点 (tx, ty),则返回 true,否则返回 false。
从点 (x, y) 可以转换到 (x, x+y) 或者 (x+y, y)。
示例 1:
输入: sx = 1, sy = 1, tx = 3, ty = 5
输出: true
解释:
可以通过以下一系列转换从起点转换到终点:
(1, 1) -> (1, 2)
(1, 2) -> (3, 2)
(3, 2) -> (3, 5)
示例 2:
输入: sx = 1, sy = 1, tx = 2, ty = 2
输出: false
示例 3:
输入: sx = 1, sy = 1, tx = 1, ty = 1
输出: true
提示:
1 <= sx, sy, tx, ty <= 10^9
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reaching-points
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
很简单但是也很明显超时的一个暴力解法
class Solution {
public boolean reachingPoints(int sx, int sy, int tx, int ty) {
//暴力的想法肯定超时,不过先写一下
//System.out.println(sx + "--" + sy);
if(sx == tx && sy == ty)
return true;
if(sx > tx || sy > ty)
return false;
return reachingPoints(sx + sy, sy, tx, ty) || reachingPoints(sx, sx + sy, tx, ty);
}
}
我这里之所以用k1,k2是因为我觉得如果减的多了会漏掉起始点,但其实是考虑多余了
因为这个题的叠加规律保证了tx肯定是大于sx的
class Solution {
public boolean reachingPoints(int sx, int sy, int tx, int ty) {
//那么该怎么搞呢,肯定不能先到达一个维度再去另一个维度
//从后往前算,应该会减少很多情况
//因为所有点下标都是大于等于1的,所以可以判断移动方向,
//如果x>y,那么上一个位置就是移动x而来的,所以是 t +y,y这样转换来的
//同理x<y,所以x,t + x;如果x=y,那么不可能
//这样写还是超时了,问题就是可能一个y很大,x很小,需要减很多次,那么这种情况怎么处理呢
//直接减去倍数就可以
if(tx == sx && ty == sy)
return true;
while(tx > 0 && ty > 0 && tx != ty){
if(tx > ty){
int k1 = (tx - ty) / ty;
int k2 = (tx - sx) / ty;
int k = Math.min(k1, k2);
tx = tx - Math.max(1, k) * ty;
}else{
int k1 = (ty - tx) / tx;
int k2 = (ty - sy) / tx;
int k = Math.min(k1, k2);
ty = ty - Math.max(1, k) * tx;
}
if(tx == sx && ty == sy)
return true;
}
return false;
}
}
直接取余,然后判断剩下的结果
class Solution:
def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:
while tx > sx and ty > sy:
if tx > ty:
tx %= ty
else:
ty %= tx
if tx < sx or ty < sy:
return False
if tx == sx:
return (ty - sy) % tx == 0
if ty == sy:
return (tx - sx) % ty == 0
return False
804. 唯一摩尔斯密码词
2022.4.10 每日一题
题目描述
国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如:
- ‘a’ 对应 “.-” ,
- ‘b’ 对应 “-…” ,
- ‘c’ 对应 “-.-.” ,以此类推。
为了方便,所有 26 个英文字母的摩尔斯密码表如下:
[“.-”,“-…”,“-.-.”,“-…”,“.”,“…-.”,“–.”,“…”,“…”,“.—”,“-.-”,“.-…”,“–”,“-.”,“—”,“.–.”,“–.-”,“.-.”,“…”,“-”,“…-”,“…-”,“.–”,“-…-”,“-.–”,“–…”]
给你一个字符串数组 words ,每个单词可以写成每个字母对应摩尔斯密码的组合。
- 例如,“cab” 可以写成 “-.-…–…” ,(即 “-.-.” + “.-” + “-…” 字符串的结合)。我们将这样一个连接过程称作 单词翻译 。
对 words 中所有单词进行单词翻译,返回不同 单词翻译 的数量。
示例 1:
输入: words = [“gin”, “zen”, “gig”, “msg”]
输出: 2
解释:
各单词翻译如下:
“gin” -> “–…-.”
“zen” -> “–…-.”
“gig” -> “–…–.”
“msg” -> “–…–.”
共有 2 种不同翻译, “–…-.” 和 “–…–.”.
示例 2:
输入:words = [“a”]
输出:1
提示:
1 <= words.length <= 100
1 <= words[i].length <= 12
words[i] 由小写英文字母组成
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/unique-morse-code-words
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
class Solution {
static String[] pwd = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
public int uniqueMorseRepresentations(String[] words) {
Set<String> set = new HashSet<>();
for(String s : words){
String temp = "";
for(char c : s.toCharArray()){
temp += pwd[c - 'a'];
}
set.add(temp);
}
return set.size();
}
}
python中要表示字符,ord
pwd = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
class Solution:
def uniqueMorseRepresentations(self, words: List[str]) -> int:
res = []
for i in range(len(words)):
b = ""
for c in words[i]:
b += pwd[ord(c) - 97]
res.append(b)
return len(set(res))
pwd = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
class Solution:
def uniqueMorseRepresentations(self, words: List[str]) -> int:
return len(set("".join(pwd[ord(c) - ord('a')] for c in word) for word in words))
357. 统计各位数字都不同的数字个数
2022.4.11 每日一题
题目描述
给你一个整数 n ,统计并返回各位数字都不同的数字 x 的个数,其中 0 <= x < 10n 。
示例 1:
输入:n = 2
输出:91
解释:答案应为除去 11、22、33、44、55、66、77、88、99 外,在 0 ≤ x < 100 范围内的所有数字。
示例 2:
输入:n = 0
输出:1
提示:
0 <= n <= 8
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/count-numbers-with-unique-digits
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
排列组合
class Solution {
public int countNumbersWithUniqueDigits(int n) {
//1位数的时候,能选10个
//2位数的时候,9 * 9 = 81
//3位数的时候,9 * 9 * 8 = 648
int res = 10;
if(n == 0)
return 1;
else if(n == 1)
return 10;
else{
int temp = 9;
int idx = 9;
while(n-- > 1){
temp *= idx;
idx--;
res += temp;
}
}
return res;
}
}
class Solution:
def countNumbersWithUniqueDigits(self, n: int) -> int:
if n == 0:
return 1
elif n == 1:
return 10
else:
res = 10
temp = 9
for i in range(n - 1):
temp *= 9 - i
res += temp
return res