lc 0228
✅ 412. Fizz Buzz
https://leetcode-cn.com/problems/fizz-buzz
描述
写一个程序,输出从 1 到 n 数字的字符串表示。
1. 如果 n 是3的倍数,输出“Fizz”;
2. 如果 n 是5的倍数,输出“Buzz”;
3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。
示例:
n = 15,
返回:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fizz-buzz
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解答
按照题意直接 for + if 吧
c数字转字符:
itoa
int num = 100;
char str[25];
itoa(num, str, 10);
printf("The number 'num' is %d and the string 'str' is %s. \n" ,
num, str);
sprintf
int num = 100;
char str[25];
sprintf(str, " %d" , num);
printf ("The number 'num' is %d and the string 'str' is %s. \n" ,
num, str);
other's c
char ** fizzBuzz(int n, int* returnSize){
*returnSize=n;
char **ans=(char**)malloc(n*sizeof(char*));
int i,temp,count;
for(i=0;i<n;i++)
{
if((i+1)%3==0)
{
if((i+1)%5==0)
{
ans[i]=(char*)malloc(9*sizeof(char));
ans[i]="FizzBuzz";
}
else
{
ans[i]=(char*)malloc(5*sizeof(char));
ans[i]="Fizz";
}
}
else if((i+1)%5==0)
{
ans[i]=(char*)malloc(5*sizeof(char));
ans[i]="Buzz";
}
else
{
count=0;
temp=i+1;
while(temp)
{
count++;
temp/=10;
}
ans[i]=(char*)malloc((count+1)*sizeof(char));
ans[i][count]='\0';
count--;
temp=i+1;
while(temp)
{
ans[i][count]=temp%10+'0';
temp/=10;
count--;
}
}
}
return ans;
}
my c tdo
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
char ** fizzBuzz(int n, int* returnSize){
char **my_vec;
my_vec = (char **) malloc (sizeof(char *) * n);
//tt3 line
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 && i % 5 == 0) {
my_vec[i-1]= "FizzBuzz";
} else if (i % 3 == 0) {
my_vec[i-1]= "Fizz";
} else if (i % 5 == 0) {
my_vec[i-1]= "Buzz";
} else {
char *tmp = (char *) malloc (sizeof(char *));//tt dont declare `tmp` at tt3 line
sprintf(tmp, "%d", i);//tt rev sprintf; good helper
my_vec[i-1] = tmp;
}
}
*returnSize = n;
return my_vec;
}
/*执行用时 :
8 ms
, 在所有 C 提交中击败了
88.31%
的用户
内存消耗 :
8.2 MB
, 在所有 C 提交中击败了
77.50%
的用户*/
py
class Solution:
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
res = []
for i in range(1,n+1):
res.append('Fizz'[i%3*len('Fizz')::]+'Buzz'[i%5*len('Buzz')::] or str(i))# todo 奇技淫巧 【::】
return res
✅ 235. 二叉搜索树的最近公共祖先
https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree
描述
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
示例 1:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
输出: 6
解释: 节点 2 和节点 8 的最近公共祖先是 6。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解答
一次 dfs 中 找到 p 和 q
找到 谁 比如,q的时候,就把当然dfs经历中的 路径上的 点, 都放在一个 数组a中
找到 p 的时候,就把 path 上的node ,放在 一个数组 b 中
看 a b 某个点之后的node 不一致 了,那么这个 : 某个点, 就是 公共祖先
评价者 思路:
使用BST 的搜索 特性
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
struct TreeNode *re = NULL;
while(root)
{
if(root->val > p->val&&root->val > q->val)
root = root->left;
else if(root->val < p->val&&root->val < q->val)
root = root->right;
else
{
re = root;
break;
}
}
return re;
}
c
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
struct TreeNode *re;
while(root) {
if (root->val > q->val && root->val > p->val) {
root = root-> left;
}
else if (root->val < q->val && root->val < p->val) {
root = root->right;
} else {
re = root;
break;
}
}
return re;
}
/*执行用时 :
52 ms
, 在所有 C 提交中击败了
36.92%
的用户
内存消耗 :
30.2 MB
, 在所有 C 提交中击败了
83.07%
的用户*/
py
使用了自己的dfs 思路, failed todo
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
print(p.val)
print(q.val)
print("----")
q_path = []
p_path = []
# each param must not null
def dfs(root: 'TreeNode', backArr: List[TreeNode]):
if root is None:
return
backArr.append(root)
if root.val == q.val:
# now we find q node
q_path = backArr[:]
print(">>>",q_path)
# return as we finished
return
if root.val == p.val:
p_path = backArr.copy()
# return as we finished
return
# otherwise, we conti dfs
if root.left:
dfs(root.left, backArr)
if root.right:
dfs(root.right, backArr)
dfs(root, [])
# as we got p_path and q_path
# we can now compare them
print(q_path)
print(p_path)
for i in range(0, min(len(q_path), len(p_path))):
if q_path[i].val == p_path[i].val:
continue
else: # q_path[i] != p_path[i]
return q_path[i - 1].val
'''
执行用时: 32 ms
输入
[6,2,8,0,4,7,9,null,null,3,5]
2
8
输出
null
预期结果
6
stdout
2
8
----
>>> [TreeNode{val: 6, left: TreeNode{val: 2, left: TreeNode{val: 0, left: None, right: None}, right: TreeNode{val: 4, left: TreeNode{val: 3, left: None, right: None}, right: TreeNode{val: 5, left: None, right: None}}}, right: TreeNode{val: 8, left: TreeNode{val: 7, left: None, right: None}, right: TreeNode{val: 9, left: None, right: None}}}, TreeNode{val: 2, left: TreeNode{val: 0, left: None, right: None}, right: TreeNode{val: 4, left: TreeNode{val: 3, left: None, right: None}, right: TreeNode{val: 5, left: None, right: None}}}, TreeNode{val: 8, left: TreeNode{val: 7, left: None, right: None}, right: TreeNode{val: 9, left: None, right: None}}]
[]
[]
'''
lc 0228的更多相关文章
- 四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT。
laviewpbt 2014.8.4 编辑 Email:laviewpbt@sina.com QQ:33184777 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...
- “LC.exe”错误
错误“LC.exe”已退出,代码为 -1. 可能的原因是: 这个第三方组件是个商业组件,他在组件的主使用类定义了 LicenseProvider(typeof(LicFileLicenseProvid ...
- 解决VS下“LC.exe已退出,代码为-1”问题
今天使用VS2015开发一个Winform程序,手一抖拖错了一个第三方控件,然后将其去掉并删除相关的引用,结果导致了LC.exe错误:"Lc.exe已退出,代码为-1 ". 经过上 ...
- 解析.NET 许可证编译器 (Lc.exe) 的原理与源代码剖析
许可证编译器 (Lc.exe) 的作用是读取包含授权信息的文本文件,并产生一个可作为资源嵌入到公用语言运行库可执行文件中的 .licenses 文件. 在使用第三方类库时,经常会看到它自带的演示程序中 ...
- Lc.exe已退出,代码为-1
编译项目,出现提示"Lc.exe已退出,代码为-1" . 解决办法: 意思就是把licenses.licx这个文件里的内容删除,但是文件还在(此时是个空文件),发生这个问题的原 ...
- "LC.exe" exited with code -1 错误
当打开一个VS程序时出现"LC.exe" exited with code -1错误,解决方法是: 删除licenses.licx文件即可
- LC.exe exited with code -1
昨天从win8.1升级到win10之后, 一切还算顺利, 就是升级时间比较长. 但是快下班的时候 遇到一个问题, 是之前在win8.1上没遇到的, 首先代码win8.1 vs2013 上跑的时候一切正 ...
- vs2012编译出错“LC.exe”已退出解决方法
“LC.exe”已退出,代码为 -1. 解决方法: 将项目Properties下的licenses.licx文件删除,重新编译即可.
- TT付款方式、前TT和后TT、LC信用证+TT付款方式
TT付款方式是以外汇现金方式结算,由您的客户将款项汇至贵公司指定的外汇银行账号内,可以要求货到后一定期限内汇款. .T/T属于商业信用,也就是说付款的最终决定权在于客户.T/T分预付,即期和远期.现在 ...
随机推荐
- THINKPHP 模板上传图片--后台接收图片
模板 {extend name="public/base" /} {block name="body"} <div class="row&quo ...
- 网站调用qq第三方登录
1. 准备工作 (1) 接入QQ登录前,网站需首先进行申请,获得对应的appid与appkey,以保证后续流程中可正确对网站与用户进行验证与授权. ① 注册QQ互联开发者账号 网址 https:/ ...
- RMQ入门解析
参照大佬博客:https://www.cnblogs.com/yoke/p/6949838.html RMQ(Range Minimum/Maximum Query), 是一种问题,即 查询给定区间 ...
- Ubuntu 安装 k8s 三驾马车 kubelet kubeadm kubectl
Ubuntu 版本是 18.04 ,用的是阿里云服务器,记录一下自己实际安装过程的操作步骤. 安装 docker 安装所需的软件 apt-get update apt-get install -y a ...
- ToolStripComboBox的DataSource和DataTable使用技巧
可以使用Items属性private void GetData() //一下数据均为测试{toolStripComboBox1.Items.Clear(); DataTabl ...
- 「题解」「BZOJ-4668」冷战
题目 点这里 思路及代码 我们可以使用并查集的按秩合并(但是不要路径压缩). 两个集合被合并起来,连上的边的权值就设为当前时间. 然后我们可以发现,询问 \(j\) 与 \(k\) 何时联通,就是查询 ...
- 线段树+扫描线 NAIPC 2019 Intersecting Rectangles
你看看你有多菜,一点线段树的小小的运用,就不会写了: 题意:如果矩阵有交集,输出1,否则输出0(不包含内嵌): 思路:本题求交集,还得不包括内嵌的情况: 做过一道是求面积的题.跟这道类似,但在这里定义 ...
- 【C语言】(指针) 将两个数排序
原理就不讲了,这里用来理解指针的使用方法 代码1: #include <stdio.h> int main() { int a, b;/* 实际变量的声明 */ int* p, * q;/ ...
- java 数据类型优先级
由低到高:byte,short,char—> int —> long—> float —> double 1. 不能对boolean类型进行类型转换. 2. 不能把对象类型转换 ...
- 你了解真正的 restful API 吗?
本文原创地址,博客:https://jsbintask.cn/2019/03/20/api/restful-api-best-practices/(食用效果最佳),转载请注明出处! 前言 在以前,一个 ...