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的更多相关文章

  1. about家庭智能设备部分硬件模块功能共享【协同工作】solution

    本人设备列表: Onda tablet {Android} wifi Desktop computer {win7.centos7} 外接蓝牙adapter PS interface 键盘.鼠标{与同 ...

  2. 【NOIP2012TG】solution

    D1T1(Vigenere) 题意:给你一个原串与一个密码串,问你按照题意规则加密后的密文. 解题思路:暴力模拟. #include <stdio.h> ],c[],u1[],u2[]; ...

  3. 【NOIP2014TG】solution

    链接:https://www.luogu.org/problem/lists?name=&orderitem=pid&tag=83|31 D1T1(rps) 题意:给你一个周期,以及胜 ...

  4. 【NOIP2016TG】solution

    传送门:https://www.luogu.org/problem/lists?name=&orderitem=pid&tag=83%7C33 D1T1(toys) 题意:有n个小人, ...

  5. 【NOIP2015TG】solution

    链接:https://www.luogu.org/problem/lists?name=&orderitem=pid&tag=83%2C32 D1T1(magic) 题意:看题目.. ...

  6. 【NOIP2013TG】solution

    链接:https://www.luogu.org/problem/lists?name=&orderitem=pid&tag=83%2C30 D1T1:转圈游戏(circle) 题意: ...

  7. 【NOIP2011TG】solution

    老师最近叫我把NOIPTG的题目给刷掉,于是就开始刷吧= = 链接:https://www.luogu.org/problem/lists?name=&orderitem=pid&ta ...

  8. 【AtCoder】AGC005F - Many Easy Problems

    题解 我们把一个点的贡献转化为一条边的贡献,因为边的数量是点的数量-1,最后再加上选点方案数\(\binom{n}{k}\)即可 一条边的贡献是\(\binom{n}{k} - \binom{a}{k ...

  9. 【C++】关键字回忆leetcode题解

    20200515 前缀和 no.560 20200518 动态规划 no.152 20200520 状态压缩 no.1371 20200521 中心扩散 no.5 20200523 滑动窗口 no.7 ...

随机推荐

  1. DevExpress21:SplashScreenManager控件实现启动闪屏和等待信息窗口

    DevExpress中SplashScreenManager这个控件的主要作用就是显示程序集加载之前的进度条显示和进行耗时操作时候的等待界面. 一.SplashScreenManager控件的使用 1 ...

  2. vue3 RFC初尝试

    由于vue3.x还没有正式发布,所以可以通过安装包vue-function-api提前尝试 npm install vue-function-api --save main.js import Vue ...

  3. Tomcat默认连接超时时间

    秒=1小时 2. 在web.xml中通过参数指定: xml 代码 <session-config>         <session-timeout>30</sessio ...

  4. go语言-for循环

    一.for循环语法: for 循环变量初始化:循环条件:循环变量迭代{ 循环体 }案例: 打印10句hello 方式一 package main import "fmt" func ...

  5. Python3.X下安装Scrapy

    Python3.X下安装Scrapy (转载) 2017年08月09日 15:19:30 jingzhilie7908 阅读数:519 标签: python   相信很多同学对于爬虫需要安装Scrap ...

  6. (转载) 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)

    安装完之后别忘了还需要安装SSMS,这是坑爹的地方之二,干嘛不集成到SQL Server安装包里还要用户自己单独下载 下载地址:https://msdn.microsoft.com/en-us/lib ...

  7. 2019/8/27 Test(luogu 五月天模拟赛)

    \(2019/8/27\)大考 \(\color{#ff0808}{\text{初二诀别赛(SAD)}}\) 题目名称 链接 寿司 \(BSOJ5111\) 秀秀的森林 \(BSOJ5125\) 分组 ...

  8. java.util.Properties 读取配置文件中的参数

    用法 getProperty方法的返回值是String类型. java.util.Properties 读取配置文件中的参数 //读取配置文件 FileInputStream inStream = n ...

  9. 02_SAE中创建数据表

    Step1:进入新浪云应用数据库点击应用名称,进入到该应用管理界面,在数据库服务中点击“共享型MySQL”: 开启MySQL服务,使用PHPMyAdmin管理数据库,进入MySQL数据库管理界面: S ...

  10. 洛谷 P1147 连续自然数和 题解

    P1147 连续自然数和 题目描述 对一个给定的自然数MM,求出所有的连续的自然数段,这些连续的自然数段中的全部数之和为MM. 例子:1998+1999+2000+2001+2002 = 100001 ...