【leetcode】

1. Two Sum

【题目】https://leetcode.com/problems/two-sum/description/

【思路】将数组 利用 map 处理 即可

【代码】

 1 class Solution {
 2 public:
 3     vector<int> twoSum(vector<int>& nums, int target) {
 4         // 哈希表存储 值->下标 的映射
 5         unordered_map<int, int> indices;
 6         
 7         for (int i = 0; i < nums.size(); i++)
 8         {
 9             int temp = target - nums[i];
10             if (indices.find(temp) != indices.end() && indices[temp] != i)
11             {
12                 return vector<int> {indices[temp], i};
13             }
14             indices.emplace(nums[i], i);
15         }
16     }
17 };
C++
 1 class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         Map<Integer, Integer> indeces = new HashMap<>();
 4         for(int i=0;i<nums.length;++i) {
 5             int temp = target - nums[i];
 6             if(indeces.containsKey(temp)) {
 7                 return new int[] {indeces.get(temp), i};
 8             }
 9             indeces.put(nums[i], i);
10         }
11         return null;
12     }
13 }
Java

【get】

C++中 HashMap 的使用

https://blog.csdn.net/charles1e/article/details/52042066

 

2. Add Two Numbers (2018/10/17)

【题目】https://leetcode.com/problems/add-two-numbers/description/

【思路】考察数据结构——链表,主要是链表的操作

 

 

3. Longest Substring Without Repeating Characters (2018/10/22)

【题目】https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

  给定一个字符串,找到一个最长的连续子串,使其满足每一个字符都不重复

【思路】

  (1)维护一个HashMap 记录出现过的字符  key:字符 value:字符在数组的下标

  (2)初始 i = j = 0

      j 向后移动 并把字符加到HashMap中

      如果遇到重复的字符,由 HashMap 更新 i , 使 i 向后跳跃 

      比如 123abcda 读到 j  == 7 时, 发现 s[7] = s[3] , i 更新为 4

 

 

4. Median of Two Sorted Arrays (2018/10/22)

【题目】https://leetcode.com/problems/median-of-two-sorted-arrays/description/

  给两个有序的数组,求两个数组合并后的中位数,给出log(m+n)的算法

【思路】

  (1)自己leetcode写了一个O(M+N)复杂度的算法,提交竟然AC了

  (2)看到log(m+n),想到二分法,关键时要如何进行二分

  官方题解讲得挺详细的 https://leetcode.com/problems/median-of-two-sorted-arrays/solution/

  

 

5. Longest Palindromic Substring (2018/10/24)

【题目】https://leetcode.com/problems/longest-palindromic-substring/description/

  大意:找一个最长子串,该子串回文

【思路】

  (1)找一个中心,向两边扩展找最长回文子串

    遍历每个中心,假设字符串长度为n,一共有 2n -1 个中心

    时间复杂度 O(n*n)

  (2)dp

   状态转移方程  dp[i][j] 表示 子串s[i] ~ s[j] 是否为回文

 

  

6. ZigZag Conversion (2018/10/25)

【题目】https://leetcode.com/problems/zigzag-conversion/description/

【思路】画出图形,找一下规律即可

 

7. Reverse Integer (2018/10/25)

【题目】将一个int数反向输出,如果反向后超出int范围,输出0

【思路】水题

 

8. String to Integer (atoi) (2018/10/25)

【题目】将字符串转化为整数

【思路】水题

 

9. Palindrome Number(2018/10/25)

【题目】判断回文数

【思路】水题

 

10. Regular Expression Matching

【题目】

【思路】

 

 

11. Container With Most Water (2018/10/31)

【题目】https://leetcode.com/problems/container-with-most-water/description/

可以抽象为如下数学表达:
* 给定一个数组a[N] , 求max{ s }, s=min(s[i],s[j])*(j-i) 且j > i

【思路】

(1)用暴力搜索 AC了,发现leetcode不怎么卡时间

(2)使用双指针,分别在开头结尾向中间移动

 

 

12. Integer to Roman(2018/10/31)

【题目】将一个整数转化为罗马符号

【思路】先转化个位的数字,再转化十位的数字....
* 注意输出的顺序

 

17. Letter Combinations of a Phone Number(2018/11/3)

【题目】输出所有序列

【思路】用树分析题目,很容易想到用dfs

 

 

19. Remove Nth Node From End of List(2018/11/3)

【题目】给一个单向链表,去除倒数第n个元素
【思路】双指针:
 两个指针间隔n个单位,
然后一起向后移动,
当第二个指针移动到末尾,第一个指针就移动到了倒数第n个元素的位置

 

22. Generate Parentheses (2018/11/4)

【题目】枚举出规定长度的括号序列
【思路】递归 O(2^(2n))
剪枝优化,递归过程中一旦检测到序列不合法,即终止添加括号

 

 

23. Merge k Sorted Lists (2018/11/7)

【题目】给出k个已经排好序的链表,合成一个排好序的链表
【思路】用最小堆维护每一个链表的第一个节点,
    每次移除最小堆的最小值,用相应的链表的下一个值代替,对最小堆进行更新,更新时间复杂度为O(logk),

     也就是找所有链表第一个节点的最小值

 【思路2】 题解中用了归并这种思想

    先是 k 组 合并成 k/2 组 

    K/2 组 合并成 k/4 组

    。。。

    直到合并为一组,算法结束

 

 

【题目】

【思路】 

 

 

posted @ 2018-09-22 11:20  chsobin  阅读(177)  评论(0编辑  收藏  举报