leetcode   地址: https://leetcode.com/contest/detail/1

(1)-- Lexicographical Numbers

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.

好久没有上leetcode了, 突然发现leetcode开始搞竞赛了, 做了 热身赛的第一题,

简单的leetcode风格的题目,使用dfs,深度优先遍历,字典序的经典做法。

class Solution {
public:
void dfs(int cur, int n, vector<int> &ret){
if(cur > n){ return; }
ret.push_back(cur);
for(int i=; i<=; i++){
dfs(cur*+i, n, ret);
}
} vector<int> lexicalOrder(int n) {
vector<int> t;
for(int i=; i<=; i++){
dfs(i, n, t);
}
return t;
}
};

(2)  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.

查找字符串中的第一个单一字符,

典型的hash 方法, 字符串只含有lowercase的字符, 构造一个26维度的数组,作为hash table。

算法复杂度为O(n), n为字符串的长度,就是扫一次字符串就可以得到。

class Solution {
public:
int firstUniqChar(string s) {
int vis[] = {};
for(int i=; i<s.length(); i++){
if(vis[s[i]-'a'] == ){
vis[s[i]-'a'] = i+;
}else{
vis[s[i]-'a'] = -;
}
}
int ans = ;
bool flag = false;
for(int i=; i<; i++){
if(vis[i] != && vis[i] != - && vis[i] < ans){
ans = vis[i];
flag = true;
}
}
if(flag){
ans = ans - ;
}else{
ans = -;
}
return ans;
}
};

(3), Longest Absolute File Path

求出最长的文件路径(记得要加上dir与dir之间的 ‘/’ , 也算是一个字符), 明显字符串的组成就是dfs的方式,不妨利用dfs数组进行一个遍历。

时间复杂度: O(n), 扫描字符串数组的长度n。

class Solution {
public:
void dfs(int cur, int depth, int *a, int& ans, string& s){
int isFile, i = cur;
while(i < s.length()){
if(s[i] == '\n'){
isFile = ;
break;
}else if(s[i] == '.'){
isFile = ;
break;
}
i++;
}
if(isFile == ){
int tmp = ;
for(int j=; j<depth; ++j){
tmp += a[j] + ;
}
while(i<s.length() && s[i] != '\n'){
i++;
}
tmp += i - cur;
if( tmp > ans){
ans = tmp;
}
if(i < s.length() && s[i] == '\n'){
int j = i+, cnt = ;
while(j<s.length() && s[j] =='\t'){
j = j + ; cnt = cnt + ;
}
if(j < s.length() ){
dfs(j, cnt, a, ans, s);
}
}
}else{
a[depth] = i - cur;
if(s[i]=='\n'){
int j = i+, cnt = ;
while(j<s.length() && s[j]=='\t'){
j = j + ;
cnt = cnt + ;
}
if(j < s.length() ){
dfs(j, cnt, a, ans, s);
}
}
}
} int lengthLongestPath(string input) {
int *a = new int[];
for(int i=; i<; i++){
a[i] = ;
}
int ans = ;
dfs(, , a, ans, input);
delete[] a;
return ans;
}
};

leetcode-Warm Up Contest-Aug.21的更多相关文章

  1. LeetCode之Weekly Contest 93

    第一题:二进制间距 问题: 给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的 ...

  2. LeetCode之Weekly Contest 102

    第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...

  3. LeetCode之Weekly Contest 91

    第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10  ...

  4. LeetCode之Weekly Contest 90

    LeetCode第90场周赛记录 第一题:亲密字符串 问题: 给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回  ...

  5. [LeetCode] 544. Output Contest Matches 输出比赛匹配对

    During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, ...

  6. Contest 7.21(贪心专练)

    这一次都主要是贪心练习 练习地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26733#overview Problem APOJ 13 ...

  7. LeetCode之Weekly Contest 101

    前一段时间比较忙,而且做这个对于我来说挺耗时间的,已经间隔了几期的没做总结了,后面有机会补齐.而且本来做这个的目的就是为了防止长时间不做把编程拉下,不在追求独立作出所有题了.以后完赛后稍微尝试下,做不 ...

  8. LeetCode之Weekly Contest 92

    第一题:转置矩阵 问题: 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9] ...

  9. LeetCode 179. 最大数(Largest Number) 21

    179. 最大数 179. Largest Number 题目描述 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 每日一算法2019/5/24Day 21LeetCode179. La ...

  10. 【Leetcode周赛】从contest1开始。(一般是10个contest写一篇文章)

    注意,以前的比赛我是自己开了 virtual contest.这个阶段的目标是加快手速,思考问题的能力和 bug-free 的能力. 前面已经有了100个contest.计划是每周做三个到五个cont ...

随机推荐

  1. Java—Lambda基础

    虽然JVM有着Scala .Groovy .Clojure 等依赖于JVM的函数语式语言,但直到Java8才算是java正式支持函数式编程: Java8中加入了Lambda的支持标志着Java正式加入 ...

  2. 烂泥:apache虚拟主机的学习与应用

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 要配置apache的虚拟主机,我们需要分以下几步进行: 1. 检查apache虚拟主机模块 2. 开启apache虚拟主机功能 3. httpd-vho ...

  3. ARM指令

    语法格式 <opcode>{<cond>}{S} <Rd>, <Rn>,<shifter_operand> {}表示是可选的部分,<& ...

  4. centos yum源配置

    5步搞定yum源配置 作者小波/QQ463431476欢迎转载! 第一步: 卸载原来的yum [root@localhost home]#rpm -qa|grep yum|xargs rpm -e - ...

  5. openwrt修改flash大小

    前言 默认openwrt trunk编译出来的flash大小为8M,但是我们手上的板子可能flash大小更大,本文以MT7620a为例,将其flash大小由8M修改为16M或者32M 增加dts文件 ...

  6. 基于.net开发chrome核心浏览器【六】

    写在前面: 距离发这个系列的上一篇文章已经过去两个多月了 因为工作上不涉及这一部分的内容,兼且琐事缠身,一直无力动笔写这个系列的第六篇文章 然而,有很多朋友都关注这个系列,希望我能再写写. 写文章有人 ...

  7. [转]Backbone.js简单入门范例

    本文转自:http://dmyz.org/archives/598 11年刚开始用前端MVC框架时写过一篇文章,当时Knockout和Backbone都在用,但之后的项目全是在用Backbone,主要 ...

  8. 2014 UESTC暑前集训动态规划专题解题报告

    A.爱管闲事 http://www.cnblogs.com/whatbeg/p/3762733.html B.轻音乐同好会 C.温泉旅馆 http://www.cnblogs.com/whatbeg/ ...

  9. NOIP2002矩形覆盖[几何DFS]

    题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4(0,7),见图一. 这 ...

  10. [转]关于vs调试正确。但是发布到iis就出现无法访问后天局面

    最近使用extjs+ashx进行ajax请求过程操作的时候发现一个问题..当我把程序发布到iis的时候就出现一只不执行到success回调函数. 当弹出状态值一看尽然是500.我就纳闷了.又没有语法错 ...