#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. Python matlab octave 矩阵运算基础

    基础总结,分别在三种软件下,计算 求逆矩阵 矩阵转置 等运算,比较异同 例子:正规方程法求多元线性回归的最优解 θ=(XTX)-1XTY octave: pwd()当前目录 ones() zeros( ...

  2. css--css选择器,伪类

    前戏 前面我们说过CSS规则由选择器和声明组成,我们要给标签设置属性,那我们就要找到对应的标签,CSS选择器可以帮我们找到我们需要的标签 css选择器有: 标签选择器 类选择器 ID选择器 全局选择器 ...

  3. [题解] cogs 2240 架设电话线路

    http://cogs.pro:8080/cogs/problem/problem.php?pid=2240 与洛谷P2885几乎一致,https://www.luogu.org/problemnew ...

  4. POJ-1200-Crazy Search(字符串Hash)

    Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33142 Accepted: 9079 Descrip ...

  5. bzoj3774 最优选择

    题目描述: 小N手上有一个N*M的方格图,控制某一个点要付出Aij的代价,然后某个点如果被控制了,或者他周围的所有点(上下左右)都被控制了,那么他就算是被选择了的.一个点如果被选择了,那么可以得到Bi ...

  6. CSS3---关于背景

    1.background-origin:设置元素背景图片的原始起始位置. background-origin : border-box | padding-box | content-box;    ...

  7. 04--activiti demo

    核心API1:ProcessEngine说明:1) 在Activiti中最核心的类,其他的类都是由他而来.2) 产生方式: ProcessEngine processEngine = ProcessE ...

  8. C#中如何使用正则表达式

    [草稿版本,谨慎阅读] 参考文档:正则表达式30分钟入门教程 如需系统学习正则表达式内容,请移步上述教程. 正则表达式按照指定的规则来匹配字符或字符串.'.' ' \b' ' \d'等等被称为是正则表 ...

  9. luogu3960 列队

    参考这篇 #include <iostream> #include <cstdio> #include <vector> using namespace std; ...

  10. Go切片基础

    package main import "fmt" //切片(Slice)本身没有数据,是对底层Array的一个view //不使用指针就可以改数组内容 //slice可以向后扩展 ...