date: 2015-09-09 20:20:58


Two Sum

Given an array of integers, 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.

大意:*寻找一个序列中的2个数字,使其相加等于targe,并输出这2个数字的index*

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

思路:

数字和下标,首选hashmap;

选择数字作为key值,方面通过get()得到value即index

key值重复情况的影响,仅仅出现在2个key相等并正好加上等于targe

但是,把put()方法放到判断之后,就不会出现这种情况了。因为在存入hashmap之前,index已经知道~

		public int[] twoSum(int[] nums, int target)
{
HashMap<Integer,Integer> result=new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++)
{
if(result.containsKey(target-nums[i]))
{
return new int[]{result.get(target-nums[i])+1,i+1};
}
else
result.put(nums[i], i);
}
return null;
}

HashMap<V,T> 泛型,所以int等基础类型不可!

HashMap允许key值重复,实际上是对相同key值的value覆盖,hashmap.put(1,2);hashmap.put(1,3);那么1.value=3.

get();通过key得到value.

EOF 1;

Add Two Numbers

You are given two linked lists representing two non-negative numbers.

The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

大意:2个链表,逆序存放数字,求和。如下例子,342+465=807.结果也是逆序输出.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

思路:

其实看懂了题目,感觉还是挺人性化的题目=。=,要是正序输出,岂不是还要逆转链表..

题目把咱正常的加法运算,右侧相加,向左进位,变成左侧相加,向右进位

从链表最左侧相加,有进位就进1(相加最多进位1),把当前相加结果%10添加到输出链表,当前相加结果/10进位到下位相加

	public ListNode addTwoNumbers(ListNode l1, ListNode l2)
{
ListNode resultListNode=new ListNode(0);
>>>>pListNode指向resultListNode,做输出用
ListNode pListNode=resultListNode;
int flag=0;
while(l1!=null||l2!=null)
{
if(l1!=null)
{
flag+=l1.val;
l1=l1.next;
}
if (l2!=null)
{
flag+=l2.val;
l2=l2.next;
}
resultListNode.next=new ListNode(flag%10);
>>>>new完节点,然后指向之.....我写的时候忘了=!=..
resultListNode=resultListNode.next;
flag/=10;
}
>>>>while_loop完后,flag有可能还有进位,比如555+555,所以最后还要判断下!显然if(true)flag==1;
if (flag!=0)
{
resultListNode.next=new ListNode(flag);
}
return pListNode.next;
}

EOF 2;

Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc",which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

大意:寻找字符串中无重复字符的最长字串

思路:

最长字串肯定是连续的:也就是找2个相同字符之间的长度;还要注意abba这种情况,主游标,和副游标,必然不能回退!

	public int lengthOfLongestSubstring(String s) {
{
if (0==s.length()) return 0;
HashMap<Character, Integer> hashMap=new HashMap<Character, Integer>();
int max=0;
for(int i=0,j=0;i<s.length();i++)
{
if(hashMap.containsKey(s.charAt(i)))
{
>>防止副游标j回退,我又没想到....=。=<<
j=Math.max(hashMap.get(s.charAt(i))+1,j);
}
hashMap.put(s.charAt(i), i);
max=Math.max(max, i-j+1);
}
return max;
}

EOF 3;

Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321; Example2: x = -123, return -321

思路:

定于为Easy的题目,居然Test Case还包含了越界的情况,我也真是醉了....

		public int reverse(int x)
{
if (x==0) return 0;
long num=0;
while(x!=0)
{
num=num*10+x%10;
if (num>Integer.MAX_VALUE||num<Integer.MIN_VALUE) {
return 0;
}
x/=10;
}
return Integer.parseInt(num+"");
}

注意:

类型 Max Value 备注
int 2147483647 int最大21亿多点..10位数
long 9223372036854775807 long最大19位
double 1.7976931348623157E308 double好大

任何类型出现越界,不是error就是乱输出,这是常识!Integer.parseInt(num+"");屡试不爽!

我用了long类型,来判断越界情况,1234567899逆转妥妥越界,但是本身并不越界。这是本题需要考虑的case!

leetcode某大神,利用越界后int数字混乱(无规律),通过简单的运算,进行了判断!

        int tail = x % 10;
int newResult = result * 10 + tail;
if ((newResult - tail) / 10 != result)
{ return 0; }

EOF 7;

leetcode1237的更多相关文章

  1. LeetCode1237找出给定方程的正整数解

    题目 给定方程f和值z,找出给定方程f(x,y)=z的正整数解x,y.f(x,y)关于x.y都是严格单调的. 题目保证 f(x, y) == z 的解处于 1 <= x, y <= 100 ...

随机推荐

  1. IO复用与select函数

    socket select函数的详细讲解 select函数详细用法解析      http://blog.chinaunix.net/uid-21411227-id-1826874.html linu ...

  2. C#之系统自带保存属性

    源代码下载链接 程序开发很多时候需要根据运行环境做不通的参数配置,通过写ini之类的文本文件是一种方法,但这种方法也同时会把数据暴露 Winform开发中可以将需要配置的字段属性保存到程序中(其实也是 ...

  3. 【转】关于Class.getResource和ClassLoader.getResource的路径问题

    Java中取资源时,经常用到Class.getResource和ClassLoader.getResource,这里来看看他们在取资源文件时候的路径问题. Class.getResource(Stri ...

  4. CEF3开发者系列之工程和代码结构

    CEF支持一系列的编程语言和操作系统,并且能很容易地整合到新的或已有的工程中去.它的设计思想就是易用且兼顾性能. CEF3支持一系列的编程语言和操作系统,并且能很容易地整合到新的或已有的工程中去.它的 ...

  5. Java for LeetCode 204 Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...

  6. java 基础第一季

    1. i安装jdk ii 配置环境变量:JAVA_HOME 配置jdk的安装路径 path            配置命令文件的位置 bin目录的安装路径 PATH_HOME 配置库文件的位置   l ...

  7. nyoj163_Phone List_字典树

    Phone List 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Given a list of phone numbers, determine if it i ...

  8. SQL Server order by语句学习回顾

    主要学习: 1.以指定的次序返回查询结果 2.按多个字段排序 3.按字串排序 4.处理排序空值 5.根据数据项的键排序 具体实例1---以指定的次序返回查询结果 n使用ORDER BY子句可以对结果集 ...

  9. 【leetcode】Surrounded Regions(middle)☆

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  10. 【python】入门学习(二)

    键盘读取字符串: name = input('What is your first name?').strip()print("Hello " + name.capitalize( ...