Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int getCountOfOnes(int n) {
- /*
- Please implement this method to
- return the number of '1's in the binary representation of n
- for any integer n, where n > 0
- Example: for n=6 the binary representation is '110' and the number of '1's in that
- representation is 2
- */
- int cnt = 0;
- String s = Integer.toBinaryString(n);
- System.out.println("Binary String = " + s);
- for(int i = 0; i < s.length(); i++){
- String j = Character.toString(s.charAt(i));
- if (Character.isDigit(s.charAt(i))){
- if (j.equalsIgnoreCase("1")) {
- cnt++;
- }
- }
- }
- return cnt;
- }
- // Please do not change this interface
- interface ListNode {
- int getItem();
- ListNode getNext();
- void setNext(ListNode next);
- }
- public static ListNode reverse(ListNode node) {
- /*
- Please implement this method to
- reverse a given linked list.
- */
- ListNode prev = null;
- ListNode cur = null;
- ListNode next = null;
- prev = node;
- cur = node.getNext();
- prev.setNext(null);
- while(cur.getNext()!=null){
- next = cur.getNext();
- cur.setNext(prev);
- }
- return cur;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement