leetcode处女作
闲来无事【真的吗?你确定→_→
在leetcode上刷了一道题。费时一小时,也是醉了。谨以此文,纪念我的伟大成果。【呵呵
题目是找出非排序数组中缺少的最小正整数。要求时间复杂度O(n),空间复杂度为常数。
首先考虑把数组排序。发现没有排序算法满足要求,放弃。
想到找出数组中最大的数和最小的数,定义k值,从最小的数开始,在数组中依次查找,有对应的即k++,没有,返回k值,即为所求。如果min到max都存在,返回max+1。
期间忘记考虑数组长度为0的情况,报错;比较时的逻辑错误;print语句忘删等各种情况报错。
不敢说收获颇丰,但起码开始了第一步。fighting!
leetcode处女作的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- HDU 1061
#include<stdio.h> #include<string.h> int a[10]; int main() { int T,n,i,k,temp,b,t; scanf ...
- bzoj 1483 [HNOI2009]梦幻布丁(链表+启发式合并)
1483: [HNOI2009]梦幻布丁 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1818 Solved: 761[Submit][Status ...
- 用BigDecimal类实现Fibonacci算法
Fibonacci(N)=Fibonacii(N-1)+Fibonacci(N-2) 其中 Fibonacci(0)=0;Fibonacci(1)=1 用循环或则递归实现Fibonacci算法很简单, ...
- VMWare虚拟机USB连接问题
错误31:连接到系统上的设备没有发挥作用 原文链接 描述 常用 VMware 虚拟机的有事应该遇到这种情况,就是装完 VMware ,启动时 VMware 下面会有个黄框中有" USB di ...
- poj 3069 Saruman's Army (贪心)
简单贪心. 从左边开始,找 r 以内最大距离的点,再在该点的右侧找到该点能覆盖的点.如图. 自己的逻辑有些混乱,最后还是参考书上代码.(<挑战程序设计> P46) /*********** ...
- (贪心5.1.2)POJ 2287 Tian Ji -- The Horse Racing
/* * POJ_2287.cpp * * Created on: 2013年10月9日 * Author: Administrator */ #include <iostream> #i ...
- js showModalDialog打开新的页面给原页面传值问题
a.html中打开一个新页面b.html,b.html页面给a.html中的input传一个值并将value赋给input框. a.html: <html> <head> ...
- ios 工程配置统一增加类的前缀(知识点也只能算知识点)
在前边的代码规范中提及:团队开发或者个人开发为了打包或者自己工程中避免创建新的类核第三方系统的重复增加类的统一前缀!!又很多人问我这种开发小技巧.下面我就普及一下: 1.首先选中你的工程配置 2.然后 ...
- 【JAVA - SSM】之MyBatis逆向工程的使用
MyBatis逆向工程可以方便的从数据库中将表自动映射到JAVA POJO类,并同时生成Mapper.xml和Mapper接口,方便实用.下面介绍一下逆向工程的使用方法. 使用逆向工程,我们最好是新建 ...
- jqgrid使用简单记录
我要为id为jqGrid的table使用jqgrid插件. $("#jqGrid").jqGrid({ url: 'data/test.json', mtype: "GE ...