Leetcode--easy系列10
#205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given "egg", "add",
return true.
Given "foo", "bar",
return false.
Given "paper", "title",
return true.
Note:
You may assume both s and t have the same length.
推断2个字符串结构是否同样(默认长度相等)。
最開始我是这样想的,将两个同构不同型的字符串按规则变为同构同型的字符串,比較转换后的字符串是否等。
如paper 'p'变为1,‘a’变为2,‘e’变为3 ‘r’变为4.则字符串变为 12134 title 类似变为12134。相等说明同构。
还有一种思路是分别遍历两个字符串,利用hash表中的s[i]位置存储t[i]中的字符。当下一次s字符串中再次出现s[j] ==s[i] 时,对于 t[j] 位置上的字符应该和先前的字符 t[i] 同样。
//0ms
bool isIsomorphic(char* s, char* t) {
int hash[128] = {0};
int i;
for( i = 0; s[i] != '\0'; i++)
{
if(!hash[s[i]])
hash[s[i]] = t[i];
else if (hash[s[i]] != t[i])
return false;
}
memset(hash,0,sizeof(hash));
for( i =0; t[i] != '\0'; i++)
{
if(!hash[t[i]])
hash[t[i]] = s[i];
else if (hash[t[i]] != s[i])
return false;
}
return true;
}
#206 Reverse Linked List
Reverse
a singly linked list.
//0ms
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode *newhead,*p,*new_p,*r;
newhead->next = head;
p = head;
r = NULL;
while(p)
{
new_p = p->next;
p->next = r;
r = p;
p = new_p;
}
return r;
}
#223
Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Assume that the total area is never beyond the maximum possible value of int.
2个对角顶点能够确定一个长方形,给定4个点的坐标。求它们构成的2个长方形覆盖的面积。
关键在于怎样依据坐标的相对大小来确定2个长方形是否相互覆盖。
//12ms
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int area = (C-A)*(D-B) + (G-E)*(H-F);
int top,bottom,left,right,cover;
if(A>=G || C<=E || B>=H || D<=F)
return area;
top = (D<=H)?D:H;
bottom = (B>=F)?B:F;
left = (A>=E)? A:E;
right = (C<=G)? C:G;
cover = (top - bottom)*(right-left);
return area-cover;
}
#226
Invert Binary Tree
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
//0ms
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* invertTree(struct TreeNode* root) {
struct TreeNode* p;
if(!root)
return NULL;
else if(!root->left && !root->right)
return root;
p = root->left;
root->left = root->right;
root->right = p; invertTree(root->left);
invertTree(root->right);
return root;
}
Leetcode--easy系列10的更多相关文章
- Leetcode算法系列(链表)之两数相加
Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...
- Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例
概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...
- Java 集合系列 10 Hashtable详细介绍(源码解析)和使用示例
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- 封装一个简单好用的打印Log的工具类And快速开发系列 10个常用工具类
快速开发系列 10个常用工具类 http://blog.csdn.net/lmj623565791/article/details/38965311 ------------------------- ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(10)- VSS源代码管理
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(10)- VSS源代码管理 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 ( ...
- 智能合约语言 Solidity 教程系列10 - 完全理解函数修改器
这是Solidity教程系列文章第10篇,带大家完全理解Solidity的函数修改器. Solidity系列完整的文章列表请查看分类-Solidity. 写在前面 Solidity 是以太坊智能合约编 ...
- hdu 2049 不easy系列之(4)——考新郎
不easy系列之(4)--考新郎 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- C#程序集系列10,强名称程序集
当一个程序集的名称,版本,文化,Public Key都做了设置,就可以把这个程序集叫做"强名称程序集".强名称程序集可以防止被仿冒或篡改.本篇首先创建一个强名称程序集,接着模拟篡改 ...
- LeetCode——single-number系列
LeetCode--single-number系列 Question 1 Given an array of integers, every element appears twice except ...
- Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法
Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法
随机推荐
- matlab 随笔
<<matlab高级编程技巧与应用:45个案例分析>> 一. 重新认识向量化编程 1.向量化编程与循环的比较 2.预分配内存更好 3.matlab中是列优先 4.归一化 数据归 ...
- 【Mysql数据库】知识点总结
本文转载自:http://www.cnblogs.com/tonghun/p/7191131.html 一 数据库常用操作 mysql -u+username -p+password:登陆数据库管理系 ...
- C++中派生类使用基类成员的问题
在C++中,派生类在定义构造函数时,会调用基类构造函数首先完成基类部分的构造: class Derive : public Base { public: Derive(string nam, int ...
- 3.3.3 使用 join 连接字段
join 命令可以将多个文件结合在一起,每个文件里的每条记录,都共享一个键值(key),键值指的是记录中的主字段,通常会是用户名称.个人姓氏.员工编号之类的数据.举例来说,两个文件,一个列出所 ...
- JavaEE JDBC 了解数据库连接池
了解数据库连接池 @author ixenos 数据库连接是有限的资源,如果用户需要离开应用一段时间,那么他占用的连接就不应该保持开放状态: 另一方面,每次查询都获取连接并在随后关闭它的代价也很高. ...
- PTA 05-树9 Huffman Codes (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/671 5-9 Huffman Codes (30分) In 1953, David ...
- 【贪心+前缀】C. Fountains
http://codeforces.com/contest/799/problem/C [题意] 有n做花园,有人有c个硬币,d个钻石 (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 ...
- Linux(4):文件属性
文件属性: # 重点: 磁盘空间不足 和 软链接与硬链接的区别 查看文件的属性: # ls lhi 文件 [root@NEO ~]# ls -lhi /etc/hosts 130078 -rw-r-- ...
- 51nod1040 最大公约数之和
求$\sum_{i=1}^{n}(i,n)$.n<=1e9. $\sum_{i=1}^{n}(i,n)=\sum_{d|n}d\sum_{i=1}^{n}[(i,n)=d]=\sum_{d|n} ...
- 如何改变linux系统的只读文件的权限
vim 编辑可以在命令模式输入 :wq! 保存退出可以用chmod 命令修改文件权限. chmod命令是非常重要的,用于改变文件或目录的访问权限.用户用它控制文件或目录的访问权限.该命令有两种用法.一 ...