LeetCode第二天&第三天
leetcode 第二天
2017年12月27日
4.(118)Pascal's Triangle

JAVA
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> triangle = new ArrayList<List<Integer>>();
//当numRows = 0 的情况
if(numRows == 0) return triangle;
//当numRows != 0 的情况
triangle.add(new ArrayList<Integer>());
triangle.get(0).add(1);
for(int i = 1; i <numRows;i++){
List<Integer> curRow = new ArrayList<Integer>();
List<Integer> preRow = triangle.get(i-1);
//first element
curRow.add(1);
for(int j = 0 ;j<preRow.size()-1;j++){
curRow.add(preRow.get(j)+preRow.get(j+1));
}
curRow.add(1);
triangle.add(curRow);
}
return triangle;
}
}
Python
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
triangle = []
if numRows == 0:
return triangle
triangle.append([1])
for i in range(1,numRows):
curRow = []
preRow = triangle[i-1]
curRow.append(1)
for j in range(len(preRow)-1):
curRow.append(preRow[j]+preRow[j+1])
curRow.append(1)
triangle.append(curRow)
return triangle
leetcode 第三天
2018年1月3日
5.(217)Contains Duplicate

JAVA
// T:O(nlogn) S:O(1)
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; ++i) {
if (nums[i] == nums[i + 1]) return true;
}
return false;
}
T:O(n) S:O(n)
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>(nums.length);
for (int x: nums) {
if (set.contains(x)) return true;
set.add(x);
}
return false;
}
6.(717)1-bit and 2-bit Characters

JAVA
class Solution {
public boolean isOneBitCharacter(int[] bits) {
int i = 0;
while (i<bits.length - 1){
i += bits[i]+1;
}
return i == bits.length-1;
}
}
class Solution {
public boolean isOneBitCharacter(int[] bits) {
int i = bits.length - 2;
while (i >= 0 && bits[i] > 0) i--;
return (bits.length - i) % 2 == 0;
}
}
Python
class Solution(object):
def isOneBitCharacter(self, bits):
i = 0
while i < len(bits) - 1:
i += bits[i] + 1
return i == len(bits) - 1
class Solution(object):
def isOneBitCharacter(self, bits):
parity = bits.pop()
while bits and bits.pop(): parity ^= 1
return parity == 0
7.(119)Pascal's Triangle II

Java
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> result = new ArrayList<Integer>();
if(rowIndex < 0) return result;
for(int i = 0 ;i <= rowIndex;i++){
result.add(1);
for(int j = i-1 ; j > 0 ; j--){
result.set(j,result.get(j)+result.get(j-1));
}
}
return result;
}
}
8.(695)Max Area of Island

JAVA
递归
class Solution {
int[][] grid;
boolean[][] seen;
public int maxAreaOfIsland(int[][] grid) {
this.grid = grid;
int result = 0;
seen = new boolean[grid.length][grid[0].length];
for(int r = 0;r<grid.length;r++)
for(int c = 0 ;c<grid[0].length;c++)
result = Math.max(result,area(r,c));
return result;
}
public int area(int r,int c){
if(r<0||r>=grid.length||c<0||c>=grid[0].length||seen[r][c]||grid[r][c]==0) return 0;
seen[r][c] = true;
return 1+area(r-1,c)+area(r,c-1)+area(r+1,c)+area(r,c+1);
}
}
9.(26)Remove Duplicates from Sorted Array

JAVA
class Solution {
public int removeDuplicates(int[] nums) {
int newLength = 1;
if(nums.length == 0) return 0;
for(int i = 0;i<nums.length;i++)
if(nums[newLength-1] != nums[i]){
nums[newLength]= nums[i];
newLength++;
}
return newLength;
}
}
10.(27)Remove Element

JAVA
class Solution {
public int removeElement(int[] nums, int val) {
int newLength = 0;
if(nums.length ==0) return newLength;
for(int i = 0;i<nums.length;i++)
if(nums[i]!=val)
nums[newLength++] = nums[i];
return newLength;
}
}
11.(121)Best Time to Buy and Sell Stock

JAVA
class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
int min = Integer.MAX_VALUE;
for(int i = 0;i<prices.length;i++){
min = Math.min(prices[i],min);
profit = Math.max(prices[i]-min,profit);
}
return profit;
}
}
12.(122)Best Time to Buy and Sell Stock II

JAVA
class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
for(int i =0;i<prices.length-1;i++)
if(prices[i+1]>prices[i])
profit += prices[i+1]-prices[i];
return profit;
}
}
13.(624)Maximum Distance in Arrays
Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to be their absolute difference |a-b|. Your task is to find the maximum distance.
Example 1:
Input:
[[1,2,3],
[4,5],
[1,2,3]]
Output: 4
Explanation:
One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
Note:
- Each given array will have at least 1 number. There will be at least two non-empty arrays.
- The total number of the integers in all the m arrays will be in the range of [2, 10000].
- The integers in the m arrays will be in the range of [-10000, 10000].
JAVA
public class Solution {
public int maxDistance(List<List<Integer>> arrays) {
int res = 0;
int min = arrays.get(0).get(0);
int max = arrays.get(0).get(arrays.get(0).size() - 1);
for (int i = 1; i < arrays.size(); i++) {
List<Integer> array = arrays.get(i);
res = Math.max(Math.abs(min - array.get(array.size() - 1)), Math.max(Math.abs(array.get(0) - max), res));
min = Math.min(min, array.get(0));
max = Math.max(max, array.get(array.size() - 1));
}
return res;
}
}
14.(35)Search Insert Position

JAVA
class Solution {
public int searchInsert(int[] nums, int target) {
for(int i =0;i<nums.length;i++){
if(nums[i] == target)
return i;
if(nums[i]>target)
return i;
}
return nums.length;
}
}
LeetCode第二天&第三天的更多相关文章
- 编程语言性能游戏排行榜,C/C++第一ATS第二JAVA第三
编程语言性能游戏排行榜,C/C++第一ATS第二JAVA第三 编程语言性能游戏排行榜,C/C++第一ATS第二JAVA第三
- 【Linux探索之旅】第二部分第三课:文件和目录,组织不会亏待你
内容简介 1.第二部分第三课:文件和目录,组织不会亏待你 2.第二部分第四课预告:文件操纵,鼓掌之中 文件和目录,组织不会亏待你 上一次课我们讲了命令行,这将成为伴随我们接下来整个Linux课程的一个 ...
- 【Web探索之旅】第二部分第三课:框架和内容管理系统
内容简介 1.第二部分第三课:框架和内容管理系统 2.第二部分第四课预告:数据库 第二部分第三课:框架和内容管理系统 上一课我们介绍了服务器端的编程语言,有PHP,Java,Python,Ruby ...
- 【C语言探索之旅】 第二部分第三课:数组
内容简介 1.课程大纲 2.第二部分第三课: 数组 3.第二部分第四课预告:字符串 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. C语 ...
- 第三篇——第二部分——第三文 配置SQL Server镜像——域环境
原文:第三篇--第二部分--第三文 配置SQL Server镜像--域环境 原文出处:http://blog.csdn.net/dba_huangzj/article/details/28904503 ...
- OneZero第二周第三次站立会议(2016.3.30)
会议时间:2016年3月30日 13:00~13:20 会议成员:冉华,张敏,王巍,夏一鸣. 会议目的:汇报前一天工作,全体成员评论,确定会后修改内容或分配下一步任务. 会议内容: 1.前端,完成功 ...
- 【Linux探索之旅】第二部分第三课:文件和文件夹,组织不会亏待你
wx_fmt=jpeg" alt="" style="max-width:100%; height:auto!important"> 内容简单介 ...
- 吴恩达课后习题第二课第三周:TensorFlow Introduction
目录 第二课第三周:TensorFlow Introduction Introduction to TensorFlow 1 - Packages 1.1 - Checking TensorFlow ...
- LeetCode 15. 3Sum(三数之和)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
随机推荐
- impala集成sentry
1.安装配置sentry 详细步骤见上一篇安装配置sentry. 2.配置impala 注:以下配置未集成kerberos安全认证 在/etc/imapla/conf目录下创建sentry-site. ...
- 合理使用软引用和弱引用,提升JVM内存使用性能
在项目运行时,OOM异常是比较处理的,因为从日志看出的发生异常的代码点可能仅仅是最后一根稻草,从中可能未必能发现OOM的原因,而且OOM未必是固定重现的. 上医治未病,与其等OOM问题发生时再通过看日 ...
- awk 的 pattern(模式)
我们知道, awk程序由一系列 pattern 以及与之对应的 action 组成的 rule 组成,rule之间用";"分号隔开, 一条输入记录与 pattern 匹配则执行与之 ...
- PHP date()函数详解
date (PHP 4, PHP 5) date - 格式化一个本地时间/日期 说明¶ string date ( string $format [, int $timestamp ] ) 返回将整数 ...
- auto和bool
一.auto' 1.只要在函数内部定义变量,默认是auto int num 等价于 auto int num = 10; 2.C语言中的auto关键字就是自动分配自动释放 二.bool类型 1.头文 ...
- JAVA常用知识点总结---集合篇
一.Collection 与 Collections的区别:1. Collections:java.util.Collections 是一个包装类.它包含有各种有关集合操作的静态多态方法.此类不能实例 ...
- ABAP 程序中退出操作
CHECK. 1)CHECK 后面要跟一个表达式,当表达式值为假(false)时,CHECK发生作用,退出循环(LOOP)或处理程序(Processing Block). 2)如果CHECK出现在循环 ...
- 使用TensorFlow Object Detection API+Google ML Engine训练自己的手掌识别器
上次使用Google ML Engine跑了一下TensorFlow Object Detection API中的Quick Start(http://www.cnblogs.com/take-fet ...
- Java设计模式——策略模式
策略模式的定义: 策略模式其实特别好理解,俗话说得好,条条大路通罗马,做的都是一件事,实现的方式却可以千万种,在这种情况下,如何使得每个人都可以根据自己的喜好来选择具体的方式,在调用时可以根据不同方式 ...
- chrome无法登陆账号,显示操作超时的解决方案
起因 今天重装了下windows操作系统,准备登陆chrome浏览器,以同步各种插件(你懂的),结果是...无法登陆账号,显示操作超时,真是无语了. 碰到了这个问题第一个直觉是:FQ.突然想到如果修改 ...