三天没有写题了,罪过!--Hash Table Start
(1)Island Perimeter

解题思路:
在矩阵上循环并记录岛(1)的个数;如果当前节点是岛,则检查其是否具有任何右邻居或下邻居,有的话邻居计数加1 ;岛的周长结果为islands * 4 - neighbors * 2.(乘2的原因是因为一条边属于两个邻居)。
代码如下:
public class Solution {
public int islandPerimeter(int[][] grid) {
int islands = 0;
int neighbours =0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == 1) {
islands++;
if (i < grid.length - 1 && grid[i+1][j] == 1) neighbours++;
if (j < grid[0].length - 1 && grid[i][j+1] == 1) neighbours++;
}
}
}
return islands * 4 - neighbours * 2;
}
}
代码中if(j<grid[i].length&&。。。)要注意!!!
(2)Single Number

解题思路:
在java程序里面的异或用法: 相同输出0,不同输出1。
例如: System.out.println(1^1); 输出0 System.out.println(1^2);输出3,因为最后2个低位都不一样,所以输出3 。
异域的概念是相同为0不同为1.如果两个数值异或后的值相同,异或前可能不同。 比如二进制:0010^0001=0011 而0000^0011=0011。 异或要慎用。
使用异或运算上述两个性质来解本题:自己与自己异或结果为0;异或满足交换律。
代码如下:
public class Solution {
public int singleNumber(int[] nums) {
if(nums == null || nums.length == 0) {
return -1;
}
int rst = 0;
for (int i = 0; i < nums.length; i++) {
rst ^= nums[i];
}
return rst;
}
}
(3)Find the Difference

最优解题思路:依旧是使用异或运算,除了字符串t中的多余字符外,其余字符都可以在异或运算中消除掉。
代码如下:
public class Solution {
public char findTheDifference(String s, String t) {
char rst = 0x00;
for (int i = 0; i < s.length(); i++) {
rst = (char)(rst ^ s.charAt(i));
}
for (int i = 0; i < t.length(); i++) {
rst = (char)(rst ^ t.charAt(i));
}
return rst;
}
}
注意字符串的长度s.length(),取某个字符s.charAt(i)的使用。
正常思路:用两个计数数组统计s和t的字母出现次数,不一样的那个就是答案。
代码如下:
public char findTheDifference(String s, String t) {
int count1[] = new int[26];
int count2[] = new int[26];
int i;
for (i = 0; i < s.length(); i++) {
count1[s.charAt(i)-'a']++;
}
for (i = 0; i < t.length(); i++) {
count2[t.charAt(i)-'a']++;
}
for (i = 0; i < 26; i++) {
if (count1[i] != count2[i])
return (char) ('a' + i);
}
return 0;
}
(4)Intersection of Two Arrays

解题思路一:
不能有重复数字,就想到使用数据类型Set。
逻辑原理: 数组一的数据存入hashset;遍历数组二如果set中存在该数据存入新的hashset中,同时从set中remove该元素,防止多个元素重复;遍历新hashset转变为array返回数据。
代码如下:
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if (nums1 == null || nums2 == null) {
return null;
}
Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums1.length; i++) {
set.add(nums1[i]);
}
Set<Integer> insertset = new HashSet<>();
for (int i = 0; i < nums2.length; i++) {
if (set.contains(nums2[i])) {
insertset.add(nums2[i]);
set.remove(nums2[i]);//防止元素重复
}
}
//变为数组返回
int[] array = new int[insertset.size()];
int index = 0;
for (Integer num : insertset) {
array[index++] = num;
}
return array;
}
}
解题思路二:
1)对数组nums1进行排序;
2)对数组nums2进行排序;
3)遍历数组nums1和nums2中元素,并比较对应的元素,
- 若相等,则判断其值是否与结果中最后保存的元素(用mark记录)是否相等,相等则直接变化两个索引,否则将该值保存到结果中,并变化两个索引
- 不等,则变化较小元素对应的索引即可。
代码如下:
public int[] intersection(int[] nums1, int[] nums2){
Arrays.sort(nums1);
Arrays.sort(nums2);
ArrayList result = new ArrayList();
int mark = 0;
for (int i = 0, j = 0; i < nums1.length && j < nums2.length; ){
if (nums1[i] == nums2[j]){
if (result.size() == 0 || nums1[i] != mark){
result.add(nums1[i]);
mark = nums1[i];
}
i++;
j++;
} else if (nums1[i] < nums2[j]) {
i++;
} else {
j++;
}
}
int[] res = new int[result.size()];
for (int i = 0; i < result.size(); i++){
res[i] = (int) result.get(i);//字符类型转换
}
return res;
}
三天没有写题了,罪过!--Hash Table Start的更多相关文章
- CMU-15445 LAB1:Extendible Hash Table, LRU, BUFFER POOL MANAGER
概述 最近又开了一个新坑,CMU的15445,这是一门介绍数据库的课程.我follow的是2018年的课程,因为2018年官方停止了对外开放实验源码,所以我用的2017年的实验,但是问题不大,内容基本 ...
- 学习hash_map从而了解如何写stl里面的hash函数和equal或者compare函数
---恢复内容开始--- 看到同事用unordered_map了所以找个帖子学习学习 http://blog.sina.com.cn/s/blog_4c98b9600100audq.html (一)为 ...
- 2018 HDU多校第三场赛后补题
2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...
- 【转载】一步一步写算法(之hash表)
转载自:http://blog.csdn.net/feixiaoxing/article/details/6885657 [ 声明:版权所有,欢迎转载,请勿用于商业用途. 联系信箱:feixiaox ...
- 一步一步写算法(之hash表)
[ 声明:版权全部,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] hash表,有时候也被称为散列表.个人觉得,hash表是介于链表和二叉树之间的一种中间结构.链 ...
- jQuery中.bind() .live() .delegate() .on()的区别 和 三种方式写光棒事件 动画
地狱的镰刀 bind(type,[data],fn) 为每个匹配元素的特定事件绑定事件处理函数. $("a").bind("click",function(){ ...
- JavaScript--------------------jQuery中.bind() .live() .delegate() .on()的区别 和 三种方式写光棒事件 动画
bind(type,[data],fn) 为每个匹配元素的特定事件绑定事件处理函数. $("a").bind("click",function(){alert( ...
- feign三:覆写feign的默认配置及feign的日志
feign三:覆写feign的默认配置及feign的日志 默认配置复写 本项目地址:http://192.168.1.103:7601 本例是通过feign调用 eureka项目中的/eureka/a ...
- 使用Typescript重构axios(三十二)——写在最后面(总结)
0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...
随机推荐
- Nginx 502 Bad Gateway 错误的原因及解决方法
http://my.oschina.net/zhouyuan/blog/118708 刚才在调试程序的时候,居然服务器502错误,昨天晚上也发生了,好像我没有做非常规的操作. 然后网上寻找了下答案, ...
- MYSQL安装--小白教程
这个是mysql的安装过程,其实mysql的安装也很简单,但是我安装了一下午!!一下午!!原因就是,我把mysql的官网都翻遍了,都没找到64bit的.msi安装包,后来才想到好像64bit的电脑可以 ...
- Javascript 中的 && 和 || 使用小结
准备两个对象用于下面的讨论. var alice = { name: "alice", toString: function () { return this.name; } }; ...
- svn服务器迁移(生成dump)
首先介绍一下dump文件 一定要进入VisualSVN服务端的安装目录里的bin目录下面,然后再执行svnadmin 相关命令. 不然会出现下图中的“svnadmin不是内部命令或外部命令,也不是可 ...
- protobuf 数据解析的2种方法
方法1: message person{required int32 age = 1;required int32 userid = 2;optional string name = 3;} mess ...
- 【转】C++怎么读写windows剪贴板的内容?比如说自动把一个字符串复制.
// 复制数据至剪切板BOOL CopyToClipboard(const char* pszData, const int nDataLen){ if(::OpenClipboard(NULL)) ...
- Mabitis 多表查询(一)resultType=“java.util.hashMap”
1.进行单表查询的时候,xml标签的写法如下 进行多表查询,且无确定返回类型时 xml标签写法如下: <select id="Volume" parameterType=&q ...
- OpenStack nova VM migration (live and cold) call flow
OpenStack nova compute supports two flavors of Virtual Machine (VM) migration: Cold migration -- mig ...
- [网络] 用 OpenVPN 实现站对站 VPN 服务
一.简介 一般情况下站对站 VPN 是用 cisco.juniper.h3c等品牌的专业设备实现的,这些设备性能可靠,价格较贵. 如果现有设备不支持,而且不能用新设备替换,或者部门预算有限,那么只能用 ...
- SQL Error (1130): Host '192.168.1.126' is not allowed to connect to this MySQL server
通过HeidiSQL连接MYSQL数据库报错: SQL Error (1130): Host '192.168.1.126' is not allowed to connect to this MyS ...