码迷,mamicode.com
首页 > 编程语言 > 详细

一天三题LeetCode(C++&JAVA)-1~3

时间:2015-04-08 09:14:09      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:leetcode   c++   java   

转载请注明出处:

http://blog.csdn.net/miaoyunzexiaobao



1.      Two Sum

https://leetcode.com/problems/two-sum/

给定一个数组,在其中找两个数,使两个数之和为给定的target。返回这两个数在数组中的位置。两个数字的位置分别用index1index2表示,要求index1<index2。例:

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

思路:

利用map的特性,其中map(key,value)代表(target-num[i],i),即key表示数字num[i]target的距离,value表示数字num[i]在数组中的位置。遍历num数组中的每个数,检测该数是否是mapkey,若是则表明查找成功,否则将该数字存入map,继续查看下一个数。以题中给出的输入输出为例:

i0时,map为空,numbers[0]=2target-numbers[i] = 9 – 2 = 7,即对numbers[0]而言,还要加7才能达到target-9,将(70)加入map

i1时,numbers[1]=7mapkey中包含该数字,查询成功。

查询到的两个数在numbers中的下标为{01}。由于index1开始计算,因此返回{12

C++:

vector<int> twoSum(vector<int> &numbers, int target) {
   <span style="white-space:pre">	</span>vector<int> result;
        int index1=0,index2=0;
        map<int,int> mInt;
 
        for(vector<int>::iterator iter =numbers.begin() ; iter != numbers.end() ; ++iter){
               if(mInt.find(*iter) !=mInt.end()){
                      index1 =(mInt.find(*iter))->second + 1;
                      index2 = iter -numbers.begin()+1;
               }else
                      mInt.insert(make_pair(target- *iter,iter-numbers.begin()));
        }
        cout<<index1<<""<<index2<<endl;
        result.push_back(index1);
        result.push_back(index2);
        return result;
}

JAVA:

public int[] twoSum(int[] numbers, int target) {
	int[] result = new int[2];
        int index1 = 0, index2 = 0;
        Map<Integer, Integer> map = newHashMap<Integer, Integer>();
        for (int i = 0; i < numbers.length;++i) {
               if (map.containsKey(numbers[i])){
                      index1 =map.get(numbers[i]) + 1;
                      index2 = i + 1;
                      break;
               } else
                      map.put(target -numbers[i], i);
        }
        result[0] = index1;
        result[1] = index2;
        return result;   
}

2.      Add Two Numbers

https://leetcode.com/problems/add-two-numbers/

给定两个以倒序形式存放在链表中的数,求两个数的和,并以倒序链表形式输出。例:

给定342和564,两个数的和为807。也就是输入(2 -> 4 -> 3) 和 (5 -> 6 -> 4),要求输出7 -> 0 -> 8:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output:
 7-> 0 -> 8

思路:

这题没什么难的,把两个链表中的每个数字依次相加,这一过程中注意某节点两数之和大于10时要在之后一个节点的两个数相加中加一即可。

C++:

ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
<span style="white-space:pre">	</span>ListNode *result = new ListNode(0);
        ListNode *cur = result;
        int count = 0;
        while(l1 != NULL || l2 != NULL){
               int i = 0 , j = 0;
               if(l1 != NULL){
                      i = l1->val;
                      l1 = l1->next;
               }
               if(l2 != NULL){
                      j = l2->val;
                      l2 = l2->next;
               }
               int sum = i + j + count;
               cur->next = new ListNode(sum %10);
               cur = cur->next;
               count = sum  / 10;
        }
        if(count != 0)
               cur->next = newListNode(count);
        return result->next;
 }

JAVA:

public ListNodeaddTwoNumbers(ListNode l1, ListNode l2) {
<span style="white-space:pre">	</span>ListNode result = new ListNode(0);
<span style="white-space:pre">	</span>ListNode cur = result;
        int count = 0;
        while (l1 != null || l2 != null) {
               int i = 0, j = 0;
               if (l1 != null) {
                      i = l1.val;
                      l1 = l1.next;
               }
               if (l2 != null) {
                      j = l2.val;
                      l2 = l2.next;
               }
               int sum = i + j + count;
               cur.next = new ListNode(sum %10);
               cur = cur.next;
               count = sum / 10;
        }
        if (count != 0)
               cur.next = new ListNode(count);
        return result.next;
}

3.      Longest Substring WithoutRepeating Characters

https://leetcode.com/problems/longest-substring-without-repeating-characters/

给定一个字符串,求最长不重复子序列长度。例:输入字符串”abcabcbb”,其最长不重复子序列是abc,返回2。输入字符串”bbbb”,其最长不重复子序列是b,返回1.

思路:

这题有点意思。可用两个指针,一个指向当前子串的头,一个指向尾,尾指针runner不断往后扫描,当有字符前面出现过了,记录当前子串长度和最优解的比较结果。之后尾指针runner暂时保持不动,移动头指针至下一个与尾指针不重复的地方,再继续移动尾指针runner,开始进行新一轮的不重复子串长度计算。即头指针walker不断往后扫描,直到扫描到一个字符和尾指runner针相同后加一,然后尾指针runner继续向后扫描。当尾指针runner到达字符串结尾,算法结束。复杂度O(n) + O(n) = O(n)

string s = “adbcabed”为例,在遇见第一个重复的a之前,其最大不重复子串长度为4.此时walkers[0],runners[4],这一不重复子串长度为4.之后将walker移至d,即s[1]。将runner继续向后以后。当runner移动到第二个b,即s[5]时,再次出现重复,该子串长度为4.walker移动至与b之后的字符,即c,也就是s[3]runner继续向后移动,直至末尾均无重复元素,此时runner指向s最后的元素d之后的位置,获得最大长度8 – 3 = 5

C++:

int lengthOfLongestSubstring(string s) {
        int maxLength = 0;
        int walker = 0 , runner = 0;
        set<char> setChar;
 
        for(int i = 0 ; i < s.length() ; ++i){
               if(setChar.find(s[i]) ==setChar.end())
                      setChar.insert(s[i]);
               else{
                      maxLength = (maxLength> runner - walker)?(maxLength):(runner - walker);
                      while(s[walker] !=s[runner]){
                             setChar.erase(*setChar.find(s[walker]));
                             ++walker;
                      }
                      ++walker;
               }                   
               ++runner;
        }
        maxLength = (maxLength > runner -walker)?(maxLength):(runner - walker);
        return maxLength;
}

JAVA:

public int lengthOfLongestSubstring(String s) {
  <span style="white-space:pre">	</span>int maxLength = 0;
        int walker = 0, runner = 0;
        Set<Character> setChar = newHashSet<Character>();
        for (int i = 0; i < s.length(); ++i){
               if(!setChar.contains(s.charAt(i)))
                      setChar.add(s.charAt(i));
               else {
                      maxLength =Math.max(maxLength, runner - walker);
                      while (s.charAt(walker) !=s.charAt(runner)) {
                             setChar.remove(s.charAt(walker));
                             ++walker;
                      }
                      ++walker;
               }
               ++runner;
        }
        maxLength = Math.max(maxLength, runner -walker);
        return maxLength;
}


一天三题LeetCode(C++&JAVA)-1~3

标签:leetcode   c++   java   

原文地址:http://blog.csdn.net/miaoyunzexiaobao/article/details/44928603

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!