#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的更多相关文章

  1. Leetcode算法系列(链表)之两数相加

    Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...

  2. Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例

    概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...

  3. Java 集合系列 10 Hashtable详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  4. 封装一个简单好用的打印Log的工具类And快速开发系列 10个常用工具类

    快速开发系列 10个常用工具类 http://blog.csdn.net/lmj623565791/article/details/38965311 ------------------------- ...

  5. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(10)- VSS源代码管理

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(10)- VSS源代码管理 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建    ( ...

  6. 智能合约语言 Solidity 教程系列10 - 完全理解函数修改器

    这是Solidity教程系列文章第10篇,带大家完全理解Solidity的函数修改器. Solidity系列完整的文章列表请查看分类-Solidity. 写在前面 Solidity 是以太坊智能合约编 ...

  7. hdu 2049 不easy系列之(4)——考新郎

    不easy系列之(4)--考新郎 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. C#程序集系列10,强名称程序集

    当一个程序集的名称,版本,文化,Public Key都做了设置,就可以把这个程序集叫做"强名称程序集".强名称程序集可以防止被仿冒或篡改.本篇首先创建一个强名称程序集,接着模拟篡改 ...

  9. LeetCode——single-number系列

    LeetCode--single-number系列 Question 1 Given an array of integers, every element appears twice except ...

  10. Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法

    Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法

随机推荐

  1. win10x64下的redis安装与使用

    先引用百度百科的一段话吧,具体可以到百科查看吧. Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年 ...

  2. ionic小白的学习路之目录结构分析、创建组件、创建页面、页面跳转

    一. 目录结构分析 hooks:编译cordova 时自定义的脚本命令,方便整合到我们的编译系统和版本控制系统中. node_modules :node 各类依赖包. resources :andro ...

  3. WebDriverException: Message: unknown error: Chrome failed to start: crashed

    the last answer WebDriverException: Message: unknown error: Chrome failed to start: crashed

  4. 内置函数filter和map

    filter filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filter()根据判断结果自动过滤掉不符合条件的元素,返回 ...

  5. Java笔记:编写第一个Java程序

    2017.6.17 1.编写第一个Java程序 创建text文本,命名第一个Java程序.txt 在里面编写Java代码 public class Demo1{ public static void ...

  6. 【MySQL】性能优化之 Index Condition Pushdown

    一 概念介绍    Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一种在存储引擎层使用索引过滤数据的一种优化方式.a 当关闭ICP时,index ...

  7. 大数据学习——flume拦截器

    flume 拦截器(interceptor)1.flume拦截器介绍拦截器是简单的插件式组件,设置在source和channel之间.source接收到的事件event,在写入channel之前,拦截 ...

  8. Ural 1960 Palindromes and Super Abilities

    Palindromes and Super Abilities Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged ...

  9. vs code 使用心得

    Jetbrains 家族的软件适合java,python开发,但是对与rust,shell等的开发,则显得有些臃肿,需要一款轻快的编辑器,经过挑选,在sublime3 与 vs code 中选则了vs ...

  10. 【BZOJ1211】树的计数(Prufer编码)

    题意:一个有n个结点的树,设它的结点分别为v1, v2, …, vn, 已知第i个结点vi的度数为di,问满足这样的条件的不同的树有多少棵. 其中1<=n<=150,输入数据保证满足条件的 ...