leetcode-Warm Up Contest-Aug.21
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的更多相关文章
- LeetCode之Weekly Contest 93
第一题:二进制间距 问题: 给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的 ...
- LeetCode之Weekly Contest 102
第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode之Weekly Contest 90
LeetCode第90场周赛记录 第一题:亲密字符串 问题: 给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回 ...
- [LeetCode] 544. Output Contest Matches 输出比赛匹配对
During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, ...
- Contest 7.21(贪心专练)
这一次都主要是贪心练习 练习地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26733#overview Problem APOJ 13 ...
- LeetCode之Weekly Contest 101
前一段时间比较忙,而且做这个对于我来说挺耗时间的,已经间隔了几期的没做总结了,后面有机会补齐.而且本来做这个的目的就是为了防止长时间不做把编程拉下,不在追求独立作出所有题了.以后完赛后稍微尝试下,做不 ...
- LeetCode之Weekly Contest 92
第一题:转置矩阵 问题: 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9] ...
- LeetCode 179. 最大数(Largest Number) 21
179. 最大数 179. Largest Number 题目描述 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 每日一算法2019/5/24Day 21LeetCode179. La ...
- 【Leetcode周赛】从contest1开始。(一般是10个contest写一篇文章)
注意,以前的比赛我是自己开了 virtual contest.这个阶段的目标是加快手速,思考问题的能力和 bug-free 的能力. 前面已经有了100个contest.计划是每周做三个到五个cont ...
随机推荐
- ixgbe 82599 固定源与目标, UDP, 64字节小包, 1488w pps 单核CPU软中断sirq 100%
ixgbe 82599 固定源与目标, UDP, 64字节小包, 1488w pps 单核CPU软中断sirq 100% 注: 测试使用, 正常应用不要开启 五元组不同, 开启ntupleethtoo ...
- [转]UpdatePanel的用法详解
本文转自:http://www.cnblogs.com/shangxia/articles/2281782.html 今天用做日历显示本月的考勤记录,用到了UpdatePanel控件,才发现对这个控件 ...
- LinuxAsm#Chapter10
Dividing and Conquering Book: Assembly Language step by step Complexity kills programs. Remember to ...
- Codeforces 486E LIS of Sequence --树状数组求LIS
题意: 一个序列可能有多个最长子序列,现在问每个元素是以下三个种类的哪一类: 1.不属于任何一个最长子序列 2.属于其中某些但不是全部最长子序列 3.属于全部最长子序列 解法: 我们先求出dp1[i] ...
- CF723D. Lakes in Berland[DFS floodfill]
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 小机房的树 codevs 2370
2370 小机房的树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 小机房有棵焕狗种的树 ...
- [No00000C]Word快捷键大全 Word2013/2010/2007/2003常用快捷键大全
Word对于我们办公来说,是不可缺少的办公软件,因为没有它我们可能无法进行许多任务.所以现在的文员和办公室工作的人,最基础的就是会熟悉的使用Office办公软件.在此,为提高大家Word使用水平,特为 ...
- 关于ubuntu的sources.list总结
一.作用 文件/etc/apt/sources.list是一个普通可编辑的文本文件,保存了ubuntu软件更新的源服务器的地址.和sources.list功能一样的是/etc/apt/sources. ...
- Json数据与Json数据转换
1.json数据 [{\"IS_DISTRIBUTOR_LIMIT\":0,\"PROVISION_PRICE\":null,\"PRO_STATUS ...
- 在把webpack作为本地开发依赖安装的时候报错
在把webpack作为本地开发依赖安装的时候报错 Refusing to install webpack as a dependency of itself 原因是package.json里的name ...