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. actionlib的身世之谜

    不知道为什么会把这么严肃认真的一篇技术整理贴起这么一个故事会风格类似的名字,就这样吧:^)shenmegui 园子里有人整理了actionlib的初学者教程,我来整理下actionlib的细节描述吧. ...

  2. scala 第一课

    val msg="Hello,World" Scala 可以根据赋值的内容推算出变量的类型.这在Scala语言中成为"type inference". Scal ...

  3. AC日记——二叉树最大宽度和高度 1501 codevs

    1501 二叉树最大宽度和高度  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver     题目描述 Description   给出一个二叉树,输出它的最大宽 ...

  4. 编辑器插件数据保存之Serializable

    Editor数据保存需求 做编辑器插件开发时,当打开一个窗口,对数值进行修改后,在关闭窗口或重新打开Unity时,希望能保存上次的数据. 相关知识 Serialization ,ScriptableO ...

  5. 解决Apache/PHP无法启动的问题

    最近经常被问到Apache无法启动的情况,所以写一篇文章,总结一下Windows下经常遇到的 Apache/PHP 无法启动的情况的解决方法. Apache/PHP 无法启动分两种情况: 1..Apa ...

  6. java 26 - 8 网络编程之 TCP协议上传图片

    上次的是上传TXT文件,这次上传的是图片.同样,上传成功需要反馈给客户端. 区别: TXT文件用记事本打开,我们可以看得懂,所以用了缓冲字符流,对通道内的字节流进行包装了. 而图片用记事本打开,我们看 ...

  7. 当一名黑客获得一份WebShell后,会做什么

    当你获得一份webshell后,你会干嘛? 我曾获得N多webshell.什么discuz.dedecms.phpwind.phpweb.aspcms等等,甚至还包括N多自己研发的线上平台. 可是,问 ...

  8. 逗号分隔的字符串转换为行数据(collection)

    逗号分隔的字符串转换为行数据(collection) CREATE OR REPLACE FUNCTION "GET_STR_TAB" (v_str in varchar2) re ...

  9. 数据表格 - DataGrid - 字段排序

    设置默认排序字段 sortName:"id",sortOrder:"desc",单独为每个字段设置排序 {field: "name", ti ...

  10. Java集合系列:-----------04fail-fast总结(通过ArrayList来说明fail-fast的原理以及解决办法)

    前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例3 f ...