lettCode-Array

1   Remove Element    lintcode-172

     描述: 删相同元素,反现有长度

     记忆:标不同元素,反标记值

 1  public int removeElement(int[] a, int elem) {
 2         // write your code here
 3         int index = 0;
 4         for (int i = 0; i < a.length; i++) {
 5             if (a[i] != elem) {
 6                 a[index++] = a[i];
 7             }
 8         }
 9         return index;
10     }
View Code

 2 Remove Duplicates from Sorted Array 1    lintCode-100

    描述:已排序,删相同,反长度

    记忆:标记不同元素,反标记

 1 public int removeDuplicates(int[] nums) {
 2         // write your code here
 3         if (nums.length == 0) {
 4             return 0;
 5         }
 6         int index = 0;
 7         for (int i = 1; i < nums.length; i++) {
 8             if (nums[index] != nums[i]) {
 9                 nums[++index] = nums[i];
10             }
11         }
12         return index + 1;
13     }
View Code

3 Remove Duplicates from Sorted Array 2    lintCode-101

   描述:已排序,删,最多两,反长度

   记忆:加标记,不等标记少二,才加长

 1     public int removeDuplicates(int[] nums) {
 2         // write your code here
 3         if (nums.length < 2) {
 4             return nums.length;
 5         }
 6         int index = 2;
 7         for (int i = 2; i < nums.length; i++) {
 8             if (nums[i] != nums[index - 2]) {
 9                 nums[index++] = nums[i];
10             }
11         }
12         return index;
13     } 
14 }
View Code

 4 Plus One   lintCode-407

   描述:数组加一反数组

   记忆:加一,进位

 1  public int[] plusOne(int[] digits) {
 2         // Write your code here
 3         int carries = 1;
 4         for (int i = digits.length - 1; i >= 0 && carries > 0; i--) {
 5             int sum = digits[i] + carries;
 6             digits[i] = sum % 10;
 7             carries = sum / 10;
 8         }
 9         
10         if (carries == 0) {
11             return digits;
12         }
13         
14         int[] rst = new int[digits.length + 1];
15         rst[0] = 1;
16         for (int i = 1; i < rst.length; i++) {
17             rst[i] = digits[i - 1];
18         }
19         
20         return rst;
21     }
View Code

 5 Pascal's Triangle  杨辉三角

  记忆:两数组链表,分类

 1 public static List<Integer> row(int rowIndex) {
 2         List<Integer> res = new ArrayList<Integer>();
 3         int temp = 1;
 4         for (int i = 1; i <= rowIndex; i++) {
 5             List<Integer> list = new ArrayList<Integer>();
 6             if (i == 1) {
 7                 list.add(0, 1);
 8             } else if (i == 2) {
 9                 list.add(0, 1);
10                 list.add(1, 1);
11             } else {
12                 list.add(0, 1);
13                 for (int k = 0; k < res.size() - 1; k++) {
14                     int sum = res.get(k) + res.get(k + 1);
15                     list.add(sum);
16                 }
17                 list.add(i - 1, 1);
18             }
19             res = list;
20         }
21         System.out.println(res);
22         return res;
23     }
View Code

 

posted on 2016-12-03 18:32  wheleetcode  阅读(426)  评论(0编辑  收藏  举报