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 ...
 
随机推荐
- 内联汇编获取Kernaer32基址.
			
DWORD GetKerner32ImageBase() { DWORD nIMageBase = 0; __asm { xor edx,edx mov ecx, fs:[0x30]; mov ecx ...
 - 深入理解Linux内核 学习笔记(3)
			
第三章 进程 可以看到很多熟悉的结构体 进程状态: 可运行状态(TASK_ RUNNING) 进程要么在CPU上执行,要么准备执行. 可巾断的等待状态(TASK_ INTERRUPTIBLE) 进程被 ...
 - 并发系列(3)之 CLH、MCS 队列锁简介
			
这篇博客主要是作为 AbstractQueuedSynchronizer 的背景知识介绍:平时接触也非常的少,如果你不感兴趣可以跳过:但是了解一下能更加的清楚 AQS 的设计思路: 一.自旋锁简介 通 ...
 - .NetCore WebAPI采坑之路(持续更新)
			
1.WebAPI新增日志过滤器or中间件后Action读取到的请求Body为空问题 案例: 自定义了一个中间件,用于记录每次访问webapi的入参,以及引用了Swagger. 先看下面这段代码: pu ...
 - Python二级-----------程序冲刺1
			
1. 仅使用 Python 基本语法,即不使用任何模块,编写 Python 程序计算下列数学表达式的结果并输出,小数点后保留3位. ...
 - arcgis api 4.x for js之基础地图篇
			
arcgis api3.x for js转向arcgis api4.x,我也是最近的3-4个月时间的事情,刚好公司有个webgis项目需要展示三维场景,项目选择arcgis api4.x.我纯碎记录一 ...
 - First Show
			
随便写写,记录美好生活 博客的内容主要是关于java后台开发所涉及到技术栈的学习记录
 - 浅谈SQL Server数据内部表现形式
			
在上篇文章 浅谈SQL Server内部运行机制 中,与大家分享了SQL Server内部运行机制,通过上次的分享,相信大家已经能解决如下几个问题: 1.SQL Server 体系结构由哪几部分组成? ...
 - redis数据库的基础
			
redis数据库 redis有以下三个特点 redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用 redis不仅仅支持简单的key-value类型的数据,同时还提 ...
 - Ecto中的changeset,schema,struct,map
			
概要 schema changeset struct map 总结 概要 Ecto 中, 对数据库的操作中经常用到 4 个类型: schema changeset struct map 在 Ecto ...