zoukankan      html  css  js  c++  java
  • C语言每日一题

    66. 加一

    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    
     /*
     从后向前(从个位)开始+1,逢九进一,不为九时结束,全为九时数组长度加一
    解法如下: 特别要注意的是returnSize,这个代表返回指针的长度,要不上层代码不知道要取多少个
    */
    int* plusOne(int* digits, int digitsSize, int* returnSize)
    {
        for (int i = digitsSize - 1; i >= 0; i--) {
            if (digits[i] == 9) {
                digits[i] = 0;
            } else {
                digits[i]++;
                *returnSize = digitsSize;
                return digits;
            }
        }
    
        int *res = malloc(sizeof(int) * (digitsSize + 1));
        memset(res, 0, sizeof(int) * (digitsSize + 1));
        *returnSize = digitsSize + 1;
        res[0] = 1;
        return res;
    }

    735. 行星碰撞

    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* asteroidCollision(int* asteroids, int asteroidsSize, int* returnSize)
    {
        *returnSize = 0;
        if (asteroids == NULL) {
            return NULL;
        }
    
        int *res = malloc(sizeof(int) * asteroidsSize);
        int idx = 0;
        for (int i = 0; i < asteroidsSize; i++) {
            res[idx++] = asteroids[i];
            // 碰撞条件:左正右负
            while (idx >= 2 && res[idx - 2] > 0 && res[idx - 1] < 0) {
                int left = res[idx - 2];
                int right = res[idx - 1];
                if (abs(left) == abs(right)) {
                    idx -= 2;
                } else if (abs(left) < abs(right)) {
                    res[idx - 2] = right;
                    idx--;
                } else {
                    idx--;
                }
            }
        }
    
        *returnSize = idx;
        return res;
    }

    3. 无重复字符的最长子串

    typedef struct {
        int c; // 要用int
        int value;
        UT_hash_handle hh;
    } hashTable;
    
    hashTable *g_hash = NULL;
    
    hashTable* FindNode(int ic)
    {
        hashTable* tmp = NULL;
        HASH_FIND_INT(g_hash, &ic, tmp);
        return tmp;
    }
    
    void AddNode(int ic)
    {
        hashTable *it = FindNode(ic);
        if (it == NULL) {
            it = malloc(sizeof(hashTable));
            it->c = ic;
            HASH_ADD_INT(g_hash, c, it);
        }
        it->value = 1;
    }
    
    void DelNode(int ic)
    {
        hashTable *it = FindNode(ic);
        if (it != NULL) {
            HASH_DEL(g_hash, it);
            it->value = 0;
            free(it);
        }
    }
    
    void ClearAllNode()
    {
        hashTable *curr, *tmp;
        HASH_ITER(hh, g_hash, curr, tmp) {
            HASH_DEL(g_hash, curr);
            free(curr);
        }
    }
    
    int lengthOfLongestSubstring(char * s)
    {
        if (s == NULL) {
            return 0;
        }
        if (s[0] == '') {
            return 0;
        }
    
        int start = 0;
        int end = 0;
        int max = 0;
        int subLen = 0;
        while (s[end] != '') {
            if (FindNode((int)s[end]) == NULL) {
                AddNode((int)s[end]);
                subLen = end - start + 1;
                max = subLen > max ? subLen : max;
                end++;
            } else {
                DelNode((int)s[start]);
                start++;
            }
        }
        ClearAllNode();
        return max;
    }
  • 相关阅读:
    Elementary Methods in Number Theory Exercise 1.2.25
    Elementary Methods in Number Theory Exercise 1.2.14
    图解欧几里德算法
    图解欧几里德算法
    Elementary Methods in Number Theory Exercise 1.2.14
    Android中的长度单位详解(dp、sp、px、in、pt、mm)
    分享下多年积累的对JAVA程序员成长之路的总结
    android异常之都是deamon惹的祸The connection to adb is down, and a severe error has occured.
    TomatoCartv1.1.8.2部署时报错
    JavaScript浏览器对象之二Document对象
  • 原文地址:https://www.cnblogs.com/kongweisi/p/15437499.html
Copyright © 2011-2022 走看看