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已经知道~
HashMap
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进位到下位相加
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这种情况,主游标,和副游标,必然不能回退!
EOF 3;
Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321; Example2: x = -123, return -321
思路:
定于为Easy的题目,居然Test Case还包含了越界的情况,我也真是醉了….
注意:
类型 | Max Value | 备注 |
---|---|---|
int | 2147483647 | int最大21亿多点..10位数 |
long | 9223372036854775807 | long最大19位 |
double | 1.7976931348623157E308 | double好大 |
任何类型出现越界,不是error就是乱输出,这是常识!Integer.parseInt(num+"");
屡试不爽!
我用了long类型,来判断越界情况,1234567899逆转妥妥越界,但是本身并不越界。这是本题需要考虑的case!
leetcode某大神,利用越界后int数字混乱(无规律),通过简单的运算,进行了判断!
EOF 7;