Leetcode 4
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的更多相关文章
- 我为什么要写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 ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- 【Vue】 ----- 浅谈vue的生命周期
一.概念 vue生命周期,又叫生命周期钩子函数,是组件从创建到销毁的过程. 二.主要的八大生命周期 1.首先,为方便观察每个周期的特点,我们模拟一个"one"组件的创建与销毁,并在 ...
- Bootstrap实现注册界面
样式一 例图 代码 <head> <meta charset="UTF-8"> <title>用户注册</title> <li ...
- 清理mac的硬盘空间,清理Xcode,清除“其他”
下面是一些清理的方法:打开Finder使用快捷键command+shift+g输入路径即可进入该文件夹 1. 移除DerivedData,建议定期清理,会重新生成 此文件夹内是模拟器运行每个APP生成 ...
- OpenCL——把vector变成scalar
https://stackoverflow.com/questions/46556471/how-may-i-convert-cast-scalar-to-vector-and-vice-versa- ...
- jmeter接口测试实战-创建用户
jmeter接口测试实战-创建用户 相信大多数看到标题的同学都会有疑问, 创建用户不是很简单吗, 调用一下创建用户接口, 传入指定入参, 用户即可创建成功, 今天我们的实战来讲讲创建场景.通过接口创建 ...
- d3.svg.line()错误:TypeError: d3.svg.line is not a function
var line_generator= d3.svg.line() .x(function (d,i) { return i; }) .y(function (d) { return d; }) 错误 ...
- HDU 6152 - Friend-Graph
Friend-Graph Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Delphi中使用ISuperObject解析Json数据
Java.Php等语言中都有成熟的框架来解析Json数据,可以让我们使用很少的代码就把格式化好的json数据转换成程序可识别的对象或者属性,同时delphi中也有这样的组件来实现此功能,即Isuper ...
- Windows Server(r12) - 配置 MySQL 远程访问
Windows Server(r12) - 配置 MySQL 远程访问 工作主要为两部分, 一部分是 Windows 防火墙, 一部分是 MySQL 自身 Windows 端口远程访问 其实就是在 W ...
- JAVA EE获取浏览器和操作系统信息
一.原理说明: 1. 浏览器访问服务端时,Http请求头上会带上客户端一些信息,可通过"user-agent"获取. //java获取方法如下,其他语言也有自己获取方法 Stri ...