Java Code Style 记录

示例代码:

 1 class Solution {
 2     /**
 3      * @param nums: A list of integers.
 4      * @return: A list of unique permutations.
 5      */
 6     public List<List<Integer>> permuteUnique(int[] nums) {
 7         // Write your code here
 8         ArrayList<List<Integer>> rst = new ArrayList<List<Integer>>();
 9         if (nums == null) {
10             return rst;
11         }
12         if (nums.length == 0){
13             rst.add(new ArrayList<Integer>());
14             return rst;
15         }
16         Arrays.sort(nums);
17         ArrayList<Integer> list = new ArrayList<Integer>();
18         int[] visited = new int[nums.length];
19         for (int i = 0; i < visited.length; i++) {
20             visited[i] = 0;
21         }
22         helper(rst, list, visited, nums);
23         return rst;
24     }
25     public void helper(ArrayList<List<Integer>> rst, List<Integer> list, int[] visited, int[] nums){
26         if (list.size() == nums.length) {
27             rst.add(new ArrayList<Integer>(list));
28             return;
29         }
30         for (int i = 0; i < nums.length; i++) {
31             if (visited[i] == 1 || (i != 0 && nums[i] == nums[i - 1] && visited[i - 1] == 0)){
32                 continue;
33             }
34             visited[i] = 1;
35             list.add(nums[i]);
36             helper(rst, list, visited, nums);
37             list.remove(list.size() - 1);
38             visited[i] = 0;
39         }
40     }
41 }

 

Code Style:

  • for后有空格
  • if后有空格
  • 运算符前后有空格(+-*/,==,!=,)
  • 逗号分号后有空格
  • if for 的大括号不可省略(- -)

Bug Free:

  • else/if后没有加括号导致只执行了第一条语句;
  • for()里的参数没有先定义(有的时候需要在外面定义);

 

 

Google Java Style:https://my.oschina.net/candiesyangyang/blog/225058

posted @ 2016-12-09 17:10  不闻余物  阅读(628)  评论(0编辑  收藏  举报