在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同, 则称这种编码为格雷码(Gray Code),请编写一个函数,使用递归的方法生成N位的格雷码。
给定一个整数n,请返回n位的格雷码,顺序为从0开始。
测试样例:
1
返回:["0","1"]
public static String[] getGray(int n) { String[] result; // write code here //特殊情况 n=1 返回 {"0","1"} if(n == 1){ result = new String[]{"0", "1"}; return result; }else{ String[] temp = getGray(n-1); result = new String[temp.length*2]; for(int i=0;i<temp.length;i++){ result[i] = "0"+temp[i]; result[temp.length+i] = "1"+temp[i]; } } return result; }
import java.util.*; public class GrayCode { public String[] getGray(int n) { String[] res = new String[]{"0","1"}; for(int i=2;i<=n;i++){ int k =(int)Math.pow(2,i); String[] temp=new String[k]; for(int j=0;j<k/2;j++){ temp[j]="0"+res[j]; temp[k-j-1]="1"+res[j]; } res=temp; } return res; } }
import java.util.*; public class GrayCode { public String[] getGray(int n) { // write code here String[] ans = new String[(int) Math.pow(2, n)]; ans[0] = "0"; ans[1] = "1"; int len = 2, len2 = len << 1; for(int i = 1; i < n; i++) { for(int j = len; j < len2; j++) { ans[j] = "1" + ans[len - 1 - j + len]; } for(int j = 0; j < len; j++) { ans[j] = "0" + ans[j]; } len = len2; len2 = len << 1; } return ans; } }
修改添加了注释
//递归 //递归的思路是n位gray码是由n-1位gray码生成,举个例子简单一些: //比如求n=3的gray码,首先知道n=2的gray码是(00,01,11,10) //那么n=3的gray码其实就是对n=2的gray码首位添加0或1生成的,添加0后变成(000,001,011,010) //添加1后需要顺序反向就变成(110,111,101,100) //组合在一起就是(000,001,011,010,110,111,101,100) public String[] getGray(int n){ String[] resStrs = null;//定义一个空数组 if(n==1){//如果等于1 则就两个唯一情况,0 ,1 resStrs = new String[]{"0","1"}; }else{//否则 ,包含多种情况,我们递归的调用方法 计算n的gray数就要考虑n-1的情况 String[] strs = getGray(n-1); resStrs = new String[2*strs.length];//gray数每增一个就会多2倍的个数 for(int i=0;i<strs.length;i++){ resStrs[i]="0"+strs[i]; //遍历,如果是添加0,则添到前面。 resStrs[resStrs.length-1-i]= "1"+strs[i];//如果是1 添加的结果要在数组中头尾数组互换 } } return resStrs; }
import java.util.*; public class GrayCode{ public String[] getGray(int n) { // write code here int length=(int) Math.pow(2,n); String[] result = new String[length]; if (n == 1) { result[0] = "0"; result[1] = "1"; return result; } String[] j=getGray(n-1); for (int i = 0; i <length / 2; i++) { result[i] = "0" + j[i]; result[length-1-i]="1"+j[i]; } return result; } }可以把数组长度改为,N-1对应数组长度的2倍。优化后代码如下:
import java.util.*; public class GrayCode{ public String[] getGray(int n) { if(n==1) return new String[]{"0","1"}; String[] prev=getGray(n-1); int length=prev.length; String[] result=new String[length*2]; for(int i=0;i<length;i++) { result[i]="0"+prev[i]; result[length*2-1-i]="1"+prev[i]; } return result; } }
public class gerryCode { // 1 : 0 1 //2 : 00 01 11 10 //3 :["000","001","011","010","110","111","101","100"] public String[] getGray(int n) { Queue<StringBuilder> queue = new LinkedList<>(); getGray(n,queue); String[] res = new String[queue.size()]; int i = 0; while ( !queue.isEmpty()){ res[i] = queue.poll().toString(); i++; } return res; } public void getGray(int n, Queue<StringBuilder> queue){ // Base if( n == 1){ queue.add(new StringBuilder().append(0)); queue.add(new StringBuilder().append(1)); return; } getGray(n-1,queue); int size = queue.size(); while (size > 0){ int i; i = size & (byte)1; StringBuilder poll = queue.poll(); StringBuilder nextSb = new StringBuilder(poll); poll.append(i); nextSb.append(1-i); queue.offer(poll); queue.offer(nextSb); size --; } } public static void main(String[] args) { gerryCode gerryCode = new gerryCode(); System.out.println(Arrays.toString(gerryCode.getGray(3))); } }
import java.util.*; public class GrayCode { public String[] getGray(int n) { // write code here if (n==1) { String[] res = {"0", "1"}; return res; } String[] lastRes = getGray(n-1); int len = (int) Math.pow(2, n); String[] curRes = new String[len]; int j = len-1; for(int i=0; i<len/2; i++){ curRes[i] = "0" + lastRes[i]; curRes[j--] = "1" + lastRes[i]; } return curRes; } }
import java.util.*; public class GrayCode { public static String[] getGray(int n) { if(n == 1) return new String[] {"0", "1"}; String[] s = getGray(n - 1); String[] res = new String[s.length * 2]; for (int i = 0; i < s.length; i ++) { res[i] = "0" + s[i]; res[s.length + i] = "1" + s[s.length - 1 - i]; } return res; } }
package 牛客; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Created by liangtian on 16/8/2. * 在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同, 则称这种编码为格雷码(Gray Code), * 请编写一个函数,使用递归的方法生成N位的格雷码。给定一个整数n,请返回n位的格雷码,顺序为从0开始。 * 输入:2 * 输出:00,01,11,10 */ public class Gray { public static List<String> getGray1(int n) { List<String> list = new ArrayList<String>(); if(n==1) { list.add("0"); list.add("1"); return list; } list = getGray1(n-1); int len = list.size(); String []array = new String[len]; for(int i=0;i<len;i++) { array[i] = list.get(i); } for(int i = 0;i<list.size();i++) { StringBuilder sb = new StringBuilder(); sb.append("0"); sb.append(list.get(i)); list.set(i,sb.toString()); } for(int i=len-1;i>=0;i--) { StringBuilder sb1 = new StringBuilder(); sb1.append("1"); sb1.append(array[i]); list.add(sb1.toString()); } return list; } public static String[] getGray(int n) { // write code here List<String> list = getGray1(n); int size = list.size(); String []str = new String[size]; for(int i=0;i<size;i++) str[i] = list.get(i); return str; } public static void main(String []args) { Scanner sc = new Scanner(System.in); String[]str = getGray(sc.nextInt()); for(int i=0;i<str.length;i++) { System.out.print(str[i]+" "); } } }
//看下下格雷码,根据规律0后面加0和1,1后面加1,0。然后尝试用着写了个递归。思路比较清晰 //第一次自己独立编写递归应用成功。。
public void name(int n,String s1,String s2){ if (n==2) { list.add(s1+"0"); list.add(s1+'1'); list.add(s2+'1'); list.add(s2+'0'); } else { name(--n,s1+'0',s1+'1'); name(n,s2+'1',s2+'0'); } } public String[] getGray(int n) { if (n==1) { return new String[]{"0","1"}; } name(n,"0","1"); String[] s=(String[]) (String[])list.toArray(new String[list.size()]); return s; }