【436】Solution for LeetCode Problems
Coding everyday. ^_^
1. Two Sum
- 重点知识:指针可以存储数值,通过 malloc 新建数组
- int* returnSize:Size of the return array. Store the value in a pointer, say 2.
*returnSize = 2 - My solution:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
*returnSize = 2;
int* returnArray = malloc(sizeof(int)*(*returnSize));
for (int i = 0; i < numsSize-1; i++) {
for (int j = i+1; j < numsSize; j++) {
if (nums[i] + nums[j] == target) {
returnArray[0] = i;
returnArray[1] = j;
return returnArray;
}
}
}
returnArray[0] = -1;
returnArray[1] = -1;
return returnArray;
}
2. Add Two Numbers
- 重点知识:不能通过数字来计算,考虑进位,考虑链表遍历和插入节点,通过 malloc 新建节点
- Don't use integer to calculate in this problem since the numbers are very long.
- Need to consider carry bit.
- This linked list's head is the first node.
- My solution:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/ struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
struct ListNode* p1;
struct ListNode* p2;
struct ListNode* l3 = NULL;
struct ListNode* p3 = NULL;
p1 = l1;
p2 = l2; int carry_bit = 0;
while (p1 != NULL || p2 != NULL || carry_bit == 1) {
int num1;
int num2;
int num3;
if (p1 == NULL && p2 == NULL && carry_bit == 1) {
num3 = 1;
carry_bit = 0;
}
else {
if (p1 == NULL) {
num1 = 0;
}
else {
num1 = p1->val;
p1 = p1->next;
}
if (p2 == NULL) {
num2 = 0;
}
else {
num2 = p2->val;
p2 = p2->next;
} num3 = num1 + num2 + carry_bit;
if (num3 >= 10) {
carry_bit = 1;
num3 = num3 - 10;
}
else {
carry_bit = 0;
}
} struct ListNode* tmp;
tmp = malloc(sizeof(struct ListNode));
if (tmp == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(1);
}
tmp->val = num3;
tmp->next = NULL; if (p3 == NULL) {
p3 = tmp;
l3 = p3;
}
else {
p3->next = tmp;
p3 = p3->next;
}
} return l3;
}
3. Longest Substring Without Repeating Characters
- 重点知识:多层遍历,时间复杂度过高
- My solution:
int lengthOfLongestSubstring(char * s){
int length = strlen(s);
if (length == 1){
return 1;
}
int max = 0;
for (int i = 0; i < length; i++) {
int flag = 1;
for (int j = i + 1; j < length & flag; j++) {
for (int k = j - 1; k >= i; k--) {
if (s[j] == s[k]) {
int tmp = j - i;
if (max < tmp) {
max = tmp;
}
i = k;
flag = 0;
break;
}
if (k == i) {
int tmp = j - i + 1;
if (max < tmp) {
max = tmp;
}
}
}
}
}
return max;
}
【436】Solution for LeetCode Problems的更多相关文章
- about家庭智能设备部分硬件模块功能共享【协同工作】solution
本人设备列表: Onda tablet {Android} wifi Desktop computer {win7.centos7} 外接蓝牙adapter PS interface 键盘.鼠标{与同 ...
- 【NOIP2012TG】solution
D1T1(Vigenere) 题意:给你一个原串与一个密码串,问你按照题意规则加密后的密文. 解题思路:暴力模拟. #include <stdio.h> ],c[],u1[],u2[]; ...
- 【NOIP2014TG】solution
链接:https://www.luogu.org/problem/lists?name=&orderitem=pid&tag=83|31 D1T1(rps) 题意:给你一个周期,以及胜 ...
- 【NOIP2016TG】solution
传送门:https://www.luogu.org/problem/lists?name=&orderitem=pid&tag=83%7C33 D1T1(toys) 题意:有n个小人, ...
- 【NOIP2015TG】solution
链接:https://www.luogu.org/problem/lists?name=&orderitem=pid&tag=83%2C32 D1T1(magic) 题意:看题目.. ...
- 【NOIP2013TG】solution
链接:https://www.luogu.org/problem/lists?name=&orderitem=pid&tag=83%2C30 D1T1:转圈游戏(circle) 题意: ...
- 【NOIP2011TG】solution
老师最近叫我把NOIPTG的题目给刷掉,于是就开始刷吧= = 链接:https://www.luogu.org/problem/lists?name=&orderitem=pid&ta ...
- 【AtCoder】AGC005F - Many Easy Problems
题解 我们把一个点的贡献转化为一条边的贡献,因为边的数量是点的数量-1,最后再加上选点方案数\(\binom{n}{k}\)即可 一条边的贡献是\(\binom{n}{k} - \binom{a}{k ...
- 【C++】关键字回忆leetcode题解
20200515 前缀和 no.560 20200518 动态规划 no.152 20200520 状态压缩 no.1371 20200521 中心扩散 no.5 20200523 滑动窗口 no.7 ...
随机推荐
- Oracle中修改某个字段可以为空
待修改字段假定为:shuifen 1.当该字段为空时,可直接修改: alter table reportqymx modify shuifen null; 2.当待修改字段不为空时:新增一列把要改变的 ...
- CheckList 如何梳理可减少上线的验证时间(总结篇)
对CheckList的执行发起的思考? (1)功能越来越多,CheckList越补充越多,执行CheckList时间越来越长,如何减少上线的验证时间?(2)减少上线验证的时间外,如何保证质量?上线后少 ...
- Java - 框架之 Spring
一. IOC 和 DI IOC : 控制反转,将对象的创建权反转给了 Spring.DI : 依赖注入,前提是必须要有 IOC 的环境,Spring 管理这个类的时候将类的依赖的属性注入(设置)进来 ...
- PostgreSQL 11 新特性之覆盖索引(Covering Index)(转载)
通常来说,索引可以用于提高查询的速度.通过索引,可以快速访问表中的指定数据,避免了表上的扫描.有时候,索引不仅仅能够用于定位表中的数据.某些查询可能只需要访问索引的数据,就能够获取所需要的结果,而不需 ...
- YAML_18 ansible 判断和循环
标准循环 模式一 - name: add several users user: name={{ item }} state=present groups=wheel with_items: - te ...
- HTTP文件上传
看到网上很多链接文件(word.pdf...)可以下载,想制作http下载链接. 其实是将某文件直接放在服务器上搭建的网站上某目录下即可,例如:http://xxx:port/UpgradePack/ ...
- 洛谷 P3367 并查集模板
#include<cstdio> using namespace std; int n,m,p; ]; int find(int x) { if(father[x]!=x) father[ ...
- 【dp】P1541 乌龟棋
链接: https://www.luogu.org/problemnew/show/P1541 [思路]: 用f[a][b][c][d]表示,第一张卡用a张,第二张卡用b张..........然后就盘 ...
- XMind 8 pro for Mac(思维导图软件)附序列号和破解教程【亲测可用!!】
年后了,又到一年面试时,最近在用思维导图整理知识点,原本使用的是在线思维导图 ProcessOn,奈何免费版的个人文件数量只能有9 张,远远不能满足我的需要,所以还是使用一个本地版的吧,but依然不想 ...
- mysql几个简单的命令记录
mysql常用命令: .登录MySQL mysql -h主机 -u用户名 -p密码 . 查看MySQL数据库的字符集 show variables like '%char%'; .查看MySQL数据表 ...