leetcode 第二天

2017年12月27日

4.(118)Pascal's Triangle

JAVA
  1. class Solution {
  2. public List<List<Integer>> generate(int numRows) {
  3. List<List<Integer>> triangle = new ArrayList<List<Integer>>();
  4. //当numRows = 0 的情况
  5. if(numRows == 0) return triangle;
  6. //当numRows != 0 的情况
  7. triangle.add(new ArrayList<Integer>());
  8. triangle.get(0).add(1);
  9. for(int i = 1; i <numRows;i++){
  10. List<Integer> curRow = new ArrayList<Integer>();
  11. List<Integer> preRow = triangle.get(i-1);
  12. //first element
  13. curRow.add(1);
  14. for(int j = 0 ;j<preRow.size()-1;j++){
  15. curRow.add(preRow.get(j)+preRow.get(j+1));
  16. }
  17. curRow.add(1);
  18. triangle.add(curRow);
  19. }
  20. return triangle;
  21. }
  22. }
Python
  1. def generate(self, numRows):
  2. """
  3. :type numRows: int
  4. :rtype: List[List[int]]
  5. """
  6. triangle = []
  7. if numRows == 0:
  8. return triangle
  9. triangle.append([1])
  10. for i in range(1,numRows):
  11. curRow = []
  12. preRow = triangle[i-1]
  13. curRow.append(1)
  14. for j in range(len(preRow)-1):
  15. curRow.append(preRow[j]+preRow[j+1])
  16. curRow.append(1)
  17. triangle.append(curRow)
  18. return triangle

leetcode 第三天

2018年1月3日

5.(217)Contains Duplicate

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

6.(717)1-bit and 2-bit Characters

JAVA
  1. class Solution {
  2. public boolean isOneBitCharacter(int[] bits) {
  3. int i = 0;
  4. while (i<bits.length - 1){
  5. i += bits[i]+1;
  6. }
  7. return i == bits.length-1;
  8. }
  9. }
  1. class Solution {
  2. public boolean isOneBitCharacter(int[] bits) {
  3. int i = bits.length - 2;
  4. while (i >= 0 && bits[i] > 0) i--;
  5. return (bits.length - i) % 2 == 0;
  6. }
  7. }
Python
  1. class Solution(object):
  2. def isOneBitCharacter(self, bits):
  3. i = 0
  4. while i < len(bits) - 1:
  5. i += bits[i] + 1
  6. return i == len(bits) - 1
  1. class Solution(object):
  2. def isOneBitCharacter(self, bits):
  3. parity = bits.pop()
  4. while bits and bits.pop(): parity ^= 1
  5. return parity == 0

7.(119)Pascal's Triangle II

Java
  1. class Solution {
  2. public List<Integer> getRow(int rowIndex) {
  3. List<Integer> result = new ArrayList<Integer>();
  4. if(rowIndex < 0) return result;
  5. for(int i = 0 ;i <= rowIndex;i++){
  6. result.add(1);
  7. for(int j = i-1 ; j > 0 ; j--){
  8. result.set(j,result.get(j)+result.get(j-1));
  9. }
  10. }
  11. return result;
  12. }
  13. }

8.(695)Max Area of Island

JAVA

递归

  1. class Solution {
  2. int[][] grid;
  3. boolean[][] seen;
  4. public int maxAreaOfIsland(int[][] grid) {
  5. this.grid = grid;
  6. int result = 0;
  7. seen = new boolean[grid.length][grid[0].length];
  8. for(int r = 0;r<grid.length;r++)
  9. for(int c = 0 ;c<grid[0].length;c++)
  10. result = Math.max(result,area(r,c));
  11. return result;
  12. }
  13. public int area(int r,int c){
  14. if(r<0||r>=grid.length||c<0||c>=grid[0].length||seen[r][c]||grid[r][c]==0) return 0;
  15. seen[r][c] = true;
  16. return 1+area(r-1,c)+area(r,c-1)+area(r+1,c)+area(r,c+1);
  17. }
  18. }

9.(26)Remove Duplicates from Sorted Array

JAVA
  1. class Solution {
  2. public int removeDuplicates(int[] nums) {
  3. int newLength = 1;
  4. if(nums.length == 0) return 0;
  5. for(int i = 0;i<nums.length;i++)
  6. if(nums[newLength-1] != nums[i]){
  7. nums[newLength]= nums[i];
  8. newLength++;
  9. }
  10. return newLength;
  11. }
  12. }

10.(27)Remove Element

JAVA
  1. class Solution {
  2. public int removeElement(int[] nums, int val) {
  3. int newLength = 0;
  4. if(nums.length ==0) return newLength;
  5. for(int i = 0;i<nums.length;i++)
  6. if(nums[i]!=val)
  7. nums[newLength++] = nums[i];
  8. return newLength;
  9. }
  10. }

11.(121)Best Time to Buy and Sell Stock

JAVA
  1. class Solution {
  2. public int maxProfit(int[] prices) {
  3. int profit = 0;
  4. int min = Integer.MAX_VALUE;
  5. for(int i = 0;i<prices.length;i++){
  6. min = Math.min(prices[i],min);
  7. profit = Math.max(prices[i]-min,profit);
  8. }
  9. return profit;
  10. }
  11. }

12.(122)Best Time to Buy and Sell Stock II

JAVA
  1. class Solution {
  2. public int maxProfit(int[] prices) {
  3. int profit = 0;
  4. for(int i =0;i<prices.length-1;i++)
  5. if(prices[i+1]>prices[i])
  6. profit += prices[i+1]-prices[i];
  7. return profit;
  8. }
  9. }

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:

  1. Each given array will have at least 1 number. There will be at least two non-empty arrays.
  2. The total number of the integers in all the m arrays will be in the range of [2, 10000].
  3. The integers in the m arrays will be in the range of [-10000, 10000].
JAVA
  1. public class Solution {
  2. public int maxDistance(List<List<Integer>> arrays) {
  3. int res = 0;
  4. int min = arrays.get(0).get(0);
  5. int max = arrays.get(0).get(arrays.get(0).size() - 1);
  6. for (int i = 1; i < arrays.size(); i++) {
  7. List<Integer> array = arrays.get(i);
  8. res = Math.max(Math.abs(min - array.get(array.size() - 1)), Math.max(Math.abs(array.get(0) - max), res));
  9. min = Math.min(min, array.get(0));
  10. max = Math.max(max, array.get(array.size() - 1));
  11. }
  12. return res;
  13. }
  14. }

14.(35)Search Insert Position

JAVA
  1. class Solution {
  2. public int searchInsert(int[] nums, int target) {
  3. for(int i =0;i<nums.length;i++){
  4. if(nums[i] == target)
  5. return i;
  6. if(nums[i]>target)
  7. return i;
  8. }
  9. return nums.length;
  10. }
  11. }

LeetCode第二天&第三天的更多相关文章

  1. 编程语言性能游戏排行榜,C/C++第一ATS第二JAVA第三

    编程语言性能游戏排行榜,C/C++第一ATS第二JAVA第三 编程语言性能游戏排行榜,C/C++第一ATS第二JAVA第三

  2. 【Linux探索之旅】第二部分第三课:文件和目录,组织不会亏待你

    内容简介 1.第二部分第三课:文件和目录,组织不会亏待你 2.第二部分第四课预告:文件操纵,鼓掌之中 文件和目录,组织不会亏待你 上一次课我们讲了命令行,这将成为伴随我们接下来整个Linux课程的一个 ...

  3. 【Web探索之旅】第二部分第三课:框架和内容管理系统

    内容简介 1.第二部分第三课:框架和内容管理系统 2.第二部分第四课预告:数据库   第二部分第三课:框架和内容管理系统 上一课我们介绍了服务器端的编程语言,有PHP,Java,Python,Ruby ...

  4. 【C语言探索之旅】 第二部分第三课:数组

    内容简介 1.课程大纲 2.第二部分第三课: 数组 3.第二部分第四课预告:字符串 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. C语 ...

  5. 第三篇——第二部分——第三文 配置SQL Server镜像——域环境

    原文:第三篇--第二部分--第三文 配置SQL Server镜像--域环境 原文出处:http://blog.csdn.net/dba_huangzj/article/details/28904503 ...

  6. OneZero第二周第三次站立会议(2016.3.30)

    会议时间:2016年3月30日  13:00~13:20 会议成员:冉华,张敏,王巍,夏一鸣. 会议目的:汇报前一天工作,全体成员评论,确定会后修改内容或分配下一步任务. 会议内容: 1.前端,完成功 ...

  7. 【Linux探索之旅】第二部分第三课:文件和文件夹,组织不会亏待你

    wx_fmt=jpeg" alt="" style="max-width:100%; height:auto!important"> 内容简单介 ...

  8. 吴恩达课后习题第二课第三周:TensorFlow Introduction

    目录 第二课第三周:TensorFlow Introduction Introduction to TensorFlow 1 - Packages 1.1 - Checking TensorFlow ...

  9. 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 ...

随机推荐

  1. js_1_变量类型

    js中有哪些变量类型? 数字(包括int和float),字符串,数组(字典,js没有字典类型,把字典看成一个对象) 如何把字符转成数字呢? obj.parseInt()         //  转化成 ...

  2. 递归演示程序(swift)

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  3. linkin大话数据结构--字符串,数组,list之间的互转

    在实际开发中,我们经常会用到字符串,字符数组,字符list,当然也会不可避免的进行这3者之间的互相转换. 在使用到Apache和Google下的common包,可以这样子实现: package tz. ...

  4. java ecplise配置

    运行java项目首先配置java运行时环境:Window->Preferences->Java->Installed JREs 修改为jdk:C:\Program Files\Jav ...

  5. JS学习--DOM

    1.概念 文档对象模型DOM,定义访问和处理HTML文档的标准方法.DOM将HTML呈现为带有元素.属性和文本的树结构(节点树). 2.document.getElementById("id ...

  6. HTTP协议篇(一):多工、数据流

    管道机制.多工 管道机制(Pipelining) HTTP 1.1 引入了管道机制(Pipelining),即客户端可通过同一个TCP连接同时发送多个请求.如果客户端需要请求两个资源,以前的做法是在同 ...

  7. CSS选择器的组合选择器之后代选择器和子元素选择器

    实例代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...

  8. yum安装centos系统依赖库

    安装centos系统依赖库,安装软件过程中,经常需要的一些库,可以在编译安装软件前执行如下命令: 首先更新系统(这步可以不执行) yum -y update 这种更新是全部更新,但是有时一些软件不想更 ...

  9. Ubuntu14.04 命令行下安装teamviewer

    下载teamviewer 链接:https://pan.baidu.com/s/1hs0BppM  密码:sdmk 上传到 /home/[user] cd /home/[user] 移动安装包到 /o ...

  10. ABP官方文档翻译 4.1 应用服务

    应用服务 IApplicationService接口 ApplicationService类 CrudService和AsyncCrudAppService类 简单的CRUD应用服务示例 自定义CRU ...