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. 内核中 EXPORT_SYMBOL 标志分析

    内核版本:Linux-4.19 1. EXPORT_SYMBOL 的作用: EXPORT_SYMBOL 定义的函数或者符号对全部内核代码公开,不用修改内核代码就可以在其它内核模块中直接调用,即使用 E ...

  2. SAMBA服务和FTP服务讲解(week3_day1)--技术流ken

    samba服务 Smb主要作为网络通信协议; Smb是基于cs架构: 完成Linux与windows之间的共享:linux与linux之间共享用NFS 第一步:安装samba [root@ken ~] ...

  3. 利用 Blob 处理 node 层返回的二进制文件流字符串并下载文件

    博客地址:https://ainyi.com/65 解释 | 背景 看到标题有点懵逼,哈哈,实际上是后端将文件处理成二进制流,返回到前端,前端处理这个二进制字符串,输出文件或下载 最近公司有个需求是用 ...

  4. function string类型的参数传递

    1.错误案例: Index:1 Uncaught ReferenceError: 系统管理 is not defined at HTMLAnchorElement.onclick (Index:1) ...

  5. js转换时间戳-转换成 yyyy-MM-dd HH:mm:ss

    比如:转换成 yyyy-MM-dd HH:mm:ss //时间戳转换方法 date:时间戳数字 function formatDate(date) { var date = new Date(date ...

  6. Flask实战第3天:url_for使用

    我们之前是通过url来找到对应的视图函数 /     =>    hello_world 那么url_for则是通过视图函数找到url hello world  =>  / 演示如下 #c ...

  7. PHP制作个人博客-广告位添加与调用 推荐文章数据调取

    上一节博客的导航我们已经动态调取,这一节我们主讲一下如何根据页面布局,后台添加广告位,及模板上动态调取广告.博客推荐文章的数据调用. 首先我们在云码博客的后台添加10条左右的测试数据,thinkcmf ...

  8. 基于python的种子搜索网站,你懂得!

    该项目是基于python的web类库django开发的一套web网站,给师弟做的毕业设计.本人的研究方向是一项关于搜索的研究项目.在该项目中,笔者开发了一个简单版的搜索网站,实现了对数据库数据的检索和 ...

  9. PDF转图片工具

    点击下载( 提取码:1ll1 ) 软件功能基于mupdf,UI使用wxpython开发 功能: 支持pdf转图片,图片格式png 支持批量转换 使用: 第一步,点击按钮添加文档到列表,或直接将待转换文 ...

  10. 查看apk签名 和 keystore 的信息

    原文出处:https://www.jianshu.com/p/90b698002215 1.keytool -printcert -file ***(把apk文件下的META- INF文件夹解压出来, ...