386. Lexicographical Numbers\387. First Unique Character in a String

题目描述

Given an integer n, return 1 - n in lexicographical order.

For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].

Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000.

实现字典序的数字排列。

代码实现

最早的时候我理解成了:

[1,10,11,12,13,14,15,16,17,18,19,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,1000,1001]

代码实现为:

class Solution {
public:
    vector<int> lexicalOrder(int n) {
        vector<int> res(n, 0);
        int cnt = 0;
        for(int i =1; i <= 9; i++) {
            int j = 0, ind = pow(10, j);
            bool isBound = false;
            while(true) {
                int base = i*ind;
                for(int k = 0; k < pow(10, j); k++) {
                    if(base + k <= n)
                        res[cnt++] = base + k;
                    else { isBound = true; break; }    
                }
                if(isBound) break;
                ind = pow(10, ++j);
            }
        }
        return res;
    }
};

后来发现是这样的顺序:

[1,10,100,1000,1001,1002,1003,1004,1005,1006,1007,1008,1009,101,1010,1011,1012,1013,1014,1015,1016,1017,1018,1019,102,1020,1021,1022,1023,1024,1025,1026,1027,1028,1029,103,1030,1031,1032,1033,1034,1035,1036,1037,1038,1039,104,1040,1041,1042,1043,1044,1045,1046,1047,1048,1049,105,1050,1051,1052,1053,1054,1055,1056,1057,1058,1059,106,1060,1061,1062,1063,1064,1065,1066,1067]

这个时候代码实现为:

class Solution {
public:
    vector<int> lexicalOrder(int n) {
        vector<int> res(n);
        int cur = 1;
        for (int i = 0; i < n; i++) {
            res[i] = cur;
            if (cur * 10 <= n) {
                cur *= 10;
            } else {
                if (cur >= n) 
                    cur /= 10;
                cur += 1;
                while (cur % 10 == 0)
                    cur /= 10;
            }
        }
        return res;
    }
};

387. First Unique Character in a String

题目理解

Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

Note: You may assume the string contain only lowercase letters.

使用哈希解决这道题目,记录数字出现的顺序。

代码实现

class Solution {
public:
    int firstUniqChar(string s) {
        int hash[26] = {0};
        int cnt = 1;
        for(auto ch:s) {
            if(hash[ch - 'a'] < 0)   hash[ch - 'a'] *= -1; 
            else if(hash[ch - 'a'] == 0) hash[ch - 'a'] = -1*cnt;
            cnt++;
        }   

        int min = INT_MIN;
        for(int i = 0; i < 26; i++)  {
            if(hash[i] < 0) {
                if(min == INT_MIN) min = i;
                else if(hash[i] > hash[min]) 
                    min = i;
            }
        }        
        return min == INT_MIN? -1:s.find_first_of(min+'a');
    }
};

参考代码来源:https://discuss.leetcode.com/topic/55131/ac-200ms-c-solution-beats-98/2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值