Array Easy

1. 268. Missing Number

  先对数组求和,用 0 ~ n本该有的和减去当前sum得到缺失的数字。

 class Solution {
public int missingNumber(int[] nums) {
int sum = 0;
for ( int i = 0 ; i < nums.length ; i++){
sum += nums[i];
}
return ((nums.length) * (nums.length + 1 )/2) - sum;
}
}

2. 169. Majority Element

  利用HashMap键值对记录次数,并用最大次数大于n/2来判定满足条件

 class Solution {
public int majorityElement(int[] nums) {
int len = nums.length;
if( len == 1)
return nums[0]; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for( int i = 0; i < nums.length ; i++){
if(!map.containsKey(nums[i])){
map.put(nums[i],1);
}else{
int times = map.get(nums[i]);
map.put(nums[i], times+1);
if(times+1 > len/2)
return nums[i];
}
}
return 0;
}
}

3. 189. Rotate Array

  先把整个数组翻转一下, 再把前k个数字翻转一下,再把后n - k个数字翻转一下。翻转的逻辑可以梳理一下

 class Solution {
public void rotate(int[] nums, int k) {
int len = nums.length;
k = k % len;
reverse(nums, 0, len-1);
reverse(nums, 0, k -1);
reverse(nums, k, len-1);
} public static void reverse(int[] nums, int l, int r){
while( l < r){
int temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l++;
r--;
}
}
}

Leetcode 4的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

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

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. PHPCMS V9 添加二级导航

    今天看了看phpcms 写到二级导航时发现点问题,查询导航栏的信息时返回的$r[arrchildid]与自己想象的不符,文档上说是返回子栏目id但是却有些不同. 开始的思路: <ul class ...

  2. element表格切入按钮以及复选框

    1,element表格切入按钮 关键代码: html:<el-table :data="tableList" border style="width: 100%&q ...

  3. Swiper4.x使用方法

    1.首先加载插件,需要用到的文件有swiper.min.js和swiper.min.css文件.可下载Swiper文件或使用CDN. <!DOCTYPE html> <html> ...

  4. git创建分支并提交到远程分支

    来自:https://www.cnblogs.com/bluestorm/p/6252900.html 侵删 git branch(分支命令的使用http://hbiao68.iteye.com/bl ...

  5. Oracle 常用Sql 语句

    Oracle数据库常常被用作项目开发的数据库之一:有时隔段时间没使用就会忘记一些常用的sql语法,所以我们有必要记录下常用的sql 语句,当我们需要时可以快速找到并运用. 1 创建表空间.创建用户及授 ...

  6. 记MVC学习过程中一次传参到View时遇到的错误

    在跟着 <PRO ASP.NET MVC5>一书进行第七章的练习的时候遇到了以上问题, 当遇到此类问题的时候应该先检查方法传输和其视图接受的数据类型是否一致, 大多时候都是因为两者数据类型 ...

  7. Linux ssh登陆慢的两种原因分析

    Linux ssh登陆慢的两种原因分析 如果做运维就一定会遇到ssh登陆Linux服务器慢的问题,问题比较好解决,一般Google之后有很多文章都告诉你解决方法,但是很少有文章分析为什么会慢,这篇文章 ...

  8. 简单介绍python的双向队列

    介绍 大家都知道利用 .append 和 .pop 方法,我们可以把列表当作栈或者队列来用(比如,把 append 和 pop(0) 合起来用,就能模拟栈的“先进先出”的特点).但是删除列表的第一个元 ...

  9. 我的第一个python web开发框架(38)——管理员管理功能

    后台管理员的管理功能,它主要用来管理后台的登录账号,绑定权限,当然如果想将后台管理扩展成企业相关管理系统,比如用于公司人事管理,在这个基础上进行适当扩展就可以了. 我们先看看界面效果(也可以看着数据字 ...

  10. 《Python 数据科学实践指南》读书笔记

    文章提纲 全书总评 C01.Python 介绍 Python 版本 Python 解释器 Python 之禅 C02.Python 基础知识 基础知识 流程控制: 函数及异常 函数: 异常 字符串 获 ...