1.  Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
1. 用两个循环
class Solution {
public int[] twoSum(int[] nums, int target) {
int [] res = new int [2];
for(int i = 0 ; i < nums.length ; i++) {
for(int j = 0 ; j < nums.length ; j++) {
if(i != j && target == nums[i] + nums[j]) {
res[0] = i;
res[1] = j;
}
}
}
return res;
}
}
2. 用Map

2. Two Sum II - Input array is sorted

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution and you may not use the same element twice.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

class Solution {
public int[] twoSum(int[] numbers, int target) {
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0 ; i < numbers.length ; i++) {
int tmp = target - numbers[i];
if(map.containsKey(tmp)) {
return new int []{map.get(tmp),i+1};
}
map.put(numbers[i],i+1);
}
throw new IllegalArgumentException("No two sum solution");
}
}

3. Two Sum IV - Input is a BST

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.

Example 1:

Input:
5
/ \
3 6
/ \ \
2 4 7 Target = 9 Output: True
class Solution {
public boolean findTarget(TreeNode root, int k) {
Set<Integer> set = new HashSet<>();
return find(root,k,set);
} public boolean find(TreeNode root, int k , Set<Integer> set) {
if(root == null) return false;
if(set.contains(k - root.val)) {
return true;
}
set.add(root.val);
return find(root.left,k,set) || find(root.right,k,set);
}
}

4. Rotate Array

Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

声明一个长度为原数组两倍的数组,然后拼接

class Solution {
public void rotate(int[] nums, int k) {
int [] tmp = new int[nums.length * 2];
for(int i = 0 ; i < nums.length ; i++) {
tmp[i] = tmp[i + nums.length] = nums[i];
}
if(nums.length < k) {
k = k % nums.length;
}
for(int i = 0 ; i < nums.length; i++) {
nums[i] = tmp[i + nums.length - k];
}
}
}

5. Word Pattern

Given a pattern and a string str, find if str follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

Examples:

  1. pattern = "abba", str = "dog cat cat dog" should return true.
  2. pattern = "abba", str = "dog cat cat fish" should return false.
  3. pattern = "aaaa", str = "dog cat cat dog" should return false.
  4. pattern = "abba", str = "dog dog dog dog" should return false.
    class Solution {
    public boolean wordPattern(String pattern, String str) {
    Map<Character,String> map = new HashMap<>();
    String [] strings = str.split(" ");
    if(pattern.length() != strings.length) {
    return false;
    }
    String tmp = "";
    for(int i = 0 ; i < pattern.length();i++) {
    if(map.containsKey(pattern.charAt(i))) {
    if(!map.get(pattern.charAt(i)).equals(strings[i])) {
    return false;
    }
    }else if(tmp.equals(strings[i])){
    //确定他不是所有的value都一样的情况,比如dog dog dog dog和abba
    return false;
    } else{
    map.put(pattern.charAt(i),strings[i]);
    tmp = strings[i];
    }
    }
    return true;
    }
    }

6.  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.

跟上面那个题一样的啊

class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character,Character> map = new HashMap<>();
if(s.length() != t.length()) {
return false;
}
Character tmp = '1';
for(int i =0 ; i < s.length();i++) {
if(map.containsKey(s.charAt(i))) {
if(!map.get(s.charAt(i)).equals(t.charAt(i))) {
return false;
}
}else if(tmp == t.charAt(i)) {
return false;
}else{
map.put(s.charAt(i),t.charAt(i));
tmp = t.charAt(i);
}
}
return true;
}
}

LeetCode-11-6的更多相关文章

  1. LeetCode 11. Container With Most Water (装最多水的容器)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  2. [LeetCode] 11. Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  3. LeetCode 11 水池蓄水问题

    今天给大家分享的是一道LeetCode中等难度的题,难度不大,但是解法蛮有意思.我们一起来看题目: Link Container With Most Water Difficulty Medium 题 ...

  4. Java实现 LeetCode 11 盛最多水的容器

    11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...

  5. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  6. LeetCode 11

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

  7. LeetCode——11. Container With Most Water

    一.题目链接:https://leetcode.com/problems/container-with-most-water/ 二.题目大意: 给定n个非负整数a1,a2....an:其中每一个整数对 ...

  8. LeetCode 11 Container With Most Water(分支​判断问题)

    题目链接 https://leetcode.com/problems/container-with-most-water/?tab=Description   Problem: 已知n条垂直于x轴的线 ...

  9. LeetCode(11)题解: Container With Most Water

    https://leetcode.com/problems/container-with-most-water/ 题目: Given n non-negative integers a1, a2, . ...

  10. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

随机推荐

  1. jQuery Ajax 学习(转)

    Ajax全称:Asynchronous JavaScript and XML(异步的JavaScript和XML)特点:在不必刷新整个网页的情况下实现局部更新,带来更好的用户体验.因为XMLHttpR ...

  2. java中的参数传递——值传递、引用传递

    参数是按值而不是按引用传递的说明 Java 应用程序有且仅有的一种参数传递机制,即按值传递. 在 Java 应用程序中永远不会传递对象,而只传递对象引用.因此是按引用传递对象.Java 应用程序按引用 ...

  3. flink checkpoint 源码分析 (一)

    转发请注明原创地址http://www.cnblogs.com/dongxiao-yang/p/8029356.html checkpoint是Flink Fault Tolerance机制的重要构成 ...

  4. Python内置函数之all()

    all()函数返回值不是True就是False. 它只能传入一个参数,而且参数必须是可迭代对象,换句话说,参数不是元组就是列表(通常情况下). all()中的可迭代对象所有元素值为True或者不包含元 ...

  5. SharePoint管理中心配置内容数据库

    SharePoint管理中心配置内容数据库         在SharePoint2010中,内容数据库是组织数据的核心. 是全部站点内容信息,如文档.列表数据和Web部件属性等存储的地方.默认地,内 ...

  6. (初学者)安装hadoop集群注意事项

    1.关闭防火墙 2.所有的hadoop操作都是hadoop用户下面的,同时需要用hadoop用户登录之后,对于其他的机器的hadoop用户可以免密登录 3.hadoop用户在root组下面,不是附加组 ...

  7. EasyUI DataGrid 相同连续列合并

    扩展方法:$.extend($.fn.datagrid.methods, { autoMergeCells: function(jq, fields) { return jq.each(functio ...

  8. 【转】Monkey测试4——Monkey命令行可用的全部选项

    Monkey命令行可用的全部选项 常规 --help 列出简单的用法. -v 命令行的每一个-v将增加反馈信息的级别. Level 0(缺省值)除启动提示.测试完成和最终结果之外,提供较少信息. Le ...

  9. dm8148 开发之---IDR帧

    IDR帧属于I帧. 解码器收到IDR帧时,将所有的参考帧队列丢弃(用x264_reference_reset函 数实现——在encoder.c文件中).这点是所有I帧共有的特性,但是收到IDR帧 时, ...

  10. 怎样在asp.net中用一般处理文件ashx实现下载功能

    /// <summary> /// 下载文件,支持大文件.续传.速度限制.支持续传的响应头Accept-Ranges.ETag,请求头Range . /// Accept-Ranges:响 ...