面试笔试杂项积累-leetcode 241-245
来源:互联网 发布:linux 查看java版本 编辑:程序博客网 时间:2024/05/13 00:00
242.242-Valid Anagram-Difficulty: Easy
Given two strings s and t, write a function to determine ift is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
思路
查找t的字母是否全和s相同(数目也相同)
使用哈希表匹配轻松解决
要注意重复情况,在哈希的value几下当前字母个数,t遇到就-1,到0还有的话就是false了
public class Solution { public bool IsAnagram(string s, string t) { if (s.Length != t.Length) return false; Hashtable hash = new Hashtable(); for (int i = 0; i < s.Length; i++) { if (hash.ContainsKey(s[i])) hash[s[i]] = (int)hash[s[i]] + 1; else hash.Add(s[i], 1); } for (int i = 0; i < s.Length; i++) { if (hash.ContainsKey(t[i])) { if ((int)hash[t[i]] <= 0) return false; hash[t[i]] = (int)hash[t[i]] - 1; } else return false; } return true; }}
1 0
- 面试笔试杂项积累-leetcode 241-245
- 面试笔试杂项积累-leetcode 1-5
- 面试笔试杂项积累-leetcode 6-10
- 面试笔试杂项积累-leetcode 11-15
- 面试笔试杂项积累-leetcode 16-20
- 面试笔试杂项积累-leetcode 21-25
- 面试笔试杂项积累-leetcode 26-30
- 面试笔试杂项积累-leetcode 31-35
- 面试笔试杂项积累-leetcode 36-40
- 面试笔试杂项积累-leetcode 41-45
- 面试笔试杂项积累-leetcode 46-50
- 面试笔试杂项积累-leetcode 51-55
- 面试笔试杂项积累-leetcode 56-60
- 面试笔试杂项积累-leetcode 61-65
- 面试笔试杂项积累-leetcode 66-70
- 面试笔试杂项积累-leetcode 71-75
- 面试笔试杂项积累-leetcode 76-80
- 面试笔试杂项积累-leetcode 81-85
- 完美运动框架(可以改变元素的透明度,宽,高,top,left,字体大小等)
- sql存储过程的加密和解密(适用于SQLSERVER2000存储过程)
- android 截图
- java集合中hashSet与TreeSet学习总结
- Object Property 参数使用
- 面试笔试杂项积累-leetcode 241-245
- Light OJ 1169 - Monkeys on Twin Tower (简单DP)
- Android批量图片加载经典系列——Volley框架实现多布局的新闻列表
- 1031. Hello World for U (20)
- 【第一天】初步接触jQuery
- 为什么Float类型不能直接相等
- 表格布局TableLayout ——实现商品浏览页面
- Centos环境下配置Aapache+2个tomcat8 的动静分离,负载均衡
- how to unlock /opt in ubuntu