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 ...
随机推荐
- Linux IPC socket编程基础
头文件 #include<unistd.h> #include <sys/types.h> #include <sys/socket.h> #include< ...
- android apk 防止反编译技术第四篇-对抗JD-GUI
又到周末一个人侘在家里无事可干,这就是程序员的悲哀啊.好了我们利用周末的时间继续介绍android apk防止反编译技术的另一种方法.前三篇我们讲了加壳技术(http://my.oschina.net ...
- Sql server2012连接Sql server 2008时出现的问题:已成功与服务器建立连接,但在登陆过程中发生错误。(provider:SSL Provider,error:0-接收到的消息异常,或格式不正确。)
以前连接是正常的,就这两天连不上了.(没有耐心的直接看末尾解决办法) 错误消息如下: 1.尝试读取或写入受保护的内存.这通常指示其他内存已损坏.(System.Data) 2.已成功与服务器建立连接, ...
- javaScript事件(二)事件处理程序
一.事件 二.事件流 以上内容见:javaScript事件(一)事件流 三.事件处理程序 前面提到,事件是用户或浏览器自身执行的某种动作,如click,load和mouseover都是事件的名字.响应 ...
- [转]How to add new table in NopCommerce
本文转自:http://www.tech-coder.com/2015/07/how-to-add-new-table-in-nopcommerce.html Hey guys I am back a ...
- SSH----MVC框架模式与分层架构
MVC框架模式 MVC框架模式是web开发中一种软件设计典范,他的全名是(Model -View -Controller),是模型(model)--视图(view)--控制器(controller)的 ...
- AC日记——Dividing poj 1014
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 69575 Accepted: 18138 Descri ...
- reflect2015破解
具体看 http://download.congci.com/download/net-reflector-7-6-1-824-wanquan-pojie#downloads *博主注:因为很多破解程 ...
- MYSQL数据库的操作
Mysql的连接方式: 1.原生函数:mysql_connect($server,$username,$password); //打开一个到Mysql服务器的连接 mysql_select_db( ...
- 适配ipone5
PROJECT和TARGETS都需要设置